Skip to content

Commit

Permalink
merged swizec's patches
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Oct 9, 2010
2 parents 662f0f9 + 134de83 commit 2bdf884
Show file tree
Hide file tree
Showing 276 changed files with 269 additions and 25,744 deletions.
4 changes: 2 additions & 2 deletions lib/jsdom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var dom = exports.dom = require("./jsdom/level1/core").dom;
exports.defaultLevel = dom.level1.core;
var dom = exports.dom = require("./jsdom/level3").dom;
exports.defaultLevel = dom.level3.html;
exports.browserAugmentation = require("./jsdom/browser").browserAugmentation;
exports.windowAugmentation = require("./jsdom/browser").windowAugmentation;

Expand Down
25 changes: 20 additions & 5 deletions lib/jsdom/browser/domtohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ var uncanon = function(str, letter) {
return '-' + letter.toLowerCase();
};

// REVIEW: escapeHTML and escapeAttr
// based on circumstantial experience
// of HTML escaping
var escapeText = function(text) {
return (text || "").replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '&lt;');

};

var escapeAttr = function(text) {
return escapeText(text).replace(/\"/g, '&quot;');
};

var styleIgnore = {
top: 1,
Expand All @@ -45,7 +58,8 @@ var stringifyElement = function(element) {
ret.start += " ";
for (i = 0; i<element.attributes.length; i++) {
attribute = element.attributes.item(i);
attributes.push(attribute.name + '="' + attribute.nodeValue + '"');
attributes.push(attribute.name + '="' +
escapeAttr(attribute.nodeValue) + '"');
}
//sys.puts('attributes: ' + sys.inspect(attributes));
}
Expand All @@ -63,7 +77,8 @@ var stringifyElement = function(element) {
use = false;
}
if (use) {
styleAttrs.push(i.replace(/([A-Z])/g, uncanon) + ': ' + element.style[i]);
styleAttrs.push(i.replace(/([A-Z])/g, uncanon) + ': ' +
escapeAttr(element.style[i]));
}
}
}
Expand Down Expand Up @@ -148,15 +163,15 @@ var generateHtmlRecursive = function(element) {
ret += generateHtmlRecursive(element.childNodes.item(i));
}
} else {
ret += element.nodeValue || "" ;
ret += escapeText(element.nodeValue);
}
ret += current.end;
break;
case element.TEXT_NODE:
ret += element.nodeValue;
ret += escapeText(element.nodeValue);
break;
case element.COMMENT_NODE:
ret += '<!-- ' + element.nodeValue + ' -->';
ret += '<!--' + element.nodeValue + '-->';
break;
case element.DOCUMENT_NODE:
ret += generateHtmlRecursive(element.documentElement);
Expand Down
86 changes: 54 additions & 32 deletions lib/jsdom/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports.windowAugmentation = function(dom, options) {
}

exports.createWindow = function(dom, options) {
options = options || {};
var document,
window = {
get document() { return document },
Expand Down Expand Up @@ -86,8 +87,18 @@ exports.createWindow = function(dom, options) {
};

window.getComputedStyle = function(node) {
//This resolves to document.defaultView.getComputedStyle
return node.style;
var s = node.style,
cs = {};

for (var n in s) {
cs[n] = s[n];
}
cs.__proto__ = {
getPropertyValue: function(name) {
return node.style[name];
}
};
return cs;
};
window.addEventListener = function() {};

Expand Down Expand Up @@ -132,11 +143,10 @@ exports.createWindow = function(dom, options) {
}

window.navigator = {
userAgent: 'Node.js (' + process.platform + '; U; rv:' +
process.version + ')',
appName: 'Node.js jsDom',
platform: process.platform,
appVersion: process.version
userAgent: 'Node.js (' + process.platform + '; U; rv:' + process.version + ')',
appName: 'Node.js jsDom',
platform: process.platform,
appVersion: process.version
};

window.window = window;
Expand Down Expand Up @@ -385,11 +395,7 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
}
}

