create-element-ns
Advanced tools
Comparing version 0.5.0 to 0.6.0
49
elm.js
@@ -1,7 +0,7 @@ | ||
var common = require('./common') | ||
var dom = require('dom-document'), | ||
decorators = require('./decorators'), | ||
namespaces = require('./namespaces'), | ||
is = require('./is'), | ||
parse = require('parse-element-selector') | ||
var is = common.is, | ||
parse = common.parseSelector, | ||
namespaces = common.namespaces | ||
module.exports = elementFactory | ||
@@ -11,35 +11,34 @@ | ||
function create(/*[,sel][,opt][,cnt]*/) { | ||
var cfg = assignL2({}, create) | ||
var cfg = clone2({}, create) | ||
for (var i=0; i<arguments.length; ++i) { | ||
var arg = arguments[i] | ||
if (i === 0 && !is.node(cfg.element)) { | ||
if (is.string(arg)) parse(arg, cfg) | ||
if (i === 0 && !cfg.element) { | ||
if (is.string(arg)) copyKeys(cfg, parse(arg)) | ||
else if (is.node(arg)) cfg.element = arg | ||
else if (is.function(arg)) cfg.element = arg(cfg) | ||
else if (Array.isArray(arg)) concatTo(cfg.children, arg) | ||
else if (is.object(arg)) assignL2(cfg, arg) | ||
else if (Array.isArray(arg)) flatConcat(cfg.children, arg) | ||
else if (is.object(arg)) clone2(cfg, arg) | ||
} | ||
else { | ||
if (Array.isArray(arg) || is.stringlike(arg) || is.node(arg) || is.function(arg)) { | ||
cfg.children = concatTo(cfg.children || [], arg) | ||
cfg.children = flatConcat(cfg.children || [], arg) | ||
} | ||
else if (is.object(arg)) assignL2(cfg, arg) | ||
else if (is.object(arg)) clone2(cfg, arg) | ||
} | ||
} | ||
if (cfg.element && cfg.element.tagName && !is.node(cfg.element)) { | ||
var xmlns = cfg.element.xmlns || namespaces[cfg.element.prefix], | ||
doc = common.document, | ||
tag = cfg.element.tagName | ||
if (!cfg.element && cfg.tag) { | ||
var xmlns = cfg.xmlns || namespaces[cfg.prefix], | ||
doc = dom.document, | ||
tag = cfg.tag | ||
cfg.element = xmlns ? doc.createElementNS(xmlns, tag) : doc.createElement(tag) | ||
} | ||
if (!cfg.partial && is.node(cfg.element)) return decorate(cfg.element, cfg) | ||
if (!cfg.partial && cfg.element) return decorate(cfg.element, cfg) | ||
cfg.partial = false | ||
return elementFactory(cfg) | ||
} | ||
return config ? assignL1(create, config) : create | ||
return config ? copyKeys(create, config) : create | ||
} | ||
function decorate(el, cfg) { | ||
var decorators = common.decorators | ||
for (var k in cfg) { | ||
@@ -50,12 +49,12 @@ if (decorators[k]) decorators[k].call(cfg, el, k, cfg[k]) | ||
} | ||
function concatTo(arr, val) { | ||
if (Array.isArray(val)) for (var i=0; i<val.length; ++i) arr.push(val[i]) | ||
function flatConcat(arr, val) { | ||
if (Array.isArray(val)) for (var i=0; i<val.length; ++i) flatConcat(arr, val[i]) | ||
else arr.push(val) | ||
return arr | ||
} | ||
function assignL1(t, s) { | ||
function copyKeys(t, s) { | ||
for (var i=0, ks=Object.keys(s); i<ks.length; ++i) t[ks[i]] = s[ks[i]] | ||
return t | ||
} | ||
function assignL2(t, s) { | ||
function clone2(t, s) { | ||
for (var i=0, ks=Object.keys(s); i<ks.length; ++i) { | ||
@@ -65,3 +64,3 @@ var k = ks[i] | ||
: Array.isArray(s[k]) ? s[k].slice() | ||
: is.object(s[k]) ? assignL1({}, s[k]) | ||
: is.object(s[k]) ? copyKeys({}, s[k]) | ||
: s[k] | ||
@@ -68,0 +67,0 @@ } |
12
index.js
@@ -1,8 +0,12 @@ | ||
var factory = require('./elm'), | ||
common = require('./common') | ||
var dom = require('dom-document'), | ||
factory = require('./elm'), | ||
decorators = require('./decorators'), | ||
namespaces = require('./namespaces') | ||
module.exports = { | ||
common: common, | ||
dom: dom, | ||
namespaces: namespaces, | ||
decorators: decorators, | ||
html: factory(), | ||
svg: factory({element: {xmlns: common.namespaces.svg}}) | ||
svg: factory({xmlns: 'http://www.w3.org/2000/svg'}) | ||
} |
{ | ||
"name": "create-element-ns", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "dom createElement hyperscript with svg, namespace and selector support", | ||
@@ -16,3 +16,5 @@ "keywords": [ | ||
"dependencies": { | ||
"object-assign": "^4.1.0" | ||
"dom-document": "^1.0.0", | ||
"object-assign": "^4.1.0", | ||
"parse-element-selector": "^0.1.0" | ||
}, | ||
@@ -19,0 +21,0 @@ "devDependencies": { |
@@ -61,3 +61,3 @@ <!-- markdownlint-disable MD004 MD007 MD010 MD041 MD022 MD024 MD032 MD036 --> | ||
* `options`: an optional `qualifier` object of attributes and properties or an optional `elementDecorator` function | ||
* `qualifier`: {properties:{}, attributes:{}, style:{}, dataset:{}}. Alias `s`, `a`, `p`, `d`, `props`, `attrs` | ||
* `qualifier`: {properties:{}, attributes:{}, style:{}, dataset:{}}. Alias: `props`, `attrs` | ||
* `elementDecorator(el) => el'` modifies an element directly | ||
@@ -68,6 +68,12 @@ * `content`: optional series of string, Element and arrays of strings and Elements | ||
### Optional additional utilities | ||
dom: dom, | ||
namespaces: namespaces, | ||
decorators: decorators, | ||
html: factory(), | ||
svg: factory({xmlns: 'http://www.w3.org/2000/svg'}) | ||
} | ||
* `.api(documentAPI)` injects an external document API like `jsdom`. Uses the global `document` if not specified. | ||
* `.ns(prefix, URI)` adds additional namespace prefix (svg is already defined). E.g. `.ns('xlink', 'http://www.w3.org/1999/xlink')` | ||
* `.factory(nsDecorators, partial, URI)` to create additional namespace functions (html and svg are already defined) | ||
* `.dom.document` injects an external document API like `jsdom`. Uses the global `document` if not specified. | ||
* `.namespaces` adds additional namespace prefix (svg is already defined). E.g. `.namespaces.xlink: 'http://www.w3.org/1999/xlink'` | ||
* `.factory(options)` to create additional preset functions E.g. `xlink = factory({xmlns: 'http://www.w3.org/1999/xlink'})` | ||
@@ -74,0 +80,0 @@ # License |
@@ -10,3 +10,3 @@ var jsdom = require('jsdom'), | ||
ceNS.common.document = document | ||
ceNS.dom.document = document | ||
@@ -21,4 +21,3 @@ ct('api', function() { | ||
ct('===', typeof el, 'object') | ||
//console.log(el) | ||
//ct('===', el.nodeName.toLowerCase(), 'div') | ||
ct('===', el.nodeName.toLowerCase(), 'div') | ||
}) | ||
@@ -68,3 +67,3 @@ ct('svg', function() { | ||
var handler = function(){}, | ||
el = htm('div', {style:{color: 'blue'}, p:{className: 'c1 c2', id: 'i1', onclick: handler}}) | ||
el = htm('div', {style:{color: 'blue'}, props:{className: 'c1 c2', id: 'i1', onclick: handler}}) | ||
ct('===', el.nodeName, 'DIV') | ||
@@ -75,3 +74,2 @@ ct('===', el.id, 'i1') | ||
ct('===', el.getAttribute('style'), 'color:blue') | ||
//ct('===', el.onclick.constructor, Function) | ||
}) | ||
@@ -99,3 +97,3 @@ ct('element namespace', function() { | ||
el1 = svg('circle[xmlns:xlink="http://www.w3.org/1999/xlink"]'), | ||
el2 = htm('circle', {a: {'xmlns:xlink':'http://www.w3.org/2000/svg'}}) | ||
el2 = htm('circle', {attrs: {'xmlns:xlink':'http://www.w3.org/2000/svg'}}) | ||
ct('===', el0.hasAttributeNS('xmlns','xlink'), true) | ||
@@ -107,3 +105,3 @@ ct('===', el1.hasAttributeNS('xmlns','xlink'), true) | ||
var fac = htm('p', {partial: true}), | ||
el0 = fac({p:{textContent: 'y'}}) | ||
el0 = fac({props:{textContent: 'y'}}) | ||
ct('===', el0.textContent, 'y') | ||
@@ -110,0 +108,0 @@ }) |
81
13011
3
10
276
+ Addeddom-document@^1.0.0
+ Addedparse-element-selector@0.1.3(transitive)