Permalink
Browse files

Merge branch 'master' of git://github.com/tmpvar/jsdom

  • Loading branch information...
2 parents b81b0fc + 0bf088e commit 11f5c7b66eff09c51e1fd3a27eefedd621a27bf5 @aredridel aredridel committed Jun 20, 2010
Showing 989 changed files with 2,905 additions and 173,035 deletions.
@@ -1,18 +1,15 @@
var sys = require('sys');
-
-var dom = require('../level1/core').dom.level1.core;
-var browser = require('./index').windowAugmentation(dom);
-
+var dom = require('../../lib/level1/core').dom.level1.core;
+var browser = require('../../lib/browser/index').windowAugmentation(dom);
var document = browser.document;
var window = browser.window;
var self = browser.self;
var navigator = browser.navigator;
var location = browser.location;
-
document.title = 'Test Title';
//GLOBAL
@@ -21,7 +18,6 @@ el.id = 'foo';
el.innerHTML = '<em>This is a test</em> This <strong class="odd">is another</strong> test ';
document.body.appendChild(el);
-
//SCOPED
var el2 = browser.document.createElement('div');
el2.id = 'foo2bar';
View
@@ -1,16 +1,12 @@
-var sys = require("sys"),
- dom = require(__dirname +"/../../lib/level1/core").dom.level1.core,
- fs = require("fs");
+var sys = require("sys"), fs = require("fs");
-var window = require(__dirname + "/../../lib/browser").windowAugmentation(dom);
+var dom = require("../../lib/level1/core").dom.level1.core;
+var window = require("../../lib/browser").windowAugmentation(dom).window;
var document = window.document;
var location = window.location;
-var navigator = window.navigator = { userAgent: "node-js" };
-global.window = window;
+var navigator = window.navigator;
fs.readFile(__dirname + "/jquery.js", function(err, data) {
- global.window.document.compareDocumentPosition = function() {};
- dom.Node.prototype.addEventListener = window.addEventListener = window.document.addEventListener = function() {};
try {
eval(data.toString());
View
@@ -4,7 +4,7 @@ var sax = require("./sax");
var sys = require("sys");
-
+// TODO: change this example to use pluggable parser
/**
setup innerHTML setter
@@ -0,0 +1,179 @@
+//Make configurable from docType??
+//Set in option
+var isXHTML = false;
+
+//List from node-htmlparser
+var singleTags = {
+ area: 1,
+ base: 1,
+ basefont: 1,
+ br: 1,
+ col: 1,
+ frame: 1,
+ hr: 1,
+ img: 1,
+ input: 1,
+ isindex: 1,
+ link: 1,
+ meta: 1,
+ param: 1,
+ embed: 1
+};
+
+var styleIgnore = {
+ top: 1,
+ left: 1
+};
+
+var stringifyElement = function(element) {
+ //sys.puts('Stringify HTML for: ' + element);
+ var tagName = element.tagName.toLowerCase(),
+ ret = {
+ start: "<" + tagName,
+ end:''
+ }, attributes = [], i,
+ attribute = null;
+
+ //sys.puts('Checking Attributes: ' + element._attributes.length);
+ //sys.puts(sys.inspect(element));
+ if (element.attributes.length) {
+ ret.start += " ";
+ for (i = 0; i<element.attributes.length; i++) {
+ attribute = element.attributes.item(i);
+ attributes.push(attribute.name + '="' + attribute.nodeValue + '"');
+ }
+ //sys.puts('attributes: ' + sys.inspect(attributes));
+ }
+ ret.start += attributes.join(" ");
+ if (element.style) {
+ var styleAttrs = [];
+ for (var i in element.style) {
+ if (!styleIgnore[i]) {
+ var use = true;
+ //sys.puts('Style: ' + i + ' :: ' + element.style[i] );
+ if (i === 'position' && element.style[i] === 'static') {
+ use = false;
+ }
+ if (element.style[i] === '') {
+ use = false;
+ }
+ if (use) {
+ styleAttrs.push(i + ': ' + element.style[i]);
+ }
+ }
+ }
+ if (styleAttrs.length) {
+ ret.start += ' style="' + styleAttrs.join('; ') + '"';
+ }
+ }
+
+ if (singleTags[tagName]) {
+ if (isXHTML) {
+ ret.start += "/";
+ }
+ ret.start += ">";
+ ret.end = '';
+ } else {
+ ret.start += ">";
+ ret.end = "</" + tagName + ">";
+ }
+
+ return ret;
+};
+
+var formatHTML = function(html) {
+ var formatted = '',
+ reg = /(>)(<)(\/*)/g,
+ html = html.replace(reg, '$1\r\n$2$3'),
+ pad = 0,
+ tags = [];
+
+ for (i in singleTags) {
+ tags.push(i);
+ }
+ tags = '<' + tags.join('|<');
+
+ html.split('\r\n').forEach(function(node, index) {
+ var indent = 0, padding = '', i;
+
+ if (node.match( /.+<\/\w[^>]*>$/ )) {
+ indent = 0;
+ } else if (node.match( /^<\/\w/ )) {
+ if (pad != 0) {
+ pad -= 1;
+ }
+ } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
+ var re = new RegExp(tags, i);
+ if (!re.exec(node)) {
+ indent = 1;
+ }
+ } else {
+ indent = 0;
+ }
+
+ for (i = 0; i < pad; i++) {
+ padding += ' ';
+ }
+
+ formatted += padding + node + '\r\n';
+ pad += indent;
+ });
+
+ return formatted;
+}
+
+var generateHtmlRecursive = function(element) {
+ //sys.puts('Generating HTML for: ' + element);
+
+ var ret = "";
+ if (element) {
+ if (element.nodeType &&
+ element.nodeType === element.ENTITY_REFERENCE_NODE)
+ {
+ element = element._entity;
+ }
+
+ switch (element.nodeType) {
+ case element.ELEMENT_NODE:
+ var current = stringifyElement(element);
+ ret += current.start;
+
+ if (element.childNodes.length > 0) {
+ for (var i=0; i<element.childNodes.length; i++) {
+ ret += generateHtmlRecursive(element.childNodes.item(i));
+ }
+ } else {
+ ret += element.nodeValue || "" ;
+ }
+ ret += current.end;
+ break;
+ case element.TEXT_NODE:
+ ret += element.nodeValue;
+ break;
+ case element.COMMENT_NODE:
+ ret += '<!-- ' + element.nodeValue + ' -->';
+ break;
+ }
+ }
+ //require('sys').puts(require('sys').inspect(ret));
+ return ret;
+};
+
+exports.domToHtml = function(dom){
+
+ var ret = "";
+ if(dom.item){
+ // node list
+ var length = dom.length;
+ for (var i=0; i<length; i++) {
+ ret += generateHtmlRecursive(dom.item(i));
+ }
+
+ } else {
+ // single node
+ ret = generateHtmlRecursive(dom);
+ }
+
+ return formatHTML(ret);
+
+}
@@ -0,0 +1,130 @@
+var HtmlToDom = function(parser){
+
+ if(parser && parser.write) {
+ // sax parser
+
+ this.appendHtmlToElement = function(html, element){
+
+ var currentElement = element, currentLevel = 0;
+
+ parser.onerror = function (e) {};
+
+ parser.ontext = function (t) {
+ var ownerDocument = currentElement.ownerDocument || currentElement;
+ var newText = ownerDocument.createTextNode(t);
+ currentElement.appendChild(newText);
+ };
+
+ parser.onopentag = function (node) {
+ var nodeName = node.name.toLowerCase(),
+ document = currentElement.ownerDocument || currentElement,
+ newElement = document.createElement(nodeName),
+ i = 0,
+ length = (node.attributes && node.attributes.length) ?
+ node.attributes.length :
+ 0;
+ for (i in node.attributes)
+ {
+ if (node.attributes.hasOwnProperty(i)) {
+ newElement.setAttribute(i, node.attributes[i]);
+ }
+ }
+ currentElement.appendChild(newElement);
+ currentElement = newElement;
+ };
+
+ parser.onclosetag = function(node) {
+ currentElement = currentElement.parentNode;
+ }
+
+ parser.write(html).close();
+
+ return element;
+ }
+
+ } else if(parser && (parser.ParseHtml || parser.DefaultHandler)) {
+
+ // Forgiving HTML parser
+
+ if(parser.ParseHtml){
+ // davglass/node-htmlparser
+ } else if(parser.DefaultHandler){
+ // tautologistics/node-htmlparser
+ parser.ParseHtml = function(rawHtml){
+ var handler = new this.DefaultHandler();
+ var parser = new this.Parser(handler);
+ parser.parseComplete(rawHtml);
+ return handler.dom;
+ }
+ }
+
+ this.appendHtmlToElement = function(html, element){
+
+ if (typeof html !== 'string') {
+ html +='';
+ }
+
+ // jos3000: not sure why this is here to clean up whitespace...
+ html = html.replace(/\n/gm, '');
+ html = html.replace(/ /gm, '');
+
+ var parsed = parser.ParseHtml(html);
+
+ for (var i = 0; i < parsed.length; i++) {
+ setChild.call(element, parsed[i]);
+ }
+
+ return element;
+
+ }
+
+
+ } else {
+
+ this.appendHtmlToElement = function(){
+ var sys = require('sys');
+ sys.puts('');
+ sys.puts('###########################################################');
+ sys.puts('# WARNING: node-htmlparser could not be found.');
+ sys.puts('# Element.innerHTML setter support has been disabled');
+ sys.puts('# Element.innerHTML getter support will still function');
+ sys.puts('# Download: http://github.com/tautologistics/node-htmlparser');
+ sys.puts('###########################################################');
+ sys.puts('');
+
+ }
+
+ }
+}
+
+// utility function for forgiving parser
+var setChild = function(node) {
+
+ var newNode, currentDocument = this.ownerDocument || this;
+ if (node.type == 'tag' || node.type == 'script' || node.type == 'style') {
+ newNode = currentDocument.createElement(node.name);
+ }
+ if (node.type == 'text') {
+ newNode = currentDocument.createTextNode(node.data);
+ }
+ if (node.type == 'comment') {
+ newNode = currentDocument.createComment(node.data);
+ }
+ if (node.attribs && newNode) {
+ for (var c in node.attribs) {
+ newNode.setAttribute(c, node.attribs[c]);
+ }
+ }
+ if (node.children && newNode) {
+ for (var c = 0; c < node.children.length; c++) {
+ setChild.call(newNode, node.children[c]);
+ }
+ }
+ if (newNode) {
+ return this.appendChild(newNode);
+ } else {
+ return null;
+ }
+};
+
+exports.HtmlToDom = HtmlToDom;
Oops, something went wrong.

0 comments on commit 11f5c7b

Please sign in to comment.