//console.log(html);
var nodes = htmltodom.appendHtmlToElement(html, this);

//sys.puts('OUT: ' + this.outerHTML);

return html;
});

Expand Down Expand Up @@ -574,8 +580,8 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
});
dom.Element.prototype.__defineGetter__('selectedIndex', function() {
if (this.tagName.toUpperCase() == 'SELECT') {
var index = 0,
options = this.getElementsByTagName('option');
var options = this.getElementsByTagName('option'),
index = options.length > 0 ? 0 : -1;

for (var i = 0; i < options.length; i++) {
if (options[i].selected) {
Expand Down Expand Up @@ -614,35 +620,51 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) {
});

dom.Element.prototype.__defineGetter__('value', function() {

var val = this.getAttribute('value') || this.nodeValue;

if (this.tagName.toUpperCase() == 'INPUT') {
return this.getAttribute('value') || '';
}
if (this.tagName.toUpperCase() == 'TEXTAREA') {
val = this.innerHTML;
return this.innerHTML;
}
if (this.tagName.toUpperCase() == 'OPTION') {
if (val === null) {
val = this.innerHTML;
}
if (this.getAttribute('value') === '') {
val = '';
}
return this.getAttribute('value') || this.innerHTML;
}

if (this.tagName.toUpperCase() == 'SELECT') {
var selected = this.options[this.selectedIndex];
val = '';
if (selected) {
val = selected.value;
}
if (this.selectedIndex == -1) {
return '';
}
return this.options[this.selectedIndex].value;
}
return val;
return undefined;
});

dom.Element.prototype.__defineSetter__('value', function(val) {
this.nodeValue = this.innerHTML = val;
return this.setAttribute('value', val);
});
if (this.nodeName.toUpperCase() == 'INPUT') {
this.setAttribute('value', val);
} else if (this.nodeName.toUpperCase() == 'TEXTAREA') {
this.nodeValue = this.innerHTML = val;
} else if (this.nodeName.toUpperCase() == 'SELECT') {
for (var i = 0; i < this.options.length; i++) {
if (this.options[i].value == val) {
this.selectedIndex = i;
}
}
} else {
//Throw Error
}
});

dom.Element.prototype.__defineGetter__('form', function() {
var e = this;
while (e) {
if (e.nodeName.toUpperCase() == 'FORM') {
return e;
}
e = e.parentNode;
}
return undefined;
});

dom.Element.prototype.__defineGetter__('textContent', function() {
var stripHTML = /<\S[^><]*>/g;
Expand Down
15 changes: 7 additions & 8 deletions lib/jsdom/level1/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,18 @@ core.Node.prototype = {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}

var found = false;
var found = false, child;
for (var i=0;i<this._children.length;i++) {
if (this._children[i] === oldChild) {
found=true;
child = this._children[i];
this._children.remove(i,i);
oldChild._parentNode = null;

if (this.id) {
if (this._ownerDocument._ids) {
this._ownerDocument._ids[this.id] = null;
delete this._ownerDocument._ids[this.id];
if (child.id) {
if (child._ownerDocument._ids) {
child._ownerDocument._ids[child.id] = null;
delete child._ownerDocument._ids[child.id];
}
}

Expand Down Expand Up @@ -887,9 +888,7 @@ core.Element.prototype = {
if (this.attributes.exists(name)) {
this._attributes.removeNamedItem(name);
}
try {
this._attributes.setNamedItem(attr);
}catch(e) {}
this._attributes.setNamedItem(attr);
if (name === 'id') {
this._ownerDocument._ids[value] = this;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/jsdom/level2/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var core = require(__dirname + "/core").dom.level2.core;

/*
// File: html2.idl
Expand Down Expand Up @@ -572,4 +574,10 @@ module html2
};
#endif // _HTML2_IDL_
*/
*/

exports.dom = {
level2 : {
html : core
}
}
Loading

0 comments on commit 2bdf884

Please sign in to comment.