jails-js 6.4.1 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -84,9 +84,6 @@ var Idiomorph = function() {
84
84
  }
85
85
  function morphOuterHTML(ctx, oldNode, newNode) {
86
86
  const oldParent = normalizeParent(oldNode);
87
- let childNodes = Array.from(oldParent.childNodes);
88
- const index = childNodes.indexOf(oldNode);
89
- const rightMargin = childNodes.length - (index + 1);
90
87
  morphChildren(
91
88
  ctx,
92
89
  oldParent,
@@ -97,8 +94,7 @@ var Idiomorph = function() {
97
94
  oldNode.nextSibling
98
95
  // end point for iteration
99
96
  );
100
- childNodes = Array.from(oldParent.childNodes);
101
- return childNodes.slice(index, childNodes.length - rightMargin);
97
+ return Array.from(oldParent.childNodes);
102
98
  }
103
99
  function saveAndRestoreFocus(ctx, fn) {
104
100
  var _a;
@@ -113,7 +109,7 @@ var Idiomorph = function() {
113
109
  const { id: activeElementId, selectionStart, selectionEnd } = activeElement;
114
110
  const results = fn();
115
111
  if (activeElementId && activeElementId !== ((_a = document.activeElement) == null ? void 0 : _a.id)) {
116
- activeElement = ctx.target.querySelector(`#${activeElementId}`);
112
+ activeElement = ctx.target.querySelector(`[id="${activeElementId}"]`);
117
113
  activeElement == null ? void 0 : activeElement.focus();
118
114
  }
119
115
  if (activeElement && !activeElement.selectionEnd && selectionEnd) {
@@ -271,7 +267,7 @@ var Idiomorph = function() {
271
267
  function moveBeforeById(parentNode, id, after, ctx) {
272
268
  const target = (
273
269
  /** @type {Element} - will always be found */
274
- ctx.target.querySelector(`#${id}`) || ctx.pantry.querySelector(`#${id}`)
270
+ ctx.target.id === id && ctx.target || ctx.target.querySelector(`[id="${id}"]`) || ctx.pantry.querySelector(`[id="${id}"]`)
275
271
  );
276
272
  removeElementFromAncestorsIdMaps(target, ctx);
277
273
  moveBefore(parentNode, target, after);
@@ -646,7 +642,10 @@ var Idiomorph = function() {
646
642
  );
647
643
  } else if (newContent instanceof Node) {
648
644
  if (newContent.parentNode) {
649
- return createDuckTypedParent(newContent);
645
+ return (
646
+ /** @type {any} */
647
+ new SlicedParentNode(newContent)
648
+ );
650
649
  } else {
651
650
  const dummyParent = document.createElement("div");
652
651
  dummyParent.append(newContent);
@@ -660,27 +659,68 @@ var Idiomorph = function() {
660
659
  return dummyParent;
661
660
  }
662
661
  }
663
- function createDuckTypedParent(newContent) {
664
- return (
665
- /** @type {Element} */
666
- /** @type {unknown} */
667
- {
668
- childNodes: [newContent],
669
- /** @ts-ignore - cover your eyes for a minute, tsc */
670
- querySelectorAll: (s) => {
671
- const elements = newContent.querySelectorAll(s);
672
- return newContent.matches(s) ? [newContent, ...elements] : elements;
673
- },
674
- /** @ts-ignore */
675
- insertBefore: (n, r) => newContent.parentNode.insertBefore(n, r),
676
- /** @ts-ignore */
677
- moveBefore: (n, r) => newContent.parentNode.moveBefore(n, r),
678
- // for later use with populateIdMapWithTree to halt upwards iteration
679
- get __idiomorphRoot() {
680
- return newContent;
681
- }
662
+ class SlicedParentNode {
663
+ /** @param {Node} node */
664
+ constructor(node) {
665
+ this.originalNode = node;
666
+ this.realParentNode = /** @type {Element} */
667
+ node.parentNode;
668
+ this.previousSibling = node.previousSibling;
669
+ this.nextSibling = node.nextSibling;
670
+ }
671
+ /** @returns {Node[]} */
672
+ get childNodes() {
673
+ const nodes = [];
674
+ let cursor = this.previousSibling ? this.previousSibling.nextSibling : this.realParentNode.firstChild;
675
+ while (cursor && cursor != this.nextSibling) {
676
+ nodes.push(cursor);
677
+ cursor = cursor.nextSibling;
682
678
  }
683
- );
679
+ return nodes;
680
+ }
681
+ /**
682
+ * @param {string} selector
683
+ * @returns {Element[]}
684
+ */
685
+ querySelectorAll(selector) {
686
+ return this.childNodes.reduce(
687
+ (results, node) => {
688
+ if (node instanceof Element) {
689
+ if (node.matches(selector)) results.push(node);
690
+ const nodeList = node.querySelectorAll(selector);
691
+ for (let i = 0; i < nodeList.length; i++) {
692
+ results.push(nodeList[i]);
693
+ }
694
+ }
695
+ return results;
696
+ },
697
+ /** @type {Element[]} */
698
+ []
699
+ );
700
+ }
701
+ /**
702
+ * @param {Node} node
703
+ * @param {Node} referenceNode
704
+ * @returns {Node}
705
+ */
706
+ insertBefore(node, referenceNode) {
707
+ return this.realParentNode.insertBefore(node, referenceNode);
708
+ }
709
+ /**
710
+ * @param {Node} node
711
+ * @param {Node} referenceNode
712
+ * @returns {Node}
713
+ */
714
+ moveBefore(node, referenceNode) {
715
+ return this.realParentNode.moveBefore(node, referenceNode);
716
+ }
717
+ /**
718
+ * for later use with populateIdMapWithTree to halt upwards iteration
719
+ * @returns {Node}
720
+ */
721
+ get __idiomorphRoot() {
722
+ return this.originalNode;
723
+ }
684
724
  }
685
725
  function parseContent(newContent) {
686
726
  let parser = new DOMParser();
@@ -985,10 +1025,13 @@ const Element$1 = ({ component, templates: templates2, start: start2 }) => {
985
1025
  }
986
1026
  };
987
1027
  };
988
- const templates = {};
989
1028
  const config = {
990
1029
  tags: ["{{", "}}"]
991
1030
  };
1031
+ const templates = {};
1032
+ const booleanAttrs = /html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)="(.*?)"/g;
1033
+ const htmlAttr = /html-([^\s]*?)="(.*?)"/g;
1034
+ const tagExpr = () => new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, "g");
992
1035
  const templateConfig$1 = (newconfig) => {
993
1036
  Object.assign(config, newconfig);
994
1037
  };
@@ -1014,31 +1057,29 @@ const compile = (html) => {
1014
1057
  `);
1015
1058
  };
1016
1059
  const tagElements = (target, keys, components) => {
1017
- target.querySelectorAll(keys.toString()).forEach((node) => {
1018
- const name = node.localName;
1019
- if (name === "template") {
1020
- return tagElements(node.content, keys, components);
1060
+ const isComponent = (key) => key in components;
1061
+ const selector = keys.join(",");
1062
+ target.querySelectorAll(selector).forEach((node) => {
1063
+ if (node.localName === "template") {
1064
+ tagElements(node.content, keys, components);
1065
+ return;
1021
1066
  }
1022
- if (node.getAttribute("html-if") && !node.id) {
1067
+ if (node.hasAttribute("html-if") && !node.id) {
1023
1068
  node.id = uuid();
1024
1069
  }
1025
- if (name in components) {
1070
+ if (isComponent(node.localName)) {
1026
1071
  node.setAttribute("tplid", uuid());
1027
1072
  }
1028
1073
  });
1029
1074
  };
1030
1075
  const transformAttributes = (html) => {
1031
- const regexTags = new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, "g");
1032
- return html.replace(/jails___scope-id/g, "%%_=$scopeid_%%").replace(regexTags, "%%_=$1_%%").replace(/html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)=\"(.*?)\"/g, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`).replace(/html-([^\s]*?)=\"(.*?)\"/g, (all, key, value) => {
1033
- if (key === "key" || key === "model" || key === "scopeid") {
1034
- return all;
1035
- }
1076
+ return html.replace(/jails___scope-id/g, "%%_=$scopeid_%%").replace(tagExpr(), "%%_=$1_%%").replace(booleanAttrs, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`).replace(htmlAttr, (all, key, value) => {
1077
+ if (["key", "model", "scopeid"].includes(key)) return all;
1036
1078
  if (value) {
1037
1079
  value = value.replace(/^{|}$/g, "");
1038
1080
  return `${key}="%%_=safe(function(){ return ${value} })_%%"`;
1039
- } else {
1040
- return all;
1041
1081
  }
1082
+ return all;
1042
1083
  });
1043
1084
  };
1044
1085
  const transformTemplate = (clone) => {