Skip to content
Browse files

Added HTMLNode.toHTML(), HTMLNode.toString() [bryan]

  • Loading branch information...
1 parent f53551d commit 04effbdeda31937976a1af9994bd18140cccba31 @gmosx committed Jan 4, 2010
Showing with 38 additions and 6 deletions.
  1. +5 −1 README.md
  2. +30 −3 engines/rhino/lib/htmlparser.js
  3. +3 −2 lib/sizzle.js
View
6 README.md
@@ -23,11 +23,15 @@ Usage
print(el.innerHTML);
});
+ print(document.toHTML());
+ print(document);
+
Credits
-------
* George Moschovitis <george.moschovitis@gmail.com>
+* Bryan Berry <bryan@olenepal.org>
Java HTML Parser
@@ -39,7 +43,7 @@ Java HTML Parser
License
-------
-Copyright (c) 2009 George Moschovitis, http://www.gmosx.com
+Copyright (c) 2009-2010 George Moschovitis, [http://www.gmosx.com](http://www.gmosx.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
View
33 engines/rhino/lib/htmlparser.js
@@ -25,7 +25,9 @@ var HTMLParser = exports.HTMLParser = function(options) {
}
HTMLParser.prototype.parseRaw = function(html) {
- return jparser.parse(new JInputSource(new JStringReader(html)));
+ var source = new JInputSource(new JStringReader(html));
+// source.setEncoding("UTF-8");
+ return jparser.parse(source);
}
HTMLParser.prototype.parse = function(html) {
@@ -39,6 +41,10 @@ var HTMLNode = function(jnode) {
this._raw = jnode;
}
+HTMLNode.prototype.getChildNodes = function() {
+ return _nodelistToArray(this._raw.getChildNodes());
+}
+
Object.defineProperty(HTMLNode.prototype, "nodeName", {
get: function() {
return String(this._raw.getNodeName());
@@ -57,6 +63,23 @@ Object.defineProperty(HTMLNode.prototype, "nodeValue", {
}
});
+HTMLNode.prototype.toString = function() {
+ return this.nodeName;
+}
+
+var JDom2Sax = Packages.nu.validator.htmlparser.dom.Dom2Sax,
+ JHtmlSerializer = Packages.nu.validator.htmlparser.sax.HtmlSerializer,
+ JStringWriter = Packages.java.io.StringWriter;
+
+HTMLNode.prototype.toHTML = function() {
+ var w = new JStringWriter(),
+ s = new JHtmlSerializer(w);
+
+ new JDom2Sax(s, s).parse(this._raw);
+
+ return String(w.toString());
+}
+
/**
* HTML Document.
*/
@@ -153,6 +176,12 @@ HTMLElement.prototype.hasAttribute = function(name) {
return this._raw.hasAttribute(name);
}
+Object.defineProperty(HTMLElement.prototype, "href", {
+ get: function() {
+ return String(this.getAttribute("href"));
+ }
+});
+
Object.defineProperty(HTMLElement.prototype, "innerHTML", {
get: function() {
return String(this._raw.getTextContent());
@@ -231,5 +260,3 @@ var _nodelistToArray = function(nodelist) {
return arr;
}
-
-
View
5 lib/sizzle.js
@@ -1,4 +1,5 @@
exports.sizzle = function(document) {
+ var window = exports;
/*!
@@ -1014,11 +1015,11 @@ var posProcess = function(selector, context){
};
// EXPOSE
-// window.Sizzle = Sizzle;
-exports.Sizzle = Sizzle;
+window.Sizzle = Sizzle;
})();
+
return exports.Sizzle;
}

0 comments on commit 04effbd

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