create-element-x
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -105,2 +105,12 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
}); | ||
var isArray = Array.isArray || function (arg) { | ||
return Object.prototype.toString.call(arg) === '[object Array]'; | ||
}; | ||
var isHTMLCollection = function isHTMLCollection(arg) { | ||
return Object.prototype.toString.call(arg) === '[object HTMLCollection]'; | ||
}; | ||
var isNodeList = function isNodeList(arg) { | ||
return Object.prototype.toString.call(arg) === '[object NodeList]'; | ||
}; | ||
var createCreateElement = exports.createCreateElement = function createCreateElement(createElement, createTextNode) { | ||
@@ -111,3 +121,7 @@ function appendChild(c) { | ||
return function (tagName, attributes, children) { | ||
return function (tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
children[_key - 2] = arguments[_key]; | ||
} | ||
var el = createElement(tagName); | ||
@@ -117,11 +131,26 @@ | ||
el.setAttribute(attr, attributes[attr]); | ||
}if (children) { | ||
if (typeof children === 'string') { | ||
el.appendChild(createTextNode(children)); | ||
} else if (children.length) { | ||
var copy = Array.prototype.slice.call(children, 0); | ||
Array.prototype.forEach.call(copy, appendChild, el); | ||
} else { | ||
el.appendChild(children); | ||
} // Support old JSX sytax that wraps children in an array, | ||
// as 3rd parameter. | ||
var cs = children[0]; | ||
var shouldCopy = void 0; | ||
if (isArray(cs) || (shouldCopy = isHTMLCollection(cs)) || (shouldCopy = isNodeList(cs))) { | ||
// In case use user provides a `NodeList` or `HTMLCollection`, | ||
// appening will have the effect of removing the item the current | ||
// collection, which in turn will cause problems which `forEach`. | ||
// So we create a copy frist: | ||
if (shouldCopy) { | ||
cs = Array.prototype.slice.call(cs, 0); | ||
} | ||
Array.prototype.forEach.call(cs, appendChild, el); | ||
// Support new JSX syntax, where each child is an additional | ||
// function parameter. | ||
} else { | ||
Array.prototype.forEach.call(children, function (child) { | ||
if (typeof child === 'string') { | ||
el.appendChild(createTextNode(child)); | ||
} else { | ||
el.appendChild(child); | ||
} | ||
}); | ||
} | ||
@@ -128,0 +157,0 @@ |
@@ -7,2 +7,2 @@ /*! | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("createElementX",[],t):"object"==typeof exports?exports.createElementX=t():e.createElementX=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createElement=t.nativeCreateElement=void 0;var r=n(1),o=t.nativeCreateElement=document.createElement;document.createElement=(0,r.createCreateElement)(o.bind(document),document.createTextNode.bind(document));var c=t.createElement=document.createElement.bind(document);t.default=c},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=t.createCreateElement=function(e,t){function n(e){"string"==typeof e?this.appendChild(t(e)):this.appendChild(e)}return function(r,o,c){var i=e(r);for(var u in o)i.setAttribute(u,o[u]);if(c)if("string"==typeof c)i.appendChild(t(c));else if(c.length){var a=Array.prototype.slice.call(c,0);Array.prototype.forEach.call(a,n,i)}else i.appendChild(c);return i}};t.default=r}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("createElementX",[],t):"object"==typeof exports?exports.createElementX=t():e.createElementX=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createElement=t.nativeCreateElement=void 0;var n=r(1),o=t.nativeCreateElement=document.createElement;document.createElement=(0,n.createCreateElement)(o.bind(document),document.createTextNode.bind(document));var c=t.createElement=document.createElement.bind(document);t.default=c},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Array.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},o=function(e){return"[object HTMLCollection]"===Object.prototype.toString.call(e)},c=function(e){return"[object NodeList]"===Object.prototype.toString.call(e)},a=t.createCreateElement=function(e,t){function r(e){"string"==typeof e?this.appendChild(t(e)):this.appendChild(e)}return function(a,i){for(var u=arguments.length,l=Array(u>2?u-2:0),f=2;f<u;f++)l[f-2]=arguments[f];var p=e(a);for(var d in i)p.setAttribute(d,i[d]);var s=l[0],y=void 0;return n(s)||(y=o(s))||(y=c(s))?(y&&(s=Array.prototype.slice.call(s,0)),Array.prototype.forEach.call(s,r,p)):Array.prototype.forEach.call(l,function(e){"string"==typeof e?p.appendChild(t(e)):p.appendChild(e)}),p}};t.default=a}])}); |
@@ -103,2 +103,12 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
}); | ||
var isArray = Array.isArray || function (arg) { | ||
return Object.prototype.toString.call(arg) === '[object Array]'; | ||
}; | ||
var isHTMLCollection = function isHTMLCollection(arg) { | ||
return Object.prototype.toString.call(arg) === '[object HTMLCollection]'; | ||
}; | ||
var isNodeList = function isNodeList(arg) { | ||
return Object.prototype.toString.call(arg) === '[object NodeList]'; | ||
}; | ||
var createCreateElement = exports.createCreateElement = function createCreateElement(createElement, createTextNode) { | ||
@@ -109,3 +119,7 @@ function appendChild(c) { | ||
return function (tagName, attributes, children) { | ||
return function (tagName, attributes) { | ||
for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
children[_key - 2] = arguments[_key]; | ||
} | ||
var el = createElement(tagName); | ||
@@ -115,11 +129,26 @@ | ||
el.setAttribute(attr, attributes[attr]); | ||
}if (children) { | ||
if (typeof children === 'string') { | ||
el.appendChild(createTextNode(children)); | ||
} else if (children.length) { | ||
var copy = Array.prototype.slice.call(children, 0); | ||
Array.prototype.forEach.call(copy, appendChild, el); | ||
} else { | ||
el.appendChild(children); | ||
} // Support old JSX sytax that wraps children in an array, | ||
// as 3rd parameter. | ||
var cs = children[0]; | ||
var shouldCopy = void 0; | ||
if (isArray(cs) || (shouldCopy = isHTMLCollection(cs)) || (shouldCopy = isNodeList(cs))) { | ||
// In case use user provides a `NodeList` or `HTMLCollection`, | ||
// appening will have the effect of removing the item the current | ||
// collection, which in turn will cause problems which `forEach`. | ||
// So we create a copy frist: | ||
if (shouldCopy) { | ||
cs = Array.prototype.slice.call(cs, 0); | ||
} | ||
Array.prototype.forEach.call(cs, appendChild, el); | ||
// Support new JSX syntax, where each child is an additional | ||
// function parameter. | ||
} else { | ||
Array.prototype.forEach.call(children, function (child) { | ||
if (typeof child === 'string') { | ||
el.appendChild(createTextNode(child)); | ||
} else { | ||
el.appendChild(child); | ||
} | ||
}); | ||
} | ||
@@ -126,0 +155,0 @@ |
@@ -7,2 +7,2 @@ /*! | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("createElementX",[],t):"object"==typeof exports?exports.createElementX=t():e.createElementX=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createElement=void 0;var r=n(1),o=t.createElement=(0,r.createCreateElement)(document.createElement.bind(document),document.createTextNode.bind(document));t.default=o},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=t.createCreateElement=function(e,t){function n(e){"string"==typeof e?this.appendChild(t(e)):this.appendChild(e)}return function(r,o,c){var i=e(r);for(var u in o)i.setAttribute(u,o[u]);if(c)if("string"==typeof c)i.appendChild(t(c));else if(c.length){var f=Array.prototype.slice.call(c,0);Array.prototype.forEach.call(f,n,i)}else i.appendChild(c);return i}};t.default=r}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("createElementX",[],t):"object"==typeof exports?exports.createElementX=t():e.createElementX=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createElement=void 0;var n=r(1),o=t.createElement=(0,n.createCreateElement)(document.createElement.bind(document),document.createTextNode.bind(document));t.default=o},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=Array.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},o=function(e){return"[object HTMLCollection]"===Object.prototype.toString.call(e)},c=function(e){return"[object NodeList]"===Object.prototype.toString.call(e)},i=t.createCreateElement=function(e,t){function r(e){"string"==typeof e?this.appendChild(t(e)):this.appendChild(e)}return function(i,u){for(var a=arguments.length,l=Array(a>2?a-2:0),f=2;f<a;f++)l[f-2]=arguments[f];var p=e(i);for(var d in u)p.setAttribute(d,u[d]);var s=l[0],y=void 0;return n(s)||(y=o(s))||(y=c(s))?(y&&(s=Array.prototype.slice.call(s,0)),Array.prototype.forEach.call(s,r,p)):Array.prototype.forEach.call(l,function(e){"string"==typeof e?p.appendChild(t(e)):p.appendChild(e)}),p}};t.default=i}])}); |
@@ -0,1 +1,8 @@ | ||
const isArray = Array.isArray | ||
|| (arg => Object.prototype.toString.call(arg) === '[object Array]'); | ||
const isHTMLCollection = arg => | ||
Object.prototype.toString.call(arg) === '[object HTMLCollection]'; | ||
const isNodeList = arg => | ||
Object.prototype.toString.call(arg) === '[object NodeList]'; | ||
export const createCreateElement = (createElement, createTextNode) => { | ||
@@ -7,3 +14,3 @@ function appendChild(c) { | ||
return (tagName, attributes, children) => { | ||
return (tagName, attributes, ...children) => { | ||
const el = createElement(tagName); | ||
@@ -13,11 +20,29 @@ | ||
if (children) { | ||
if (typeof children === 'string') { | ||
el.appendChild(createTextNode(children)); | ||
} else if (children.length) { | ||
const copy = Array.prototype.slice.call(children, 0); | ||
Array.prototype.forEach.call(copy, appendChild, el); | ||
} else { | ||
el.appendChild(children); | ||
// Support old JSX sytax that wraps children in an array, | ||
// as 3rd parameter. | ||
let cs = children[0]; | ||
let shouldCopy; | ||
if (isArray(cs) | ||
|| (shouldCopy = isHTMLCollection(cs)) | ||
|| (shouldCopy = isNodeList(cs)) | ||
) { | ||
// In case use user provides a `NodeList` or `HTMLCollection`, | ||
// appening will have the effect of removing the item the current | ||
// collection, which in turn will cause problems which `forEach`. | ||
// So we create a copy frist: | ||
if (shouldCopy) { | ||
cs = Array.prototype.slice.call(cs, 0); | ||
} | ||
Array.prototype.forEach.call(cs, appendChild, el); | ||
// Support new JSX syntax, where each child is an additional | ||
// function parameter. | ||
} else { | ||
Array.prototype.forEach.call(children, (child) => { | ||
if (typeof child === 'string') { | ||
el.appendChild(createTextNode(child)); | ||
} else { | ||
el.appendChild(child); | ||
} | ||
}); | ||
} | ||
@@ -24,0 +49,0 @@ |
{ | ||
"name": "create-element-x", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Extends document.createElement to conform to the target API of JSX", | ||
@@ -14,8 +14,8 @@ "main": "index.js", | ||
"clean": "rm -rf dist", | ||
"test": "npm run test:lint && npm run test:test", | ||
"test:lint": "eslint .", | ||
"test:test": "mocha --require babel-core/register", | ||
"lint": "eslint .", | ||
"test": "mocha --require babel-core/register", | ||
"build": "npm run build:prod & npm run build:dev & wait", | ||
"build:prod": "webpack --env prod", | ||
"build:dev": "webpack", | ||
"preversion": "npm run lint", | ||
"prepublishOnly": "npm run test && npm run clean && npm run build" | ||
@@ -22,0 +22,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38043
361