@talyaron1/mithril 0.0.0-stage → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Leo Horie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,117 @@
1
- # Temporary Holding Version
1
+ # Mithril.js
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm Version](https://img.shields.io/npm/v/@talyaron1/mithril.svg)](https://www.npmjs.com/package/@talyaron1/mithril)  
4
+ [![License](https://img.shields.io/npm/l/mithril.svg)](https://github.com/MithrilJS/mithril.js/blob/main/LICENSE)  
5
+ [![npm Downloads](https://img.shields.io/npm/dm/@talyaron1/mithril.svg)](https://www.npmjs.com/package/@talyaron1/mithril)  
6
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/talyaron/mithril.js/.github%2Fworkflows%2Ftest.yml?branch=main)](https://github.com/talyaron/mithril.js/actions)  
7
+ [![Donate at OpenCollective](https://img.shields.io/opencollective/all/mithriljs.svg?colorB=brightgreen)](https://opencollective.com/mithriljs)  
8
+ [![Zulip, join chat](https://img.shields.io/badge/zulip-join_chat-brightgreen.svg)](https://mithril.zulipchat.com/)
9
+
10
+ > **This is a fork.** [talyaron/mithril.js](https://github.com/talyaron/mithril.js) is Mithril.js v2 with bug fixes that are not in an upstream release. The API is unchanged. See the [list of changes](docs/recent-changes.md), and [Installation](#installation) below.
11
+
12
+ - [What is Mithril.js?](#what-is-mithriljs)
13
+ - [Installation](#installation)
14
+ - [Documentation](#documentation)
15
+ - [Getting Help](#getting-help)
16
+ - [Contributing](#contributing)
17
+
18
+ ## What is Mithril.js?
19
+
20
+ A modern client-side JavaScript framework for building Single Page Applications. It's small (<!-- size -->9.15 KB<!-- /size --> gzipped), fast and provides routing and XHR utilities out of the box.
21
+
22
+ Mithril.js is used by companies like Vimeo and Nike, and open source platforms like Lichess 👍.
23
+
24
+ Mithril.js supports Firefox ESR, and the last two versions of Firefox, Edge, Safari, and Chrome. No polyfills required. 👌
25
+
26
+ ## Installation
27
+
28
+ This fork is published on npm as [`@talyaron1/mithril`](https://www.npmjs.com/package/@talyaron1/mithril).
29
+
30
+ ### npm
31
+
32
+ Install it under the name `mithril`:
33
+
34
+ ```bash
35
+ npm install mithril@npm:@talyaron1/mithril
36
+ ```
37
+
38
+ With yarn or pnpm:
39
+
40
+ ```bash
41
+ yarn add mithril@npm:@talyaron1/mithril
42
+ ```
43
+
44
+ ```bash
45
+ pnpm add mithril@npm:@talyaron1/mithril
46
+ ```
47
+
48
+ That adds this line to your `package.json`:
49
+
50
+ ```json
51
+ "dependencies": {
52
+ "mithril": "npm:@talyaron1/mithril@^2.4.0"
53
+ }
54
+ ```
55
+
56
+ Because the package is installed under the name `mithril`, your code does not change, and other packages that depend on `mithril` use this fork too:
57
+
58
+ ```js
59
+ import m from "mithril"
60
+ import Stream from "mithril/stream"
61
+ ```
62
+
63
+ If your app already uses the upstream `mithril` package, run the same install command: it replaces the upstream package with this fork.
64
+
65
+ ### TypeScript
66
+
67
+ The type definitions from DefinitelyTyped work with this fork, as long as it is installed under the name `mithril`:
68
+
69
+ ```bash
70
+ npm install @types/mithril --save-dev
71
+ ```
72
+
73
+ ### CDN
74
+
75
+ ```html
76
+ <!-- Development: whichever you prefer -->
77
+ <script src="https://unpkg.com/@talyaron1/mithril/mithril.js"></script>
78
+ <script src="https://cdn.jsdelivr.net/npm/@talyaron1/mithril/mithril.js"></script>
79
+
80
+ <!-- Production: whichever you prefer -->
81
+ <script src="https://unpkg.com/@talyaron1/mithril/mithril.min.js"></script>
82
+ <script src="https://cdn.jsdelivr.net/npm/@talyaron1/mithril/mithril.min.js"></script>
83
+ ```
84
+
85
+ ### From GitHub
86
+
87
+ To install straight from this repository, pinned to a release tag:
88
+
89
+ ```bash
90
+ npm install mithril@github:talyaron/mithril.js#v2.4.1-fork
91
+ ```
92
+
93
+ ### The upstream package
94
+
95
+ The original package, without the fixes in this fork, is [`mithril`](https://www.npmjs.com/package/mithril): `npm install mithril`.
96
+
97
+ The ["Getting started" guide](https://mithril.js.org/#getting-started) is a good place to start learning how to use Mithril.js.
98
+
99
+ ## Documentation
100
+
101
+ Documentation lives on [mithril.js.org](https://mithril.js.org).
102
+
103
+ You may be interested in the [API Docs](https://mithril.js.org/api.html), a [Simple Application](https://mithril.js.org/simple-application.html), or perhaps some [Examples](https://mithril.js.org/examples.html).
104
+
105
+ ## Getting Help
106
+
107
+ Mithril.js has an active & welcoming community on [Zulip](https://mithril.zulipchat.com/), or feel free to ask questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/mithril.js) using the `mithril.js` tag.
108
+
109
+ ## Contributing
110
+
111
+ There's a [Contributing FAQ](https://mithril.js.org/contributing.html) on the Mithril.js site that hopefully helps, but if not definitely hop into the [Zulip stream](https://mithril.zulipchat.com/) and ask away!
112
+
113
+ ---
114
+
115
+ Thanks for reading!
116
+
117
+ 🎁
@@ -0,0 +1,49 @@
1
+ "use strict"
2
+
3
+ var Vnode = require("../render/vnode")
4
+
5
+ module.exports = function(render, schedule, console) {
6
+ var subscriptions = []
7
+ var pending = false
8
+ var offset = -1
9
+
10
+ function sync() {
11
+ for (offset = 0; offset < subscriptions.length; offset += 2) {
12
+ try { render(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw) }
13
+ catch (e) { console.error(e) }
14
+ }
15
+ offset = -1
16
+ }
17
+
18
+ function redraw() {
19
+ if (!pending) {
20
+ pending = true
21
+ schedule(function() {
22
+ pending = false
23
+ sync()
24
+ })
25
+ }
26
+ }
27
+
28
+ redraw.sync = sync
29
+
30
+ function mount(root, component) {
31
+ if (component != null && component.view == null && typeof component !== "function") {
32
+ throw new TypeError("m.mount expects a component, not a vnode.")
33
+ }
34
+
35
+ var index = subscriptions.indexOf(root)
36
+ if (index >= 0) {
37
+ subscriptions.splice(index, 2)
38
+ if (index <= offset) offset -= 2
39
+ render(root, [])
40
+ }
41
+
42
+ if (component != null) {
43
+ subscriptions.push(root, component)
44
+ render(root, Vnode(component), redraw)
45
+ }
46
+ }
47
+
48
+ return {mount: mount, redraw: redraw}
49
+ }
package/api/router.js ADDED
@@ -0,0 +1,252 @@
1
+ "use strict"
2
+
3
+ var Vnode = require("../render/vnode")
4
+ var hyperscript = require("../render/hyperscript")
5
+
6
+ var decodeURIComponentSafe = require("../util/decodeURIComponentSafe")
7
+ var buildPathname = require("../pathname/build")
8
+ var parsePathname = require("../pathname/parse")
9
+ var compileTemplate = require("../pathname/compileTemplate")
10
+ var censor = require("../util/censor")
11
+
12
+ module.exports = function($window, mountRedraw) {
13
+ var p = Promise.resolve()
14
+
15
+ var scheduled = false
16
+
17
+ var ready = false
18
+ var hasBeenResolved = false
19
+
20
+ var dom, compiled, fallbackRoute
21
+
22
+ var currentResolver, component, attrs, currentPath, lastUpdate
23
+
24
+ var RouterRoot = {
25
+ onremove: function() {
26
+ ready = hasBeenResolved = false
27
+ $window.removeEventListener("popstate", fireAsync, false)
28
+ },
29
+ view: function() {
30
+ // The route has already been resolved.
31
+ // Therefore, the following early return is not needed.
32
+ // if (!hasBeenResolved) return
33
+
34
+ var vnode = Vnode(component, attrs.key, attrs)
35
+ if (currentResolver) return currentResolver.render(vnode)
36
+ // Wrap in a fragment to preserve existing key semantics
37
+ return [vnode]
38
+ },
39
+ }
40
+
41
+ var SKIP = route.SKIP = {}
42
+
43
+ function resolveRoute() {
44
+ scheduled = false
45
+ // Consider the pathname holistically. The prefix might even be invalid,
46
+ // but that's not our problem.
47
+ var prefix = $window.location.hash
48
+ if (route.prefix[0] !== "#") {
49
+ prefix = $window.location.search + prefix
50
+ if (route.prefix[0] !== "?") {
51
+ prefix = $window.location.pathname + prefix
52
+ if (prefix[0] !== "/") prefix = "/" + prefix
53
+ }
54
+ }
55
+ // This seemingly useless `.concat()` speeds up the tests quite a bit,
56
+ // since the representation is consistently a relatively poorly
57
+ // optimized cons string.
58
+ var path = prefix.concat()
59
+ .replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponentSafe)
60
+ .slice(route.prefix.length)
61
+ var data = parsePathname(path)
62
+
63
+ Object.assign(data.params, $window.history.state)
64
+
65
+ function reject(e) {
66
+ console.error(e)
67
+ route.set(fallbackRoute, null, {replace: true})
68
+ }
69
+
70
+ loop(0)
71
+ function loop(i) {
72
+ for (; i < compiled.length; i++) {
73
+ if (compiled[i].check(data)) {
74
+ var payload = compiled[i].component
75
+ var matchedRoute = compiled[i].route
76
+ var localComp = payload
77
+ var update = lastUpdate = function(comp) {
78
+ if (update !== lastUpdate) return
79
+ if (comp === SKIP) return loop(i + 1)
80
+ component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
81
+ attrs = data.params, currentPath = path, lastUpdate = null
82
+ currentResolver = payload.render ? payload : null
83
+ if (hasBeenResolved) mountRedraw.redraw()
84
+ else {
85
+ hasBeenResolved = true
86
+ mountRedraw.mount(dom, RouterRoot)
87
+ }
88
+ }
89
+ // There's no understating how much I *wish* I could
90
+ // use `async`/`await` here...
91
+ if (payload.view || typeof payload === "function") {
92
+ payload = {}
93
+ update(localComp)
94
+ }
95
+ else if (payload.onmatch) {
96
+ p.then(function () {
97
+ return payload.onmatch(data.params, path, matchedRoute)
98
+ }).then(update, path === fallbackRoute ? null : function (e) {
99
+ // Only the latest navigation may redirect to the fallback
100
+ // route. A stale rejection is still reported.
101
+ if (update === lastUpdate) reject(e)
102
+ else console.error(e)
103
+ })
104
+ }
105
+ else update(/* "div" */)
106
+ return
107
+ }
108
+ }
109
+
110
+ if (path === fallbackRoute) {
111
+ throw new Error("Could not resolve default route " + fallbackRoute + ".")
112
+ }
113
+ route.set(fallbackRoute, null, {replace: true})
114
+ }
115
+ }
116
+
117
+ function fireAsync() {
118
+ if (!scheduled) {
119
+ scheduled = true
120
+ // TODO: just do `mountRedraw.redraw()` here and elide the timer
121
+ // dependency. Note that this will muck with tests a *lot*, so it's
122
+ // not as easy of a change as it sounds.
123
+ setTimeout(resolveRoute)
124
+ }
125
+ }
126
+
127
+ function route(root, defaultRoute, routes) {
128
+ if (!root) throw new TypeError("DOM element being rendered to does not exist.")
129
+
130
+ compiled = Object.keys(routes).map(function(route) {
131
+ if (route[0] !== "/") throw new SyntaxError("Routes must start with a '/'.")
132
+ if ((/:([^\/\.-]+)(\.{3})?:/).test(route)) {
133
+ throw new SyntaxError("Route parameter names must be separated with either '/', '.', or '-'.")
134
+ }
135
+ return {
136
+ route: route,
137
+ component: routes[route],
138
+ check: compileTemplate(route),
139
+ }
140
+ })
141
+ fallbackRoute = defaultRoute
142
+ if (defaultRoute != null) {
143
+ var defaultData = parsePathname(defaultRoute)
144
+
145
+ if (!compiled.some(function (i) { return i.check(defaultData) })) {
146
+ throw new ReferenceError("Default route doesn't match any known routes.")
147
+ }
148
+ }
149
+ dom = root
150
+
151
+ $window.addEventListener("popstate", fireAsync, false)
152
+
153
+ ready = true
154
+
155
+ // The RouterRoot component is mounted when the route is first resolved.
156
+ resolveRoute()
157
+ }
158
+ route.set = function(path, data, options) {
159
+ // A navigation that interrupts a pending one replaces its history entry.
160
+ // Note: `options` belongs to the caller, so it must not be modified.
161
+ var replace = lastUpdate != null || options != null && options.replace
162
+ lastUpdate = null
163
+
164
+ path = buildPathname(path, data)
165
+ if (ready) {
166
+ fireAsync()
167
+ var state = options ? options.state : null
168
+ var title = options ? options.title : null
169
+ if (replace) $window.history.replaceState(state, title, route.prefix + path)
170
+ else $window.history.pushState(state, title, route.prefix + path)
171
+ }
172
+ else {
173
+ $window.location.href = route.prefix + path
174
+ }
175
+ }
176
+ route.get = function() {return currentPath}
177
+ route.prefix = "#!"
178
+ route.Link = {
179
+ view: function(vnode) {
180
+ // Omit the used parameters from the rendered element - they are
181
+ // internal. Also, censor the various lifecycle methods.
182
+ //
183
+ // We don't strip the other parameters because for convenience we
184
+ // let them be specified in the selector as well.
185
+ var child = hyperscript(
186
+ vnode.attrs.selector || "a",
187
+ censor(vnode.attrs, ["options", "params", "selector", "onclick"]),
188
+ vnode.children
189
+ )
190
+ var options, onclick, href
191
+
192
+ // Let's provide a *right* way to disable a route link, rather than
193
+ // letting people screw up accessibility on accident.
194
+ //
195
+ // The attribute is coerced so users don't get surprised over
196
+ // `disabled: 0` resulting in a button that's somehow routable
197
+ // despite being visibly disabled.
198
+ if (child.attrs.disabled = Boolean(child.attrs.disabled)) {
199
+ child.attrs.href = null
200
+ child.attrs["aria-disabled"] = "true"
201
+ // If you *really* do want add `onclick` on a disabled link, use
202
+ // an `oncreate` hook to add it.
203
+ } else {
204
+ options = vnode.attrs.options
205
+ onclick = vnode.attrs.onclick
206
+ // Easier to build it now to keep it isomorphic.
207
+ href = buildPathname(child.attrs.href, vnode.attrs.params)
208
+ child.attrs.href = route.prefix + href
209
+ child.attrs.onclick = function(e) {
210
+ var result
211
+ if (typeof onclick === "function") {
212
+ result = onclick.call(e.currentTarget, e)
213
+ } else if (onclick == null || typeof onclick !== "object") {
214
+ // do nothing
215
+ } else if (typeof onclick.handleEvent === "function") {
216
+ onclick.handleEvent(e)
217
+ }
218
+
219
+ // Adapted from React Router's implementation:
220
+ // https://github.com/ReactTraining/react-router/blob/520a0acd48ae1b066eb0b07d6d4d1790a1d02482/packages/react-router-dom/modules/Link.js
221
+ //
222
+ // Try to be flexible and intuitive in how we handle links.
223
+ // Fun fact: links aren't as obvious to get right as you
224
+ // would expect. There's a lot more valid ways to click a
225
+ // link than this, and one might want to not simply click a
226
+ // link, but right click or command-click it to copy the
227
+ // link target, etc. Nope, this isn't just for blind people.
228
+ if (
229
+ // Skip if `onclick` prevented default
230
+ result !== false && !e.defaultPrevented &&
231
+ // Ignore everything but left clicks
232
+ (e.button === 0 || e.which === 0 || e.which === 1) &&
233
+ // Let the browser handle `target=_blank`, etc.
234
+ (!e.currentTarget.target || e.currentTarget.target === "_self") &&
235
+ // No modifier keys
236
+ !e.ctrlKey && !e.metaKey && !e.shiftKey && !e.altKey
237
+ ) {
238
+ e.preventDefault()
239
+ e.redraw = false
240
+ route.set(href, null, options)
241
+ }
242
+ }
243
+ }
244
+ return child
245
+ },
246
+ }
247
+ route.param = function(key) {
248
+ return attrs && key != null ? attrs[key] : attrs
249
+ }
250
+
251
+ return route
252
+ }
package/browser.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict"
2
+
3
+ var m = require("./index")
4
+ if (typeof module !== "undefined") module["exports"] = m
5
+ else window.m = m
package/hyperscript.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict"
2
+
3
+ var hyperscript = require("./render/hyperscript")
4
+
5
+ hyperscript.trust = require("./render/trust")
6
+ hyperscript.fragment = require("./render/fragment")
7
+
8
+ module.exports = hyperscript
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict"
2
+
3
+ var hyperscript = require("./hyperscript")
4
+ var mountRedraw = require("./mount-redraw")
5
+ var request = require("./request")
6
+ var router = require("./route")
7
+
8
+ var m = function m() { return hyperscript.apply(this, arguments) }
9
+ m.m = hyperscript
10
+ m.trust = hyperscript.trust
11
+ m.fragment = hyperscript.fragment
12
+ m.Fragment = "["
13
+ m.mount = mountRedraw.mount
14
+ m.route = router
15
+ m.render = require("./render")
16
+ m.redraw = mountRedraw.redraw
17
+ m.request = request.request
18
+ m.parseQueryString = require("./querystring/parse")
19
+ m.buildQueryString = require("./querystring/build")
20
+ m.parsePathname = require("./pathname/parse")
21
+ m.buildPathname = require("./pathname/build")
22
+ m.vnode = require("./render/vnode")
23
+ m.censor = require("./util/censor")
24
+ m.domFor = require("./render/domFor")
25
+
26
+ module.exports = m