Partially recast the router API to be a lot more intuitive. (#2469)

* Recast the router API to be a lot more intuitive.

Fixes #2387
Fixes #2072
Fixes quite a few issues reported on Gitter.

For `m.route.Link`:

- More intuitive
- More accessible
- More ergonomic
- It can be disabled
- It can be cancelled
- It can be changed
- Oh, and you can use it isomorphically.

For `m.route.prefix`

- You can *read* it.
- You can write to it, of course.
- It's literally just setting a property.

For the router itself (and the rest of Mithril):

- You can now `require("mithril")` and all its submodules without a DOM
  at all. There is a catch: you can't instantiate any routes, you can't
  mount anything, and you can't invoke `m.render` in any capacity. You
  can only use `m.route.Link`, `m.route.prefix`, hyperscript stuff, and
  `mithril/stream`, and you can use `m.request` with `background: true`
  if you use a global XHR polyfill. (You can't use `m.request` without
  `background: true` except with a DOM to redraw with.) The goal here is
  to try to get out of the way for simple testing and to defer the
  inevitable `TypeError`s for the relevant DOM methods to runtime.

  The factory requires no arguments, and in terms of globals, you can
  just figure out based on what errors are thrown what globals to
  define. Their values don't matter - they just need to be set to
  *something*, even if it's just `null` or `undefined`, before Mithril
  executes.

Had to make quite a few other changes throughout the docs and tests to
update them accordingly. Oh, and that massive router overhaul enabled me
to do all this.

Also, slip in a few drive-by fixes to the mocks so they're a little
easier to work with and can accept more URLs. This was required for a
few of the tests.

* Update changelog + numbers, add forgotten bundle option

* Add PR numbers to changelog [skip ci]

* Allow continuing to the next match by returning `false`.

* Update numbers again
This commit is contained in:
Isiah Meadows 2019-07-12 15:29:37 -04:00 committed by GitHub
parent ace4e77ace
commit 582bda56dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 983 additions and 566 deletions

View file

@ -19,6 +19,21 @@ module.exports = function(options) {
var spy = options.spy || function(f){return f}
var spymap = []
// This way I'm not also implementing a partial `URL` polyfill. Based on the
// regexp at http://urlregex.com/, but adapted to allow relative URLs and
// care only about HTTP(S) URLs.
var urlHash = "#[?!/+=&;%@.\\w_-]*"
var urlQuery = "\\?[!/+=&;%@.\\w_-]*"
var urlPath = "/[+~%/.\\w_-]*"
var urlRelative = urlPath + "(?:" + urlQuery + ")?(?:" + urlHash + ")?"
var urlDomain = "https?://[A-Za-z0-9][A-Za-z0-9.-]+[A-Za-z0-9]"
var validURLRegex = new RegExp(
"^" + urlDomain + "(" + urlRelative + ")?$|" +
"^" + urlRelative + "$|" +
"^" + urlQuery + "(?:" + urlHash + ")?$|" +
"^" + urlHash + "$"
)
var hasOwn = ({}.hasOwnProperty)
function registerSpies(element, spies) {
@ -440,7 +455,13 @@ module.exports = function(options) {
if (this.namespaceURI === "http://www.w3.org/2000/svg") {
var val = this.hasAttribute("href") ? this.attributes.href.value : ""
return {baseVal: val, animVal: val}
} else return this.attributes["href"] === undefined ? "" : "[FIXME implement]"
} else if (this.namespaceURI === "http://www.w3.org/1999/xhtml") {
if (!this.hasAttribute("href")) return ""
// HACK: if it's valid already, there's nothing to implement.
var value = this.attributes.href.value
if (validURLRegex.test(value)) return value
}
return "[FIXME implement]"
},
set: function(value) {
// This is a readonly attribute for SVG, todo investigate MathML which may have yet another IDL