diff --git a/cjs/3rd/disconnected.js b/cjs/3rd/disconnected.js deleted file mode 100644 index 611103de..00000000 --- a/cjs/3rd/disconnected.js +++ /dev/null @@ -1,110 +0,0 @@ -'use strict'; -/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ -/*! (c) Andrea Giammarchi */ -function disconnected(poly) {'use strict'; - var CONNECTED = 'connected'; - var DISCONNECTED = 'dis' + CONNECTED; - var Event = poly.Event; - var WeakSet = poly.WeakSet; - var notObserving = true; - var observer = new WeakSet; - return function observe(node) { - if (notObserving) { - notObserving = !notObserving; - startObserving(node.ownerDocument); - } - observer.add(node); - return node; - }; - function startObserving(document) { - var dispatched = null; - try { - (new MutationObserver(changes)).observe( - document, - {subtree: true, childList: true} - ); - } - catch(o_O) { - var timer = 0; - var records = []; - var reschedule = function (record) { - records.push(record); - clearTimeout(timer); - timer = setTimeout( - function () { - changes(records.splice(timer = 0, records.length)); - }, - 0 - ); - }; - document.addEventListener( - 'DOMNodeRemoved', - function (event) { - reschedule({addedNodes: [], removedNodes: [event.target]}); - }, - true - ); - document.addEventListener( - 'DOMNodeInserted', - function (event) { - reschedule({addedNodes: [event.target], removedNodes: []}); - }, - true - ); - } - function changes(records) { - dispatched = new Tracker; - for (var - record, - length = records.length, - i = 0; i < length; i++ - ) { - record = records[i]; - dispatchAll(record.removedNodes, DISCONNECTED, CONNECTED); - dispatchAll(record.addedNodes, CONNECTED, DISCONNECTED); - } - dispatched = null; - } - function dispatchAll(nodes, type, counter) { - for (var - node, - event = new Event(type), - length = nodes.length, - i = 0; i < length; - (node = nodes[i++]).nodeType === 1 && - dispatchTarget(node, event, type, counter) - ); - } - function dispatchTarget(node, event, type, counter) { - if (observer.has(node) && !dispatched[type].has(node)) { - dispatched[counter].delete(node); - dispatched[type].add(node); - node.dispatchEvent(event); - /* - // The event is not bubbling (perf reason: should it?), - // hence there's no way to know if - // stop/Immediate/Propagation() was called. - // Should DOM Level 0 work at all? - // I say it's a YAGNI case for the time being, - // and easy to implement in user-land. - if (!event.cancelBubble) { - var fn = node['on' + type]; - if (fn) - fn.call(node, event); - } - */ - } - for (var - children = node.children, - length = children.length, - i = 0; i < length; - dispatchTarget(children[i++], event, type, counter) - ); - } - function Tracker() { - this[CONNECTED] = new WeakSet; - this[DISCONNECTED] = new WeakSet; - } - } -} -Object.defineProperty(exports, '__esModule', {value: true}).default = disconnected; diff --git a/cjs/3rd/domdiff.js b/cjs/3rd/domdiff.js deleted file mode 100644 index 67b3b079..00000000 --- a/cjs/3rd/domdiff.js +++ /dev/null @@ -1,223 +0,0 @@ -'use strict'; -/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ -/*! (c) 2018 Andrea Giammarchi (ISC) */ - -const { - eqeq, identity, indexOf, isReversed, next, append, remove, smartDiff -} = require('./utils.js'); - -const domdiff = ( - parentNode, // where changes happen - currentNodes, // Array of current items/nodes - futureNodes, // Array of future items/nodes - options // optional object with one of the following properties - // before: domNode - // compare(generic, generic) => true if same generic - // node(generic) => Node -) => { - if (!options) - options = {}; - - const compare = options.compare || eqeq; - const get = options.node || identity; - const before = options.before == null ? null : get(options.before, 0); - - const currentLength = currentNodes.length; - let currentEnd = currentLength; - let currentStart = 0; - - let futureEnd = futureNodes.length; - let futureStart = 0; - - // common prefix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentStart], futureNodes[futureStart]) - ) { - currentStart++; - futureStart++; - } - - // common suffix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1]) - ) { - currentEnd--; - futureEnd--; - } - - const currentSame = currentStart === currentEnd; - const futureSame = futureStart === futureEnd; - - // same list - if (currentSame && futureSame) - return futureNodes; - - // only stuff to add - if (currentSame && futureStart < futureEnd) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentStart, currentLength, before) - ); - return futureNodes; - } - - // only stuff to remove - if (futureSame && currentStart < currentEnd) { - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - const currentChanges = currentEnd - currentStart; - const futureChanges = futureEnd - futureStart; - let i = -1; - - // 2 simple indels: the shortest sequence is a subsequence of the longest - if (currentChanges < futureChanges) { - i = indexOf( - futureNodes, - futureStart, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ); - // inner diff - if (-1 < i) { - append( - get, - parentNode, - futureNodes, - futureStart, - i, - get(currentNodes[currentStart], 0) - ); - append( - get, - parentNode, - futureNodes, - i + currentChanges, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - } - /* istanbul ignore else */ - else if (futureChanges < currentChanges) { - i = indexOf( - currentNodes, - currentStart, - currentEnd, - futureNodes, - futureStart, - futureEnd, - compare - ); - // outer diff - if (-1 < i) { - remove( - get, - parentNode, - currentNodes, - currentStart, - i - ); - remove( - get, - parentNode, - currentNodes, - i + futureChanges, - currentEnd - ); - return futureNodes; - } - } - - // common case with one replacement for many nodes - // or many nodes replaced for a single one - /* istanbul ignore else */ - if ((currentChanges < 2 || futureChanges < 2)) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - get(currentNodes[currentStart], 0) - ); - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - // the half match diff part has been skipped in petit-dom - // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L391-L397 - // accordingly, I think it's safe to skip in here too - // if one day it'll come out like the speediest thing ever to do - // then I might add it in here too - - // Extra: before going too fancy, what about reversed lists ? - // This should bail out pretty quickly if that's not the case. - if ( - currentChanges === futureChanges && - isReversed( - futureNodes, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ) - ) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - - // last resort through a smart diff - smartDiff( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges, - currentLength, - compare, - before - ); - - return futureNodes; -}; - -Object.defineProperty(exports, '__esModule', {value: true}).default = domdiff; diff --git a/cjs/3rd/utils.js b/cjs/3rd/utils.js deleted file mode 100644 index 63cc0722..00000000 --- a/cjs/3rd/utils.js +++ /dev/null @@ -1,396 +0,0 @@ -'use strict'; -/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ -const append = (get, parent, children, start, end, before) => { - if ((end - start) < 2) - parent.insertBefore(get(children[start], 1), before); - else { - const fragment = parent.ownerDocument.createDocumentFragment(); - while (start < end) - fragment.appendChild(get(children[start++], 1)); - parent.insertBefore(fragment, before); - } -}; -exports.append = append; - -const eqeq = (a, b) => a == b; -exports.eqeq = eqeq; - -const identity = O => O; -exports.identity = identity; - -const indexOf = ( - moreNodes, - moreStart, - moreEnd, - lessNodes, - lessStart, - lessEnd, - compare -) => { - const length = lessEnd - lessStart; - /* istanbul ignore if */ - if (length < 1) - return -1; - while ((moreEnd - moreStart) >= length) { - let m = moreStart; - let l = lessStart; - while ( - m < moreEnd && - l < lessEnd && - compare(moreNodes[m], lessNodes[l]) - ) { - m++; - l++; - } - if (l === lessEnd) - return moreStart; - moreStart = m + 1; - } - return -1; -}; -exports.indexOf = indexOf; - -const isReversed = ( - futureNodes, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare -) => { - while ( - currentStart < currentEnd && - compare( - currentNodes[currentStart], - futureNodes[futureEnd - 1] - )) { - currentStart++; - futureEnd--; - }; - return futureEnd === 0; -}; -exports.isReversed = isReversed; - -const next = (get, list, i, length, before) => i < length ? - get(list[i], 0) : - (0 < i ? - get(list[i - 1], -0).nextSibling : - before); -exports.next = next; - -const remove = (get, parent, children, start, end) => { - if ((end - start) < 2) - parent.removeChild(get(children[start], -1)); - else { - const range = parent.ownerDocument.createRange(); - range.setStartBefore(get(children[start], -1)); - range.setEndAfter(get(children[end - 1], -1)); - range.deleteContents(); - } -}; -exports.remove = remove; - -// - - - - - - - - - - - - - - - - - - - -// diff related constants and utilities -// - - - - - - - - - - - - - - - - - - - - -const DELETION = -1; -const INSERTION = 1; -const SKIP = 0; -const SKIP_OND = 50; - -/* istanbul ignore next */ -const Rel = typeof Map === 'undefined' ? - function () { - const k = [], v = []; - return { - has: key => -1 < k.indexOf(key), - get: key => v[k.indexOf(key)], - set: (key, value) => { - const i = k.indexOf(key); - v[i < 0 ? (k.push(key) - 1) : i] = value; - } - }; - } : - Map -; - -const HS = ( - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges -) => { - - let k = 0; - /* istanbul ignore next */ - let minLen = futureChanges < currentChanges ? futureChanges : currentChanges; - const link = Array(minLen++); - const tresh = Array(minLen); - tresh[0] = -1; - - for (let i = 1; i < minLen; i++) - tresh[i] = currentEnd; - - const keymap = new Rel; - for (let i = currentStart; i < currentEnd; i++) - keymap.set(currentNodes[i], i); - - for (let i = futureStart; i < futureEnd; i++) { - const idxInOld = keymap.get(futureNodes[i]); - if (idxInOld != null) { - k = findK(tresh, minLen, idxInOld); - /* istanbul ignore else */ - if (-1 < k) { - tresh[k] = idxInOld; - link[k] = { - newi: i, - oldi: idxInOld, - prev: link[k - 1] - }; - } - } - } - - k = --minLen; - --currentEnd; - while (tresh[k] > currentEnd) --k; - - minLen = currentChanges + futureChanges - k; - const diff = Array(minLen); - let ptr = link[k]; - --futureEnd; - while (ptr) { - const {newi, oldi} = ptr; - while (futureEnd > newi) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd > oldi) { - diff[--minLen] = DELETION; - --currentEnd; - } - diff[--minLen] = SKIP; - --futureEnd; - --currentEnd; - ptr = ptr.prev; - } - while (futureEnd >= futureStart) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd >= currentStart) { - diff[--minLen] = DELETION; - --currentEnd; - } - return diff; -}; - -// this is pretty much the same petit-dom code without the delete map part -// https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L556-L561 -const OND = ( - futureNodes, - futureStart, - rows, - currentNodes, - currentStart, - cols, - compare -) => { - const length = rows + cols; - const v = []; - let d, k, r, c, pv, cv, pd; - outer: for (d = 0; d <= length; d++) { - /* istanbul ignore if */ - if (d > SKIP_OND) - return null; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - cv = v[d] = []; - for (k = -d; k <= d; k += 2) { - if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) { - c = pv[pd + k + 1]; - } else { - c = pv[pd + k - 1] + 1; - } - r = c - k; - while ( - c < cols && - r < rows && - compare( - currentNodes[currentStart + c], - futureNodes[futureStart + r] - ) - ) { - c++; - r++; - } - if (c === cols && r === rows) { - break outer; - } - cv[d + k] = c; - } - } - - const diff = Array(d / 2 + length / 2); - let diffIdx = diff.length - 1; - for (d = v.length - 1; d >= 0; d--) { - while ( - c > 0 && - r > 0 && - compare( - currentNodes[currentStart + c - 1], - futureNodes[futureStart + r - 1] - ) - ) { - // diagonal edge = equality - diff[diffIdx--] = SKIP; - c--; - r--; - } - if (!d) - break; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - k = c - r; - if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) { - // vertical edge = insertion - r--; - diff[diffIdx--] = INSERTION; - } else { - // horizontal edge = deletion - c--; - diff[diffIdx--] = DELETION; - } - } - return diff; -}; - -const applyDiff = ( - diff, - get, - parentNode, - futureNodes, - futureStart, - currentNodes, - currentStart, - currentLength, - before -) => { - const live = new Rel; - const length = diff.length; - let currentIndex = currentStart; - let i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - futureStart++; - currentIndex++; - break; - case INSERTION: - // TODO: bulk appends for sequential nodes - live.set(futureNodes[futureStart], 1); - append( - get, - parentNode, - futureNodes, - futureStart++, - futureStart, - currentIndex < currentLength ? - get(currentNodes[currentIndex], 1) : - before - ); - break; - case DELETION: - currentIndex++; - break; - } - } - i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - currentStart++; - break; - case DELETION: - // TODO: bulk removes for sequential nodes - if (live.has(currentNodes[currentStart])) - currentStart++; - else - remove( - get, - parentNode, - currentNodes, - currentStart++, - currentStart - ); - break; - } - } -}; - -const findK = (ktr, length, j) => { - let lo = 1; - let hi = length; - while (lo < hi) { - const mid = ((lo + hi) / 2) >>> 0; - if (j < ktr[mid]) - hi = mid; - else - lo = mid + 1; - } - return lo; -} - -const smartDiff = ( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges, - currentLength, - compare, - before -) => { - applyDiff( - OND( - futureNodes, - futureStart, - futureChanges, - currentNodes, - currentStart, - currentChanges, - compare - ) || - HS( - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges - ), - get, - parentNode, - futureNodes, - futureStart, - currentNodes, - currentStart, - currentLength, - before - ); -}; -exports.smartDiff = smartDiff; diff --git a/cjs/classes/Component.js b/cjs/classes/Component.js index 1667e96d..7e5c299c 100644 --- a/cjs/classes/Component.js +++ b/cjs/classes/Component.js @@ -1,5 +1,7 @@ 'use strict'; -const { Map, WeakMap } = require('../shared/poorlyfills.js'); +const CustomEvent = (m => m.__esModule ? m.default : m)(require('@ungap/custom-event')); +const Map = (m => m.__esModule ? m.default : m)(require('@ungap/essential-map')); +const WeakMap = (m => m.__esModule ? m.default : m)(require('@ungap/weakmap')); // hyperHTML.Component is a very basic class // able to create Custom Elements like components diff --git a/cjs/hyper/render.js b/cjs/hyper/render.js index 9b8a4faf..d457ef9b 100644 --- a/cjs/hyper/render.js +++ b/cjs/hyper/render.js @@ -1,5 +1,6 @@ 'use strict'; -const {WeakMap} = require('../shared/poorlyfills.js'); +const WeakMap = (m => m.__esModule ? m.default : m)(require('@ungap/weakmap')); + const {UIDC, VOID_ELEMENTS} = require('../shared/constants.js'); const Updates = (m => m.__esModule ? m.default : m)(require('../objects/Updates.js')); const { diff --git a/cjs/hyper/wire.js b/cjs/hyper/wire.js index be8eefa5..9ec20815 100644 --- a/cjs/hyper/wire.js +++ b/cjs/hyper/wire.js @@ -1,6 +1,8 @@ 'use strict'; +const WeakMap = (m => m.__esModule ? m.default : m)(require('@ungap/weakmap')); +const trim = (m => m.__esModule ? m.default : m)(require('@ungap/trim')); + const {ELEMENT_NODE, SVG_NAMESPACE} = require('../shared/constants.js'); -const {WeakMap, trim} = require('../shared/poorlyfills.js'); const {fragment} = require('../shared/easy-dom.js'); const {append, slice, unique} = require('../shared/utils.js'); const Wire = (m => m.__esModule ? m.default : m)(require('../classes/Wire.js')); diff --git a/cjs/index.d.ts b/cjs/index.d.ts deleted file mode 100644 index 02ce463a..00000000 --- a/cjs/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import hyper from ".."; -export * from '..'; -export default hyper; diff --git a/cjs/index.js b/cjs/index.js index e742253d..182e154d 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -1,6 +1,9 @@ 'use strict'; /*! (c) Andrea Giammarchi (ISC) */ +const WeakMap = (m => m.__esModule ? m.default : m)(require('@ungap/weakmap')); +const WeakSet = (m => m.__esModule ? m.default : m)(require('@ungap/essential-weakset')); +const diff = (m => m.__esModule ? m.default : m)(require('domdiff')); const Component = (m => m.__esModule ? m.default : m)(require('./classes/Component.js')); const {setup} = require('./classes/Component.js'); const Intent = (m => m.__esModule ? m.default : m)(require('./objects/Intent.js')); @@ -8,8 +11,6 @@ const {observe} = require('./objects/Updates.js'); const wire = (m => m.__esModule ? m.default : m)(require('./hyper/wire.js')); const {content, weakly} = require('./hyper/wire.js'); const render = (m => m.__esModule ? m.default : m)(require('./hyper/render.js')); -const diff = (m => m.__esModule ? m.default : m)(require('./3rd/domdiff.js')); -const { WeakMap, WeakSet } = require('./shared/poorlyfills.js'); const { G } = require('./shared/constants.js'); // all functions are self bound to the right context diff --git a/cjs/objects/Updates.js b/cjs/objects/Updates.js index f4a55cea..853ac657 100644 --- a/cjs/objects/Updates.js +++ b/cjs/objects/Updates.js @@ -1,4 +1,12 @@ 'use strict'; +const CustomEvent = (m => m.__esModule ? m.default : m)(require('@ungap/custom-event')); +const WeakSet = (m => m.__esModule ? m.default : m)(require('@ungap/essential-weakset')); +const isArray = (m => m.__esModule ? m.default : m)(require('@ungap/is-array')); +const trim = (m => m.__esModule ? m.default : m)(require('@ungap/trim')); + +const disconnected = (m => m.__esModule ? m.default : m)(require('disconnected')); +const domdiff = (m => m.__esModule ? m.default : m)(require('domdiff')); + const { CONNECTED, DISCONNECTED, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE, TEXT_NODE, OWNER_SVG_ELEMENT, SHOULD_USE_TEXT_CONTENT, G, UID, UIDC } = require('../shared/constants.js'); @@ -8,16 +16,13 @@ const Wire = (m => m.__esModule ? m.default : m)(require('../classes/Wire.js')); const Path = (m => m.__esModule ? m.default : m)(require('./Path.js')); const Style = (m => m.__esModule ? m.default : m)(require('./Style.js')); const Intent = (m => m.__esModule ? m.default : m)(require('./Intent.js')); -const domdiff = (m => m.__esModule ? m.default : m)(require('../3rd/domdiff.js')); // see /^script$/i.test(nodeName) bit down here // import { create as createElement, text } from '../shared/easy-dom.js'; const { text } = require('../shared/easy-dom.js'); -const { Event, WeakSet, isArray, trim } = require('../shared/poorlyfills.js'); const { createFragment, slice } = require('../shared/utils.js'); -const disconnected = (m => m.__esModule ? m.default : m)(require('../3rd/disconnected.js')); const { document } = G; -const observe = disconnected({Event, WeakSet}); +const observe = disconnected({Event: CustomEvent, WeakSet}); // a basic dictionary used to filter already cached attributes // while looking for special hyperHTML values. diff --git a/cjs/shared/domdiff.js b/cjs/shared/domdiff.js deleted file mode 100644 index 67b3b079..00000000 --- a/cjs/shared/domdiff.js +++ /dev/null @@ -1,223 +0,0 @@ -'use strict'; -/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ -/*! (c) 2018 Andrea Giammarchi (ISC) */ - -const { - eqeq, identity, indexOf, isReversed, next, append, remove, smartDiff -} = require('./utils.js'); - -const domdiff = ( - parentNode, // where changes happen - currentNodes, // Array of current items/nodes - futureNodes, // Array of future items/nodes - options // optional object with one of the following properties - // before: domNode - // compare(generic, generic) => true if same generic - // node(generic) => Node -) => { - if (!options) - options = {}; - - const compare = options.compare || eqeq; - const get = options.node || identity; - const before = options.before == null ? null : get(options.before, 0); - - const currentLength = currentNodes.length; - let currentEnd = currentLength; - let currentStart = 0; - - let futureEnd = futureNodes.length; - let futureStart = 0; - - // common prefix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentStart], futureNodes[futureStart]) - ) { - currentStart++; - futureStart++; - } - - // common suffix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1]) - ) { - currentEnd--; - futureEnd--; - } - - const currentSame = currentStart === currentEnd; - const futureSame = futureStart === futureEnd; - - // same list - if (currentSame && futureSame) - return futureNodes; - - // only stuff to add - if (currentSame && futureStart < futureEnd) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentStart, currentLength, before) - ); - return futureNodes; - } - - // only stuff to remove - if (futureSame && currentStart < currentEnd) { - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - const currentChanges = currentEnd - currentStart; - const futureChanges = futureEnd - futureStart; - let i = -1; - - // 2 simple indels: the shortest sequence is a subsequence of the longest - if (currentChanges < futureChanges) { - i = indexOf( - futureNodes, - futureStart, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ); - // inner diff - if (-1 < i) { - append( - get, - parentNode, - futureNodes, - futureStart, - i, - get(currentNodes[currentStart], 0) - ); - append( - get, - parentNode, - futureNodes, - i + currentChanges, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - } - /* istanbul ignore else */ - else if (futureChanges < currentChanges) { - i = indexOf( - currentNodes, - currentStart, - currentEnd, - futureNodes, - futureStart, - futureEnd, - compare - ); - // outer diff - if (-1 < i) { - remove( - get, - parentNode, - currentNodes, - currentStart, - i - ); - remove( - get, - parentNode, - currentNodes, - i + futureChanges, - currentEnd - ); - return futureNodes; - } - } - - // common case with one replacement for many nodes - // or many nodes replaced for a single one - /* istanbul ignore else */ - if ((currentChanges < 2 || futureChanges < 2)) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - get(currentNodes[currentStart], 0) - ); - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - // the half match diff part has been skipped in petit-dom - // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L391-L397 - // accordingly, I think it's safe to skip in here too - // if one day it'll come out like the speediest thing ever to do - // then I might add it in here too - - // Extra: before going too fancy, what about reversed lists ? - // This should bail out pretty quickly if that's not the case. - if ( - currentChanges === futureChanges && - isReversed( - futureNodes, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ) - ) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - - // last resort through a smart diff - smartDiff( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges, - currentLength, - compare, - before - ); - - return futureNodes; -}; - -Object.defineProperty(exports, '__esModule', {value: true}).default = domdiff; diff --git a/cjs/shared/poorlyfills.js b/cjs/shared/poorlyfills.js deleted file mode 100644 index a4f44b34..00000000 --- a/cjs/shared/poorlyfills.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; -const {G, UID} = require('./constants.js'); - -// you know that kind of basics you need to cover -// your use case only but you don't want to bloat the library? -// There's even a package in here: -// https://www.npmjs.com/package/poorlyfills - -// used to dispatch simple events -let Event = G.Event; -try { - new Event('Event'); -} catch(o_O) { - Event = function (type) { - const e = document.createEvent('Event'); - e.initEvent(type, false, false); - return e; - }; -} -exports.Event = Event; - -// used to store template literals -/* istanbul ignore next */ -const Map = G.Map || function Map() { - const keys = [], values = []; - return { - get(obj) { - return values[keys.indexOf(obj)]; - }, - set(obj, value) { - values[keys.push(obj) - 1] = value; - } - }; -}; -exports.Map = Map; - -// used to store wired content -let ID = 0; -const WeakMap = G.WeakMap || function WeakMap() { - const key = UID + ID++; - return { - delete(obj) { delete obj[key]; }, - get(obj) { return obj[key]; }, - set(obj, value) { - Object.defineProperty(obj, key, { - configurable: true, - value - }); - } - }; -}; -exports.WeakMap = WeakMap; - -// used to store hyper.Components -const WeakSet = G.WeakSet || function WeakSet() { - const wm = new WeakMap; - return { - delete(obj) { wm.delete(obj); }, - add(obj) { wm.set(obj, true); }, - has(obj) { return wm.get(obj) === true; } - }; -}; -exports.WeakSet = WeakSet; - -// used to be sure IE9 or older Androids work as expected -const isArray = Array.isArray || (toString => - arr => toString.call(arr) === '[object Array]' -)({}.toString); -exports.isArray = isArray; - -const trim = UID.trim || function () { - return this.replace(/^\s+|\s+$/g, ''); -}; -exports.trim = trim; diff --git a/cjs/shared/utils.js b/cjs/shared/utils.js index 2c02c0de..72ddddb3 100644 --- a/cjs/shared/utils.js +++ b/cjs/shared/utils.js @@ -1,4 +1,7 @@ 'use strict'; +const Map = (m => m.__esModule ? m.default : m)(require('@ungap/essential-map')); +const WeakMap = (m => m.__esModule ? m.default : m)(require('@ungap/weakmap')); + const {attrName, attrSeeker} = require('./re.js'); const { @@ -19,8 +22,6 @@ const { const {create, doc, fragment} = require('./easy-dom.js'); -const {Map, WeakMap} = require('./poorlyfills.js'); - // appends an array of nodes // to a generic node/fragment // When available, uses append passing all arguments at once diff --git a/esm.js b/esm.js index 800f1b13..b7f9af46 100644 --- a/esm.js +++ b/esm.js @@ -1,4 +1,5 @@ -/*! (c) Andrea Giammarchi (ISC) */var hyperHTML=function(e){"use strict";function t(){return this}function n(e){this.childNodes=e,this.length=e.length,this.first=e[0],this.last=e[this.length-1],this._=null}function r(){}function i(e){var t=Fe.get(this);return t&&t.template===Y(e)?a.apply(t.updates,arguments):o.apply(this,arguments),this}function o(e){e=Y(e);var t=Be.get(e)||u.call(this,e),n=U(this.ownerDocument,t.fragment),r=ze.create(n,t.paths);Fe.set(this,{template:e,updates:r}),a.apply(r,arguments),this.textContent="",this.appendChild(n)}function a(){for(var e=arguments.length,t=1;t\"'=]+",M="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",$="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",_=new RegExp(M+L+$+"+)([ "+T+"]*/?>)","g"),D=new RegExp(M+L+$+"*)([ "+T+"]*/>)","g"),P=function(e,t){return R(e).createElement(t)},R=function(e){return e.ownerDocument||e},H=function(e){return R(e).createDocumentFragment()},W=function(e,t){return R(e).createTextNode(t)},z=H(document),F="append"in z,B="content"in P(document,"template");z.appendChild(W(z,"g")),z.appendChild(W(z,""));var Z=1===z.cloneNode(!0).childNodes.length,V="importNode"in document,G=F?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",G(r,X.call(n.querySelectorAll(i)))}else n.innerHTML=t,G(r,X.call(n.childNodes));return r},ne=B?function(e,t){var n=H(e),r=R(e).createElementNS(s,"svg");return r.innerHTML=t,G(n,X.call(r.childNodes)),n}:function(e,t){var n=H(e),r=P(e,"div");return r.innerHTML=''+t+"",G(n,X.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=H(this.first)),(t||e)&&G(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=R(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var re=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ie(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ie(t,n,e);return t},ie=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},oe={create:function(e,t,n){return{type:e,name:n,node:t,path:re(t)}},find:function(e,t){for(var n=t.length,r=0;r=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var g=Array(l),b=f[c];for(--n;b;){for(var w=b,y=w.newi,N=w.oldi;n>y;)g[--l]=1,--n;for(;a>N;)g[--l]=-1,--a;g[--l]=0,--n,--a,b=b.prev}for(;n>=t;)g[--l]=1,--n;for(;a>=o;)g[--l]=-1,--a;return g},Ne=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,f=void 0,s=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],f=-l;f<=l;f+=2){for(d=f===-l||f!==l&&h[p+f-1]=0;l--){for(;d>0&&s>0&&a(r[i+d-1],e[t+s-1]);)m[g--]=0,d--,s--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],f=d-s,f===-l||f!==l&&h[p+f-1]>>0;n"},Ge=new b,Ie=function(e,t){return null==e?qe(t||"html"):Je(e,t||"html")},qe=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=Y(u);var c=o!==u;return c&&(o=u,r=H(document),n="svg"===e?document.createElementNS(s,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&G(r,X.call(n.childNodes)),t=Ke(r)),t}},Je=function(e,t){var n=t.indexOf(":"),r=Ge.get(e),i=t;return-1=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var b=Array(l),w=s[c];for(--n;w;){for(var y=w,N=y.newi,E=y.oldi;n>N;)b[--l]=1,--n;for(;a>E;)b[--l]=-1,--a;b[--l]=0,--n,--a,w=w.prev}for(;n>=t;)b[--l]=1,--n;for(;a>=o;)b[--l]=-1,--a;return b},k=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,s=void 0,f=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],s=-l;s<=l;s+=2){for(d=s===-l||s!==l&&h[p+s-1]=0;l--){for(;d>0&&f>0&&a(r[i+d-1],e[t+f-1]);)m[g--]=0,d--,f--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],s=d-f,s===-l||s!==l&&h[p+s-1]>>0;n\"'=]+",J="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",K="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",Q=new RegExp(J+q+K+"+)([ "+I+"]*/?>)","g"),U=new RegExp(J+q+K+"*)([ "+I+"]*/>)","g"),X=function(e,t){return Y(e).createElement(t)},Y=function(e){return e.ownerDocument||e},ee=function(e){return Y(e).createDocumentFragment()},te=function(e,t){return Y(e).createTextNode(t)},ne=ee(document),re="append"in ne,ie="content"in X(document,"template");ne.appendChild(te(ne,"g")),ne.appendChild(te(ne,""));var oe=1===ne.cloneNode(!0).childNodes.length,ae="importNode"in document,ue=re?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",ue(r,ve.call(n.querySelectorAll(i)))}else n.innerHTML=t,ue(r,ve.call(n.childNodes));return r},be=ie?function(e,t){var n=ee(e),r=Y(e).createElementNS(B,"svg");return r.innerHTML=t,ue(n,ve.call(r.childNodes)),n}:function(e,t){var n=ee(e),r=X(e,"div");return r.innerHTML=''+t+"",ue(n,ve.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=ee(this.first)),(t||e)&&ue(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=Y(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var we=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ye(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ye(t,n,e);return t},ye=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},Ne={create:function(e,t,n){return{type:e,name:n,node:t,path:we(t)}},find:function(e,t){for(var n=t.length,r=0;r"},qe=new s,Je=function(e,t){return null==e?Ke(t||"html"):Qe(e,t||"html")},Ke=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=pe(u);var c=o!==u;return c&&(o=u,r=ee(document),n="svg"===e?document.createElementNS(B,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&ue(r,ve.call(n.childNodes)),t=Ue(r)),t}},Qe=function(e,t){var n=t.indexOf(":"),r=qe.get(e),i=t;return-1 true if same generic - // node(generic) => Node -) => { - if (!options) - options = {}; - - const compare = options.compare || eqeq; - const get = options.node || identity; - const before = options.before == null ? null : get(options.before, 0); - - const currentLength = currentNodes.length; - let currentEnd = currentLength; - let currentStart = 0; - - let futureEnd = futureNodes.length; - let futureStart = 0; - - // common prefix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentStart], futureNodes[futureStart]) - ) { - currentStart++; - futureStart++; - } - - // common suffix - while ( - currentStart < currentEnd && - futureStart < futureEnd && - compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1]) - ) { - currentEnd--; - futureEnd--; - } - - const currentSame = currentStart === currentEnd; - const futureSame = futureStart === futureEnd; - - // same list - if (currentSame && futureSame) - return futureNodes; - - // only stuff to add - if (currentSame && futureStart < futureEnd) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentStart, currentLength, before) - ); - return futureNodes; - } - - // only stuff to remove - if (futureSame && currentStart < currentEnd) { - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - const currentChanges = currentEnd - currentStart; - const futureChanges = futureEnd - futureStart; - let i = -1; - - // 2 simple indels: the shortest sequence is a subsequence of the longest - if (currentChanges < futureChanges) { - i = indexOf( - futureNodes, - futureStart, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ); - // inner diff - if (-1 < i) { - append( - get, - parentNode, - futureNodes, - futureStart, - i, - get(currentNodes[currentStart], 0) - ); - append( - get, - parentNode, - futureNodes, - i + currentChanges, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - } - /* istanbul ignore else */ - else if (futureChanges < currentChanges) { - i = indexOf( - currentNodes, - currentStart, - currentEnd, - futureNodes, - futureStart, - futureEnd, - compare - ); - // outer diff - if (-1 < i) { - remove( - get, - parentNode, - currentNodes, - currentStart, - i - ); - remove( - get, - parentNode, - currentNodes, - i + futureChanges, - currentEnd - ); - return futureNodes; - } - } - - // common case with one replacement for many nodes - // or many nodes replaced for a single one - /* istanbul ignore else */ - if ((currentChanges < 2 || futureChanges < 2)) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - get(currentNodes[currentStart], 0) - ); - remove( - get, - parentNode, - currentNodes, - currentStart, - currentEnd - ); - return futureNodes; - } - - // the half match diff part has been skipped in petit-dom - // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L391-L397 - // accordingly, I think it's safe to skip in here too - // if one day it'll come out like the speediest thing ever to do - // then I might add it in here too - - // Extra: before going too fancy, what about reversed lists ? - // This should bail out pretty quickly if that's not the case. - if ( - currentChanges === futureChanges && - isReversed( - futureNodes, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare - ) - ) { - append( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - next(get, currentNodes, currentEnd, currentLength, before) - ); - return futureNodes; - } - - // last resort through a smart diff - smartDiff( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges, - currentLength, - compare, - before - ); - - return futureNodes; -}; - -export default domdiff; diff --git a/esm/3rd/utils.js b/esm/3rd/utils.js deleted file mode 100644 index ec6cdd82..00000000 --- a/esm/3rd/utils.js +++ /dev/null @@ -1,387 +0,0 @@ -/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ -export const append = (get, parent, children, start, end, before) => { - if ((end - start) < 2) - parent.insertBefore(get(children[start], 1), before); - else { - const fragment = parent.ownerDocument.createDocumentFragment(); - while (start < end) - fragment.appendChild(get(children[start++], 1)); - parent.insertBefore(fragment, before); - } -}; - -export const eqeq = (a, b) => a == b; - -export const identity = O => O; - -export const indexOf = ( - moreNodes, - moreStart, - moreEnd, - lessNodes, - lessStart, - lessEnd, - compare -) => { - const length = lessEnd - lessStart; - /* istanbul ignore if */ - if (length < 1) - return -1; - while ((moreEnd - moreStart) >= length) { - let m = moreStart; - let l = lessStart; - while ( - m < moreEnd && - l < lessEnd && - compare(moreNodes[m], lessNodes[l]) - ) { - m++; - l++; - } - if (l === lessEnd) - return moreStart; - moreStart = m + 1; - } - return -1; -}; - -export const isReversed = ( - futureNodes, - futureEnd, - currentNodes, - currentStart, - currentEnd, - compare -) => { - while ( - currentStart < currentEnd && - compare( - currentNodes[currentStart], - futureNodes[futureEnd - 1] - )) { - currentStart++; - futureEnd--; - }; - return futureEnd === 0; -}; - -export const next = (get, list, i, length, before) => i < length ? - get(list[i], 0) : - (0 < i ? - get(list[i - 1], -0).nextSibling : - before); - -export const remove = (get, parent, children, start, end) => { - if ((end - start) < 2) - parent.removeChild(get(children[start], -1)); - else { - const range = parent.ownerDocument.createRange(); - range.setStartBefore(get(children[start], -1)); - range.setEndAfter(get(children[end - 1], -1)); - range.deleteContents(); - } -}; - -// - - - - - - - - - - - - - - - - - - - -// diff related constants and utilities -// - - - - - - - - - - - - - - - - - - - - -const DELETION = -1; -const INSERTION = 1; -const SKIP = 0; -const SKIP_OND = 50; - -/* istanbul ignore next */ -const Rel = typeof Map === 'undefined' ? - function () { - const k = [], v = []; - return { - has: key => -1 < k.indexOf(key), - get: key => v[k.indexOf(key)], - set: (key, value) => { - const i = k.indexOf(key); - v[i < 0 ? (k.push(key) - 1) : i] = value; - } - }; - } : - Map -; - -const HS = ( - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges -) => { - - let k = 0; - /* istanbul ignore next */ - let minLen = futureChanges < currentChanges ? futureChanges : currentChanges; - const link = Array(minLen++); - const tresh = Array(minLen); - tresh[0] = -1; - - for (let i = 1; i < minLen; i++) - tresh[i] = currentEnd; - - const keymap = new Rel; - for (let i = currentStart; i < currentEnd; i++) - keymap.set(currentNodes[i], i); - - for (let i = futureStart; i < futureEnd; i++) { - const idxInOld = keymap.get(futureNodes[i]); - if (idxInOld != null) { - k = findK(tresh, minLen, idxInOld); - /* istanbul ignore else */ - if (-1 < k) { - tresh[k] = idxInOld; - link[k] = { - newi: i, - oldi: idxInOld, - prev: link[k - 1] - }; - } - } - } - - k = --minLen; - --currentEnd; - while (tresh[k] > currentEnd) --k; - - minLen = currentChanges + futureChanges - k; - const diff = Array(minLen); - let ptr = link[k]; - --futureEnd; - while (ptr) { - const {newi, oldi} = ptr; - while (futureEnd > newi) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd > oldi) { - diff[--minLen] = DELETION; - --currentEnd; - } - diff[--minLen] = SKIP; - --futureEnd; - --currentEnd; - ptr = ptr.prev; - } - while (futureEnd >= futureStart) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd >= currentStart) { - diff[--minLen] = DELETION; - --currentEnd; - } - return diff; -}; - -// this is pretty much the same petit-dom code without the delete map part -// https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L556-L561 -const OND = ( - futureNodes, - futureStart, - rows, - currentNodes, - currentStart, - cols, - compare -) => { - const length = rows + cols; - const v = []; - let d, k, r, c, pv, cv, pd; - outer: for (d = 0; d <= length; d++) { - /* istanbul ignore if */ - if (d > SKIP_OND) - return null; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - cv = v[d] = []; - for (k = -d; k <= d; k += 2) { - if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) { - c = pv[pd + k + 1]; - } else { - c = pv[pd + k - 1] + 1; - } - r = c - k; - while ( - c < cols && - r < rows && - compare( - currentNodes[currentStart + c], - futureNodes[futureStart + r] - ) - ) { - c++; - r++; - } - if (c === cols && r === rows) { - break outer; - } - cv[d + k] = c; - } - } - - const diff = Array(d / 2 + length / 2); - let diffIdx = diff.length - 1; - for (d = v.length - 1; d >= 0; d--) { - while ( - c > 0 && - r > 0 && - compare( - currentNodes[currentStart + c - 1], - futureNodes[futureStart + r - 1] - ) - ) { - // diagonal edge = equality - diff[diffIdx--] = SKIP; - c--; - r--; - } - if (!d) - break; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - k = c - r; - if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) { - // vertical edge = insertion - r--; - diff[diffIdx--] = INSERTION; - } else { - // horizontal edge = deletion - c--; - diff[diffIdx--] = DELETION; - } - } - return diff; -}; - -const applyDiff = ( - diff, - get, - parentNode, - futureNodes, - futureStart, - currentNodes, - currentStart, - currentLength, - before -) => { - const live = new Rel; - const length = diff.length; - let currentIndex = currentStart; - let i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - futureStart++; - currentIndex++; - break; - case INSERTION: - // TODO: bulk appends for sequential nodes - live.set(futureNodes[futureStart], 1); - append( - get, - parentNode, - futureNodes, - futureStart++, - futureStart, - currentIndex < currentLength ? - get(currentNodes[currentIndex], 1) : - before - ); - break; - case DELETION: - currentIndex++; - break; - } - } - i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - currentStart++; - break; - case DELETION: - // TODO: bulk removes for sequential nodes - if (live.has(currentNodes[currentStart])) - currentStart++; - else - remove( - get, - parentNode, - currentNodes, - currentStart++, - currentStart - ); - break; - } - } -}; - -const findK = (ktr, length, j) => { - let lo = 1; - let hi = length; - while (lo < hi) { - const mid = ((lo + hi) / 2) >>> 0; - if (j < ktr[mid]) - hi = mid; - else - lo = mid + 1; - } - return lo; -} - -export const smartDiff = ( - get, - parentNode, - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges, - currentLength, - compare, - before -) => { - applyDiff( - OND( - futureNodes, - futureStart, - futureChanges, - currentNodes, - currentStart, - currentChanges, - compare - ) || - HS( - futureNodes, - futureStart, - futureEnd, - futureChanges, - currentNodes, - currentStart, - currentEnd, - currentChanges - ), - get, - parentNode, - futureNodes, - futureStart, - currentNodes, - currentStart, - currentLength, - before - ); -}; diff --git a/esm/classes/Component.js b/esm/classes/Component.js index 04dc3fc2..ca73f2cb 100644 --- a/esm/classes/Component.js +++ b/esm/classes/Component.js @@ -1,4 +1,6 @@ -import { Map, WeakMap } from '../shared/poorlyfills.js'; +import CustomEvent from '@ungap/custom-event'; +import Map from '@ungap/essential-map'; +import WeakMap from '@ungap/weakmap'; // hyperHTML.Component is a very basic class // able to create Custom Elements like components diff --git a/esm/hyper/render.js b/esm/hyper/render.js index 05d27f30..6131cd1c 100644 --- a/esm/hyper/render.js +++ b/esm/hyper/render.js @@ -1,4 +1,5 @@ -import {WeakMap} from '../shared/poorlyfills.js'; +import WeakMap from '@ungap/weakmap'; + import {UIDC, VOID_ELEMENTS} from '../shared/constants.js'; import Updates from '../objects/Updates.js'; import { diff --git a/esm/hyper/wire.js b/esm/hyper/wire.js index 47b02a7c..fc41c91d 100644 --- a/esm/hyper/wire.js +++ b/esm/hyper/wire.js @@ -1,5 +1,7 @@ +import WeakMap from '@ungap/weakmap'; +import trim from '@ungap/trim'; + import {ELEMENT_NODE, SVG_NAMESPACE} from '../shared/constants.js'; -import {WeakMap, trim} from '../shared/poorlyfills.js'; import {fragment} from '../shared/easy-dom.js'; import {append, slice, unique} from '../shared/utils.js'; import Wire from '../classes/Wire.js'; diff --git a/esm/index.js b/esm/index.js index 013b4919..ec4b515d 100644 --- a/esm/index.js +++ b/esm/index.js @@ -1,12 +1,13 @@ /*! (c) Andrea Giammarchi (ISC) */ +import WeakMap from '@ungap/weakmap'; +import WeakSet from '@ungap/essential-weakset'; +import diff from 'domdiff'; import Component, {setup} from './classes/Component.js'; import Intent from './objects/Intent.js'; import {observe} from './objects/Updates.js'; import wire, {content, weakly} from './hyper/wire.js'; import render from './hyper/render.js'; -import diff from './3rd/domdiff.js'; -import { WeakMap, WeakSet } from './shared/poorlyfills.js'; import { G } from './shared/constants.js'; // all functions are self bound to the right context diff --git a/esm/objects/Updates.js b/esm/objects/Updates.js index 9ebb0f72..e494ded6 100644 --- a/esm/objects/Updates.js +++ b/esm/objects/Updates.js @@ -1,3 +1,11 @@ +import CustomEvent from '@ungap/custom-event'; +import WeakSet from '@ungap/essential-weakset'; +import isArray from '@ungap/is-array'; +import trim from '@ungap/trim'; + +import disconnected from 'disconnected'; +import domdiff from 'domdiff'; + import { CONNECTED, DISCONNECTED, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE, TEXT_NODE, @@ -11,16 +19,13 @@ import Wire from '../classes/Wire.js'; import Path from './Path.js'; import Style from './Style.js'; import Intent from './Intent.js'; -import domdiff from '../3rd/domdiff.js'; // see /^script$/i.test(nodeName) bit down here // import { create as createElement, text } from '../shared/easy-dom.js'; import { text } from '../shared/easy-dom.js'; -import { Event, WeakSet, isArray, trim } from '../shared/poorlyfills.js'; import { createFragment, slice } from '../shared/utils.js'; -import disconnected from '../3rd/disconnected.js'; const { document } = G; -const observe = disconnected({Event, WeakSet}); +const observe = disconnected({Event: CustomEvent, WeakSet}); // a basic dictionary used to filter already cached attributes // while looking for special hyperHTML values. diff --git a/esm/shared/poorlyfills.js b/esm/shared/poorlyfills.js deleted file mode 100644 index 4dcfaf27..00000000 --- a/esm/shared/poorlyfills.js +++ /dev/null @@ -1,68 +0,0 @@ -import {G, UID} from './constants.js'; - -// you know that kind of basics you need to cover -// your use case only but you don't want to bloat the library? -// There's even a package in here: -// https://www.npmjs.com/package/poorlyfills - -// used to dispatch simple events -let Event = G.Event; -try { - new Event('Event'); -} catch(o_O) { - Event = function (type) { - const e = document.createEvent('Event'); - e.initEvent(type, false, false); - return e; - }; -} -export {Event}; - -// used to store template literals -/* istanbul ignore next */ -export const Map = G.Map || function Map() { - const keys = [], values = []; - return { - get(obj) { - return values[keys.indexOf(obj)]; - }, - set(obj, value) { - values[keys.push(obj) - 1] = value; - } - }; -}; - -// used to store wired content -let ID = 0; -export const WeakMap = G.WeakMap || function WeakMap() { - const key = UID + ID++; - return { - delete(obj) { delete obj[key]; }, - get(obj) { return obj[key]; }, - set(obj, value) { - Object.defineProperty(obj, key, { - configurable: true, - value - }); - } - }; -}; - -// used to store hyper.Components -export const WeakSet = G.WeakSet || function WeakSet() { - const wm = new WeakMap; - return { - delete(obj) { wm.delete(obj); }, - add(obj) { wm.set(obj, true); }, - has(obj) { return wm.get(obj) === true; } - }; -}; - -// used to be sure IE9 or older Androids work as expected -export const isArray = Array.isArray || (toString => - arr => toString.call(arr) === '[object Array]' -)({}.toString); - -export const trim = UID.trim || function () { - return this.replace(/^\s+|\s+$/g, ''); -}; diff --git a/esm/shared/utils.js b/esm/shared/utils.js index 9eb368a9..079d8979 100644 --- a/esm/shared/utils.js +++ b/esm/shared/utils.js @@ -1,3 +1,6 @@ +import Map from '@ungap/essential-map'; +import WeakMap from '@ungap/weakmap'; + import {attrName, attrSeeker} from './re.js'; import { @@ -18,8 +21,6 @@ import { import {create, doc, fragment} from './easy-dom.js'; -import {Map, WeakMap} from './poorlyfills.js'; - // appends an array of nodes // to a generic node/fragment // When available, uses append passing all arguments at once diff --git a/index.js b/index.js index 70c05e86..3646a2af 100644 --- a/index.js +++ b/index.js @@ -1,1107 +1,1138 @@ var hyperHTML = (function (global) { 'use strict'; - var G = document.defaultView; - - // Node.CONSTANTS - // 'cause some engine has no global Node defined - // (i.e. Node, NativeScript, basicHTML ... ) - var ELEMENT_NODE = 1; - var TEXT_NODE = 3; - var COMMENT_NODE = 8; - var DOCUMENT_FRAGMENT_NODE = 11; - - // HTML related constants - var VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i; - - // SVG related constants - var OWNER_SVG_ELEMENT = 'ownerSVGElement'; - var SVG_NAMESPACE = 'http://www.w3.org/2000/svg'; - - // Custom Elements / MutationObserver constants - var CONNECTED = 'connected'; - var DISCONNECTED = 'dis' + CONNECTED; - - // hyperHTML related constants - var EXPANDO = '_hyper: '; - var SHOULD_USE_TEXT_CONTENT = /^(?:style|textarea)$/i; - var UID = EXPANDO + (Math.random() * new Date() | 0) + ';'; - var UIDC = ''; - - // you know that kind of basics you need to cover - // your use case only but you don't want to bloat the library? - // There's even a package in here: - // https://www.npmjs.com/package/poorlyfills - - // used to dispatch simple events - var Event = G.Event; + /*! (c) Andrea Giammarchi - ISC */ + var self = null || /* istanbul ignore next */{}; try { - new Event('Event'); - } catch (o_O) { - Event = function Event(type) { - var e = document.createEvent('Event'); - e.initEvent(type, false, false); - return e; - }; + self.WeakMap = WeakMap; + } catch (WeakMap) { + // this could be better but 90% of the time + // it's everything developers need as fallback + self.WeakMap = function (id, Object) { + + var dP = Object.defineProperty; + var hOP = Object.hasOwnProperty; + var proto = WeakMap.prototype; + proto.delete = function (key) { + return this.has(key) && delete key[this._]; + }; + proto.get = function (key) { + return this.has(key) ? key[this._] : void 0; + }; + proto.has = function (key) { + return hOP.call(key, this._); + }; + proto.set = function (key, value) { + dP(key, this._, { configurable: true, value: value }); + return this; + }; + return WeakMap; + function WeakMap(iterable) { + dP(this, '_', { value: '_@ungap/weakmap' + id++ }); + if (iterable) iterable.forEach(add, this); + } + function add(pair) { + this.set(pair[0], pair[1]); + } + }(Math.random(), Object); } + var WeakMap$1 = self.WeakMap; - // used to store template literals - /* istanbul ignore next */ - var Map$1 = G.Map || function Map() { - var keys = [], - values = []; - return { - get: function get(obj) { - return values[keys.indexOf(obj)]; - }, - set: function set(obj, value) { - values[keys.push(obj) - 1] = value; - } - }; - }; + /*! (c) Andrea Giammarchi - ISC */ + var self$1 = null || /* istanbul ignore next */{}; + try { + self$1.WeakSet = WeakSet; + } catch (WeakSet) { + (function (id, dP) { + var proto = WeakSet.prototype; + proto.add = function (object) { + if (!this.has(object)) dP(object, this._, { value: true, configurable: true }); + return this; + }; + proto.has = function (object) { + return this.hasOwnProperty.call(object, this._); + }; + proto.delete = function (object) { + return this.has(object) && delete object[this._]; + }; + self$1.WeakSet = WeakSet; + function WeakSet() { - // used to store wired content - var ID = 0; - var WeakMap = G.WeakMap || function WeakMap() { - var key = UID + ID++; - return { - delete: function _delete(obj) { - delete obj[key]; - }, - get: function get(obj) { - return obj[key]; - }, - set: function set(obj, value) { - Object.defineProperty(obj, key, { - configurable: true, - value: value - }); + dP(this, '_', { value: '_@ungap/weakmap' + id++ }); } - }; - }; + })(Math.random(), Object.defineProperty); + } + var WeakSet$1 = self$1.WeakSet; - // used to store hyper.Components - var WeakSet = G.WeakSet || function WeakSet() { - var wm = new WeakMap(); - return { - delete: function _delete(obj) { - wm.delete(obj); - }, - add: function add(obj) { - wm.set(obj, true); - }, - has: function has(obj) { - return wm.get(obj) === true; + /*! (c) Andrea Giammarchi - ISC */ + var self$2 = null || /* istanbul ignore next */{}; + try { + self$2.Map = Map; + } catch (Map) { + self$2.Map = function Map() { + var i = 0; + var k = []; + var v = []; + return { + delete: function _delete(key) { + var had = contains(key); + if (had) { + k.splice(i, 1); + v.splice(i, 1); + } + return had; + }, + get: function get(key) { + return contains(key) ? v[i] : void 0; + }, + has: function has(key) { + return contains(key); + }, + set: function set(key, value) { + v[contains(key) ? i : k.push(key) - 1] = value; + return this; + } + }; + function contains(v) { + i = k.indexOf(v); + return -1 < i; } }; - }; + } + var Map$1 = self$2.Map; - // used to be sure IE9 or older Androids work as expected - var isArray = Array.isArray || function (toString) { - return function (arr) { - return toString.call(arr) === '[object Array]'; - }; - }({}.toString); + var append = function append(get, parent, children, start, end, before) { + if (end - start < 2) parent.insertBefore(get(children[start], 1), before);else { + var fragment = parent.ownerDocument.createDocumentFragment(); + while (start < end) { + fragment.appendChild(get(children[start++], 1)); + }parent.insertBefore(fragment, before); + } + }; - var trim = UID.trim || function () { - return this.replace(/^\s+|\s+$/g, ''); + var eqeq = function eqeq(a, b) { + return a == b; }; - // hyperHTML.Component is a very basic class - // able to create Custom Elements like components - // including the ability to listen to connect/disconnect - // events via onconnect/ondisconnect attributes - // Components can be created imperatively or declaratively. - // The main difference is that declared components - // will not automatically render on setState(...) - // to simplify state handling on render. - function Component() { - return this; // this is needed in Edge !!! - } + var identity = function identity(O) { + return O; + }; - // Component is lazily setup because it needs - // wire mechanism as lazy content - function setup(content) { - // there are various weakly referenced variables in here - // and mostly are to use Component.for(...) static method. - var children = new WeakMap(); - var create = Object.create; - var createEntry = function createEntry(wm, id, component) { - wm.set(id, component); - return component; - }; - var get = function get(Class, info, context, id) { - var relation = info.get(Class) || relate(Class, info); - switch (typeof id) { - case 'object': - case 'function': - var wm = relation.w || (relation.w = new WeakMap()); - return wm.get(id) || createEntry(wm, id, new Class(context)); - default: - var sm = relation.p || (relation.p = create(null)); - return sm[id] || (sm[id] = new Class(context)); - } - }; - var relate = function relate(Class, info) { - var relation = { w: null, p: null }; - info.set(Class, relation); - return relation; - }; - var set = function set(context) { - var info = new Map$1(); - children.set(context, info); - return info; - }; - // The Component Class - Object.defineProperties(Component, { - // Component.for(context[, id]) is a convenient way - // to automatically relate data/context to children components - // If not created yet, the new Component(context) is weakly stored - // and after that same instance would always be returned. - for: { - configurable: true, - value: function value(context, id) { - return get(this, children.get(context) || set(context), context, id == null ? 'default' : id); - } + var indexOf = function indexOf(moreNodes, moreStart, moreEnd, lessNodes, lessStart, lessEnd, compare) { + var length = lessEnd - lessStart; + /* istanbul ignore if */ + if (length < 1) return -1; + while (moreEnd - moreStart >= length) { + var m = moreStart; + var l = lessStart; + while (m < moreEnd && l < lessEnd && compare(moreNodes[m], lessNodes[l])) { + m++; + l++; } - }); - Object.defineProperties(Component.prototype, { - // all events are handled with the component as context - handleEvent: { - value: function value(e) { - var ct = e.currentTarget; - this['getAttribute' in ct && ct.getAttribute('data-call') || 'on' + e.type](e); - } - }, - // components will lazily define html or svg properties - // as soon as these are invoked within the .render() method - // Such render() method is not provided by the base class - // but it must be available through the Component extend. - // Declared components could implement a - // render(props) method too and use props as needed. - html: lazyGetter('html', content), - svg: lazyGetter('svg', content), - // the state is a very basic/simple mechanism inspired by Preact - state: lazyGetter('state', function () { - return this.defaultState; - }), - // it is possible to define a default state that'd be always an object otherwise - defaultState: { - get: function get() { - return {}; - } - }, - // dispatch a bubbling, cancelable, custom event - // through the first known/available node - dispatch: { - value: function value(type, detail) { - var _wire$ = this._wire$; + if (l === lessEnd) return moreStart; + moreStart = m + 1; + } + return -1; + }; - if (_wire$) { - var event = new CustomEvent(type, { - bubbles: true, - cancelable: true, - detail: detail - }); - event.component = this; - return (_wire$.dispatchEvent ? _wire$ : _wire$.childNodes[0]).dispatchEvent(event); - } - return false; - } - }, - // setting some property state through a new object - // or a callback, triggers also automatically a render - // unless explicitly specified to not do so (render === false) - setState: { - value: function value(state, render) { - var target = this.state; - var source = typeof state === 'function' ? state.call(this, target) : state; - for (var key in source) { - target[key] = source[key]; - }if (render !== false) this.render(); - return this; - } - } - }); - } + var isReversed = function isReversed(futureNodes, futureEnd, currentNodes, currentStart, currentEnd, compare) { + while (currentStart < currentEnd && compare(currentNodes[currentStart], futureNodes[futureEnd - 1])) { + currentStart++; + futureEnd--; + } return futureEnd === 0; + }; - // instead of a secret key I could've used a WeakMap - // However, attaching a property directly will result - // into better performance with thousands of components - // hanging around, and less memory pressure caused by the WeakMap - var lazyGetter = function lazyGetter(type, fn) { - var secret = '_' + type + '$'; - return { - get: function get() { - return this[secret] || setValue(this, secret, fn.call(this, type)); - }, - set: function set(value) { - setValue(this, secret, value); - } - }; + var next = function next(get, list, i, length, before) { + return i < length ? get(list[i], 0) : 0 < i ? get(list[i - 1], -0).nextSibling : before; }; - // shortcut to set value on get or set(value) - var setValue = function setValue(self, secret, value) { - return Object.defineProperty(self, secret, { - configurable: true, - value: typeof value === 'function' ? function () { - return self._wire$ = value.apply(this, arguments); - } : value - })[secret]; + var remove = function remove(get, parent, children, start, end) { + if (end - start < 2) parent.removeChild(get(children[start], -1));else { + var range = parent.ownerDocument.createRange(); + range.setStartBefore(get(children[start], -1)); + range.setEndAfter(get(children[end - 1], -1)); + range.deleteContents(); + } }; - var attributes = {}; - var intents = {}; - var keys = []; - var hasOwnProperty = intents.hasOwnProperty; + // - - - - - - - - - - - - - - - - - - - + // diff related constants and utilities + // - - - - - - - - - - - - - - - - - - - - var length = 0; + var DELETION = -1; + var INSERTION = 1; + var SKIP = 0; + var SKIP_OND = 50; - var Intent = { + var HS = function HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges) { - // used to invoke right away hyper:attributes - attributes: attributes, + var k = 0; + /* istanbul ignore next */ + var minLen = futureChanges < currentChanges ? futureChanges : currentChanges; + var link = Array(minLen++); + var tresh = Array(minLen); + tresh[0] = -1; - // hyperHTML.define('intent', (object, update) => {...}) - // can be used to define a third parts update mechanism - // when every other known mechanism failed. - // hyper.define('user', info => info.name); - // hyper(node)`

${{user}}

`; - define: function define(intent, callback) { - if (intent.indexOf('-') < 0) { - if (!(intent in intents)) { - length = keys.push(intent); + for (var i = 1; i < minLen; i++) { + tresh[i] = currentEnd; + }var keymap = new Map$1(); + for (var _i = currentStart; _i < currentEnd; _i++) { + keymap.set(currentNodes[_i], _i); + }for (var _i2 = futureStart; _i2 < futureEnd; _i2++) { + var idxInOld = keymap.get(futureNodes[_i2]); + if (idxInOld != null) { + k = findK(tresh, minLen, idxInOld); + /* istanbul ignore else */ + if (-1 < k) { + tresh[k] = idxInOld; + link[k] = { + newi: _i2, + oldi: idxInOld, + prev: link[k - 1] + }; } - intents[intent] = callback; - } else { - attributes[intent] = callback; } - }, + } - // this method is used internally as last resort - // to retrieve a value out of an object - invoke: function invoke(object, callback) { - for (var i = 0; i < length; i++) { - var key = keys[i]; - if (hasOwnProperty.call(object, key)) { - return intents[key](object[key], callback); - } + k = --minLen; + --currentEnd; + while (tresh[k] > currentEnd) { + --k; + }minLen = currentChanges + futureChanges - k; + var diff = Array(minLen); + var ptr = link[k]; + --futureEnd; + while (ptr) { + var _ptr = ptr, + newi = _ptr.newi, + oldi = _ptr.oldi; + + while (futureEnd > newi) { + diff[--minLen] = INSERTION; + --futureEnd; + } + while (currentEnd > oldi) { + diff[--minLen] = DELETION; + --currentEnd; } + diff[--minLen] = SKIP; + --futureEnd; + --currentEnd; + ptr = ptr.prev; + } + while (futureEnd >= futureStart) { + diff[--minLen] = INSERTION; + --futureEnd; + } + while (currentEnd >= currentStart) { + diff[--minLen] = DELETION; + --currentEnd; } + return diff; }; - // TODO: I'd love to code-cover RegExp too here - // these are fundamental for this library - - var spaces = ' \\f\\n\\r\\t'; - var almostEverything = '[^ ' + spaces + '\\/>"\'=]+'; - var attrName = '[ ' + spaces + ']+' + almostEverything; - var tagName = '<([A-Za-z]+[A-Za-z0-9:_-]*)((?:'; - var attrPartials = '(?:=(?:\'[^\']*?\'|"[^"]*?"|<[^>]*?>|' + almostEverything + '))?)'; - - var attrSeeker = new RegExp(tagName + attrName + attrPartials + '+)([ ' + spaces + ']*/?>)', 'g'); - - var selfClosing = new RegExp(tagName + attrName + attrPartials + '*)([ ' + spaces + ']*/>)', 'g'); + // this is pretty much the same petit-dom code without the delete map part + // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L556-L561 + var OND = function OND(futureNodes, futureStart, rows, currentNodes, currentStart, cols, compare) { + var length = rows + cols; + var v = []; + var d = void 0, + k = void 0, + r = void 0, + c = void 0, + pv = void 0, + cv = void 0, + pd = void 0; + outer: for (d = 0; d <= length; d++) { + /* istanbul ignore if */ + if (d > SKIP_OND) return null; + pd = d - 1; + /* istanbul ignore next */ + pv = d ? v[d - 1] : [0, 0]; + cv = v[d] = []; + for (k = -d; k <= d; k += 2) { + if (k === -d || k !== d && pv[pd + k - 1] < pv[pd + k + 1]) { + c = pv[pd + k + 1]; + } else { + c = pv[pd + k - 1] + 1; + } + r = c - k; + while (c < cols && r < rows && compare(currentNodes[currentStart + c], futureNodes[futureStart + r])) { + c++; + r++; + } + if (c === cols && r === rows) { + break outer; + } + cv[d + k] = c; + } + } - // these are tiny helpers to simplify most common operations needed here - var create = function create(node, type) { - return doc(node).createElement(type); + var diff = Array(d / 2 + length / 2); + var diffIdx = diff.length - 1; + for (d = v.length - 1; d >= 0; d--) { + while (c > 0 && r > 0 && compare(currentNodes[currentStart + c - 1], futureNodes[futureStart + r - 1])) { + // diagonal edge = equality + diff[diffIdx--] = SKIP; + c--; + r--; + } + if (!d) break; + pd = d - 1; + /* istanbul ignore next */ + pv = d ? v[d - 1] : [0, 0]; + k = c - r; + if (k === -d || k !== d && pv[pd + k - 1] < pv[pd + k + 1]) { + // vertical edge = insertion + r--; + diff[diffIdx--] = INSERTION; + } else { + // horizontal edge = deletion + c--; + diff[diffIdx--] = DELETION; + } + } + return diff; }; - var doc = function doc(node) { - return node.ownerDocument || node; + + var applyDiff = function applyDiff(diff, get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before) { + var live = new Map$1(); + var length = diff.length; + var currentIndex = currentStart; + var i = 0; + while (i < length) { + switch (diff[i++]) { + case SKIP: + futureStart++; + currentIndex++; + break; + case INSERTION: + // TODO: bulk appends for sequential nodes + live.set(futureNodes[futureStart], 1); + append(get, parentNode, futureNodes, futureStart++, futureStart, currentIndex < currentLength ? get(currentNodes[currentIndex], 1) : before); + break; + case DELETION: + currentIndex++; + break; + } + } + i = 0; + while (i < length) { + switch (diff[i++]) { + case SKIP: + currentStart++; + break; + case DELETION: + // TODO: bulk removes for sequential nodes + if (live.has(currentNodes[currentStart])) currentStart++;else remove(get, parentNode, currentNodes, currentStart++, currentStart); + break; + } + } }; - var fragment = function fragment(node) { - return doc(node).createDocumentFragment(); + + var findK = function findK(ktr, length, j) { + var lo = 1; + var hi = length; + while (lo < hi) { + var mid = (lo + hi) / 2 >>> 0; + if (j < ktr[mid]) hi = mid;else lo = mid + 1; + } + return lo; }; - var text = function text(node, _text) { - return doc(node).createTextNode(_text); + + var smartDiff = function smartDiff(get, parentNode, futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges, currentLength, compare, before) { + applyDiff(OND(futureNodes, futureStart, futureChanges, currentNodes, currentStart, currentChanges, compare) || HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges), get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before); }; - var testFragment = fragment(document); + /*! (c) 2018 Andrea Giammarchi (ISC) */ - // DOM4 node.append(...many) - var hasAppend = 'append' in testFragment; + var domdiff = function domdiff(parentNode, // where changes happen + currentNodes, // Array of current items/nodes + futureNodes, // Array of future items/nodes + options // optional object with one of the following properties + // before: domNode + // compare(generic, generic) => true if same generic + // node(generic) => Node + ) { + if (!options) options = {}; - // detect old browsers without HTMLTemplateElement content support - var hasContent = 'content' in create(document, 'template'); + var compare = options.compare || eqeq; + var get = options.node || identity; + var before = options.before == null ? null : get(options.before, 0); - // IE 11 has problems with cloning templates: it "forgets" empty childNodes - testFragment.appendChild(text(testFragment, 'g')); - testFragment.appendChild(text(testFragment, '')); - var hasDoomedCloneNode = testFragment.cloneNode(true).childNodes.length === 1; + var currentLength = currentNodes.length; + var currentEnd = currentLength; + var currentStart = 0; - // old browsers need to fallback to cloneNode - // Custom Elements V0 and V1 will work polyfilled - // but native implementations need importNode instead - // (specially Chromium and its old V0 implementation) - var hasImportNode = 'importNode' in document; + var futureEnd = futureNodes.length; + var futureStart = 0; - // appends an array of nodes - // to a generic node/fragment - // When available, uses append passing all arguments at once - // hoping that's somehow faster, even if append has more checks on type - var append = hasAppend ? function (node, childNodes) { - node.append.apply(node, childNodes); - } : function (node, childNodes) { - var length = childNodes.length; - for (var i = 0; i < length; i++) { - node.appendChild(childNodes[i]); + // common prefix + while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentStart], futureNodes[futureStart])) { + currentStart++; + futureStart++; } - }; - var findAttributes = new RegExp('(' + attrName + '=)([\'"]?)' + UIDC + '\\2', 'gi'); - var comments = function comments($0, $1, $2, $3) { - return '<' + $1 + $2.replace(findAttributes, replaceAttributes) + $3; - }; - var replaceAttributes = function replaceAttributes($0, $1, $2) { - return $1 + ($2 || '"') + UID + ($2 || '"'); - }; - - // given a node and a generic HTML content, - // create either an SVG or an HTML fragment - // where such content will be injected - var createFragment = function createFragment(node, html) { - return (OWNER_SVG_ELEMENT in node ? SVGFragment : HTMLFragment)(node, html.replace(attrSeeker, comments)); - }; - - // IE/Edge shenanigans proof cloneNode - // it goes through all nodes manually - // instead of relying the engine to suddenly - // merge nodes together - var cloneNode = hasDoomedCloneNode ? function (node) { - var clone = node.cloneNode(); - var childNodes = node.childNodes || - // this is an excess of caution - // but some node, in IE, might not - // have childNodes property. - // The following fallback ensure working code - // in older IE without compromising performance - // or any other browser/engine involved. - /* istanbul ignore next */ - []; - var length = childNodes.length; - for (var i = 0; i < length; i++) { - clone.appendChild(cloneNode(childNodes[i])); + // common suffix + while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1])) { + currentEnd--; + futureEnd--; } - return clone; - } : - // the following ignore is due code-coverage - // combination of not having document.importNode - // but having a working node.cloneNode. - // This shenario is common on older Android/WebKit browsers - // but basicHTML here tests just two major cases: - // with document.importNode or with broken cloneNode. - /* istanbul ignore next */ - function (node) { - return node.cloneNode(true); - }; - - // used to import html into fragments - var importNode = hasImportNode ? function (doc$$1, node) { - return doc$$1.importNode(node, true); - } : function (doc$$1, node) { - return cloneNode(node); - }; - - // just recycling a one-off array to use slice - // in every needed place - var slice = [].slice; - // lazy evaluated, returns the unique identity - // of a template literal, as tempalte literal itself. - // By default, ES2015 template literals are unique - // tag`a${1}z` === tag`a${2}z` - // even if interpolated values are different - // the template chunks are in a frozen Array - // that is identical each time you use the same - // literal to represent same static content - // around its own interpolations. - var unique = function unique(template) { - return _TL(template); - }; + var currentSame = currentStart === currentEnd; + var futureSame = futureStart === futureEnd; - // https://codepen.io/WebReflection/pen/dqZrpV?editors=0010 - // TL returns a unique version of the template - // it needs lazy feature detection - // (cannot trust literals with transpiled code) - var _TL = function TL(t) { - if ( - // TypeScript template literals are not standard - t.propertyIsEnumerable('raw') || !Object.isFrozen(t.raw) || - // Firefox < 55 has not standard implementation neither - /Firefox\/(\d+)/.test((G.navigator || {}).userAgent) && parseFloat(RegExp.$1) < 55) { - var T = {}; - _TL = function TL(t) { - var k = '^' + t.join('^'); - return T[k] || (T[k] = t); - }; - } else { - // make TL an identity like function - _TL = function TL(t) { - return t; - }; - } - return _TL(t); - }; + // same list + if (currentSame && futureSame) return futureNodes; - // used to store templates objects - // since neither Map nor WeakMap are safe - var TemplateMap = function TemplateMap() { - try { - var wm = new WeakMap(); - var o_O = Object.freeze([]); - wm.set(o_O, true); - if (!wm.get(o_O)) throw o_O; - return wm; - } catch (o_O) { - // inevitable legacy code leaks due - // https://github.com/tc39/ecma262/pull/890 - return new Map$1(); + // only stuff to add + if (currentSame && futureStart < futureEnd) { + append(get, parentNode, futureNodes, futureStart, futureEnd, next(get, currentNodes, currentStart, currentLength, before)); + return futureNodes; } - }; - // create document fragments via native template - // with a fallback for browsers that won't be able - // to deal with some injected element such or others - var HTMLFragment = hasContent ? function (node, html) { - var container = create(node, 'template'); - container.innerHTML = html; - return container.content; - } : function (node, html) { - var container = create(node, 'template'); - var content = fragment(node); - if (/^[^\S]*?<(col(?:group)?|t(?:head|body|foot|r|d|h))/i.test(html)) { - var selector = RegExp.$1; - container.innerHTML = '' + html + '
'; - append(content, slice.call(container.querySelectorAll(selector))); - } else { - container.innerHTML = html; - append(content, slice.call(container.childNodes)); + // only stuff to remove + if (futureSame && currentStart < currentEnd) { + remove(get, parentNode, currentNodes, currentStart, currentEnd); + return futureNodes; } - return content; - }; - - // creates SVG fragment with a fallback for IE that needs SVG - // within the HTML content - var SVGFragment = hasContent ? function (node, html) { - var content = fragment(node); - var container = doc(node).createElementNS(SVG_NAMESPACE, 'svg'); - container.innerHTML = html; - append(content, slice.call(container.childNodes)); - return content; - } : function (node, html) { - var content = fragment(node); - var container = create(node, 'div'); - container.innerHTML = '' + html + ''; - append(content, slice.call(container.firstChild.childNodes)); - return content; - }; - function Wire(childNodes) { - this.childNodes = childNodes; - this.length = childNodes.length; - this.first = childNodes[0]; - this.last = childNodes[this.length - 1]; - this._ = null; - } + var currentChanges = currentEnd - currentStart; + var futureChanges = futureEnd - futureStart; + var i = -1; - // when a wire is inserted, all its nodes will follow - Wire.prototype.valueOf = function valueOf(different) { - var noFragment = this._ == null; - if (noFragment) this._ = fragment(this.first); + // 2 simple indels: the shortest sequence is a subsequence of the longest + if (currentChanges < futureChanges) { + i = indexOf(futureNodes, futureStart, futureEnd, currentNodes, currentStart, currentEnd, compare); + // inner diff + if (-1 < i) { + append(get, parentNode, futureNodes, futureStart, i, get(currentNodes[currentStart], 0)); + append(get, parentNode, futureNodes, i + currentChanges, futureEnd, next(get, currentNodes, currentEnd, currentLength, before)); + return futureNodes; + } + } /* istanbul ignore else */ - if (noFragment || different) append(this._, this.childNodes); - return this._; - }; + else if (futureChanges < currentChanges) { + i = indexOf(currentNodes, currentStart, currentEnd, futureNodes, futureStart, futureEnd, compare); + // outer diff + if (-1 < i) { + remove(get, parentNode, currentNodes, currentStart, i); + remove(get, parentNode, currentNodes, i + futureChanges, currentEnd); + return futureNodes; + } + } - // when a wire is removed, all its nodes must be removed as well - Wire.prototype.remove = function remove() { - this._ = null; - var first = this.first; - var last = this.last; - if (this.length === 2) { - last.parentNode.removeChild(last); - } else { - var range = doc(first).createRange(); - range.setStartBefore(this.childNodes[1]); - range.setEndAfter(last); - range.deleteContents(); + // common case with one replacement for many nodes + // or many nodes replaced for a single one + /* istanbul ignore else */ + if (currentChanges < 2 || futureChanges < 2) { + append(get, parentNode, futureNodes, futureStart, futureEnd, get(currentNodes[currentStart], 0)); + remove(get, parentNode, currentNodes, currentStart, currentEnd); + return futureNodes; } - return first; - }; - // every template literal interpolation indicates - // a precise target in the DOM the template is representing. - // `

some ${'content'}

` - // hyperHTML finds only once per template literal, - // hence once per entire application life-cycle, - // all nodes that are related to interpolations. - // These nodes are stored as indexes used to retrieve, - // once per upgrade, nodes that will change on each future update. - // A path example is [2, 0, 1] representing the operation: - // node.childNodes[2].childNodes[0].childNodes[1] - // Attributes are addressed via their owner node and their name. - var createPath = function createPath(node) { - var path = []; - var parentNode = void 0; - switch (node.nodeType) { - case ELEMENT_NODE: - case DOCUMENT_FRAGMENT_NODE: - parentNode = node; - break; - case COMMENT_NODE: - parentNode = node.parentNode; - prepend(path, parentNode, node); - break; - default: - parentNode = node.ownerElement; - break; - } - for (node = parentNode; parentNode = parentNode.parentNode; node = parentNode) { - prepend(path, parentNode, node); + // the half match diff part has been skipped in petit-dom + // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L391-L397 + // accordingly, I think it's safe to skip in here too + // if one day it'll come out like the speediest thing ever to do + // then I might add it in here too + + // Extra: before going too fancy, what about reversed lists ? + // This should bail out pretty quickly if that's not the case. + if (currentChanges === futureChanges && isReversed(futureNodes, futureEnd, currentNodes, currentStart, currentEnd, compare)) { + append(get, parentNode, futureNodes, futureStart, futureEnd, next(get, currentNodes, currentEnd, currentLength, before)); + return futureNodes; } - return path; - }; - var prepend = function prepend(path, parent, node) { - path.unshift(path.indexOf.call(parent.childNodes, node)); - }; + // last resort through a smart diff + smartDiff(get, parentNode, futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges, currentLength, compare, before); - var Path = { - create: function create(type, node, name) { - return { type: type, name: name, node: node, path: createPath(node) }; - }, - find: function find(node, path) { - var length = path.length; - for (var i = 0; i < length; i++) { - node = node.childNodes[path[i]]; - } - return node; - } + return futureNodes; }; - // from https://github.com/developit/preact/blob/33fc697ac11762a1cb6e71e9847670d047af7ce5/src/constants.js - var IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i; + /*! (c) Andrea Giammarchi - ISC */ + var self$3 = null || /* istanbul ignore next */{}; + try { + self$3.CustomEvent = new CustomEvent('.').constructor; + } catch (CustomEvent) { + self$3.CustomEvent = function CustomEvent(type, init) { + if (!init) init = {}; + var e = document.createEvent('Event'); + var bubbles = !!init.bubbles; + var cancelable = !!init.cancelable; + e.initEvent(type, bubbles, cancelable); + e.bubbles = bubbles; + e.cancelable = cancelable; + e.detail = init.detail; + return e; + }; + } + var CustomEvent$1 = self$3.CustomEvent; - // style is handled as both string and object - // even if the target is an SVG element (consistency) - var Style = (function (node, original, isSVG) { - if (isSVG) { - var style = original.cloneNode(true); - style.value = ''; - node.setAttributeNode(style); - return update(style, isSVG); - } - return update(node.style, isSVG); - }); + // hyperHTML.Component is a very basic class + // able to create Custom Elements like components + // including the ability to listen to connect/disconnect + // events via onconnect/ondisconnect attributes + // Components can be created imperatively or declaratively. + // The main difference is that declared components + // will not automatically render on setState(...) + // to simplify state handling on render. + function Component() { + return this; // this is needed in Edge !!! + } - // the update takes care or changing/replacing - // only properties that are different or - // in case of string, the whole node - var update = function update(style, isSVG) { - var oldType = void 0, - oldValue = void 0; - return function (newValue) { - switch (typeof newValue) { + // Component is lazily setup because it needs + // wire mechanism as lazy content + function setup(content) { + // there are various weakly referenced variables in here + // and mostly are to use Component.for(...) static method. + var children = new WeakMap$1(); + var create = Object.create; + var createEntry = function createEntry(wm, id, component) { + wm.set(id, component); + return component; + }; + var get = function get(Class, info, context, id) { + var relation = info.get(Class) || relate(Class, info); + switch (typeof id) { case 'object': - if (newValue) { - if (oldType === 'object') { - if (!isSVG) { - if (oldValue !== newValue) { - for (var key in oldValue) { - if (!(key in newValue)) { - style[key] = ''; - } - } - } - } - } else { - if (isSVG) style.value = '';else style.cssText = ''; - } - var info = isSVG ? {} : style; - for (var _key in newValue) { - var value = newValue[_key]; - var styleValue = typeof value === 'number' && !IS_NON_DIMENSIONAL.test(_key) ? value + 'px' : value; - if (!isSVG && /^--/.test(_key)) info.setProperty(_key, styleValue);else info[_key] = styleValue; - } - oldType = 'object'; - if (isSVG) style.value = toStyle(oldValue = info);else oldValue = newValue; - break; - } + case 'function': + var wm = relation.w || (relation.w = new WeakMap$1()); + return wm.get(id) || createEntry(wm, id, new Class(context)); default: - if (oldValue != newValue) { - oldType = 'string'; - oldValue = newValue; - if (isSVG) style.value = newValue || '';else style.cssText = newValue || ''; + var sm = relation.p || (relation.p = create(null)); + return sm[id] || (sm[id] = new Class(context)); + } + }; + var relate = function relate(Class, info) { + var relation = { w: null, p: null }; + info.set(Class, relation); + return relation; + }; + var set = function set(context) { + var info = new Map$1(); + children.set(context, info); + return info; + }; + // The Component Class + Object.defineProperties(Component, { + // Component.for(context[, id]) is a convenient way + // to automatically relate data/context to children components + // If not created yet, the new Component(context) is weakly stored + // and after that same instance would always be returned. + for: { + configurable: true, + value: function value(context, id) { + return get(this, children.get(context) || set(context), context, id == null ? 'default' : id); + } + } + }); + Object.defineProperties(Component.prototype, { + // all events are handled with the component as context + handleEvent: { + value: function value(e) { + var ct = e.currentTarget; + this['getAttribute' in ct && ct.getAttribute('data-call') || 'on' + e.type](e); + } + }, + // components will lazily define html or svg properties + // as soon as these are invoked within the .render() method + // Such render() method is not provided by the base class + // but it must be available through the Component extend. + // Declared components could implement a + // render(props) method too and use props as needed. + html: lazyGetter('html', content), + svg: lazyGetter('svg', content), + // the state is a very basic/simple mechanism inspired by Preact + state: lazyGetter('state', function () { + return this.defaultState; + }), + // it is possible to define a default state that'd be always an object otherwise + defaultState: { + get: function get() { + return {}; + } + }, + // dispatch a bubbling, cancelable, custom event + // through the first known/available node + dispatch: { + value: function value(type, detail) { + var _wire$ = this._wire$; + + if (_wire$) { + var event = new CustomEvent$1(type, { + bubbles: true, + cancelable: true, + detail: detail + }); + event.component = this; + return (_wire$.dispatchEvent ? _wire$ : _wire$.childNodes[0]).dispatchEvent(event); } - break; + return false; + } + }, + // setting some property state through a new object + // or a callback, triggers also automatically a render + // unless explicitly specified to not do so (render === false) + setState: { + value: function value(state, render) { + var target = this.state; + var source = typeof state === 'function' ? state.call(this, target) : state; + for (var key in source) { + target[key] = source[key]; + }if (render !== false) this.render(); + return this; + } + } + }); + } + + // instead of a secret key I could've used a WeakMap + // However, attaching a property directly will result + // into better performance with thousands of components + // hanging around, and less memory pressure caused by the WeakMap + var lazyGetter = function lazyGetter(type, fn) { + var secret = '_' + type + '$'; + return { + get: function get() { + return this[secret] || setValue(this, secret, fn.call(this, type)); + }, + set: function set(value) { + setValue(this, secret, value); } }; }; - var hyphen = /([^A-Z])([A-Z]+)/g; - var ized = function ized($0, $1, $2) { - return $1 + '-' + $2.toLowerCase(); - }; - var toStyle = function toStyle(object) { - var css = []; - for (var key in object) { - css.push(key.replace(hyphen, ized), ':', object[key], ';'); - } - return css.join(''); + // shortcut to set value on get or set(value) + var setValue = function setValue(self, secret, value) { + return Object.defineProperty(self, secret, { + configurable: true, + value: typeof value === 'function' ? function () { + return self._wire$ = value.apply(this, arguments); + } : value + })[secret]; }; - /* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ - var append$1 = function append(get, parent, children, start, end, before) { - if (end - start < 2) parent.insertBefore(get(children[start], 1), before);else { - var fragment = parent.ownerDocument.createDocumentFragment(); - while (start < end) { - fragment.appendChild(get(children[start++], 1)); - }parent.insertBefore(fragment, before); - } - }; + var attributes = {}; + var intents = {}; + var keys = []; + var hasOwnProperty = intents.hasOwnProperty; - var eqeq = function eqeq(a, b) { - return a == b; - }; + var length = 0; - var identity = function identity(O) { - return O; - }; + var Intent = { - var indexOf = function indexOf(moreNodes, moreStart, moreEnd, lessNodes, lessStart, lessEnd, compare) { - var length = lessEnd - lessStart; - /* istanbul ignore if */ - if (length < 1) return -1; - while (moreEnd - moreStart >= length) { - var m = moreStart; - var l = lessStart; - while (m < moreEnd && l < lessEnd && compare(moreNodes[m], lessNodes[l])) { - m++; - l++; + // used to invoke right away hyper:attributes + attributes: attributes, + + // hyperHTML.define('intent', (object, update) => {...}) + // can be used to define a third parts update mechanism + // when every other known mechanism failed. + // hyper.define('user', info => info.name); + // hyper(node)`

${{user}}

`; + define: function define(intent, callback) { + if (intent.indexOf('-') < 0) { + if (!(intent in intents)) { + length = keys.push(intent); + } + intents[intent] = callback; + } else { + attributes[intent] = callback; + } + }, + + // this method is used internally as last resort + // to retrieve a value out of an object + invoke: function invoke(object, callback) { + for (var i = 0; i < length; i++) { + var key = keys[i]; + if (hasOwnProperty.call(object, key)) { + return intents[key](object[key], callback); + } } - if (l === lessEnd) return moreStart; - moreStart = m + 1; } - return -1; }; - var isReversed = function isReversed(futureNodes, futureEnd, currentNodes, currentStart, currentEnd, compare) { - while (currentStart < currentEnd && compare(currentNodes[currentStart], futureNodes[futureEnd - 1])) { - currentStart++; - futureEnd--; - } return futureEnd === 0; - }; + var isArray = Array.isArray || function (toString) { + var $ = toString.call([]); + return function isArray(object) { + return toString.call(object) === $; + }; + }({}.toString); - var next = function next(get, list, i, length, before) { - return i < length ? get(list[i], 0) : 0 < i ? get(list[i - 1], -0).nextSibling : before; + var trim = ''.trim || function () { + return String(this).replace(/^\s+|\s+/g, ''); }; - var remove = function remove(get, parent, children, start, end) { - if (end - start < 2) parent.removeChild(get(children[start], -1));else { - var range = parent.ownerDocument.createRange(); - range.setStartBefore(get(children[start], -1)); - range.setEndAfter(get(children[end - 1], -1)); - range.deleteContents(); + /*! (c) Andrea Giammarchi */ + function disconnected(poly) { + + var CONNECTED = 'connected'; + var DISCONNECTED = 'dis' + CONNECTED; + var Event = poly.Event; + var WeakSet = poly.WeakSet; + var notObserving = true; + var observer = new WeakSet(); + return function observe(node) { + if (notObserving) { + notObserving = !notObserving; + startObserving(node.ownerDocument); + } + observer.add(node); + return node; + }; + function startObserving(document) { + var dispatched = null; + try { + new MutationObserver(changes).observe(document, { subtree: true, childList: true }); + } catch (o_O) { + var timer = 0; + var records = []; + var reschedule = function reschedule(record) { + records.push(record); + clearTimeout(timer); + timer = setTimeout(function () { + changes(records.splice(timer = 0, records.length)); + }, 0); + }; + document.addEventListener('DOMNodeRemoved', function (event) { + reschedule({ addedNodes: [], removedNodes: [event.target] }); + }, true); + document.addEventListener('DOMNodeInserted', function (event) { + reschedule({ addedNodes: [event.target], removedNodes: [] }); + }, true); + } + function changes(records) { + dispatched = new Tracker(); + for (var record, length = records.length, i = 0; i < length; i++) { + record = records[i]; + dispatchAll(record.removedNodes, DISCONNECTED, CONNECTED); + dispatchAll(record.addedNodes, CONNECTED, DISCONNECTED); + } + dispatched = null; + } + function dispatchAll(nodes, type, counter) { + for (var node, event = new Event(type), length = nodes.length, i = 0; i < length; (node = nodes[i++]).nodeType === 1 && dispatchTarget(node, event, type, counter)) {} + } + function dispatchTarget(node, event, type, counter) { + if (observer.has(node) && !dispatched[type].has(node)) { + dispatched[counter].delete(node); + dispatched[type].add(node); + node.dispatchEvent(event); + /* + // The event is not bubbling (perf reason: should it?), + // hence there's no way to know if + // stop/Immediate/Propagation() was called. + // Should DOM Level 0 work at all? + // I say it's a YAGNI case for the time being, + // and easy to implement in user-land. + if (!event.cancelBubble) { + var fn = node['on' + type]; + if (fn) + fn.call(node, event); + } + */ + } + for (var children = node.children, length = children.length, i = 0; i < length; dispatchTarget(children[i++], event, type, counter)) {} + } + function Tracker() { + this[CONNECTED] = new WeakSet(); + this[DISCONNECTED] = new WeakSet(); + } } - }; + } - // - - - - - - - - - - - - - - - - - - - - // diff related constants and utilities - // - - - - - - - - - - - - - - - - - - - + var G = document.defaultView; - var DELETION = -1; - var INSERTION = 1; - var SKIP = 0; - var SKIP_OND = 50; + // Node.CONSTANTS + // 'cause some engine has no global Node defined + // (i.e. Node, NativeScript, basicHTML ... ) + var ELEMENT_NODE = 1; + var TEXT_NODE = 3; + var COMMENT_NODE = 8; + var DOCUMENT_FRAGMENT_NODE = 11; - /* istanbul ignore next */ - var Rel = typeof Map === 'undefined' ? function () { - var k = [], - v = []; - return { - has: function has(key) { - return -1 < k.indexOf(key); - }, - get: function get(key) { - return v[k.indexOf(key)]; - }, - set: function set(key, value) { - var i = k.indexOf(key); - v[i < 0 ? k.push(key) - 1 : i] = value; - } - }; - } : Map; + // HTML related constants + var VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i; - var HS = function HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges) { + // SVG related constants + var OWNER_SVG_ELEMENT = 'ownerSVGElement'; + var SVG_NAMESPACE = 'http://www.w3.org/2000/svg'; - var k = 0; - /* istanbul ignore next */ - var minLen = futureChanges < currentChanges ? futureChanges : currentChanges; - var link = Array(minLen++); - var tresh = Array(minLen); - tresh[0] = -1; + // Custom Elements / MutationObserver constants + var CONNECTED = 'connected'; + var DISCONNECTED = 'dis' + CONNECTED; - for (var i = 1; i < minLen; i++) { - tresh[i] = currentEnd; - }var keymap = new Rel(); - for (var _i = currentStart; _i < currentEnd; _i++) { - keymap.set(currentNodes[_i], _i); - }for (var _i2 = futureStart; _i2 < futureEnd; _i2++) { - var idxInOld = keymap.get(futureNodes[_i2]); - if (idxInOld != null) { - k = findK(tresh, minLen, idxInOld); - /* istanbul ignore else */ - if (-1 < k) { - tresh[k] = idxInOld; - link[k] = { - newi: _i2, - oldi: idxInOld, - prev: link[k - 1] - }; - } - } - } + // hyperHTML related constants + var EXPANDO = '_hyper: '; + var SHOULD_USE_TEXT_CONTENT = /^(?:style|textarea)$/i; + var UID = EXPANDO + (Math.random() * new Date() | 0) + ';'; + var UIDC = ''; - k = --minLen; - --currentEnd; - while (tresh[k] > currentEnd) { - --k; - }minLen = currentChanges + futureChanges - k; - var diff = Array(minLen); - var ptr = link[k]; - --futureEnd; - while (ptr) { - var _ptr = ptr, - newi = _ptr.newi, - oldi = _ptr.oldi; + // TODO: I'd love to code-cover RegExp too here + // these are fundamental for this library - while (futureEnd > newi) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd > oldi) { - diff[--minLen] = DELETION; - --currentEnd; - } - diff[--minLen] = SKIP; - --futureEnd; - --currentEnd; - ptr = ptr.prev; - } - while (futureEnd >= futureStart) { - diff[--minLen] = INSERTION; - --futureEnd; - } - while (currentEnd >= currentStart) { - diff[--minLen] = DELETION; - --currentEnd; - } - return diff; - }; + var spaces = ' \\f\\n\\r\\t'; + var almostEverything = '[^ ' + spaces + '\\/>"\'=]+'; + var attrName = '[ ' + spaces + ']+' + almostEverything; + var tagName = '<([A-Za-z]+[A-Za-z0-9:_-]*)((?:'; + var attrPartials = '(?:=(?:\'[^\']*?\'|"[^"]*?"|<[^>]*?>|' + almostEverything + '))?)'; - // this is pretty much the same petit-dom code without the delete map part - // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L556-L561 - var OND = function OND(futureNodes, futureStart, rows, currentNodes, currentStart, cols, compare) { - var length = rows + cols; - var v = []; - var d = void 0, - k = void 0, - r = void 0, - c = void 0, - pv = void 0, - cv = void 0, - pd = void 0; - outer: for (d = 0; d <= length; d++) { - /* istanbul ignore if */ - if (d > SKIP_OND) return null; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - cv = v[d] = []; - for (k = -d; k <= d; k += 2) { - if (k === -d || k !== d && pv[pd + k - 1] < pv[pd + k + 1]) { - c = pv[pd + k + 1]; - } else { - c = pv[pd + k - 1] + 1; - } - r = c - k; - while (c < cols && r < rows && compare(currentNodes[currentStart + c], futureNodes[futureStart + r])) { - c++; - r++; - } - if (c === cols && r === rows) { - break outer; - } - cv[d + k] = c; - } - } + var attrSeeker = new RegExp(tagName + attrName + attrPartials + '+)([ ' + spaces + ']*/?>)', 'g'); - var diff = Array(d / 2 + length / 2); - var diffIdx = diff.length - 1; - for (d = v.length - 1; d >= 0; d--) { - while (c > 0 && r > 0 && compare(currentNodes[currentStart + c - 1], futureNodes[futureStart + r - 1])) { - // diagonal edge = equality - diff[diffIdx--] = SKIP; - c--; - r--; - } - if (!d) break; - pd = d - 1; - /* istanbul ignore next */ - pv = d ? v[d - 1] : [0, 0]; - k = c - r; - if (k === -d || k !== d && pv[pd + k - 1] < pv[pd + k + 1]) { - // vertical edge = insertion - r--; - diff[diffIdx--] = INSERTION; - } else { - // horizontal edge = deletion - c--; - diff[diffIdx--] = DELETION; - } - } - return diff; + var selfClosing = new RegExp(tagName + attrName + attrPartials + '*)([ ' + spaces + ']*/>)', 'g'); + + // these are tiny helpers to simplify most common operations needed here + var create = function create(node, type) { + return doc(node).createElement(type); + }; + var doc = function doc(node) { + return node.ownerDocument || node; + }; + var fragment = function fragment(node) { + return doc(node).createDocumentFragment(); + }; + var text = function text(node, _text) { + return doc(node).createTextNode(_text); }; - var applyDiff = function applyDiff(diff, get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before) { - var live = new Rel(); - var length = diff.length; - var currentIndex = currentStart; - var i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - futureStart++; - currentIndex++; - break; - case INSERTION: - // TODO: bulk appends for sequential nodes - live.set(futureNodes[futureStart], 1); - append$1(get, parentNode, futureNodes, futureStart++, futureStart, currentIndex < currentLength ? get(currentNodes[currentIndex], 1) : before); - break; - case DELETION: - currentIndex++; - break; - } - } - i = 0; - while (i < length) { - switch (diff[i++]) { - case SKIP: - currentStart++; - break; - case DELETION: - // TODO: bulk removes for sequential nodes - if (live.has(currentNodes[currentStart])) currentStart++;else remove(get, parentNode, currentNodes, currentStart++, currentStart); - break; - } + var testFragment = fragment(document); + + // DOM4 node.append(...many) + var hasAppend = 'append' in testFragment; + + // detect old browsers without HTMLTemplateElement content support + var hasContent = 'content' in create(document, 'template'); + + // IE 11 has problems with cloning templates: it "forgets" empty childNodes + testFragment.appendChild(text(testFragment, 'g')); + testFragment.appendChild(text(testFragment, '')); + var hasDoomedCloneNode = testFragment.cloneNode(true).childNodes.length === 1; + + // old browsers need to fallback to cloneNode + // Custom Elements V0 and V1 will work polyfilled + // but native implementations need importNode instead + // (specially Chromium and its old V0 implementation) + var hasImportNode = 'importNode' in document; + + // appends an array of nodes + // to a generic node/fragment + // When available, uses append passing all arguments at once + // hoping that's somehow faster, even if append has more checks on type + var append$1 = hasAppend ? function (node, childNodes) { + node.append.apply(node, childNodes); + } : function (node, childNodes) { + var length = childNodes.length; + for (var i = 0; i < length; i++) { + node.appendChild(childNodes[i]); } }; - var findK = function findK(ktr, length, j) { - var lo = 1; - var hi = length; - while (lo < hi) { - var mid = (lo + hi) / 2 >>> 0; - if (j < ktr[mid]) hi = mid;else lo = mid + 1; - } - return lo; + var findAttributes = new RegExp('(' + attrName + '=)([\'"]?)' + UIDC + '\\2', 'gi'); + var comments = function comments($0, $1, $2, $3) { + return '<' + $1 + $2.replace(findAttributes, replaceAttributes) + $3; + }; + var replaceAttributes = function replaceAttributes($0, $1, $2) { + return $1 + ($2 || '"') + UID + ($2 || '"'); }; - var smartDiff = function smartDiff(get, parentNode, futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges, currentLength, compare, before) { - applyDiff(OND(futureNodes, futureStart, futureChanges, currentNodes, currentStart, currentChanges, compare) || HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges), get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before); + // given a node and a generic HTML content, + // create either an SVG or an HTML fragment + // where such content will be injected + var createFragment = function createFragment(node, html) { + return (OWNER_SVG_ELEMENT in node ? SVGFragment : HTMLFragment)(node, html.replace(attrSeeker, comments)); }; - /* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ + // IE/Edge shenanigans proof cloneNode + // it goes through all nodes manually + // instead of relying the engine to suddenly + // merge nodes together + var cloneNode = hasDoomedCloneNode ? function (node) { + var clone = node.cloneNode(); + var childNodes = node.childNodes || + // this is an excess of caution + // but some node, in IE, might not + // have childNodes property. + // The following fallback ensure working code + // in older IE without compromising performance + // or any other browser/engine involved. + /* istanbul ignore next */ + []; + var length = childNodes.length; + for (var i = 0; i < length; i++) { + clone.appendChild(cloneNode(childNodes[i])); + } + return clone; + } : + // the following ignore is due code-coverage + // combination of not having document.importNode + // but having a working node.cloneNode. + // This shenario is common on older Android/WebKit browsers + // but basicHTML here tests just two major cases: + // with document.importNode or with broken cloneNode. + /* istanbul ignore next */ + function (node) { + return node.cloneNode(true); + }; - var domdiff = function domdiff(parentNode, // where changes happen - currentNodes, // Array of current items/nodes - futureNodes, // Array of future items/nodes - options // optional object with one of the following properties - // before: domNode - // compare(generic, generic) => true if same generic - // node(generic) => Node - ) { - if (!options) options = {}; + // used to import html into fragments + var importNode = hasImportNode ? function (doc$$1, node) { + return doc$$1.importNode(node, true); + } : function (doc$$1, node) { + return cloneNode(node); + }; - var compare = options.compare || eqeq; - var get = options.node || identity; - var before = options.before == null ? null : get(options.before, 0); + // just recycling a one-off array to use slice + // in every needed place + var slice = [].slice; - var currentLength = currentNodes.length; - var currentEnd = currentLength; - var currentStart = 0; + // lazy evaluated, returns the unique identity + // of a template literal, as tempalte literal itself. + // By default, ES2015 template literals are unique + // tag`a${1}z` === tag`a${2}z` + // even if interpolated values are different + // the template chunks are in a frozen Array + // that is identical each time you use the same + // literal to represent same static content + // around its own interpolations. + var unique = function unique(template) { + return _TL(template); + }; - var futureEnd = futureNodes.length; - var futureStart = 0; + // https://codepen.io/WebReflection/pen/dqZrpV?editors=0010 + // TL returns a unique version of the template + // it needs lazy feature detection + // (cannot trust literals with transpiled code) + var _TL = function TL(t) { + if ( + // TypeScript template literals are not standard + t.propertyIsEnumerable('raw') || !Object.isFrozen(t.raw) || + // Firefox < 55 has not standard implementation neither + /Firefox\/(\d+)/.test((G.navigator || {}).userAgent) && parseFloat(RegExp.$1) < 55) { + var T = {}; + _TL = function TL(t) { + var k = '^' + t.join('^'); + return T[k] || (T[k] = t); + }; + } else { + // make TL an identity like function + _TL = function TL(t) { + return t; + }; + } + return _TL(t); + }; - // common prefix - while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentStart], futureNodes[futureStart])) { - currentStart++; - futureStart++; + // used to store templates objects + // since neither Map nor WeakMap are safe + var TemplateMap = function TemplateMap() { + try { + var wm = new WeakMap$1(); + var o_O = Object.freeze([]); + wm.set(o_O, true); + if (!wm.get(o_O)) throw o_O; + return wm; + } catch (o_O) { + // inevitable legacy code leaks due + // https://github.com/tc39/ecma262/pull/890 + return new Map$1(); } + }; - // common suffix - while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1])) { - currentEnd--; - futureEnd--; + // create document fragments via native template + // with a fallback for browsers that won't be able + // to deal with some injected element such or others + var HTMLFragment = hasContent ? function (node, html) { + var container = create(node, 'template'); + container.innerHTML = html; + return container.content; + } : function (node, html) { + var container = create(node, 'template'); + var content = fragment(node); + if (/^[^\S]*?<(col(?:group)?|t(?:head|body|foot|r|d|h))/i.test(html)) { + var selector = RegExp.$1; + container.innerHTML = '' + html + '
'; + append$1(content, slice.call(container.querySelectorAll(selector))); + } else { + container.innerHTML = html; + append$1(content, slice.call(container.childNodes)); } + return content; + }; - var currentSame = currentStart === currentEnd; - var futureSame = futureStart === futureEnd; + // creates SVG fragment with a fallback for IE that needs SVG + // within the HTML content + var SVGFragment = hasContent ? function (node, html) { + var content = fragment(node); + var container = doc(node).createElementNS(SVG_NAMESPACE, 'svg'); + container.innerHTML = html; + append$1(content, slice.call(container.childNodes)); + return content; + } : function (node, html) { + var content = fragment(node); + var container = create(node, 'div'); + container.innerHTML = '' + html + ''; + append$1(content, slice.call(container.firstChild.childNodes)); + return content; + }; + + function Wire(childNodes) { + this.childNodes = childNodes; + this.length = childNodes.length; + this.first = childNodes[0]; + this.last = childNodes[this.length - 1]; + this._ = null; + } - // same list - if (currentSame && futureSame) return futureNodes; + // when a wire is inserted, all its nodes will follow + Wire.prototype.valueOf = function valueOf(different) { + var noFragment = this._ == null; + if (noFragment) this._ = fragment(this.first); + /* istanbul ignore else */ + if (noFragment || different) append$1(this._, this.childNodes); + return this._; + }; - // only stuff to add - if (currentSame && futureStart < futureEnd) { - append$1(get, parentNode, futureNodes, futureStart, futureEnd, next(get, currentNodes, currentStart, currentLength, before)); - return futureNodes; + // when a wire is removed, all its nodes must be removed as well + Wire.prototype.remove = function remove() { + this._ = null; + var first = this.first; + var last = this.last; + if (this.length === 2) { + last.parentNode.removeChild(last); + } else { + var range = doc(first).createRange(); + range.setStartBefore(this.childNodes[1]); + range.setEndAfter(last); + range.deleteContents(); } + return first; + }; - // only stuff to remove - if (futureSame && currentStart < currentEnd) { - remove(get, parentNode, currentNodes, currentStart, currentEnd); - return futureNodes; + // every template literal interpolation indicates + // a precise target in the DOM the template is representing. + // `

some ${'content'}

` + // hyperHTML finds only once per template literal, + // hence once per entire application life-cycle, + // all nodes that are related to interpolations. + // These nodes are stored as indexes used to retrieve, + // once per upgrade, nodes that will change on each future update. + // A path example is [2, 0, 1] representing the operation: + // node.childNodes[2].childNodes[0].childNodes[1] + // Attributes are addressed via their owner node and their name. + var createPath = function createPath(node) { + var path = []; + var parentNode = void 0; + switch (node.nodeType) { + case ELEMENT_NODE: + case DOCUMENT_FRAGMENT_NODE: + parentNode = node; + break; + case COMMENT_NODE: + parentNode = node.parentNode; + prepend(path, parentNode, node); + break; + default: + parentNode = node.ownerElement; + break; + } + for (node = parentNode; parentNode = parentNode.parentNode; node = parentNode) { + prepend(path, parentNode, node); } + return path; + }; - var currentChanges = currentEnd - currentStart; - var futureChanges = futureEnd - futureStart; - var i = -1; + var prepend = function prepend(path, parent, node) { + path.unshift(path.indexOf.call(parent.childNodes, node)); + }; - // 2 simple indels: the shortest sequence is a subsequence of the longest - if (currentChanges < futureChanges) { - i = indexOf(futureNodes, futureStart, futureEnd, currentNodes, currentStart, currentEnd, compare); - // inner diff - if (-1 < i) { - append$1(get, parentNode, futureNodes, futureStart, i, get(currentNodes[currentStart], 0)); - append$1(get, parentNode, futureNodes, i + currentChanges, futureEnd, next(get, currentNodes, currentEnd, currentLength, before)); - return futureNodes; - } - } - /* istanbul ignore else */ - else if (futureChanges < currentChanges) { - i = indexOf(currentNodes, currentStart, currentEnd, futureNodes, futureStart, futureEnd, compare); - // outer diff - if (-1 < i) { - remove(get, parentNode, currentNodes, currentStart, i); - remove(get, parentNode, currentNodes, i + futureChanges, currentEnd); - return futureNodes; - } + var Path = { + create: function create(type, node, name) { + return { type: type, name: name, node: node, path: createPath(node) }; + }, + find: function find(node, path) { + var length = path.length; + for (var i = 0; i < length; i++) { + node = node.childNodes[path[i]]; } - - // common case with one replacement for many nodes - // or many nodes replaced for a single one - /* istanbul ignore else */ - if (currentChanges < 2 || futureChanges < 2) { - append$1(get, parentNode, futureNodes, futureStart, futureEnd, get(currentNodes[currentStart], 0)); - remove(get, parentNode, currentNodes, currentStart, currentEnd); - return futureNodes; + return node; } + }; - // the half match diff part has been skipped in petit-dom - // https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L391-L397 - // accordingly, I think it's safe to skip in here too - // if one day it'll come out like the speediest thing ever to do - // then I might add it in here too + // from https://github.com/developit/preact/blob/33fc697ac11762a1cb6e71e9847670d047af7ce5/src/constants.js + var IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i; - // Extra: before going too fancy, what about reversed lists ? - // This should bail out pretty quickly if that's not the case. - if (currentChanges === futureChanges && isReversed(futureNodes, futureEnd, currentNodes, currentStart, currentEnd, compare)) { - append$1(get, parentNode, futureNodes, futureStart, futureEnd, next(get, currentNodes, currentEnd, currentLength, before)); - return futureNodes; + // style is handled as both string and object + // even if the target is an SVG element (consistency) + var Style = (function (node, original, isSVG) { + if (isSVG) { + var style = original.cloneNode(true); + style.value = ''; + node.setAttributeNode(style); + return update(style, isSVG); } + return update(node.style, isSVG); + }); - // last resort through a smart diff - smartDiff(get, parentNode, futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges, currentLength, compare, before); - - return futureNodes; - }; - - /* AUTOMATICALLY IMPORTED, DO NOT MODIFY */ - /*! (c) Andrea Giammarchi */ - function disconnected(poly) { - - var CONNECTED = 'connected'; - var DISCONNECTED = 'dis' + CONNECTED; - var Event = poly.Event; - var WeakSet = poly.WeakSet; - var notObserving = true; - var observer = new WeakSet(); - return function observe(node) { - if (notObserving) { - notObserving = !notObserving; - startObserving(node.ownerDocument); - } - observer.add(node); - return node; - }; - function startObserving(document) { - var dispatched = null; - try { - new MutationObserver(changes).observe(document, { subtree: true, childList: true }); - } catch (o_O) { - var timer = 0; - var records = []; - var reschedule = function reschedule(record) { - records.push(record); - clearTimeout(timer); - timer = setTimeout(function () { - changes(records.splice(timer = 0, records.length)); - }, 0); - }; - document.addEventListener('DOMNodeRemoved', function (event) { - reschedule({ addedNodes: [], removedNodes: [event.target] }); - }, true); - document.addEventListener('DOMNodeInserted', function (event) { - reschedule({ addedNodes: [event.target], removedNodes: [] }); - }, true); - } - function changes(records) { - dispatched = new Tracker(); - for (var record, length = records.length, i = 0; i < length; i++) { - record = records[i]; - dispatchAll(record.removedNodes, DISCONNECTED, CONNECTED); - dispatchAll(record.addedNodes, CONNECTED, DISCONNECTED); - } - dispatched = null; - } - function dispatchAll(nodes, type, counter) { - for (var node, event = new Event(type), length = nodes.length, i = 0; i < length; (node = nodes[i++]).nodeType === 1 && dispatchTarget(node, event, type, counter)) {} - } - function dispatchTarget(node, event, type, counter) { - if (observer.has(node) && !dispatched[type].has(node)) { - dispatched[counter].delete(node); - dispatched[type].add(node); - node.dispatchEvent(event); - /* - // The event is not bubbling (perf reason: should it?), - // hence there's no way to know if - // stop/Immediate/Propagation() was called. - // Should DOM Level 0 work at all? - // I say it's a YAGNI case for the time being, - // and easy to implement in user-land. - if (!event.cancelBubble) { - var fn = node['on' + type]; - if (fn) - fn.call(node, event); + // the update takes care or changing/replacing + // only properties that are different or + // in case of string, the whole node + var update = function update(style, isSVG) { + var oldType = void 0, + oldValue = void 0; + return function (newValue) { + switch (typeof newValue) { + case 'object': + if (newValue) { + if (oldType === 'object') { + if (!isSVG) { + if (oldValue !== newValue) { + for (var key in oldValue) { + if (!(key in newValue)) { + style[key] = ''; + } + } + } + } + } else { + if (isSVG) style.value = '';else style.cssText = ''; + } + var info = isSVG ? {} : style; + for (var _key in newValue) { + var value = newValue[_key]; + var styleValue = typeof value === 'number' && !IS_NON_DIMENSIONAL.test(_key) ? value + 'px' : value; + if (!isSVG && /^--/.test(_key)) info.setProperty(_key, styleValue);else info[_key] = styleValue; + } + oldType = 'object'; + if (isSVG) style.value = toStyle(oldValue = info);else oldValue = newValue; + break; } - */ - } - for (var children = node.children, length = children.length, i = 0; i < length; dispatchTarget(children[i++], event, type, counter)) {} - } - function Tracker() { - this[CONNECTED] = new WeakSet(); - this[DISCONNECTED] = new WeakSet(); + default: + if (oldValue != newValue) { + oldType = 'string'; + oldValue = newValue; + if (isSVG) style.value = newValue || '';else style.cssText = newValue || ''; + } + break; } + }; + }; + + var hyphen = /([^A-Z])([A-Z]+)/g; + var ized = function ized($0, $1, $2) { + return $1 + '-' + $2.toLowerCase(); + }; + var toStyle = function toStyle(object) { + var css = []; + for (var key in object) { + css.push(key.replace(hyphen, ized), ':', object[key], ';'); } - } + return css.join(''); + }; var document$1 = G.document; - var observe = disconnected({ Event: Event, WeakSet: WeakSet }); + var observe = disconnected({ Event: CustomEvent$1, WeakSet: WeakSet$1 }); // a basic dictionary used to filter already cached attributes // while looking for special hyperHTML values. @@ -1501,7 +1532,7 @@ var hyperHTML = (function (global) { // a weak collection of contexts that // are already known to hyperHTML - var bewitched = new WeakMap(); + var bewitched = new WeakMap$1(); // all unique template literals var templates = TemplateMap(); @@ -1565,7 +1596,7 @@ var hyperHTML = (function (global) { }; // all wires used per each context - var wires = new WeakMap(); + var wires = new WeakMap$1(); // A wire is a callback used as tag function // to lazily relate a generic object to a template literal. @@ -1604,7 +1635,7 @@ var hyperHTML = (function (global) { updates.apply(null, arguments); if (setup) { if (type === 'svg') { - append(content, slice.call(container.childNodes)); + append$1(content, slice.call(container.childNodes)); } wire = wireContent(content); } @@ -1675,8 +1706,8 @@ var hyperHTML = (function (global) { // i.e. those still targeting IE hyper._ = { global: G, - WeakMap: WeakMap, - WeakSet: WeakSet + WeakMap: WeakMap$1, + WeakSet: WeakSet$1 }; // the wire content is the lazy defined diff --git a/min.js b/min.js index ee9dc0d9..11204346 100644 --- a/min.js +++ b/min.js @@ -1,2 +1,3 @@ -/*! (c) Andrea Giammarchi (ISC) */var hyperHTML=function(e){"use strict";function t(){return this}function n(e){this.childNodes=e,this.length=e.length,this.first=e[0],this.last=e[this.length-1],this._=null}function r(){}function i(e){var t=Fe.get(this);return t&&t.template===Y(e)?a.apply(t.updates,arguments):o.apply(this,arguments),this}function o(e){e=Y(e);var t=Be.get(e)||u.call(this,e),n=U(this.ownerDocument,t.fragment),r=ze.create(n,t.paths);Fe.set(this,{template:e,updates:r}),a.apply(r,arguments),this.textContent="",this.appendChild(n)}function a(){for(var e=arguments.length,t=1;t\"'=]+",M="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",$="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",_=new RegExp(M+L+$+"+)([ "+T+"]*/?>)","g"),D=new RegExp(M+L+$+"*)([ "+T+"]*/>)","g"),P=function(e,t){return R(e).createElement(t)},R=function(e){return e.ownerDocument||e},H=function(e){return R(e).createDocumentFragment()},W=function(e,t){return R(e).createTextNode(t)},z=H(document),F="append"in z,B="content"in P(document,"template");z.appendChild(W(z,"g")),z.appendChild(W(z,""));var Z=1===z.cloneNode(!0).childNodes.length,V="importNode"in document,G=F?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",G(r,X.call(n.querySelectorAll(i)))}else n.innerHTML=t,G(r,X.call(n.childNodes));return r},ne=B?function(e,t){var n=H(e),r=R(e).createElementNS(s,"svg");return r.innerHTML=t,G(n,X.call(r.childNodes)),n}:function(e,t){var n=H(e),r=P(e,"div");return r.innerHTML=''+t+"",G(n,X.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=H(this.first)),(t||e)&&G(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=R(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var re=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ie(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ie(t,n,e);return t},ie=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},oe={create:function(e,t,n){return{type:e,name:n,node:t,path:re(t)}},find:function(e,t){for(var n=t.length,r=0;r=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var g=Array(l),b=f[c];for(--n;b;){for(var w=b,y=w.newi,N=w.oldi;n>y;)g[--l]=1,--n;for(;a>N;)g[--l]=-1,--a;g[--l]=0,--n,--a,b=b.prev}for(;n>=t;)g[--l]=1,--n;for(;a>=o;)g[--l]=-1,--a;return g},Ne=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,f=void 0,s=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],f=-l;f<=l;f+=2){for(d=f===-l||f!==l&&h[p+f-1]=0;l--){for(;d>0&&s>0&&a(r[i+d-1],e[t+s-1]);)m[g--]=0,d--,s--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],f=d-s,f===-l||f!==l&&h[p+f-1]>>0;n"},Ge=new b,Ie=function(e,t){return null==e?qe(t||"html"):Je(e,t||"html")},qe=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=Y(u);var c=o!==u;return c&&(o=u,r=H(document),n="svg"===e?document.createElementNS(s,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&G(r,X.call(n.childNodes)),t=Ke(r)),t}},Je=function(e,t){var n=t.indexOf(":"),r=Ge.get(e),i=t;return-1=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var b=Array(l),w=s[c];for(--n;w;){for(var y=w,N=y.newi,E=y.oldi;n>N;)b[--l]=1,--n;for(;a>E;)b[--l]=-1,--a;b[--l]=0,--n,--a,w=w.prev}for(;n>=t;)b[--l]=1,--n;for(;a>=o;)b[--l]=-1,--a;return b},k=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,s=void 0,f=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],s=-l;s<=l;s+=2){for(d=s===-l||s!==l&&h[p+s-1]=0;l--){for(;d>0&&f>0&&a(r[i+d-1],e[t+f-1]);)m[g--]=0,d--,f--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],s=d-f,s===-l||s!==l&&h[p+s-1]>>0;n\"'=]+",J="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",K="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",Q=new RegExp(J+q+K+"+)([ "+I+"]*/?>)","g"),U=new RegExp(J+q+K+"*)([ "+I+"]*/>)","g"),X=function(e,t){return Y(e).createElement(t)},Y=function(e){return e.ownerDocument||e},ee=function(e){return Y(e).createDocumentFragment()},te=function(e,t){return Y(e).createTextNode(t)},ne=ee(document),re="append"in ne,ie="content"in X(document,"template");ne.appendChild(te(ne,"g")),ne.appendChild(te(ne,""));var oe=1===ne.cloneNode(!0).childNodes.length,ae="importNode"in document,ue=re?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",ue(r,ve.call(n.querySelectorAll(i)))}else n.innerHTML=t,ue(r,ve.call(n.childNodes));return r},be=ie?function(e,t){var n=ee(e),r=Y(e).createElementNS(B,"svg");return r.innerHTML=t,ue(n,ve.call(r.childNodes)),n}:function(e,t){var n=ee(e),r=X(e,"div");return r.innerHTML=''+t+"",ue(n,ve.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=ee(this.first)),(t||e)&&ue(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=Y(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var we=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ye(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ye(t,n,e);return t},ye=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},Ne={create:function(e,t,n){return{type:e,name:n,node:t,path:we(t)}},find:function(e,t){for(var n=t.length,r=0;r"},qe=new s,Je=function(e,t){return null==e?Ke(t||"html"):Qe(e,t||"html")},Ke=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=pe(u);var c=o!==u;return c&&(o=u,r=ee(document),n="svg"===e?document.createElementNS(B,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&ue(r,ve.call(n.childNodes)),t=Ue(r)),t}},Qe=function(e,t){var n=t.indexOf(":"),r=qe.get(e),i=t;return-1\n _templateObject35 = _taggedTemplateLiteral(['

', '

'], ['

', '

']), _templateObject36 = _taggedTemplateLiteral([''], ['']), _templateObject37 = _taggedTemplateLiteral(['

'], ['

']), - _templateObject38 = _taggedTemplateLiteral([''], ['']), + _templateObject38 = _taggedTemplateLiteral([''], ['']), _templateObject39 = _taggedTemplateLiteral([''], ['']), _templateObject40 = _taggedTemplateLiteral(['\n '], ['\n ']), _templateObject41 = _taggedTemplateLiteral(['\n
First name: ', '
\n

'], ['\n
First name: ', '
\n

']), @@ -527,7 +527,15 @@ tressa.async(function (done) { return tressa.async(function (done) { var div = document.createElement('div'); document.body.appendChild(div); - hyperHTML.bind(div)(_templateObject38, function () { + hyperHTML.bind(div)(_templateObject38, function (event) { + if (/loaded|complete/.test(event.readyState)) setTimeout(function () { + tressa.assert(true, 'executed'); + done(); + }); + }, function () { + tressa.assert(true, 'executed'); + done(); + }, function () { tressa.assert(true, 'executed'); done(); }); @@ -849,7 +857,7 @@ tressa.async(function (done) { tressa.assert(div.querySelector('p').getAttribute('attr') === 'test', 'the

is defined'); p.render().click(); tressa.assert(p.clicked, 'the event worked'); - render(_templateObject11, [Rect.for({ x: 789, y: 123 })]); + render(_templateObject11, [hyperHTML.Component.for.call(Rect, { x: 789, y: 123 })]); tressa.assert(div.querySelector('rect').getAttribute('x') == '789', 'the for(state) worked'); }).then(function () { return tressa.async(function (done) { @@ -1115,7 +1123,7 @@ tressa.async(function (done) { tressa.assert(div.children[2].getAttribute('test') == "2", 'third node ok'); div = hyperHTML.wire()(_templateObject66); tressa.assert(div.children.length === 1, 'one svg'); - tressa.assert(div.children[0].children.length === 2, 'two paths'); + tressa.assert(div.querySelectorAll('path').length === 2, 'two paths'); }).then(function () { tressa.log('## '); function check(form) { @@ -1315,6 +1323,12 @@ tressa.async(function (done) { var a = document.createElement('div'); var b = document.createElement('div'); + var method = hyperHTML.Component.for; + if (!MenuSimple.for) { + MenuSimple.for = method; + MenuWeakMap.for = method; + MenuItem.for = method; + } hyperHTML.bind(a)(_templateObject11, MenuSimple.for(a).render({ items: [{ name: 'item 1' }, { name: 'item 2' }, { name: 'item 3' }] })); diff --git a/test/runner.js b/test/runner.js index 463ffdb4..641b0d0b 100644 --- a/test/runner.js +++ b/test/runner.js @@ -73,8 +73,6 @@ setTimeout(function () { createDocumentFragment.call(document); }; - global.WeakMap = global.WeakSet = void 0; - global.Event = function (type) { var e = global.document.createEvent('Event'); e.initEvent(type, false, false); diff --git a/test/test.js b/test/test.js index 3a09db44..6506a123 100644 --- a/test/test.js +++ b/test/test.js @@ -485,10 +485,24 @@ tressa.async(function (done) { return tressa.async(function (done) { var div = document.createElement('div'); document.body.appendChild(div); - hyperHTML.bind(div)``; + hyperHTML.bind(div)``; // in nodejs case if (!('onload' in document.defaultView)) { var evt = document.createEvent('Event'); @@ -816,7 +830,7 @@ tressa.async(function (done) { p.render().click(); tressa.assert(p.clicked, 'the event worked'); render`${[ - Rect.for({x: 789, y: 123}) + hyperHTML.Component.for.call(Rect, {x: 789, y: 123}) ]}`; tressa.assert(div.querySelector('rect').getAttribute('x') == '789', 'the for(state) worked'); }) @@ -1024,7 +1038,7 @@ tressa.async(function (done) { `; tressa.assert(div.children.length === 1, 'one svg'); - tressa.assert(div.children[0].children.length === 2, 'two paths'); + tressa.assert(div.querySelectorAll('path').length === 2, 'two paths'); }) .then(function () { tressa.log('## '); @@ -1184,6 +1198,12 @@ tressa.async(function (done) { } var a = document.createElement('div'); var b = document.createElement('div'); + var method = hyperHTML.Component.for; + if (!MenuSimple.for) { + MenuSimple.for = method; + MenuWeakMap.for = method; + MenuItem.for = method; + } hyperHTML.bind(a)`${MenuSimple.for(a).render({ items: [{name: 'item 1'}, {name: 'item 2'}, {name: 'item 3'}] })}`; diff --git a/umd.js b/umd.js index 60db51dd..028c0f9f 100644 --- a/umd.js +++ b/umd.js @@ -1,4 +1,5 @@ (function(A,G){if(typeof define=='function'&&define.amd)define([],G);else if(typeof module=='object'&&module.exports)module.exports=G();else A.hyperHTML=G()}(typeof self!='undefined'?self:this,function(){ -/*! (c) Andrea Giammarchi (ISC) */var hyperHTML=function(e){"use strict";function t(){return this}function n(e){this.childNodes=e,this.length=e.length,this.first=e[0],this.last=e[this.length-1],this._=null}function r(){}function i(e){var t=Fe.get(this);return t&&t.template===Y(e)?a.apply(t.updates,arguments):o.apply(this,arguments),this}function o(e){e=Y(e);var t=Be.get(e)||u.call(this,e),n=U(this.ownerDocument,t.fragment),r=ze.create(n,t.paths);Fe.set(this,{template:e,updates:r}),a.apply(r,arguments),this.textContent="",this.appendChild(n)}function a(){for(var e=arguments.length,t=1;t\"'=]+",M="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",$="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",_=new RegExp(M+L+$+"+)([ "+T+"]*/?>)","g"),D=new RegExp(M+L+$+"*)([ "+T+"]*/>)","g"),P=function(e,t){return R(e).createElement(t)},R=function(e){return e.ownerDocument||e},H=function(e){return R(e).createDocumentFragment()},W=function(e,t){return R(e).createTextNode(t)},z=H(document),F="append"in z,B="content"in P(document,"template");z.appendChild(W(z,"g")),z.appendChild(W(z,""));var Z=1===z.cloneNode(!0).childNodes.length,V="importNode"in document,G=F?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",G(r,X.call(n.querySelectorAll(i)))}else n.innerHTML=t,G(r,X.call(n.childNodes));return r},ne=B?function(e,t){var n=H(e),r=R(e).createElementNS(s,"svg");return r.innerHTML=t,G(n,X.call(r.childNodes)),n}:function(e,t){var n=H(e),r=P(e,"div");return r.innerHTML=''+t+"",G(n,X.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=H(this.first)),(t||e)&&G(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=R(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var re=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ie(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ie(t,n,e);return t},ie=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},oe={create:function(e,t,n){return{type:e,name:n,node:t,path:re(t)}},find:function(e,t){for(var n=t.length,r=0;r=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var g=Array(l),b=f[c];for(--n;b;){for(var w=b,y=w.newi,N=w.oldi;n>y;)g[--l]=1,--n;for(;a>N;)g[--l]=-1,--a;g[--l]=0,--n,--a,b=b.prev}for(;n>=t;)g[--l]=1,--n;for(;a>=o;)g[--l]=-1,--a;return g},Ne=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,f=void 0,s=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],f=-l;f<=l;f+=2){for(d=f===-l||f!==l&&h[p+f-1]=0;l--){for(;d>0&&s>0&&a(r[i+d-1],e[t+s-1]);)m[g--]=0,d--,s--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],f=d-s,f===-l||f!==l&&h[p+f-1]>>0;n"},Ge=new b,Ie=function(e,t){return null==e?qe(t||"html"):Je(e,t||"html")},qe=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=Y(u);var c=o!==u;return c&&(o=u,r=H(document),n="svg"===e?document.createElementNS(s,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&G(r,X.call(n.childNodes)),t=Ke(r)),t}},Je=function(e,t){var n=t.indexOf(":"),r=Ge.get(e),i=t;return-1=u;){for(var c=t,l=i;ca;)--c;l=u+r-c;var b=Array(l),w=s[c];for(--n;w;){for(var y=w,N=y.newi,E=y.oldi;n>N;)b[--l]=1,--n;for(;a>E;)b[--l]=-1,--a;b[--l]=0,--n,--a,w=w.prev}for(;n>=t;)b[--l]=1,--n;for(;a>=o;)b[--l]=-1,--a;return b},k=function(e,t,n,r,i,o,a){var u=n+o,c=[],l=void 0,s=void 0,f=void 0,d=void 0,h=void 0,v=void 0,p=void 0;e:for(l=0;l<=u;l++){if(l>50)return null;for(p=l-1,h=l?c[l-1]:[0,0],v=c[l]=[],s=-l;s<=l;s+=2){for(d=s===-l||s!==l&&h[p+s-1]=0;l--){for(;d>0&&f>0&&a(r[i+d-1],e[t+f-1]);)m[g--]=0,d--,f--;if(!l)break;p=l-1,h=l?c[l-1]:[0,0],s=d-f,s===-l||s!==l&&h[p+s-1]>>0;n\"'=]+",J="<([A-Za-z]+[A-Za-z0-9:_-]*)((?:",K="(?:=(?:'[^']*?'|\"[^\"]*?\"|<[^>]*?>|[^ \\f\\n\\r\\t\\/>\"'=]+))?)",Q=new RegExp(J+q+K+"+)([ "+I+"]*/?>)","g"),U=new RegExp(J+q+K+"*)([ "+I+"]*/>)","g"),X=function(e,t){return Y(e).createElement(t)},Y=function(e){return e.ownerDocument||e},ee=function(e){return Y(e).createDocumentFragment()},te=function(e,t){return Y(e).createTextNode(t)},ne=ee(document),re="append"in ne,ie="content"in X(document,"template");ne.appendChild(te(ne,"g")),ne.appendChild(te(ne,""));var oe=1===ne.cloneNode(!0).childNodes.length,ae="importNode"in document,ue=re?function(e,t){e.append.apply(e,t)}:function(e,t){for(var n=t.length,r=0;r"+t+"",ue(r,ve.call(n.querySelectorAll(i)))}else n.innerHTML=t,ue(r,ve.call(n.childNodes));return r},be=ie?function(e,t){var n=ee(e),r=Y(e).createElementNS(B,"svg");return r.innerHTML=t,ue(n,ve.call(r.childNodes)),n}:function(e,t){var n=ee(e),r=X(e,"div");return r.innerHTML=''+t+"",ue(n,ve.call(r.firstChild.childNodes)),n};n.prototype.valueOf=function(e){var t=null==this._;return t&&(this._=ee(this.first)),(t||e)&&ue(this._,this.childNodes),this._},n.prototype.remove=function(){this._=null;var e=this.first,t=this.last;if(2===this.length)t.parentNode.removeChild(t);else{var n=Y(e).createRange();n.setStartBefore(this.childNodes[1]),n.setEndAfter(t),n.deleteContents()}return e};var we=function(e){var t=[],n=void 0;switch(e.nodeType){case 1:case 11:n=e;break;case 8:n=e.parentNode,ye(t,n,e);break;default:n=e.ownerElement}for(e=n;n=n.parentNode;e=n)ye(t,n,e);return t},ye=function(e,t,n){e.unshift(e.indexOf.call(t.childNodes,n))},Ne={create:function(e,t,n){return{type:e,name:n,node:t,path:we(t)}},find:function(e,t){for(var n=t.length,r=0;r"},qe=new s,Je=function(e,t){return null==e?Ke(t||"html"):Qe(e,t||"html")},Ke=function(e){var t=void 0,n=void 0,r=void 0,o=void 0,a=void 0;return function(u){u=pe(u);var c=o!==u;return c&&(o=u,r=ee(document),n="svg"===e?document.createElementNS(B,"svg"):r,a=i.bind(n)),a.apply(null,arguments),c&&("svg"===e&&ue(r,ve.call(n.childNodes)),t=Ue(r)),t}},Qe=function(e,t){var n=t.indexOf(":"),r=qe.get(e),i=t;return-1