From e68218e35af0c60dcd13d1c099cf4e1760aee807 Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Tue, 18 Jul 2023 00:05:34 +0100 Subject: :heavy_plus_sign: Vendor morphdom as the dom patching library. --- src/runtime.ffi.mjs | 1990 +++++++++++++++++++-------------------------------- 1 file changed, 736 insertions(+), 1254 deletions(-) (limited to 'src') diff --git a/src/runtime.ffi.mjs b/src/runtime.ffi.mjs index f4cf0d1..28066fe 100644 --- a/src/runtime.ffi.mjs +++ b/src/runtime.ffi.mjs @@ -1,1300 +1,782 @@ -// This module is vendored and lightly modified from the original diffhtml source -// available at: +// This file is vendored from https://github.com/patrick-steele-idem/morphdom/ +// and is licensed under the MIT license. For a copy of the original license +// head on over to: // -// https://github.com/tbranyen/diffhtml/ -// -// You can read a copy of the original license, unchanged, at: -// -// https://github.com/tbranyen/diffhtml/blob/master/LICENSE +// https://github.com/patrick-steele-idem/morphdom/blob/master/LICENSE // -function t(e, t) { - var n = Object.keys(e); - if (Object.getOwnPropertySymbols) { - var r = Object.getOwnPropertySymbols(e); - t && - (r = r.filter(function (t) { - return Object.getOwnPropertyDescriptor(e, t).enumerable; - })), - n.push.apply(n, r); - } - return n; -} -function n(e) { - for (var n = 1; n < arguments.length; n++) { - var r = null != arguments[n] ? arguments[n] : {}; - n % 2 - ? t(Object(r), !0).forEach(function (t) { - i(e, t, r[t]); - }) - : Object.getOwnPropertyDescriptors - ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) - : t(Object(r)).forEach(function (t) { - Object.defineProperty(e, t, Object.getOwnPropertyDescriptor(r, t)); - }); - } - return e; -} -function r(e, t) { - if (!(e instanceof t)) - throw new TypeError("Cannot call a class as a function"); -} -function a(e, t) { - for (var n = 0; n < t.length; n++) { - var r = t[n]; - (r.enumerable = r.enumerable || !1), - (r.configurable = !0), - "value" in r && (r.writable = !0), - Object.defineProperty(e, r.key, r); - } -} -function o(e, t, n) { - return ( - t && a(e.prototype, t), - n && a(e, n), - Object.defineProperty(e, "prototype", { writable: !1 }), - e - ); -} -function i(e, t, n) { - return ( - t in e - ? Object.defineProperty(e, t, { - value: n, - enumerable: !0, - configurable: !0, - writable: !0, - }) - : (e[t] = n), - e - ); -} -function s(e) { - return c(e) || d(e) || l(e) || f(); -} -function c(e) { - if (Array.isArray(e)) return u(e); -} -function d(e) { +var DOCUMENT_FRAGMENT_NODE = 11; + +function morphAttrs(fromNode, toNode) { + var toNodeAttrs = toNode.attributes; + var attr; + var attrName; + var attrNamespaceURI; + var attrValue; + var fromValue; + + // document-fragments dont have attributes so lets not do anything if ( - ("undefined" != typeof Symbol && null != e[Symbol.iterator]) || - null != e["@@iterator"] - ) - return Array.from(e); -} -function l(e, t) { - if (e) { - if ("string" == typeof e) return u(e, t); - var n = Object.prototype.toString.call(e).slice(8, -1); - return ( - "Object" === n && e.constructor && (n = e.constructor.name), - "Map" === n || "Set" === n - ? Array.from(e) - : "Arguments" === n || - /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n) - ? u(e, t) - : void 0 - ); - } -} -function u(e, t) { - (null == t || t > e.length) && (t = e.length); - for (var n = 0, r = new Array(t); n < t; n++) r[n] = e[n]; - return r; -} -function f() { - throw new TypeError( - "Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method." - ); -} -function h(e, t) { - var n = String(e); - switch (t) { - case "boolean": - return "false" !== n; - case "string": - return n; - case "number": - return te(n, 10); - case "object": - return ne(n); - } -} -function v(e, t) { - var n = - arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : typeof t, - r = arguments.length > 3 ? arguments[3] : void 0, - a = ee.location, - o = ee.URLSearchParams, - i = void 0 !== o, - s = void 0 !== a, - c = i && s, - d = W.env; - if (r && e in r) return r[e]; - var l = "DIFF_".concat(e.replace(/[^a-zA-Z0-9]/, "")); - if (c) { - var u = new o(a.search), - f = l.toLowerCase(); - if (u.has(f)) return h(decodeURIComponent(String(u.get(f))), n); - } - var v = l.toUpperCase(); - return d && v in W.env ? h(W.env[v.toUpperCase()], n) : t; -} -function p(e) { - for ( - var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [], - n = 0; - n < e.length; - n++ + toNode.nodeType === DOCUMENT_FRAGMENT_NODE || + fromNode.nodeType === DOCUMENT_FRAGMENT_NODE ) { - var r = e[n]; - r && r.rawNodeName === ve ? p(r.childNodes, t) : r && t.push(r); - } - return t; -} -function m(e, t, r) { - var a = null; - if (he.protected.has(e) || he.allocated.has(e)) a = e; - else if (!e || fe(e)) { - var o = e ? e.length : 0; - r = []; - for (var i = 0; i < o; i++) { - var c = e && !e[i]; - c || (e && r.push(e[i])); - } - a = m(ve, null, r); + return; } - if (a) return a; - var d = "object" == typeof e, - l = e; - if (e && d && "ownerDocument" in l) { - if (l.nodeType === B.TEXT) { - var u = m(pe, l.nodeValue); - return X.set(u, l), u; - } - (t = {}), (r = []); - var f = l.attributes; - if (l.nodeType === B.ELEMENT && f && f.length) - for (var h = 0; h < f.length; h++) { - var v = f[h], - T = v.name, - g = v.value; - g === U.STR && T in l ? (t[T] = e[T]) : (t[T] = g); + + // update attributes on original DOM element + for (var i = toNodeAttrs.length - 1; i >= 0; i--) { + attr = toNodeAttrs[i]; + attrName = attr.name; + attrNamespaceURI = attr.namespaceURI; + attrValue = attr.value; + + if (attrNamespaceURI) { + attrName = attr.localName || attrName; + fromValue = fromNode.getAttributeNS(attrNamespaceURI, attrName); + + if (fromValue !== attrValue) { + if (attr.prefix === "xmlns") { + attrName = attr.name; // It's not allowed to set an attribute with the XMLNS namespace without specifying the `xmlns` prefix + } + fromNode.setAttributeNS(attrNamespaceURI, attrName, attrValue); } - if (l.nodeType === B.ELEMENT || l.nodeType === B.FRAGMENT) { - r = []; - for (var b = 0; b < l.childNodes.length; b++) { - var N = l.childNodes[b]; - r.push(m(N)); + } else { + fromValue = fromNode.getAttribute(attrName); + + if (fromValue !== attrValue) { + fromNode.setAttribute(attrName, attrValue); } } - return ( - X.forEach(function (t, n) { - t === e && (a = n); - }), - (a = a || m(l.nodeName, t, r)), - (a.attributes = n(n({}, a.attributes), t)), - (a.childNodes = r), - X.set(a, l), - a - ); - } - if (d) { - var y = e.rawNodeName, - E = e.nodeName, - w = e.nodeValue, - S = e.attributes, - O = e.childNodes, - k = e.children, - R = y || E, - C = m(R, S || null, k || O); - return w && (C.nodeValue = w), C; } - for ( - var M = arguments.length, A = new Array(M > 3 ? M - 3 : 0), x = 3; - x < M; - x++ - ) - A[x - 3] = arguments[x]; - A.length && (r = [r].concat(A)), (a = ue.get()); - var L = e === pe; - "string" == typeof e - ? ((a.rawNodeName = e), (a.nodeName = a.rawNodeName.toLowerCase())) - : ((a.rawNodeName = e), (a.nodeName = ve)), - (a.nodeValue = U.STR), - (a.key = U.STR), - (a.childNodes.length = 0), - (a.attributes = {}); - var D = fe(t) || "object" != typeof t, - I = D ? t : r, - V = p(fe(I) ? I : [I]); - if (L) { - var j = V.join(U.STR); - return (a.nodeType = B.TEXT), (a.nodeValue = String(j)), a; - } - if ( - (a.nodeName === ve - ? (a.nodeType = B.FRAGMENT) - : (a.nodeType = "#comment" === e ? B.COMMENT : B.ELEMENT), - I && V.length && (!t || !t.childNodes)) - ) - for (var _ = 0; _ < V.length; _++) { - var P = V[_]; - if (fe(P)) { - var H; - (H = a.childNodes).push.apply(H, s(P)); - } else { - if (!P) continue; - if (P.nodeType === B.FRAGMENT && "string" == typeof P.rawNodeName) { - var F; - (F = a.childNodes).push.apply(F, s(P.childNodes)); - } else - P && "object" == typeof P - ? a.childNodes.push(m(P)) - : a.childNodes.push(m(pe, null, P)); + + // Remove any extra attributes found on the original DOM element that + // weren't found on the target element. + var fromNodeAttrs = fromNode.attributes; + + for (var d = fromNodeAttrs.length - 1; d >= 0; d--) { + attr = fromNodeAttrs[d]; + attrName = attr.name; + attrNamespaceURI = attr.namespaceURI; + + if (attrNamespaceURI) { + attrName = attr.localName || attrName; + + if (!toNode.hasAttributeNS(attrNamespaceURI, attrName)) { + fromNode.removeAttributeNS(attrNamespaceURI, attrName); + } + } else { + if (!toNode.hasAttribute(attrName)) { + fromNode.removeAttribute(attrName); } } - if ( - t && - "object" == typeof t && - !fe(t) && - ((a.attributes = n({}, t)), t.childNodes) - ) { - var z = "object" == typeof t.childNodes; - a.childNodes.push(z ? m(t.childNodes) : m("#text", t.childNodes)); } - return ( - "script" === a.nodeName && - a.attributes.src && - (a.key = String(a.attributes.src)), - a.attributes && "key" in a.attributes && (a.key = String(a.attributes.key)), - Y.size && - Y.forEach(function (e, t) { - (t = e(a)) && (a = m(t)); - }), - a - ); } -function T(e) { - var t = e.mount, - n = e.input, - r = n, - a = ge++; - return v("collectMetrics", !1) - ? function (e) { - e = "[".concat(a, "] ").concat(e); - var n = t.host; - t && n - ? (e = "".concat(n.constructor.name, " ").concat(e)) - : r && - "function" == typeof r.rawNodeName && - (e = "".concat(r.rawNodeName.name, " ").concat(e)); - var o = "".concat(e, "-end"); - if (Te.has(e)) { - var i = Te.get(e) || 0, - s = (performance.now() - i).toFixed(3); - Te.delete(e), - performance.mark(o), - performance.measure( - "".concat(me, " ").concat(e, " (").concat(s, "ms)"), - e, - o - ); - } else Te.set(e, performance.now()), performance.mark(e); - } - : U.FUN; + +var range; // Create a range object for efficently rendering strings to elements. +var NS_XHTML = "http://www.w3.org/1999/xhtml"; + +var doc = typeof document === "undefined" ? undefined : document; +var HAS_TEMPLATE_SUPPORT = !!doc && "content" in doc.createElement("template"); +var HAS_RANGE_SUPPORT = + !!doc && doc.createRange && "createContextualFragment" in doc.createRange(); + +function createFragmentFromTemplate(str) { + var template = doc.createElement("template"); + template.innerHTML = str; + return template.content.childNodes[0]; } -function g(e) { - if ((be(e), e.childNodes.length)) - for (var t = 0; t < e.childNodes.length; t++) g(e.childNodes[t]); + +function createFragmentFromRange(str) { + if (!range) { + range = doc.createRange(); + range.selectNode(doc.body); + } + + var fragment = range.createContextualFragment(str); + return fragment.childNodes[0]; } -function b(e) { - if (e.childNodes.length) - for (var t = 0; t < e.childNodes.length; t++) b(e.childNodes[t]); - X.delete(e), Ne(e); + +function createFragmentFromWrap(str) { + var fragment = doc.createElement("body"); + fragment.innerHTML = str; + return fragment.childNodes[0]; } -function N() { - ye.allocated.forEach(function (e) { - (e.attributes = {}), - (e.childNodes.length = 0), - ye.free.add(e), - ye.allocated.delete(e), - X.delete(e); - }); + +/** + * This is about the same + * var html = new DOMParser().parseFromString(str, 'text/html'); + * return html.body.firstChild; + * + * @method toElement + * @param {String} str + */ +function toElement(str) { + str = str.trim(); + if (HAS_TEMPLATE_SUPPORT) { + // avoid restrictions on content for things like `Hi` which + // createContextualFragment doesn't support + //