Commit
- Replaced use of Object.keys, Array.isArray and Array.prototype.map with their Underscore equivalents. - Fall back to DOM 0 event registration when addEventListener is not available. - Fall back to jQuery.isPlainObject for attribute object check when __proto__ is not available.
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,10 @@ function domBuilder(json, refs) { | |
if (typeof json === 'string') return document.createTextNode(json); | ||
|
||
// Pass through html elements as-is | ||
if (json instanceof HTMLElement) return json; | ||
if (_.isElement(json)) return json; | ||
|
||
// Stringify any other value types | ||
if (!Array.isArray(json)) return document.createTextNode(json + ""); | ||
if (!_.isArray(json)) return document.createTextNode(json + ""); | ||
|
||
// Empty arrays are just empty fragments. | ||
if (!json.length) return document.createDocumentFragment(); | ||
|
@@ -38,7 +38,7 @@ function domBuilder(json, refs) { | |
node = document.createElement(tag); | ||
first = true; | ||
var classes = part.match(CLASS_MATCH); | ||
if (classes) node.setAttribute('class', classes.map(stripFirst).join(' ')); | ||
if (classes) node.setAttribute('class', _.map(classes, stripFirst).join(' ')); | ||
var id = part.match(ID_MATCH); | ||
if (id) node.setAttribute('id', id[0].substr(1)); | ||
var ref = part.match(REF_MATCH); | ||
|
@@ -50,7 +50,9 @@ function domBuilder(json, refs) { | |
} | ||
|
||
// Except the first item if it's an attribute object | ||
if (first && typeof part === 'object' && part.__proto__ === Object.prototype) { | ||
if (first && typeof part === 'object' && (part.__proto__ | ||
? part.__proto__ === Object.prototype | ||
: jQuery.isPlainObject(part))) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
creationix
|
||
setAttrs(node, part); | ||
} else { | ||
node.appendChild(domBuilder(part, refs)); | ||
|
@@ -61,7 +63,7 @@ function domBuilder(json, refs) { | |
}; | ||
|
||
function setAttrs(node, attrs) { | ||
var keys = Object.keys(attrs); | ||
var keys = _.keys(attrs); | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
var key = keys[i]; | ||
var value = attrs[key]; | ||
|
@@ -70,15 +72,20 @@ function setAttrs(node, attrs) { | |
} else if (key === "css") { | ||
setStyle(node.style, value); | ||
} else if (key.substr(0, 2) === "on") { | ||
node.addEventListener(key.substr(2), value, false); | ||
if (node.addEventListener) { | ||
node.addEventListener(key.substr(2), value, false) | ||
} | ||
else { | ||
node[key] = value; | ||
} | ||
} else { | ||
node.setAttribute(key, value); | ||
} | ||
} | ||
} | ||
|
||
function setStyle(style, attrs) { | ||
var keys = Object.keys(attrs); | ||
var keys = _.keys(attrs); | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
var key = keys[i]; | ||
style[key] = attrs[key]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"name": "dombuilder", | ||
"version": "0.1.0", | ||
"description": "An easy dombuilder using json-ml style syntax", | ||
"name": "backbone-dombuilder", | ||
"version": "0.1.0-mod1", | ||
"description": "Fork of creationix/dombuilder which uses Backbone.js dependencies for IE8 compatibility", | ||
"main": "dombuilder.js", | ||
"scripts": { | ||
"test": "phantomjs test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/creationix/dombuilder.git" | ||
"url": "https://github.com/insin/backbone-dombuilder.git" | ||
} | ||
} |
Actually, I think the
__proto__
check can simply be removed. The_.isElement
and_.isArray
checks above already filter out the other object types we care about.