create-element-ns
Advanced tools
Comparing version 0.2.0 to 0.3.0
37
api.js
var is = require('./is') | ||
var decorators = { | ||
a: setAttributes, | ||
attrs: setAttributes, | ||
attributes: setAttributes, | ||
p: setProperties, | ||
props: setProperties, | ||
properties: setProperties, | ||
children: setChildren, | ||
dataset: setObj, | ||
style: setStyle, | ||
s: setStyle, | ||
} | ||
var api = { | ||
@@ -20,3 +8,9 @@ document: typeof document !== 'undefined' ? document : null, | ||
}, | ||
decorators: decorators | ||
decorators: { | ||
attributes: setAttributes, attrs: setAttributes, a: setAttributes, | ||
properties: setProperties, props: setProperties, p: setProperties, | ||
style: setStyle, s: setStyle, | ||
dataset: setObj, | ||
children: setChildren, | ||
} | ||
} | ||
@@ -28,9 +22,12 @@ | ||
function setChildren(e, k, v) { | ||
for (var i=0; i<v.length; ++i) { | ||
var itm = v[i] | ||
var node = is.node(itm) ? itm | ||
: is.stringlike(itm) ? api.document.createTextNode(itm) | ||
: is.node(itm) ? itm | ||
: null | ||
if (node) e.appendChild(node) | ||
if (v.length === 1 && is.stringlike(v[0]) && !e.childNodes.length) e.textContent = v[0] | ||
else { | ||
for (var i=0; i<v.length; ++i) { | ||
var itm = v[i] | ||
var node = is.node(itm) ? itm | ||
: is.stringlike(itm) ? api.document.createTextNode(itm) | ||
: is.node(itm) ? itm | ||
: null | ||
if (node) e.appendChild(node) | ||
} | ||
} | ||
@@ -37,0 +34,0 @@ } |
50
elm.js
@@ -7,6 +7,10 @@ var parse = require('./sel'), | ||
function elementFactory(decorators, isPartial, ns) { | ||
function create(def /*[,opt][,cnt]*/) { | ||
var el = getElement(decorators, def, ns), | ||
function elementFactory(config) { | ||
var decorators = api.decorators | ||
function create(sel /*[,opt][,cnt]*/) { | ||
var cfg = config ? copyKeys({}, config) : {}, | ||
el = getElement(sel, cfg), | ||
cntIdx = 1 | ||
if (is.function(arguments[1])) { | ||
@@ -18,3 +22,3 @@ ++cntIdx | ||
++cntIdx | ||
decorate(el, arguments[1], decorators) | ||
decorate(el, arguments[1]) | ||
} | ||
@@ -25,33 +29,29 @@ if (cntIdx < arguments.length) { | ||
var itm = arguments[i] | ||
if (itm) concatTo(cnt, itm) | ||
if (itm || itm === 0) concatTo(cnt, itm) | ||
} | ||
if (cnt.length) { | ||
if (cnt.length === 1 && is.stringlike(cnt[0]) && !el.childNodes.length) { | ||
el.textContent = cnt[0] | ||
} | ||
else decorators.children(el, null, cnt) | ||
} | ||
if (cnt.length) decorators.children(el, null, cnt) | ||
} | ||
if (isPartial) return function factory(d) { | ||
return is.function(d) ? d(el.cloneNode(true)) : decorate(el, d, decorators) | ||
if (!cfg.partial) return el | ||
delete cfg.partial | ||
return function factory(d) { // TODO factories all the way down | ||
return is.function(d) ? d(el.cloneNode(true)) : decorate(el, d) | ||
} | ||
else return el | ||
} | ||
return create | ||
} | ||
function getElement(decorators, def, ns) { | ||
if (is.node(def)) return def | ||
if (is.function(def)) return def() | ||
function getElement(sel, cfg) { | ||
if (is.node(sel)) return decorate(sel, cfg) | ||
if (is.function(sel)) return decorate(sel(), cfg) | ||
var cfg = is.string(def) ? parse(def) | ||
: is.object(def) ? def | ||
: {} | ||
if (is.string(sel)) parse(sel, cfg) | ||
else if (is.object(sel)) copyKeys(cfg, sel) | ||
var xmlns = cfg.xmlns || api.namespaces[cfg.prefix] || ns, | ||
var xmlns = cfg.xmlns || api.namespaces[cfg.prefix], | ||
tag = cfg.tagName || 'div', | ||
doc = api.document, | ||
el = xmlns ? doc.createElementNS(xmlns, tag) : doc.createElement(tag) | ||
return decorate(el, cfg, decorators) | ||
return decorate(el, cfg) | ||
} | ||
function decorate(el, cfg, decorators) { | ||
function decorate(el, cfg) { | ||
var decorators = api.decorators | ||
for (var k in cfg) { | ||
@@ -67,1 +67,5 @@ if (decorators[k]) decorators[k](el, k, cfg[k]) | ||
} | ||
function copyKeys(t, s) { | ||
for (var i=0, ks=Object.keys(s); i<ks.length; ++i) t[ks[i]] = s[ks[i]] | ||
return t | ||
} |
12
index.js
var elm = require('./elm'), | ||
api = require('./api'), | ||
is = require('./is') | ||
is = require('./is'), | ||
sel = require('./sel') | ||
@@ -8,2 +9,3 @@ module.exports = { | ||
ns: setNs, | ||
sel: sel, | ||
is: is, | ||
@@ -13,8 +15,8 @@ factory: elm, | ||
html: { | ||
el: elm(api.decorators), | ||
fn: elm(api.decorators, true) | ||
el: elm(), | ||
fn: elm({partial: true}) | ||
}, | ||
svg: { | ||
el: elm(api.decorators, false, api.namespaces.svg), | ||
fn: elm(api.decorators, true, api.namespaces.svg) | ||
el: elm({xmlns: api.namespaces.svg}), | ||
fn: elm({xmlns: api.namespaces.svg, partial: true}) | ||
} | ||
@@ -21,0 +23,0 @@ } |
15
is.js
@@ -0,1 +1,3 @@ | ||
var isArray = Array.isArray | ||
module.exports = { | ||
@@ -6,4 +8,8 @@ node: isNode, | ||
object: isObject, | ||
stringlike: stringLike | ||
array: isArray, | ||
stringlike: stringLike, | ||
eitherOr: eitherOr | ||
} | ||
function isNode(node) { | ||
@@ -19,3 +25,3 @@ return node && node.nodeName && node.nodeType > 0 | ||
function stringLike(val) { | ||
return val && val.constructor === String || val.constructor === Number | ||
return (val && (val.constructor === String || val.constructor === Number)) || val === 0 | ||
} | ||
@@ -25,1 +31,6 @@ function isFunction(fcn) { | ||
} | ||
function eitherOr(itm) { | ||
var c = itm === undefined ? undefined : itm === null ? null : itm.constructor || Object | ||
for (var i=1; i<arguments.length; ++i) if (c === arguments[i]) return true | ||
return false | ||
} |
{ | ||
"name": "create-element-ns", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "dom createElement hyperscript with svg, namespace and selector support", | ||
"keywords": [ | ||
"dom", "createElement", | ||
"dom", | ||
"createElement", | ||
"hyperscript", | ||
"svg", "xml", "namespace" | ||
"svg", | ||
"xml", | ||
"namespace" | ||
], | ||
"author": "Hugo Villeneuve", | ||
"main": ".index.js", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"object-assign": "^4.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -14,0 +19,0 @@ "cotest": "^1.6.1" |
31
sel.js
@@ -16,5 +16,12 @@ var markers = { | ||
} | ||
/** | ||
* parse Element Selector string and return a markup definition | ||
* @param {string} sel - W3 selector string | ||
* @param {Object} [def] - predefined options | ||
* @returns {Object} markup definition | ||
*/ | ||
module.exports = function parseSel(sel, def) { | ||
if (!def) def = {attributes: {}} // [, tagName: ''][, xmlns: ''][, prefix: ''] | ||
else if (!def.attributes) def.attributes = {} | ||
module.exports = function parseSel(sel) { | ||
var res = {tagName: '', attributes: {}} | ||
var ctx = { | ||
@@ -30,3 +37,3 @@ c: markers.tag, | ||
if(act.f) { // callback and reset | ||
ctx.f(res, ctx) | ||
ctx.f(def, ctx) | ||
ctx.k = act.k | ||
@@ -41,4 +48,4 @@ ctx.f = act.f | ||
} | ||
ctx.f(res, ctx) | ||
return checkTagNS(res) | ||
ctx.f(def, ctx) | ||
return checkTagNS(def) | ||
} | ||
@@ -48,9 +55,2 @@ function setId(res, ctx) { | ||
} | ||
function setTN(res, ctx) { | ||
res.tagName = ctx.v | ||
} | ||
function setAttribute(res, ctx) { | ||
if (ctx.k === 'xmlns') res.xmlns = ctx.v | ||
else res.attributes[ctx.k] = ctx.v || true | ||
} | ||
function appendClass(res, ctx) { | ||
@@ -63,2 +63,9 @@ var att = res.attributes | ||
} | ||
function setAttribute(res, ctx) { | ||
if (ctx.k === 'xmlns') res.xmlns = ctx.v | ||
else res.attributes[ctx.k] = ctx.v || true | ||
} | ||
function setTN(res, ctx) { | ||
res.tagName = ctx.v | ||
} | ||
function checkTagNS(res) { | ||
@@ -65,0 +72,0 @@ var tagIndex = res.tagName.indexOf(':') |
@@ -1,2 +0,2 @@ | ||
var jsdom = require('jsdom').jsdom, | ||
var jsdom = require('jsdom'), | ||
ct = require('cotest'), | ||
@@ -7,5 +7,6 @@ main = require('../index') | ||
svg = main.svg.el, | ||
DOM = jsdom().defaultView | ||
document = jsdom.jsdom(), | ||
DOM = document.defaultView | ||
main.api(DOM.document) | ||
main.api(document) | ||
@@ -38,2 +39,7 @@ ct('html', function() { | ||
}) | ||
ct('html falsy children', function() { | ||
var el = htm('div', '', 0, null, [undefined, 0]) | ||
ct('===', el.childNodes.length, 2) | ||
ct('===', el.childNodes[1].textContent, '0') | ||
}) | ||
ct('mixed nested namespace', function() { | ||
@@ -40,0 +46,0 @@ var el = htm('div', svg('svg'), htm('p', 'text')) |
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
16087
399
1
+ Addedobject-assign@^4.1.0
+ Addedobject-assign@4.1.1(transitive)