@simonbuchan/html
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e,t){const n=[];let o=t;for(;e!==o;){let e=0;if(!o)throw new Error("Could not find target from root");const t=o.parentNode;for(;null!==(o=o.previousSibling);)++e;n.unshift(e),o=t}return n}function t(e,t){for(const n of t)e=e.childNodes[n];return e}function n(t,n){const o=t.createElement("template");o.innerHTML=n.join("⚡");const{content:r}=o,l=t.createNodeIterator(r,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT),s=[];let a=l.nextNode();for(;a;){const t=l.nextNode();switch(a.nodeType){case Node.TEXT_NODE:{let t,n=a;for(;0<=(t=n.data.indexOf("⚡"));){const o=n;n=n.splitText(t+1);const l=t?o.splitText(t):o;t=0;let a={type:"node",path:e(r,l)};s.push(a)}break}case Node.ELEMENT_NODE:{const t=a;for(const n of t.attributes)"⚡"===n.value&&s.push({type:"attr-value",path:e(r,t),name:n.name});break}}a=t}if(s.length!==n.length-1)throw new Error(`Could not find all replacements in template: ${n.join("⚡")}`);return{element:o,content:r,slots:s}}function o(e,n,o){const r=e.importNode(n.content,!0),a=[],i=[],c=[];for(let e=0;e!==n.slots.length;e++){const o=n.slots[e],l=t(r,o.path);switch(o.type){default:throw new Error(`Unhandled type for template slot: ${JSON.stringify(o)}`);case"node":const e={type:"node",startAfter:l.previousSibling,endBefore:l.nextSibling};a.push(e),i.push(e);break;case"attr-value":const t={type:"attr-value",element:l,name:o.name};a.push(t),c.push(t)}}for(let t=0;t!==n.slots.length;t++){const n=a[t],r=o[t];switch(n.type){case"node":l(e,n,r);break;case"attr-value":s(n,r)}}return{fragment:r,slots:a,nodeSlots:i,attrValueSlots:c}}function r(e,t){const n=[];if(e)for(let o=e;(o=o.nextSibling)&&o!==t;)n.push(o);else for(let e=t;e=e.previousSibling;)n.unshift(e);return n}function l(e,t,n){const o=r(t.startAfter,t.endBefore);if("string"==typeof n){let r=o.find((e=>e.nodeType===Node.TEXT_NODE));r?r.data=n:(r=e.createTextNode(n),t.endBefore.before(r));for(const e of o)e!==r&&e.remove()}else if(null!=n&&!1!==n){o.includes(n)||t.endBefore.before(n);for(const e of o)e!==n&&e.remove()}else for(const e of o)e.remove()}function s(e,t){"string"==typeof t?e.element.setAttribute(e.name,t):null!=t&&!1!==t?(console.warn("Cannot assign non-string value to attribute slot %O: %O",name,t),e.element.removeAttribute(e.name)):e.element.removeAttribute(e.name)}const a=Symbol("template");function i(e,...t){var r;const l=null!==(r=e[a])&&void 0!==r?r:e[a]=n(document,e),{fragment:s}=o(document,l,t);return s}function c(e,...t){var r;const l=null!==(r=e[a])&&void 0!==r?r:e[a]=n(document,e);if(1!==l.content.childElementCount)throw new Error(`html.element template should have a single top-level element: ${e.join("⚡")}`);const{fragment:s}=o(document,l,t);return s.firstElementChild}i.el=c,i.element=c,i.instance=function(e,...t){var i;const c=null!==(i=e[a])&&void 0!==i?i:e[a]=n(document,e),{fragment:u,slots:f,attrValueSlots:d,nodeSlots:m}=o(document,c,t);return{fragment:u,node:u.firstChild,element:u.firstElementChild,slots:f.map((e=>({update(t){!function(e,t,n){switch(t.type){case"node":l(e,t,n);break;case"attr-value":s(t,n)}}(document,e,t)}}))),attrSlots:d.map((({element:e,name:t})=>({element:e,name:t,remove(){e.removeAttribute(t)},set(n){e.setAttribute(t,n)}}))),childSlots:m.map((({startAfter:e,endBefore:t})=>{return{nodes:()=>r(e,t),range:n,clear(){n().deleteContents()},prepend(...n){e?e.after(...n):t.parentNode.prepend(...n)},append(...e){t.before(...e)},replace(...e){n().deleteContents(),t.before(...e)}};function n(){const n=document.createRange();return e?n.setStartAfter(e):n.setStart(t.parentNode,0),n.setEndBefore(t),n}}))}},module.exports=i; | ||
"use strict";function e(e,t){const n=[];let o=t;for(;e!==o;){let e=0;if(!o)throw new Error("Could not find target from root");const t=o.parentNode;for(;null!==(o=o.previousSibling);)++e;n.unshift(e),o=t}return n}function t(e,t){for(const n of t)e=e.childNodes[n];return e}function n(e,n,o){const l=e.importNode(n.content,!0),a=[],i=[],c=[];for(let e=0;e!==n.slots.length;e++){const o=n.slots[e],r=t(l,o.path);switch(o.type){default:throw new Error(`Unhandled type for template slot: ${JSON.stringify(o)}`);case"node":const e={type:"node",startAfter:r.previousSibling,endBefore:r.nextSibling};a.push(e),i.push(e);break;case"attr-value":const t={type:"attr-value",element:r,name:o.name};a.push(t),c.push(t)}}for(let t=0;t!==n.slots.length;t++){const n=a[t],l=o[t];switch(n.type){case"node":r(e,n,l);break;case"attr-value":s(n,l)}}return{fragment:l,slots:a,nodeSlots:i,attrValueSlots:c}}function o(e,t){const n=[];if(e)for(let o=e;(o=o.nextSibling)&&o!==t;)n.push(o);else for(let e=t;e=e.previousSibling;)n.unshift(e);return n}function r(e,t,n){const r=o(t.startAfter,t.endBefore);if("string"==typeof n){let o=r.find((e=>e.nodeType===Node.TEXT_NODE));o?o.data=n:(o=e.createTextNode(n),t.endBefore.before(o));for(const e of r)e!==o&&e.remove()}else if(null!=n&&!1!==n){r.includes(n)||t.endBefore.before(n);for(const e of r)e!==n&&e.remove()}else for(const e of r)e.remove()}function s(e,t){"string"==typeof t?e.element.setAttribute(e.name,t):null!=t&&!1!==t?(console.warn("Cannot assign non-string value to attribute slot %O: %O",name,t),e.element.removeAttribute(e.name)):e.element.removeAttribute(e.name)}const l=new WeakMap;function a(t){let n=l.get(t);return n||(n=function(t,n){const o=t.createElement("template");o.innerHTML=n.join("⚡");const{content:r}=o,s=t.createNodeIterator(r,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT),l=[];let a=s.nextNode();for(;a;){const t=s.nextNode();switch(a.nodeType){case Node.TEXT_NODE:{let t,n=a;for(;0<=(t=n.data.indexOf("⚡"));){const o=n;n=n.splitText(t+1);const s=t?o.splitText(t):o;t=0;let a={type:"node",path:e(r,s)};l.push(a)}break}case Node.ELEMENT_NODE:{const t=a;for(const n of t.attributes)"⚡"===n.value&&l.push({type:"attr-value",path:e(r,t),name:n.name});break}}a=t}if(l.length!==n.length-1)throw new Error(`Could not find all replacements in template: ${n.join("⚡")}`);return{element:o,content:r,slots:l}}(document,t),l.set(t,n)),n}function i(e,...t){const o=a(e),{fragment:r}=n(document,o,t);return r}function c(e,...t){const o=a(e);if(1!==o.content.childElementCount)throw new Error(`html.element template should have a single top-level element: ${e.join("⚡")}`);const{fragment:r}=n(document,o,t);return r.firstElementChild}i.el=c,i.element=c,i.instance=function(e,...t){const l=a(e),{fragment:i,slots:c,attrValueSlots:f,nodeSlots:u}=n(document,l,t);return{fragment:i,node:i.firstChild,element:i.firstElementChild,slots:c.map((e=>({update(t){!function(e,t,n){switch(t.type){case"node":r(e,t,n);break;case"attr-value":s(t,n)}}(document,e,t)}}))),attrSlots:f.map((({element:e,name:t})=>({element:e,name:t,remove(){e.removeAttribute(t)},set(n){e.setAttribute(t,n)}}))),childSlots:u.map((({startAfter:e,endBefore:t})=>{return{nodes:()=>o(e,t),range:n,clear(){n().deleteContents()},prepend(...n){e?e.after(...n):t.parentNode.prepend(...n)},append(...e){t.before(...e)},replace(...e){n().deleteContents(),t.before(...e)}};function n(){const n=document.createRange();return e?n.setStartAfter(e):n.setStart(t.parentNode,0),n.setEndBefore(t),n}}))}},module.exports=i; |
import { collectChildNodes, createTemplate, createTemplateInstance, replacement, updateSlot, } from "./template"; | ||
const templateSymbol = Symbol("template"); | ||
const templates = new WeakMap(); | ||
function getTemplate(strings) { | ||
let template = templates.get(strings); | ||
if (!template) { | ||
template = createTemplate(document, strings); | ||
templates.set(strings, template); | ||
} | ||
return template; | ||
} | ||
/** Create some HTML content from a template string. */ | ||
export default function html(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = createTemplate(document, strings)); | ||
const template = getTemplate(strings); | ||
const { fragment } = createTemplateInstance(document, template, args); | ||
@@ -14,4 +21,3 @@ return fragment; | ||
function htmlElement(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = createTemplate(document, strings)); | ||
const template = getTemplate(strings); | ||
if (template.content.childElementCount !== 1) { | ||
@@ -26,4 +32,3 @@ throw new Error(`html.element template should have a single top-level element: ${strings.join(replacement)}`); | ||
function htmlInstance(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = createTemplate(document, strings)); | ||
const template = getTemplate(strings); | ||
const { fragment, slots, attrValueSlots, nodeSlots } = createTemplateInstance(document, template, args); | ||
@@ -30,0 +35,0 @@ return { |
@@ -1,9 +0,3 @@ | ||
import { Template, TemplateArgument } from "./template"; | ||
declare const templateSymbol: unique symbol; | ||
import { TemplateArgument } from "./template"; | ||
export { TemplateArgument }; | ||
declare global { | ||
interface TemplateStringsArray { | ||
[templateSymbol]?: Template; | ||
} | ||
} | ||
/** Create some HTML content from a template string. */ | ||
@@ -10,0 +4,0 @@ declare function html(strings: TemplateStringsArray, ...args: TemplateArgument[]): DocumentFragment; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const template_1 = require("./template"); | ||
const templateSymbol = Symbol("template"); | ||
const templates = new WeakMap(); | ||
function getTemplate(strings) { | ||
let template = templates.get(strings); | ||
if (!template) { | ||
template = (0, template_1.createTemplate)(document, strings); | ||
templates.set(strings, template); | ||
} | ||
return template; | ||
} | ||
/** Create some HTML content from a template string. */ | ||
function html(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = (0, template_1.createTemplate)(document, strings)); | ||
const template = getTemplate(strings); | ||
const { fragment } = (0, template_1.createTemplateInstance)(document, template, args); | ||
@@ -17,4 +24,3 @@ return fragment; | ||
function htmlElement(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = (0, template_1.createTemplate)(document, strings)); | ||
const template = getTemplate(strings); | ||
if (template.content.childElementCount !== 1) { | ||
@@ -29,4 +35,3 @@ throw new Error(`html.element template should have a single top-level element: ${strings.join(template_1.replacement)}`); | ||
function htmlInstance(strings, ...args) { | ||
var _a; | ||
const template = (_a = strings[templateSymbol]) !== null && _a !== void 0 ? _a : (strings[templateSymbol] = (0, template_1.createTemplate)(document, strings)); | ||
const template = getTemplate(strings); | ||
const { fragment, slots, attrValueSlots, nodeSlots } = (0, template_1.createTemplateInstance)(document, template, args); | ||
@@ -33,0 +38,0 @@ return { |
{ | ||
"name": "@simonbuchan/html", | ||
"license": "MIT", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"main": "lib/html.js", | ||
@@ -6,0 +6,0 @@ "module": "lib-esm/html.js", |
@@ -11,10 +11,13 @@ import { | ||
const templateSymbol = Symbol("template"); | ||
export { TemplateArgument }; | ||
declare global { | ||
interface TemplateStringsArray { | ||
[templateSymbol]?: Template; | ||
const templates = new WeakMap<TemplateStringsArray, Template>(); | ||
function getTemplate(strings: TemplateStringsArray): Template { | ||
let template = templates.get(strings); | ||
if (!template) { | ||
template = createTemplate(document, strings); | ||
templates.set(strings, template); | ||
} | ||
return template; | ||
} | ||
@@ -24,3 +27,3 @@ | ||
export default function html(strings: TemplateStringsArray, ...args: TemplateArgument[]): DocumentFragment { | ||
const template = strings[templateSymbol] ??= createTemplate(document, strings); | ||
const template = getTemplate(strings); | ||
const { fragment } = createTemplateInstance(document, template, args); | ||
@@ -34,3 +37,3 @@ return fragment; | ||
function htmlElement(strings: TemplateStringsArray, ...args: TemplateArgument[]): Element { | ||
const template = strings[templateSymbol] ??= createTemplate(document, strings); | ||
const template = getTemplate(strings); | ||
@@ -79,3 +82,3 @@ if (template.content.childElementCount !== 1) { | ||
function htmlInstance(strings: TemplateStringsArray, ...args: TemplateArgument[]): Instance { | ||
const template = strings[templateSymbol] ??= createTemplate(document, strings); | ||
const template = getTemplate(strings); | ||
@@ -82,0 +85,0 @@ const { fragment, slots, attrValueSlots, nodeSlots } = createTemplateInstance(document, template, args); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1148
78552