Skip to content
Browse files

version 1.4.6

  • Loading branch information...
1 parent dc79bb5 commit 0537676e6d91d6479423d739e3f1e9602d1d9591 @glebm committed May 1, 2012
Showing with 50 additions and 16 deletions.
  1. +4 −0 README.md
  2. +2 −2 dombrew.coffee
  3. +39 −13 dombrew.js
  4. +1 −1 dombrew.min.js
  5. +4 −0 test/test.html
View
4 README.md
@@ -70,6 +70,10 @@ You will need node and coffee-script installed:
# Changelog / What's new
+v1.4.6
+
+* css: null and undefined values are skipped
+
v1.4.5
* #append and #prepend skip falsy values
View
4 dombrew.coffee
@@ -68,7 +68,7 @@ class Node
attr['class'] && (@e.className = joinValues(attr['class'])) && delete attr['class']
attr['text'] && (@e.innerText = joinValues(attr['text'])) && delete attr['text']
attr['html'] && (@e.innerHTML = joinValues(attr['html'])) && delete attr['html']
- (s[prop] = value for prop, value of css) if attr['css'] && (s = @e.style) && (css = attr['css']) && delete attr['css']
+ (s[prop] = value for prop, value of css when value?) if attr['css'] && (s = @e.style) && (css = attr['css']) && delete attr['css']
flattenHash(attr)
@e.setAttribute(name, value) for name, value of attr
@@ -111,7 +111,7 @@ Node::asHTML = Node::html
a = [frag]
new Node(a[0], a[1], a[2])
-D.VERSION = D.version = '1.4.5'
+D.VERSION = D.version = '1.4.6'
# innerText fix (Firefox)
if (navigator.appName != 'Microsoft Internet Explorer') && !HTMLElement::innerText && HTMLElement::__defineGetter__
View
52 dombrew.js
@@ -5,6 +5,8 @@ d = document;
Node = (function() {
var dotHashRe, flattenHash, joinValues, parseElem;
+ Node.name = 'Node';
+
Node.prototype._brew = 1;
flattenHash = function(attr) {
@@ -25,10 +27,16 @@ Node = (function() {
parseElem = function(elem, attr) {
var classes, elemType, piece, pieces, pos, _i, _len;
- if (!dotHashRe.test(elem)) return elem;
+ if (!dotHashRe.test(elem)) {
+ return elem;
+ }
attr['class'] || (attr['class'] = []);
- if (typeof attr['class'] === 'string') attr['class'] = [attr['class']];
- if (dotHashRe.test(elem.charAt(0))) elem = "div" + elem;
+ if (typeof attr['class'] === 'string') {
+ attr['class'] = [attr['class']];
+ }
+ if (dotHashRe.test(elem.charAt(0))) {
+ elem = "div" + elem;
+ }
pieces = elem.split(dotHashRe);
elemType = pieces.shift();
pos = elemType.length;
@@ -42,25 +50,33 @@ Node = (function() {
}
pos += piece.length + 1;
}
- if (!attr['class'].length) delete attr['class'];
+ if (!attr['class'].length) {
+ delete attr['class'];
+ }
return elemType;
};
joinValues = function(value) {
var i, length, r;
- if (typeof value !== 'object') return value;
+ if (typeof value !== 'object') {
+ return value;
+ }
r = [];
i = -1;
length = value.length;
while (++i < length) {
- if (value[i]) r.push(value[i]);
+ if (value[i]) {
+ r.push(value[i]);
+ }
}
return r.join(' ');
};
function Node(elem, attr, more) {
var css, name, prop, s, value;
- if (!(attr != null)) attr = {};
+ if (!(attr != null)) {
+ attr = {};
+ }
if (elem.nodeType) {
this.e = elem;
return;
@@ -81,7 +97,9 @@ Node = (function() {
if (attr['css'] && (s = this.e.style) && (css = attr['css']) && delete attr['css']) {
for (prop in css) {
value = css[prop];
- s[prop] = value;
+ if (value != null) {
+ s[prop] = value;
+ }
}
}
flattenHash(attr);
@@ -94,10 +112,14 @@ Node = (function() {
Node.prototype.append = function() {
var a, node, _i, _len;
a = arguments;
- if (a && a[0] && "splice" in a[0]) a = a[0];
+ if (a && a[0] && "splice" in a[0]) {
+ a = a[0];
+ }
for (_i = 0, _len = a.length; _i < _len; _i++) {
node = a[_i];
- if (!(node)) continue;
+ if (!(node)) {
+ continue;
+ }
('_brew' in node) && (node = node.dom());
this.e.appendChild(node);
}
@@ -107,10 +129,14 @@ Node = (function() {
Node.prototype.prepend = function() {
var a, node, _i, _len;
a = arguments;
- if (a && a[0] && "splice" in a[0]) a = a[0];
+ if (a && a[0] && "splice" in a[0]) {
+ a = a[0];
+ }
for (_i = 0, _len = a.length; _i < _len; _i++) {
node = a[_i];
- if (!(node)) continue;
+ if (!(node)) {
+ continue;
+ }
('_brew' in node) && (node = node.dom());
this.e.insertBefore(node, this.e.firstChild);
}
@@ -155,7 +181,7 @@ this.DOMBrew = D = function() {
return new Node(a[0], a[1], a[2]);
};
-D.VERSION = D.version = '1.4.5';
+D.VERSION = D.version = '1.4.6';
if ((navigator.appName !== 'Microsoft Internet Explorer') && !HTMLElement.prototype.innerText && HTMLElement.prototype.__defineGetter__) {
HTMLElement.prototype.__defineGetter__("innerText", function() {
View
2 dombrew.min.js
@@ -1 +1 @@
-var D,Node,d;d=document,Node=function(){function f(a,f,g){var h,i,j,k,l;f==null&&(f={});if(a.nodeType){this.e=a;return}if(a==="text"){this.e=d.createTextNode(f);return}typeof f=="string"&&(g||(g={}),g.text=f,f=g),this.e=d.createElement(e(a,f)),f["class"]&&(this.e.className=c(f["class"]))&&delete f["class"],f.text&&(this.e.innerText=c(f.text))&&delete f.text,f.html&&(this.e.innerHTML=c(f.html))&&delete f.html;if(f.css&&(k=this.e.style)&&(h=f.css)&&delete f.css)for(j in h)l=h[j],k[j]=l;b(f);for(i in f)l=f[i],this.e.setAttribute(i,l)}var a,b,c,e;return f.prototype._brew=1,b=function(a){var b,c,d,e;for(b in a){c=a[b];if(typeof a[b]=="object"){for(d in c)e=c[d],a[b+"-"+d.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()]=e;delete a[b]}}},a=/[.#]/,e=function(b,c){var d,e,f,g,h,i,j;if(!a.test(b))return b;c["class"]||(c["class"]=[]),typeof c["class"]=="string"&&(c["class"]=[c["class"]]),a.test(b.charAt(0))&&(b="div"+b),g=b.split(a),e=g.shift(),h=e.length,d=c["class"];for(i=0,j=g.length;i<j;i++)f=g[i],b.charAt(h)==="#"?c.id=f:d.push(f),h+=f.length+1;return c["class"].length||delete c["class"],e},c=function(a){var b,c,d;if(typeof a!="object")return a;d=[],b=-1,c=a.length;while(++b<c)a[b]&&d.push(a[b]);return d.join(" ")},f.prototype.append=function(){var a,b,c,d;a=arguments,a&&a[0]&&"splice"in a[0]&&(a=a[0]);for(c=0,d=a.length;c<d;c++){b=a[c];if(!b)continue;"_brew"in b&&(b=b.dom()),this.e.appendChild(b)}return this},f.prototype.prepend=function(){var a,b,c,d;a=arguments,a&&a[0]&&"splice"in a[0]&&(a=a[0]);for(c=0,d=a.length;c<d;c++){b=a[c];if(!b)continue;"_brew"in b&&(b=b.dom()),this.e.insertBefore(b,this.e.firstChild)}return this},f.prototype.dom=function(){return this.e},f.prototype.html=function(){var a;return a=d.createElement("div"),a.appendChild(this.e),a.innerHTML},f}(),Node.prototype.asDOM=Node.prototype.dom,Node.prototype.asHTML=Node.prototype.html,this.DOMBrew=D=function(){var a,b,c,e,f,g;a=arguments,typeof a[0]=="object"&&"splice"in a[0]?e=a[0]:a.length>1&&typeof a[1]=="object"&&"_brew"in a[1]&&(e=a);if(e){b=d.createDocumentFragment();for(f=0,g=e.length;f<g;f++)c=e[f],b.appendChild(c.e?c.e:c);a=[b]}return new Node(a[0],a[1],a[2])},D.VERSION=D.version="1.4.5",navigator.appName!=="Microsoft Internet Explorer"&&!HTMLElement.prototype.innerText&&HTMLElement.prototype.__defineGetter__&&(HTMLElement.prototype.__defineGetter__("innerText",function(){return this.textContent}),HTMLElement.prototype.__defineSetter__("innerText",function(a){return this.textContent=a}));
+var D,Node,d;d=document,Node=function(){function f(a,f,g){var h,i,j,k,l;f==null&&(f={});if(a.nodeType){this.e=a;return}if(a==="text"){this.e=d.createTextNode(f);return}typeof f=="string"&&(g||(g={}),g.text=f,f=g),this.e=d.createElement(e(a,f)),f["class"]&&(this.e.className=c(f["class"]))&&delete f["class"],f.text&&(this.e.innerText=c(f.text))&&delete f.text,f.html&&(this.e.innerHTML=c(f.html))&&delete f.html;if(f.css&&(k=this.e.style)&&(h=f.css)&&delete f.css)for(j in h)l=h[j],l!=null&&(k[j]=l);b(f);for(i in f)l=f[i],this.e.setAttribute(i,l)}var a,b,c,e;return f.name="Node",f.prototype._brew=1,b=function(a){var b,c,d,e;for(b in a){c=a[b];if(typeof a[b]=="object"){for(d in c)e=c[d],a[b+"-"+d.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()]=e;delete a[b]}}},a=/[.#]/,e=function(b,c){var d,e,f,g,h,i,j;if(!a.test(b))return b;c["class"]||(c["class"]=[]),typeof c["class"]=="string"&&(c["class"]=[c["class"]]),a.test(b.charAt(0))&&(b="div"+b),g=b.split(a),e=g.shift(),h=e.length,d=c["class"];for(i=0,j=g.length;i<j;i++)f=g[i],b.charAt(h)==="#"?c.id=f:d.push(f),h+=f.length+1;return c["class"].length||delete c["class"],e},c=function(a){var b,c,d;if(typeof a!="object")return a;d=[],b=-1,c=a.length;while(++b<c)a[b]&&d.push(a[b]);return d.join(" ")},f.prototype.append=function(){var a,b,c,d;a=arguments,a&&a[0]&&"splice"in a[0]&&(a=a[0]);for(c=0,d=a.length;c<d;c++){b=a[c];if(!b)continue;"_brew"in b&&(b=b.dom()),this.e.appendChild(b)}return this},f.prototype.prepend=function(){var a,b,c,d;a=arguments,a&&a[0]&&"splice"in a[0]&&(a=a[0]);for(c=0,d=a.length;c<d;c++){b=a[c];if(!b)continue;"_brew"in b&&(b=b.dom()),this.e.insertBefore(b,this.e.firstChild)}return this},f.prototype.dom=function(){return this.e},f.prototype.html=function(){var a;return a=d.createElement("div"),a.appendChild(this.e),a.innerHTML},f}(),Node.prototype.asDOM=Node.prototype.dom,Node.prototype.asHTML=Node.prototype.html,this.DOMBrew=D=function(){var a,b,c,e,f,g;a=arguments,typeof a[0]=="object"&&"splice"in a[0]?e=a[0]:a.length>1&&typeof a[1]=="object"&&"_brew"in a[1]&&(e=a);if(e){b=d.createDocumentFragment();for(f=0,g=e.length;f<g;f++)c=e[f],b.appendChild(c.e?c.e:c);a=[b]}return new Node(a[0],a[1],a[2])},D.VERSION=D.version="1.4.6",navigator.appName!=="Microsoft Internet Explorer"&&!HTMLElement.prototype.innerText&&HTMLElement.prototype.__defineGetter__&&(HTMLElement.prototype.__defineGetter__("innerText",function(){return this.textContent}),HTMLElement.prototype.__defineSetter__("innerText",function(a){return this.textContent=a}));
View
4 test/test.html
@@ -181,6 +181,10 @@
assert(/style.*color.*red/.test($b('span', {css: { color: 'red' }}).html()));
});
+ test("$b('span', css: { marginLeft: null }) // should ignore marginLeft", function() {
+ assert(!/margin/.test($b('span', {css: { marginLeft: null }}).html()));
+ });
+
test("$b(DOMElement).prepend($b('em'))", function() {
var $elem = $('<div><span></span></div>');

0 comments on commit 0537676

Please sign in to comment.
Something went wrong with that request. Please try again.