Skip to content

Commit

Permalink
added test and patch to ensure styles set on an element via element.s…
Browse files Browse the repository at this point in the history
…tyle.display='block' for example, are serialized during innerHTML. thanks to star for noting this issue
  • Loading branch information
thatcher committed Aug 18, 2010
1 parent c37c427 commit 04ab596
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.properties
Expand Up @@ -2,7 +2,7 @@
PROJECT: env-js
BUILD_MAJOR: 1
BUILD_MINOR: 2
BUILD_ID: 29
BUILD_ID: 30
BUILD_VERSION: ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_ID}
BUILD: ${PROJECT}.${BUILD_VERSION}
VERSION: ${BUILD} ${DSTAMP}
Expand Down
9 changes: 9 additions & 0 deletions specs/css/spec.js
Expand Up @@ -141,6 +141,15 @@ test('adding style element', function() {

});

test('serializing attribute when set with element.style.name', function(){
var divWrapper = document.createElement('div');
var div = document.createElement('div');
divWrapper.appendChild(div);
div.id = 'styleSerializeTest';
div.style.display = 'block';
equals(divWrapper.innerHTML, '<div id="styleSerializeTest" style="display: block;"/>', 'styles serialized');
});

test('box model', function(){
var div1 = document.createElement('div');
div1.id = "div1";
Expand Down
19 changes: 16 additions & 3 deletions src/css/htmlelement.js
Expand Up @@ -20,7 +20,7 @@ __extend__(HTMLElement.prototype, {
$css2properties[this.css2uuid] = new CSS2Properties(this);
}
return $css2properties[this.css2uuid];
},
}
});

/**
Expand All @@ -41,7 +41,7 @@ var updateCss2Props = function(elem, values) {
$css2properties[elem.css2uuid] = new CSS2Properties(elem);
}
__cssTextToStyles__($css2properties[elem.css2uuid], values);
}
};

var origSetAttribute = HTMLElement.prototype.setAttribute;

Expand All @@ -51,4 +51,17 @@ HTMLElement.prototype.setAttribute = function(name, value) {
if (name === "style") {
updateCss2Props(this, value);
}
}
};

var origGetAttribute = HTMLElement.prototype.getAttribute;

HTMLElement.prototype.getAttribute = function(name) {
//console.log("CSS set attribute: " + name + ", " + value);
var style;
if (name === "style") {
style = this.style.cssText;
return style===""?null:style;
}else{
return origGetAttribute.apply(this, arguments);
}
};
9 changes: 9 additions & 0 deletions src/html/element.js
Expand Up @@ -154,6 +154,7 @@ __extend__(HTMLElement.prototype, {
name = (this.tagName+"").toLowerCase(),
attrs,
attrstring = "",
style = false,
i;

// serialize namespace declarations
Expand All @@ -171,7 +172,15 @@ __extend__(HTMLElement.prototype, {
attrs = this.attributes;
for(i=0;i< attrs.length;i++){
attrstring += " "+attrs[i].name+'="'+attrs[i].xml+'"';
if(attrs[i].name == 'style'){
style = true;
}
}
if(!style ){
style = this.getAttribute('style');
if(style!='')
attrstring += ' style="'+style+'"';
}

if(this.hasChildNodes()){
// serialize this Element
Expand Down

0 comments on commit 04ab596

Please sign in to comment.