typed-dom
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -1,2 +0,2 @@ | ||
/*! typed-dom v0.0.16 https://github.com/falsandtru/typed-dom | (c) 2016, falsandtru | MIT License */ | ||
/*! typed-dom v0.0.17 https://github.com/falsandtru/typed-dom | (c) 2016, falsandtru | MIT License */ | ||
require = function e(t, n, r) { | ||
@@ -41,28 +41,28 @@ function s(o, u) { | ||
'use strict'; | ||
function build(factory, attrs, contents) { | ||
var raw = factory(); | ||
if (contents === void 0) | ||
function build(factory, attrs, children) { | ||
var element = factory(); | ||
if (children === void 0) | ||
return Object.freeze({ | ||
raw: raw, | ||
contents: contents | ||
element: element, | ||
children: children | ||
}); | ||
var mode = typeof contents === 'string' ? 'text' : Array.isArray(contents) ? 'array' : 'object'; | ||
var mode = typeof children === 'string' ? 'text' : Array.isArray(children) ? 'array' : 'object'; | ||
switch (mode) { | ||
case 'text': | ||
contents = document.createTextNode(contents); | ||
void raw.appendChild(contents); | ||
children = document.createTextNode(children); | ||
void element.appendChild(children); | ||
break; | ||
default: | ||
void Object.keys(attrs).forEach(function (name) { | ||
return raw.setAttribute(name, attrs[name] || ''); | ||
return element.setAttribute(name, attrs[name] || ''); | ||
}); | ||
void Object.keys(contents).forEach(function (k) { | ||
return void raw.appendChild(contents[k].raw); | ||
void Object.keys(children).forEach(function (k) { | ||
return void element.appendChild(children[k].element); | ||
}); | ||
switch (mode) { | ||
case 'array': | ||
void Object.freeze(contents); | ||
void Object.freeze(children); | ||
break; | ||
case 'object': | ||
void observe(contents); | ||
void observe(children); | ||
break; | ||
@@ -72,16 +72,16 @@ } | ||
return Object.freeze({ | ||
raw: raw, | ||
get contents() { | ||
element: element, | ||
get children() { | ||
switch (mode) { | ||
case 'text': | ||
return contents.data; | ||
return children.data; | ||
default: | ||
return contents; | ||
return children; | ||
} | ||
}, | ||
set contents(cs) { | ||
set children(cs) { | ||
switch (mode) { | ||
case 'text': | ||
contents.data = cs; | ||
cs = contents; | ||
children.data = cs; | ||
cs = children; | ||
break; | ||
@@ -96,4 +96,4 @@ case 'array': | ||
return os; | ||
}, contents.slice()).map(function (a) { | ||
return a.raw; | ||
}, children.slice()).map(function (a) { | ||
return a.element; | ||
}).forEach(function (a) { | ||
@@ -103,5 +103,5 @@ return void a.remove(); | ||
void cs.map(function (a) { | ||
return a.raw; | ||
return a.element; | ||
}).forEach(function (c) { | ||
return void raw.appendChild(c); | ||
return void element.appendChild(c); | ||
}); | ||
@@ -111,3 +111,3 @@ break; | ||
void Object.keys(cs).forEach(function (k) { | ||
return void raw.replaceChild(cs[k].raw, contents[k].raw); | ||
return void element.replaceChild(cs[k].element, children[k].element); | ||
}); | ||
@@ -117,10 +117,10 @@ cs = observe(cs); | ||
} | ||
contents = cs; | ||
children = cs; | ||
} | ||
}); | ||
function observe(contents) { | ||
function observe(children) { | ||
var cache = {}; | ||
return Object.keys(contents).reduce(function (contents, k) { | ||
cache[k] = contents[k]; | ||
Object.defineProperty(contents, k, { | ||
return Object.keys(children).reduce(function (children, k) { | ||
cache[k] = children[k]; | ||
Object.defineProperty(children, k, { | ||
get: function () { | ||
@@ -132,7 +132,7 @@ return cache[k]; | ||
cache[k] = newElt; | ||
raw.replaceChild(newElt.raw, oldElt.raw); | ||
element.replaceChild(newElt.element, oldElt.element); | ||
} | ||
}); | ||
return contents; | ||
}, contents); | ||
return children; | ||
}, children); | ||
} | ||
@@ -221,2 +221,3 @@ } | ||
'meta', | ||
'meter', | ||
'nav', | ||
@@ -273,6 +274,6 @@ 'nextid', | ||
].reduce(function (obj, tag) { | ||
obj[tag] = function (attrs, contents, factory) { | ||
if (!contents || typeof contents === 'function') { | ||
factory = contents; | ||
contents = attrs; | ||
obj[tag] = function (attrs, children, factory) { | ||
if (!children || typeof children === 'function') { | ||
factory = children; | ||
children = attrs; | ||
attrs = {}; | ||
@@ -282,3 +283,3 @@ } | ||
return document.createElement(tag); | ||
}, attrs, contents); | ||
}, attrs, children); | ||
}; | ||
@@ -285,0 +286,0 @@ return obj; |
@@ -1,2 +0,2 @@ | ||
/*! typed-dom v0.0.16 https://github.com/falsandtru/typed-dom | (c) 2016, falsandtru | MIT License */ | ||
require=function e(t,r,n){function o(a,c){if(!r[a]){if(!t[a]){var u="function"==typeof require&&require;if(!c&&u)return u(a,!0);if(i)return i(a,!0);var d=new Error("Cannot find module '"+a+"'");throw d.code="MODULE_NOT_FOUND",d}var f=r[a]={exports:{}};t[a][0].call(f.exports,function(e){var r=t[a][1][e];return o(r?r:e)},f,f.exports,e,t,r,n)}return r[a].exports}for(var i="function"==typeof require&&require,a=0;a<n.length;a++)o(n[a]);return o}({1:[function(e,t,r){},{}],2:[function(e,t,r){arguments[4][1][0].apply(r,arguments)},{dup:1}],3:[function(e,t,r){"use strict";function n(e,t,r){function n(e){var t={};return Object.keys(e).reduce(function(e,r){return t[r]=e[r],Object.defineProperty(e,r,{get:function(){return t[r]},set:function(e){var n=t[r];t[r]=e,o.replaceChild(e.raw,n.raw)}}),e},e)}var o=e();if(void 0===r)return Object.freeze({raw:o,contents:r});var i="string"==typeof r?"text":Array.isArray(r)?"array":"object";switch(i){case"text":r=document.createTextNode(r),void o.appendChild(r);break;default:switch(void Object.keys(t).forEach(function(e){return o.setAttribute(e,t[e]||"")}),void Object.keys(r).forEach(function(e){return void o.appendChild(r[e].raw)}),i){case"array":void Object.freeze(r);break;case"object":void n(r)}}return Object.freeze({raw:o,get contents(){switch(i){case"text":return r.data;default:return r}},set contents(e){switch(i){case"text":r.data=e,e=r;break;case"array":e=Object.freeze(e),void e.reduce(function(e,t){var r=e.indexOf(t);return r===-1?e:(void e.splice(r,1),e)},r.slice()).map(function(e){return e.raw}).forEach(function(e){return void e.remove()}),void e.map(function(e){return e.raw}).forEach(function(e){return void o.appendChild(e)});break;case"object":void Object.keys(e).forEach(function(t){return void o.replaceChild(e[t].raw,r[t].raw)}),e=n(e)}r=e}})}r.build=n},{}],4:[function(e,t,r){"use strict";var n=e("./builder");r.TypedHTML=["a","abbr","acronym","address","applet","area","article","aside","audio","b","base","basefont","bdo","big","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","datalist","dd","del","dfn","dir","div","dl","dt","em","embed","fieldset","figcaption","figure","font","footer","form","frame","frameset","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","isindex","kbd","keygen","label","legend","li","link","listing","map","mark","marquee","menu","meta","nav","nextid","nobr","noframes","noscript","object","ol","optgroup","option","p","param","picture","plaintext","pre","progress","q","rt","ruby","s","samp","script","section","select","small","source","span","strike","strong","style","sub","sup","table","tbody","td","template","textarea","tfoot","th","thead","title","tr","track","tt","u","ul","var","video","wbr","xmp","custom"].reduce(function(e,t){return e[t]=function(e,r,o){return r&&"function"!=typeof r||(o=r,r=e,e={}),n.build(o||function(){return document.createElement(t)},e,r)},e},{})},{"./builder":3}],5:[function(e,t,r){"use strict";var n=e("./dom/html");r.default=n.TypedHTML},{"./dom/html":4}],6:[function(e,t,r){arguments[4][1][0].apply(r,arguments)},{dup:1}],"typed-dom":[function(e,t,r){"use strict";function n(e){for(var t in e)r.hasOwnProperty(t)||(r[t]=e[t])}n(e("./src/export"));var o=e("./src/export");r.default=o.default,r.__esModule=!0},{"./src/export":5}]},{},[1,2,6,"typed-dom"]); | ||
/*! typed-dom v0.0.17 https://github.com/falsandtru/typed-dom | (c) 2016, falsandtru | MIT License */ | ||
require=function e(t,r,n){function o(c,a){if(!r[c]){if(!t[c]){var u="function"==typeof require&&require;if(!a&&u)return u(c,!0);if(i)return i(c,!0);var d=new Error("Cannot find module '"+c+"'");throw d.code="MODULE_NOT_FOUND",d}var f=r[c]={exports:{}};t[c][0].call(f.exports,function(e){var r=t[c][1][e];return o(r?r:e)},f,f.exports,e,t,r,n)}return r[c].exports}for(var i="function"==typeof require&&require,c=0;c<n.length;c++)o(n[c]);return o}({1:[function(e,t,r){},{}],2:[function(e,t,r){arguments[4][1][0].apply(r,arguments)},{dup:1}],3:[function(e,t,r){"use strict";function n(e,t,r){function n(e){var t={};return Object.keys(e).reduce(function(e,r){return t[r]=e[r],Object.defineProperty(e,r,{get:function(){return t[r]},set:function(e){var n=t[r];t[r]=e,o.replaceChild(e.element,n.element)}}),e},e)}var o=e();if(void 0===r)return Object.freeze({element:o,children:r});var i="string"==typeof r?"text":Array.isArray(r)?"array":"object";switch(i){case"text":r=document.createTextNode(r),void o.appendChild(r);break;default:switch(void Object.keys(t).forEach(function(e){return o.setAttribute(e,t[e]||"")}),void Object.keys(r).forEach(function(e){return void o.appendChild(r[e].element)}),i){case"array":void Object.freeze(r);break;case"object":void n(r)}}return Object.freeze({element:o,get children(){switch(i){case"text":return r.data;default:return r}},set children(e){switch(i){case"text":r.data=e,e=r;break;case"array":e=Object.freeze(e),void e.reduce(function(e,t){var r=e.indexOf(t);return r===-1?e:(void e.splice(r,1),e)},r.slice()).map(function(e){return e.element}).forEach(function(e){return void e.remove()}),void e.map(function(e){return e.element}).forEach(function(e){return void o.appendChild(e)});break;case"object":void Object.keys(e).forEach(function(t){return void o.replaceChild(e[t].element,r[t].element)}),e=n(e)}r=e}})}r.build=n},{}],4:[function(e,t,r){"use strict";var n=e("./builder");r.TypedHTML=["a","abbr","acronym","address","applet","area","article","aside","audio","b","base","basefont","bdo","big","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","datalist","dd","del","dfn","dir","div","dl","dt","em","embed","fieldset","figcaption","figure","font","footer","form","frame","frameset","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","isindex","kbd","keygen","label","legend","li","link","listing","map","mark","marquee","menu","meta","meter","nav","nextid","nobr","noframes","noscript","object","ol","optgroup","option","p","param","picture","plaintext","pre","progress","q","rt","ruby","s","samp","script","section","select","small","source","span","strike","strong","style","sub","sup","table","tbody","td","template","textarea","tfoot","th","thead","title","tr","track","tt","u","ul","var","video","wbr","xmp","custom"].reduce(function(e,t){return e[t]=function(e,r,o){return r&&"function"!=typeof r||(o=r,r=e,e={}),n.build(o||function(){return document.createElement(t)},e,r)},e},{})},{"./builder":3}],5:[function(e,t,r){"use strict";var n=e("./dom/html");r.default=n.TypedHTML},{"./dom/html":4}],6:[function(e,t,r){arguments[4][1][0].apply(r,arguments)},{dup:1}],"typed-dom":[function(e,t,r){"use strict";function n(e){for(var t in e)r.hasOwnProperty(t)||(r[t]=e[t])}n(e("./src/export"));var o=e("./src/export");r.default=o.default,r.__esModule=!0},{"./src/export":5}]},{},[1,2,6,"typed-dom"]); |
{ | ||
"name": "typed-dom", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Static typed html dom builder.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -19,10 +19,7 @@ # typed-dom | ||
}); | ||
assert(component.raw.nodeName === 'ARTICLE'); | ||
assert(component.raw.outerHTML === '<article><h1>title</h1><ul><li>item</li><li>item</li></ul></article>'); | ||
assert(component.contents.title.raw.nodeName === 'H1'); | ||
assert(component.contents.title.raw.outerHTML === '<h1>title</h1>'); | ||
assert(component.contents.title.contents === 'title'); | ||
assert(component.contents.content.raw.nodeName === 'UL'); | ||
assert(component.contents.content.raw.outerHTML === '<ul><li>item</li><li>item</li></ul>'); | ||
assert(component.contents.content.contents[0].content === 'item'); | ||
assert(component.element.outerHTML === '<article><h1>title</h1><ul><li>item</li><li>item</li></ul></article>'); | ||
assert(component.children.title.element.outerHTML === '<h1>title</h1>'); | ||
assert(component.children.title.children === 'title'); | ||
assert(component.children.content.element.outerHTML === '<ul><li>item</li><li>item</li></ul>'); | ||
assert(component.children.content.children[0].content === 'item'); | ||
``` |
@@ -5,12 +5,12 @@ import { TypedHTML, TypedHTMLContents } from 'typed-dom'; | ||
<S extends string, T extends HTMLElement, U extends TypedHTMLContents<HTMLElement>> | ||
(factory: () => T, attrs: {}, contents: U) | ||
(factory: () => T, attrs: {}, children: U) | ||
: TypedHTML<S, T, U> { | ||
const raw = factory(); | ||
if (contents === void 0) return <TypedHTML<S, T, U>>Object.freeze({ | ||
raw, | ||
contents: <U>contents | ||
const element = factory(); | ||
if (children === void 0) return <TypedHTML<S, T, U>>Object.freeze({ | ||
element, | ||
children: <U>children | ||
}); | ||
const mode = typeof contents === 'string' | ||
const mode = typeof children === 'string' | ||
? 'text' | ||
: Array.isArray(contents) | ||
: Array.isArray(children) | ||
? 'array' | ||
@@ -20,16 +20,16 @@ : 'object'; | ||
case 'text': | ||
contents = <any>document.createTextNode(<string>contents) | ||
void raw.appendChild(<Text><any>contents); | ||
children = <any>document.createTextNode(<string>children) | ||
void element.appendChild(<Text><any>children); | ||
break; | ||
default: | ||
void Object.keys(attrs) | ||
.forEach(name => raw.setAttribute(name, attrs[name] || '')); | ||
void Object.keys(contents) | ||
.forEach(k => void raw.appendChild(contents[k].raw)); | ||
.forEach(name => element.setAttribute(name, attrs[name] || '')); | ||
void Object.keys(children) | ||
.forEach(k => void element.appendChild(children[k].element)); | ||
switch (mode) { | ||
case 'array': | ||
void Object.freeze(contents); | ||
void Object.freeze(children); | ||
break; | ||
case 'object': | ||
void observe(contents); | ||
void observe(children); | ||
break; | ||
@@ -39,16 +39,16 @@ } | ||
return <TypedHTML<S, T, U>>Object.freeze({ | ||
raw, | ||
get contents(): U { | ||
element, | ||
get children(): U { | ||
switch (mode) { | ||
case 'text': | ||
return <U>(<Text><any>contents).data; | ||
return <U>(<Text><any>children).data; | ||
default: | ||
return contents; | ||
return children; | ||
} | ||
}, | ||
set contents(cs) { | ||
set children(cs) { | ||
switch (mode) { | ||
case 'text': | ||
(<Text><any>contents).data = <string>cs; | ||
cs = contents; | ||
(<Text><any>children).data = <string>cs; | ||
cs = children; | ||
break; | ||
@@ -64,9 +64,9 @@ | ||
return os; | ||
}, (<TypedHTML<string, T, any>[]><any>contents).slice()) | ||
.map(a => (<TypedHTML<string, T, any>>a).raw) | ||
}, (<TypedHTML<string, T, any>[]><any>children).slice()) | ||
.map(a => (<TypedHTML<string, T, any>>a).element) | ||
.forEach(a => | ||
void a.remove()); | ||
void (<TypedHTML<string, T, any>[]><any>cs) | ||
.map(a => (<TypedHTML<string, T, any>>a).raw) | ||
.forEach(c => void raw.appendChild(c)); | ||
.map(a => (<TypedHTML<string, T, any>>a).element) | ||
.forEach(c => void element.appendChild(c)); | ||
break; | ||
@@ -76,3 +76,3 @@ | ||
void Object.keys(cs) | ||
.forEach(k => void raw.replaceChild(cs[k].raw, contents[k].raw)); | ||
.forEach(k => void element.replaceChild(cs[k].element, children[k].element)); | ||
cs = observe(cs); | ||
@@ -82,12 +82,12 @@ break; | ||
} | ||
contents = cs; | ||
children = cs; | ||
} | ||
}); | ||
function observe(contents: U): U { | ||
function observe(children: U): U { | ||
const cache = {}; | ||
return Object.keys(contents) | ||
.reduce((contents, k) => { | ||
cache[k] = contents[k]; | ||
Object.defineProperty(contents, k, { | ||
return Object.keys(children) | ||
.reduce((children, k) => { | ||
cache[k] = children[k]; | ||
Object.defineProperty(children, k, { | ||
get() { | ||
@@ -99,8 +99,8 @@ return cache[k]; | ||
cache[k] = newElt; | ||
raw.replaceChild(newElt.raw, oldElt.raw); | ||
element.replaceChild(newElt.element, oldElt.element); | ||
} | ||
}); | ||
return contents; | ||
}, contents); | ||
return children; | ||
}, children); | ||
} | ||
} |
@@ -79,2 +79,3 @@ import { TypedHTML as ITypedHTML, TypedHTMLContents } from 'typed-dom'; | ||
'meta', | ||
'meter', | ||
'nav', | ||
@@ -135,12 +136,12 @@ 'nextid', | ||
<T extends TypedHTMLContents<HTMLElement>> | ||
(attrs: { [name: string]: string; }, contents: T, factory?: () => HTMLElement) | ||
(attrs: { [name: string]: string; }, children: T, factory?: () => HTMLElement) | ||
: TypedHTML<string, HTMLElement, T> => { | ||
if (!contents || typeof contents === 'function') { | ||
factory = <any>contents; | ||
contents = <any>attrs; | ||
if (!children || typeof children === 'function') { | ||
factory = <any>children; | ||
children = <any>attrs; | ||
attrs = {}; | ||
} | ||
return build(factory || (() => document.createElement(tag)), attrs, contents); | ||
return build(factory || (() => document.createElement(tag)), attrs, children); | ||
} | ||
return obj; | ||
}, <TypedHTML<string, HTMLElement, TypedHTMLContents<HTMLElement>>>{}); |
@@ -12,5 +12,5 @@ import { Sequence } from 'spica'; | ||
const dom = TypedHTML.script({ id: 'test', src: './' }, []); | ||
assert(dom.raw.id === 'test'); | ||
assert(dom.raw.getAttribute('src') === './'); | ||
assert.deepStrictEqual(dom.contents, []); | ||
assert(dom.element.id === 'test'); | ||
assert(dom.element.getAttribute('src') === './'); | ||
assert.deepStrictEqual(dom.children, []); | ||
}); | ||
@@ -20,5 +20,5 @@ | ||
const dom = TypedHTML.script({ id: 'test', src: './' }, {}); | ||
assert(dom.raw.id === 'test'); | ||
assert(dom.raw.getAttribute('src') === './'); | ||
assert.deepStrictEqual(dom.contents, {}); | ||
assert(dom.element.id === 'test'); | ||
assert(dom.element.getAttribute('src') === './'); | ||
assert.deepStrictEqual(dom.children, {}); | ||
}); | ||
@@ -32,4 +32,4 @@ | ||
}); | ||
assert(dom.raw.id === 'test'); | ||
assert.deepStrictEqual(dom.contents, []); | ||
assert(dom.element.id === 'test'); | ||
assert.deepStrictEqual(dom.children, []); | ||
}); | ||
@@ -43,4 +43,4 @@ | ||
}); | ||
assert(dom.raw.id === 'test'); | ||
assert.deepStrictEqual(dom.contents, {}); | ||
assert(dom.element.id === 'test'); | ||
assert.deepStrictEqual(dom.children, {}); | ||
}); | ||
@@ -50,7 +50,7 @@ | ||
const dom = TypedHTML.div('<script>'); | ||
assert(dom.raw.innerHTML === '<script>'); | ||
assert(dom.contents === '<script>'); | ||
dom.contents = '<script>'; | ||
assert(dom.raw.innerHTML === '<script>'); | ||
assert(dom.contents === '<script>'); | ||
assert(dom.element.innerHTML === '<script>'); | ||
assert(dom.children === '<script>'); | ||
dom.children = '<script>'; | ||
assert(dom.element.innerHTML === '<script>'); | ||
assert(dom.children === '<script>'); | ||
}); | ||
@@ -60,4 +60,4 @@ | ||
const empty = TypedHTML.div(); | ||
assert(empty.raw.outerHTML === '<div></div>'); | ||
assert(empty.contents === void 0); | ||
assert(empty.element.outerHTML === '<div></div>'); | ||
assert(empty.children === void 0); | ||
}); | ||
@@ -70,5 +70,5 @@ | ||
}); | ||
assert(struct.raw.outerHTML === '<article><h1>title</h1><p><a></a></p></article>'); | ||
assert(struct.contents.title.raw === struct.raw.firstChild); | ||
assert(struct.contents.content.raw === struct.raw.lastChild); | ||
assert(struct.element.outerHTML === '<article><h1>title</h1><p><a></a></p></article>'); | ||
assert(struct.children.title.element === struct.element.firstChild); | ||
assert(struct.children.content.element === struct.element.lastChild); | ||
}); | ||
@@ -80,8 +80,8 @@ | ||
}); | ||
struct.contents = { | ||
struct.children = { | ||
title: TypedHTML.h1('b') | ||
}; | ||
assert(struct.contents.title.raw.textContent === 'b'); | ||
assert(struct.contents.title.raw === struct.raw.firstChild); | ||
assert(struct.contents.title.contents === 'b'); | ||
assert(struct.children.title.element.textContent === 'b'); | ||
assert(struct.children.title.element === struct.element.firstChild); | ||
assert(struct.children.title.children === 'b'); | ||
}); | ||
@@ -93,10 +93,10 @@ | ||
}); | ||
struct.contents.title = TypedHTML.h1('b'); | ||
assert(struct.contents.title.raw.textContent === 'b'); | ||
assert(struct.contents.title.raw === struct.raw.firstChild); | ||
assert(struct.contents.title.contents === 'b'); | ||
struct.contents.title.contents = 'c'; | ||
assert(struct.contents.title.raw.textContent === 'c'); | ||
assert(struct.contents.title.raw === struct.raw.firstChild); | ||
assert(struct.contents.title.contents === 'c'); | ||
struct.children.title = TypedHTML.h1('b'); | ||
assert(struct.children.title.element.textContent === 'b'); | ||
assert(struct.children.title.element === struct.element.firstChild); | ||
assert(struct.children.title.children === 'b'); | ||
struct.children.title.children = 'c'; | ||
assert(struct.children.title.element.textContent === 'c'); | ||
assert(struct.children.title.element === struct.element.firstChild); | ||
assert(struct.children.title.children === 'c'); | ||
}); | ||
@@ -109,5 +109,5 @@ | ||
]); | ||
assert(collection.raw.outerHTML === '<ul><li>1</li><li>2</li></ul>'); | ||
assert(collection.contents.length === 2); | ||
assert(collection.contents.every(({raw}, i) => raw === collection.raw.children[i])); | ||
assert(collection.element.outerHTML === '<ul><li>1</li><li>2</li></ul>'); | ||
assert(collection.children.length === 2); | ||
assert(collection.children.every(({element}, i) => element === collection.element.children[i])); | ||
}); | ||
@@ -119,15 +119,15 @@ | ||
]); | ||
collection.contents = [ | ||
collection.children = [ | ||
TypedHTML.li('2'), | ||
TypedHTML.li('3') | ||
]; | ||
assert(collection.raw.outerHTML === '<ul><li>2</li><li>3</li></ul>'); | ||
assert(collection.contents.length === 2); | ||
assert(collection.contents.every(({raw}, i) => raw === collection.raw.children[i])); | ||
collection.contents = [ | ||
assert(collection.element.outerHTML === '<ul><li>2</li><li>3</li></ul>'); | ||
assert(collection.children.length === 2); | ||
assert(collection.children.every(({element}, i) => element === collection.element.children[i])); | ||
collection.children = [ | ||
TypedHTML.li('4') | ||
]; | ||
assert(collection.raw.outerHTML === '<ul><li>4</li></ul>'); | ||
assert(collection.contents.length === 1); | ||
assert(collection.contents.every(({raw}, i) => raw === collection.raw.children[i])); | ||
assert(collection.element.outerHTML === '<ul><li>4</li></ul>'); | ||
assert(collection.children.length === 1); | ||
assert(collection.children.every(({element}, i) => element === collection.element.children[i])); | ||
@@ -146,13 +146,13 @@ // property test | ||
.forEach(([os, ns]) => { | ||
collection.contents = os; | ||
collection.children = os; | ||
Sequence.zip( | ||
Sequence.from(Array.from(collection.raw.children)), | ||
Sequence.from(os.map(({raw}) => raw))) | ||
Sequence.from(Array.from(collection.element.children)), | ||
Sequence.from(os.map(({element}) => element))) | ||
.extract() | ||
.forEach(([a, b]) => | ||
void assert(a === b)); | ||
collection.contents = ns; | ||
collection.children = ns; | ||
Sequence.zip( | ||
Sequence.from(Array.from(collection.raw.children)), | ||
Sequence.from(ns.map(({raw}) => raw))) | ||
Sequence.from(Array.from(collection.element.children)), | ||
Sequence.from(ns.map(({element}) => element))) | ||
.extract() | ||
@@ -168,8 +168,8 @@ .forEach(([a, b]) => | ||
]); | ||
assert.throws(() => collection.contents[0] = TypedHTML.li()); | ||
assert.throws(() => collection.contents.push(TypedHTML.li())); | ||
assert.throws(() => collection.contents.pop()); | ||
assert.throws(() => collection.contents.length = 0); | ||
assert(collection.contents.length === 1); | ||
assert(collection.contents.every(({raw}, i) => raw === collection.raw.children[i])); | ||
assert.throws(() => collection.children[0] = TypedHTML.li()); | ||
assert.throws(() => collection.children.push(TypedHTML.li())); | ||
assert.throws(() => collection.children.pop()); | ||
assert.throws(() => collection.children.length = 0); | ||
assert(collection.children.length === 1); | ||
assert(collection.children.every(({element}, i) => element === collection.element.children[i])); | ||
}); | ||
@@ -176,0 +176,0 @@ |
@@ -13,4 +13,4 @@ /** | ||
export interface TypedHTML<S extends string, T extends HTMLElement, U extends TypedHTMLContents<HTMLElement>> extends HTML<S> { | ||
raw: T; | ||
contents: U; | ||
element: T; | ||
children: U; | ||
} | ||
@@ -20,4 +20,4 @@ export type TypedHTMLContents<T extends HTMLElement> = never | string | TypedHTML<string, T, any>[] | { [name: string]: TypedHTML<string, T, any>; }; | ||
(): TypedHTML<S, T, never>; | ||
<U extends TypedHTMLContents<HTMLElement>>(contents: U, factory?: () => T): TypedHTML<S, T, U>; | ||
<U extends TypedHTMLContents<HTMLElement>>(attrs: { [name: string]: string; }, contents: U, factory?: () => T): TypedHTML<S, T, U>; | ||
<U extends TypedHTMLContents<HTMLElement>>(children: U, factory?: () => T): TypedHTML<S, T, U>; | ||
<U extends TypedHTMLContents<HTMLElement>>(attrs: { [name: string]: string; }, children: U, factory?: () => T): TypedHTML<S, T, U>; | ||
} | ||
@@ -27,2 +27,3 @@ | ||
// lib.dom.d.ts | ||
//[K in keyof HTMLElementTagNameMap]: TypedHTMLBuilder<HTMLElementTagNameMap[K], K>; | ||
a: TypedHTMLBuilder<HTMLAnchorElement, 'a'>; | ||
@@ -100,2 +101,3 @@ abbr: TypedHTMLBuilder<HTMLElement, 'abbr'>; | ||
meta: TypedHTMLBuilder<HTMLMetaElement, 'meta'>; | ||
meter: TypedHTMLBuilder<HTMLMeterElement, 'meter'>; | ||
nav: TypedHTMLBuilder<HTMLElement, 'nav'>; | ||
@@ -151,5 +153,5 @@ nextid: TypedHTMLBuilder<HTMLUnknownElement, 'nextid'>; | ||
// custom | ||
custom<T extends TypedHTMLContents<HTMLElement>, U extends HTMLElement, V extends string>(contents: T, factory: () => U, identity: V): TypedHTML<V, U, T>; | ||
custom<T extends TypedHTMLContents<HTMLElement>, U extends HTMLElement, V extends string>(children: T, factory: () => U, identity: V): TypedHTML<V, U, T>; | ||
}; | ||
export default TypedHTML; | ||
} |
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
42761
1113
25