diff --git a/README.md b/README.md index 7f691e3..c406b9c 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ But don't. If you are using crel in an environment that supports Proxies, you can also use the new API: ```javascript -var crel = require('crel').proxy; +var crel = require('crel').createProxy(); var element = crel.div( crel.h1('Crello World!'), @@ -109,6 +109,18 @@ var element = crel.div( ); ``` +To allow for , you can optionally pass a keyTransform function to crel.createProxy(): + +```javascript +var crel = require('crel').createProxy(function(key){ + return key.replace(/([0-9a-z])([A-Z])/g, '$1-$2').toLowerCase(); +}); + +var element = crel.myElement(); + +element.outerHTML === '' +``` + # Browser support Crel works in everything (as far as I know), but of course... diff --git a/crel.js b/crel.js index ffbe269..d1e2660 100644 --- a/crel.js +++ b/crel.js @@ -1,166 +1,176 @@ -//Copyright (C) 2012 Kory Nunn - -//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -/* - - This code is not formatted for readability, but rather run-speed and to assist compilers. - - However, the code's intention should be transparent. - - *** IE SUPPORT *** - - If you require this library to work in IE7, add the following after declaring crel. - - var testDiv = document.createElement('div'), - testLabel = document.createElement('label'); - - testDiv.setAttribute('class', 'a'); - testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined; - testDiv.setAttribute('name','a'); - testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){ - element.id = value; - }:undefined; - - - testLabel.setAttribute('for', 'a'); - testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined; - - - -*/ - -(function (root, factory) { - if (typeof exports === 'object') { - module.exports = factory(); - } else if (typeof define === 'function' && define.amd) { - define(factory); - } else { - root.crel = factory(); - } -}(this, function () { - var fn = 'function', - obj = 'object', - nodeType = 'nodeType', - textContent = 'textContent', - setAttribute = 'setAttribute', - attrMapString = 'attrMap', - isNodeString = 'isNode', - isElementString = 'isElement', - d = typeof document === obj ? document : {}, - isType = function(a, type){ - return typeof a === type; - }, - isNode = typeof Node === fn ? function (object) { - return object instanceof Node; - } : - // in IE <= 8 Node is an object, obviously.. - function(object){ - return object && - isType(object, obj) && - (nodeType in object) && - isType(object.ownerDocument,obj); - }, - isElement = function (object) { - return crel[isNodeString](object) && object[nodeType] === 1; - }, - isArray = function(a){ - return a instanceof Array; - }, - appendChild = function(element, child) { - if (isArray(child)) { - child.map(function(subChild){ - appendChild(element, subChild); - }); - return; - } - if(!crel[isNodeString](child)){ - child = d.createTextNode(child); - } - element.appendChild(child); - }; - - - function crel(){ - var args = arguments, //Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance. - element = args[0], - child, - settings = args[1], - childIndex = 2, - argumentsLength = args.length, - attributeMap = crel[attrMapString]; - - element = crel[isElementString](element) ? element : d.createElement(element); - // shortcut - if(argumentsLength === 1){ - return element; - } - - if(!isType(settings,obj) || crel[isNodeString](settings) || isArray(settings)) { - --childIndex; - settings = null; - } - - // shortcut if there is only one child that is a string - if((argumentsLength - childIndex) === 1 && isType(args[childIndex], 'string') && element[textContent] !== undefined){ - element[textContent] = args[childIndex]; - }else{ - for(; childIndex < argumentsLength; ++childIndex){ - child = args[childIndex]; - - if(child == null){ - continue; - } - - if (isArray(child)) { - for (var i=0; i < child.length; ++i) { - appendChild(element, child[i]); - } - } else { - appendChild(element, child); - } - } - } - - for(var key in settings){ - if(!attributeMap[key]){ - if(isType(settings[key],fn)){ - element[key] = settings[key]; - }else{ - element[setAttribute](key, settings[key]); - } - }else{ - var attr = attributeMap[key]; - if(typeof attr === fn){ - attr(element, settings[key]); - }else{ - element[setAttribute](attr, settings[key]); - } - } - } - - return element; - } - - // Used for mapping one kind of attribute to the supported version of that in bad browsers. - crel[attrMapString] = {}; - - crel[isElementString] = isElement; - - crel[isNodeString] = isNode; - - if(typeof Proxy !== 'undefined'){ - crel.proxy = new Proxy(crel, { - get: function(target, key){ - !(key in crel) && (crel[key] = crel.bind(null, key)); - return crel[key]; - } - }); - } - - return crel; -})); +//Copyright (C) 2012 Kory Nunn + +//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* + + This code is not formatted for readability, but rather run-speed and to assist compilers. + + However, the code's intention should be transparent. + + *** IE SUPPORT *** + + If you require this library to work in IE7, add the following after declaring crel. + + var testDiv = document.createElement('div'), + testLabel = document.createElement('label'); + + testDiv.setAttribute('class', 'a'); + testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined; + testDiv.setAttribute('name','a'); + testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){ + element.id = value; + }:undefined; + + + testLabel.setAttribute('for', 'a'); + testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined; + + + +*/ + +(function (root, factory) { + if (typeof exports === 'object') { + module.exports = factory(); + } else if (typeof define === 'function' && define.amd) { + define(factory); + } else { + root.crel = factory(); + } +}(this, function () { + var fn = 'function', + obj = 'object', + nodeType = 'nodeType', + textContent = 'textContent', + setAttribute = 'setAttribute', + attrMapString = 'attrMap', + isNodeString = 'isNode', + isElementString = 'isElement', + d = typeof document === obj ? document : {}, + isType = function(a, type){ + return typeof a === type; + }, + isNode = typeof Node === fn ? function (object) { + return object instanceof Node; + } : + // in IE <= 8 Node is an object, obviously.. + function(object){ + return object && + isType(object, obj) && + (nodeType in object) && + isType(object.ownerDocument,obj); + }, + isElement = function (object) { + return crel[isNodeString](object) && object[nodeType] === 1; + }, + isArray = function(a){ + return a instanceof Array; + }, + appendChild = function(element, child) { + if (isArray(child)) { + child.map(function(subChild){ + appendChild(element, subChild); + }); + return; + } + if(!crel[isNodeString](child)){ + child = d.createTextNode(child); + } + element.appendChild(child); + }; + + + function crel(){ + var args = arguments, //Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance. + element = args[0], + child, + settings = args[1], + childIndex = 2, + argumentsLength = args.length, + attributeMap = crel[attrMapString]; + + element = crel[isElementString](element) ? element : d.createElement(element); + // shortcut + if(argumentsLength === 1){ + return element; + } + + if(!isType(settings,obj) || crel[isNodeString](settings) || isArray(settings)) { + --childIndex; + settings = null; + } + + // shortcut if there is only one child that is a string + if((argumentsLength - childIndex) === 1 && isType(args[childIndex], 'string') && element[textContent] !== undefined){ + element[textContent] = args[childIndex]; + }else{ + for(; childIndex < argumentsLength; ++childIndex){ + child = args[childIndex]; + + if(child == null){ + continue; + } + + if (isArray(child)) { + for (var i=0; i < child.length; ++i) { + appendChild(element, child[i]); + } + } else { + appendChild(element, child); + } + } + } + + for(var key in settings){ + if(!attributeMap[key]){ + if(isType(settings[key],fn)){ + element[key] = settings[key]; + }else{ + element[setAttribute](key, settings[key]); + } + }else{ + var attr = attributeMap[key]; + if(typeof attr === fn){ + attr(element, settings[key]); + }else{ + element[setAttribute](attr, settings[key]); + } + } + } + + return element; + } + + // Used for mapping one kind of attribute to the supported version of that in bad browsers. + crel[attrMapString] = {}; + + crel[isElementString] = isElement; + + crel[isNodeString] = isNode; + + crel.createProxy = function(keyTransform){ + if(typeof Proxy !== 'undefined'){ + if(!keyTransform){ + keyTransform = function(x){ return x }; + } + + return new Proxy(crel, { + get: function(target, key){ + var nodeKey = keyTransform(key); + var fn = crel[nodeKey]; + if (!fn) { + fn = crel[nodeKey] = crel.bind(null, nodeKey); + } + return fn; + } + }); + } + } + + return crel; +})); diff --git a/package-lock.json b/package-lock.json index 1146777..8befef6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "crel", - "version": "3.1.0", + "version": "4.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9dc2c3d..f6beb58 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "tags": [ "DOM" ], - "version": "3.1.0", + "version": "4.0.0", "main": "crel.js", "dependencies": {}, "devDependencies": { diff --git a/test/index.js b/test/index.js index 09b2918..ef873d5 100644 --- a/test/index.js +++ b/test/index.js @@ -165,7 +165,7 @@ if(typeof Proxy !== 'undefined'){ test('proxy API', function(t) { t.plan(4); - var proxyCrel = crel.proxy; + var proxyCrel = crel.createProxy(); var testElement = proxyCrel.div({class: 'foo'}, proxyCrel.span('bar') @@ -194,4 +194,34 @@ if(typeof Proxy !== 'undefined'){ t.end(); }); -} \ No newline at end of file + test('proxy API with key conversion', function(t) { + t.plan(4); + + var proxyCrel = crel.createProxy(function(key){ + return key.replace(/([0-9a-z])([A-Z])/g, '$1-$2').toLowerCase(); + }); + var testElement = proxyCrel.myTable(proxyCrel.span('bar')); + + t.equal( + testElement.tagName, + 'MY-TABLE' + ); + + t.equal( + testElement.childNodes.length, + 1 + ); + + t.equal( + testElement.childNodes[0].tagName, + 'SPAN' + ); + + t.equal( + testElement.childNodes[0].textContent, + 'bar' + ); + + t.end(); + }); +}