Skip to content

Commit

Permalink
fixed serialization of elements with a style attr
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed May 22, 2011
1 parent 948d1a2 commit 8604429
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
.*.swp
gmon.out
v8.log
node_modules
34 changes: 32 additions & 2 deletions lib/jsdom/browser/domtohtml.js
Expand Up @@ -40,7 +40,9 @@ var HTMLEncode = require('./htmlencoding').HTMLEncode;

var styleIgnore = {
top: 1,
left: 1
left: 1,
length : 1,
_importants : 1
};

exports.stringifyElement = function stringifyElement(element) {
Expand All @@ -61,10 +63,38 @@ exports.stringifyElement = function stringifyElement(element) {
attributes.push(attribute.name + '="' +
HTMLEncode(attribute.nodeValue) + '"');
}
//sys.puts('attributes: ' + sys.inspect(attributes));
}
ret.start += attributes.join(" ");

if (element.style) {
var
styleAttrs = [],
keys = Object.keys(element.style),
i, key,
l=keys.length;
for (i=0; i<l; i++) {
key = keys[i];
value = element.style[key];

if (!styleIgnore[key] && typeof value !== 'function') {
var use = true;
if (i === 'position' && value === 'static') {
use = false;
}
if (element.style[i] === '') {
use = false;
}
if (use) {
styleAttrs.push(key.replace(expr.upperCaseChars, uncanon) + ': ' +
HTMLEncode(value));
}
}
}
if (styleAttrs.length) {
ret.start += ' style="' + styleAttrs.join('; ') + '"';
}
}

if (singleTags[tagName]) {
if (isXHTML) {
ret.start += "/";
Expand Down
1 change: 1 addition & 0 deletions lib/jsdom/browser/htmlencoding.js
Expand Up @@ -136,5 +136,6 @@ function specialCharToEntity(s) {

exports.HTMLEncode = function encode(s) {
if (!s) return '';
if (!s.replace) return s;
return s.replace(specialCharRegExp, specialCharToEntity);
};
16 changes: 8 additions & 8 deletions lib/jsdom/level2/style.js
Expand Up @@ -128,8 +128,8 @@ function evaluateStyleAttribute(data) {
// {state: 'name'}, so instead we just build a dummy sheet.
var styleSheet = cssom.parse('dummy{' + data + '}')
, style = this.style;
console.log('evaluating style on ' + this.tagName + ': ' + data + ' ->')
console.log(styleSheet);
//console.log('evaluating style on ' + this.tagName + ': ' + data + ' ->')
//console.log(styleSheet);
while (style.length > 0) {
// TODO: find a non-n^2 way to remove all properties (this calls splice
// n times).
Expand All @@ -149,9 +149,9 @@ function evaluateStyleAttribute(data) {
html.HTMLElement.prototype.__defineGetter__('style', function() {
if (!this._cssStyleDeclaration) {
this._cssStyleDeclaration = new cssom.CSSStyleDeclaration;
console.log('creating style atribute on ' + this.nodeName)
//console.log('creating style atribute on ' + this.nodeName)
this.addEventListener('DOMAttrModified', function(e) {
console.log('style modified')
//console.log('style modified')
if ('style' === e.attrName) {
evaluateStyleAttribute.call(this, e.newValue);
}
Expand Down Expand Up @@ -199,12 +199,12 @@ html.HTMLLinkElement.prototype.__defineGetter__('sheet', getOrCreateSheet);

assert.equal(undefined, html.HTMLStyleElement._init)
html.HTMLStyleElement._init = function() {
console.log('init style')
//console.log('init style')
this.addEventListener('DOMNodeInsertedIntoDocument', function() {
console.log('style inserted')
console.log('sheet: ', this.sheet);
//console.log('style inserted')
//console.log('sheet: ', this.sheet);
if (this.type && this.type !== 'text/css') {
console.log('bad type: ' + this.type)
//console.log('bad type: ' + this.type)
return;
}
evaluateStylesheet.call(this, this.textContent, this.sheet, this._ownerDocument.URL);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -84,7 +84,8 @@
],
"dependencies": {
"htmlparser": ">=1.7.0",
"request" : ">=1.0.0"
"request" : ">=1.0.0",
"cssom" : ">=0.2.0"
},
"devDependencies" : {
"mjsunit.runner" : ">=0.1.0"
Expand Down

0 comments on commit 8604429

Please sign in to comment.