Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added DefaultHandler option "enforceEmptyTags" so that XML can be par…

…sed correctly
  • Loading branch information...
commit b954e7fec9da6a969bfcc1d3e40e1e1d57369fbb 1 parent e8b2910
@tautologistics authored
View
9 CHANGELOG
@@ -0,0 +1,9 @@
+
+v1.5.0
+ * Added DefaultHandler option "enforceEmptyTags" so that XML can be parsed correctly
+
+v1.4.2
+ * Added tests for parsing XML with namespaces
+
+v1.4.1
+ * Added minified version
View
25 README.md
@@ -1,5 +1,5 @@
#NodeHtmlParser
-A forgiving HTML parser written in JS for both the browser and NodeJS (yes, despite the name it works just fine in any modern browser). The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.
+A forgiving HTML/XML parser written in JS for both the browser and NodeJS (yes, despite the name it works just fine in any modern browser). The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.
##Running Tests
@@ -141,6 +141,29 @@ becomes:
}
]
+###Option: enforceEmptyTags
+Indicates whether the DOM should prevent children on tags marked as empty in the HTML spec. Typically this should be set to "true" HTML parsing and "false" for XML parsing. The default value is "true".
+
+####Example: true
+The following HTML:
+ <link>text</link>
+becomes:
+ [ { raw: 'link', data: 'link', type: 'tag', name: 'link' }
+ , { raw: 'text', data: 'text', type: 'text' }
+ ]
+
+####Example: false
+The following HTML:
+ <link>text</link>
+becomes:
+ [ { raw: 'link'
+ , data: 'link'
+ , type: 'tag'
+ , name: 'link'
+ , children: [ { raw: 'text', data: 'text', type: 'text' } ]
+ }
+ ]
+
##DomUtils
###TBD (see utils_example.js for now)
View
9 node-htmlparser.js
@@ -18,6 +18,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
***********************************************/
+/* v1.5.0 */
(function () {
@@ -416,6 +417,8 @@ function DefaultHandler (callback, options) {
this._options.ignoreWhitespace = false; //Keep whitespace-only text nodes
if (this._options.verbose == undefined)
this._options.verbose = true; //Keep data property for tags and raw property for all
+ if (this._options.enforceEmptyTags == undefined)
+ this._options.enforceEmptyTags = true; //Don't allow children for HTML tags defined as empty in spec
if ((typeof callback) == "function")
this._callback = callback;
}
@@ -508,7 +511,7 @@ function DefaultHandler (callback, options) {
if (element.type != ElementType.Text && element.type != ElementType.Comment && element.type != ElementType.Directive) {
if (element.name.charAt(0) != "/") { //Ignore closing tags that obviously don't have an opening tag
this.dom.push(element);
- if (!DefaultHandler._emptyTags[element.name]) { //Don't add tags to the tag stack that can't have children
+ if (!this._options.enforceEmptyTags || !DefaultHandler._emptyTags[element.name]) { //Don't add tags to the tag stack that can't have children
this._tagStack.push(element);
}
}
@@ -524,7 +527,7 @@ function DefaultHandler (callback, options) {
//This is a closing tag, scan the tagStack to find the matching opening tag
//and pop the stack up to the opening tag's parent
var baseName = element.name.substring(1);
- if (!DefaultHandler._emptyTags[baseName]) {
+ if (!this._options.enforceEmptyTags || !DefaultHandler._emptyTags[baseName]) {
var pos = this._tagStack.length - 1;
while (pos > -1 && this._tagStack[pos--].name != baseName) { }
if (pos > -1 || this._tagStack[0].name == baseName)
@@ -536,7 +539,7 @@ function DefaultHandler (callback, options) {
if (!this._tagStack.last().children)
this._tagStack.last().children = [];
this._tagStack.last().children.push(element);
- if (!DefaultHandler._emptyTags[element.name]) //Don't add tags to the tag stack that can't have children
+ if (!this._options.enforceEmptyTags || !DefaultHandler._emptyTags[element.name]) //Don't add tags to the tag stack that can't have children
this._tagStack.push(element);
}
}
View
37 node-htmlparser.min.js
@@ -1,21 +1,22 @@
/***********************************************
- Copyright 2010, Chris Winberry <chris@winberry.net>. All rights reserved.
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to
- deal in the Software without restriction, including without limitation the
- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- sell copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
+Copyright 2010, Chris Winberry <chris@winberry.net>. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- IN THE SOFTWARE.
- ***********************************************/
-(function(){function e(a){this.validateHandler(a);this._handler=a;this.reset()}function g(a,d){this.reset();this._options=d?d:{};if(this._options.ignoreWhitespace==undefined)this._options.ignoreWhitespace=false;if(this._options.verbose==undefined)this._options.verbose=true;if(typeof a=="function")this._callback=a}if(!(typeof require=="function"&&typeof exports=="object"&&typeof module=="object"&&typeof __filename=="string"&&typeof __dirname=="string")){if(this.Tautologistics){if(this.Tautologistics.NodeHtmlParser)return}else this.Tautologistics= {};this.Tautologistics.NodeHtmlParser={};exports=this.Tautologistics.NodeHtmlParser}var c={Text:"text",Directive:"directive",Comment:"comment",Script:"script",Style:"style",Tag:"tag"};e._reTrim=/(^\s+|\s+$)/g;e._reTrimComment=/(^\!--|--$)/g;e._reWhitespace=/\s/g;e._reTagName=/^\s*(\/?)\s*([^\s\/]+)/;e._reAttrib=/([^=<>\"\'\s]+)\s*=\s*"([^"]*)"|([^=<>\"\'\s]+)\s*=\s*'([^']*)'|([^=<>\"\'\s]+)\s*=\s*([^'"\s]+)|([^=<>\"\'\s\/]+)/g;e._reTags=/[\<\>]/g;e.prototype.parseComplete=function(a){this.reset(); this.parseChunk(a);this.done()};e.prototype.parseChunk=function(a){this._done&&this.handleError(new Error("Attempted to parse chunk after parsing already done"));this._buffer+=a;this.parseTags()};e.prototype.done=function(){if(!this._done){this._done=true;if(this._buffer.length){var a=this._buffer;this._buffer="";a={raw:a,data:this._parseState==c.Text?a:a.replace(e._reTrim,""),type:this._parseState};if(this._parseState==c.Tag||this._parseState==c.Script||this._parseState==c.Style)a.name=this.parseTagName(a.data); this.parseAttribs(a);this._elements.push(a)}this.writeHandler();this._handler.done()}};e.prototype.reset=function(){this._buffer="";this._done=false;this._elements=[];this._next=this._current=this._elementsCurrent=0;this._parseState=c.Text;this._prevTagSep="";this._tagStack=[];this._handler.reset()};e.prototype._handler=null;e.prototype._buffer=null;e.prototype._done=false;e.prototype._elements=null;e.prototype._elementsCurrent=0;e.prototype._current=0;e.prototype._next=0;e.prototype._parseState= c.Text;e.prototype._prevTagSep="";e.prototype._tagStack=null;e.prototype.parseTagAttribs=function(a){for(var d=a.length,b=0;b<d;){var h=a[b++];if(h.type==c.Tag||h.type==c.Script||h.type==c.style)this.parseAttribs(h)}return a};e.prototype.parseAttribs=function(a){if(!(a.type!=c.Script&&a.type!=c.Style&&a.type!=c.Tag)){var d=a.data.split(e._reWhitespace,1)[0];d=a.data.substring(d.length);if(!(d.length<1)){var b;for(e._reAttrib.lastIndex=0;b=e._reAttrib.exec(d);){if(a.attribs==undefined)a.attribs={}; if(typeof b[1]=="string"&&b[1].length)a.attribs[b[1]]=b[2];else if(typeof b[3]=="string"&&b[3].length)a.attribs[b[3].toString()]=b[4].toString();else if(typeof b[5]=="string"&&b[5].length)a.attribs[b[5]]=b[6];else if(typeof b[7]=="string"&&b[7].length)a.attribs[b[7]]=b[7]}}}};e.prototype.parseTagName=function(a){if(a==null||a=="")return"";a=e._reTagName.exec(a);if(!a)return"";return(a[1]?"/":"")+a[2]};e.prototype.parseTags=function(){for(var a=this._buffer.length-1;e._reTags.test(this._buffer);){this._next= e._reTags.lastIndex-1;var d=this._buffer.charAt(this._next),b=this._buffer.substring(this._current,this._next);b={raw:b,data:this._parseState==c.Text?b:b.replace(e._reTrim,""),type:this._parseState};var h=this.parseTagName(b.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==c.Script)if(h=="/script")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=c.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==c.Text){var f=this._elements[this._elements.length- 1];f.raw=f.data=f.raw+this._prevTagSep+b.raw;b.raw=b.data=""}}}else if(this._tagStack[this._tagStack.length-1]==c.Style)if(h=="/style")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=c.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==c.Text)if(b.raw!=""){f=this._elements[this._elements.length-1];f.raw=f.data=f.raw+this._prevTagSep+b.raw;b.raw=b.data=""}else f.raw=f.data=f.raw+this._prevTagSep;else if(b.raw!="")b.raw=b.data=b.raw}}else if(this._tagStack[this._tagStack.length- 1]==c.Comment){var i=b.raw.length;if(b.raw.charAt(i-2)=="-"&&b.raw.charAt(i-1)=="-"&&d==">"){this._tagStack.pop();if(this._elements.length&&this._elements[this._elements.length-1].type==c.Comment){f=this._elements[this._elements.length-1];f.raw=f.data=(f.raw+b.raw).replace(e._reTrimComment,"");b.raw=b.data="";b.type=c.Text}else b.type=c.Comment}else{b.type=c.Comment;if(this._elements.length&&this._elements[this._elements.length-1].type==c.Comment){f=this._elements[this._elements.length-1];f.raw=f.data= f.raw+b.raw+d;b.raw=b.data="";b.type=c.Text}else b.raw=b.data=b.raw+d}}if(b.type==c.Tag){b.name=h;if(b.raw.indexOf("!--")==0){b.type=c.Comment;delete b.name;i=b.raw.length;if(b.raw.charAt(i-1)=="-"&&b.raw.charAt(i-2)=="-"&&d==">")b.raw=b.data=b.raw.replace(e._reTrimComment,"");else{b.raw+=d;this._tagStack.push(c.Comment)}}else if(b.raw.indexOf("!")==0)b.type=c.Directive;else if(b.name=="script"){b.type=c.Script;b.data.charAt(b.data.length-1)!="/"&&this._tagStack.push(c.Script)}else if(b.name=="/script")b.type= c.Script;else if(b.name=="style"){b.type=c.Style;b.data.charAt(b.data.length-1)!="/"&&this._tagStack.push(c.Style)}else if(b.name=="/style")b.type=c.Style;if(b.name&&b.name.charAt(0)=="/")b.data=b.name}if(b.raw!=""||b.type!=c.Text){this.parseAttribs(b);this._elements.push(b);b.type!=c.Text&&b.type!=c.Comment&&b.type!=c.Directive&&b.data.charAt(b.data.length-1)=="/"&&this._elements.push({raw:"/"+b.name,data:"/"+b.name,name:"/"+b.name,type:b.type})}this._parseState=d=="<"?c.Tag:c.Text;this._current= this._next+1;this._prevTagSep=d}this._buffer=this._current<=a?this._buffer.substring(this._current):"";this._current=0;this.writeHandler()};e.prototype.validateHandler=function(a){if(typeof a!="object")throw new Error("Handler is not an object");if(typeof a.reset!="function")throw new Error("Handler method 'reset' is invalid");if(typeof a.done!="function")throw new Error("Handler method 'done' is invalid");if(typeof a.writeTag!="function")throw new Error("Handler method 'writeTag' is invalid");if(typeof a.writeText!= "function")throw new Error("Handler method 'writeText' is invalid");if(typeof a.writeComment!="function")throw new Error("Handler method 'writeComment' is invalid");if(typeof a.writeDirective!="function")throw new Error("Handler method 'writeDirective' is invalid");};e.prototype.writeHandler=function(a){if(!(this._tagStack.length&&!!!a))for(;this._elements.length;){a=this._elements.shift();switch(a.type){case c.Comment:this._handler.writeComment(a);break;case c.Directive:this._handler.writeDirective(a); break;case c.Text:this._handler.writeText(a);break;default:this._handler.writeTag(a);break}}};e.prototype.handleError=function(a){if(typeof this._handler.error=="function")this._handler.error(a);else throw a;};g._emptyTags={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};g.reWhitespace=/^\s*$/;g.prototype.dom=null;g.prototype.reset=function(){this.dom=[];this._done=false;this._tagStack=[];this._tagStack.last=function(){return this.length?this[this.length- 1]:null}};g.prototype.done=function(){this._done=true;this.handleCallback(null)};g.prototype.writeTag=function(a){this.handleElement(a)};g.prototype.writeText=function(a){if(this._options.ignoreWhitespace)if(g.reWhitespace.test(a.data))return;this.handleElement(a)};g.prototype.writeComment=function(a){this.handleElement(a)};g.prototype.writeDirective=function(a){this.handleElement(a)};g.prototype.error=function(a){this.handleCallback(a)};g.prototype._options=null;g.prototype._callback=null;g.prototype._done= false;g.prototype._tagStack=null;g.prototype.handleCallback=function(a){if(typeof this._callback!="function")if(a)throw a;else return;this._callback(a,this.dom)};g.prototype.handleElement=function(a){this._done&&this.handleCallback(new Error("Writing to the handler after done() called is not allowed without a reset()"));if(!this._options.verbose){delete a.raw;if(a.type=="tag"||a.type=="script"||a.type=="style")delete a.data}if(this._tagStack.last())if(a.type!=c.Text&&a.type!=c.Comment&&a.type!=c.Directive)if(a.name.charAt(0)== "/"){a=a.name.substring(1);if(!g._emptyTags[a]){for(var d=this._tagStack.length-1;d>-1&&this._tagStack[d--].name!=a;);if(d>-1||this._tagStack[0].name==a)for(;d<this._tagStack.length-1;)this._tagStack.pop()}}else{if(!this._tagStack.last().children)this._tagStack.last().children=[];this._tagStack.last().children.push(a);g._emptyTags[a.name]||this._tagStack.push(a)}else{if(!this._tagStack.last().children)this._tagStack.last().children=[];this._tagStack.last().children.push(a)}else if(a.type!=c.Text&& a.type!=c.Comment&&a.type!=c.Directive){if(a.name.charAt(0)!="/"){this.dom.push(a);g._emptyTags[a.name]||this._tagStack.push(a)}}else this.dom.push(a)};var j={testElement:function(a,d){if(!d)return false;for(var b in a)if(b=="tag_name"){if(d.type!="tag"&&d.type!="script"&&d.type!="style")return false;return a.tag_name(d.name)}else if(b=="tag_type")return a.tag_type(d.type);else if(b=="tag_contains"){if(d.type!="text"&&d.type!="comment"&&d.type!="directive")return false;return a.tag_contains(d.data)}else return d.attribs&& a[b](d.attribs[b]);return true},getElements:function(a,d){function b(l){return typeof a[i]=="function"?l:function(m){return m==l}}if(!d)return[];var h=[],f;for(var i in a)a[i]=b(a[i]);j.testElement(a,d)&&h.push(d);if(d.children)f=d.children;else if(d instanceof Array)f=d;else return h;for(var k=0;k<f.length;k++)h=h.concat(j.getElements(a,f[k]));return h},getElementById:function(a,d){var b=j.getElements({id:a},d);return b.length?b[0]:null},getElementsByTagName:function(a,d){return j.getElements({tag_name:a}, d)},getElementsByTagType:function(a,d){return j.getElements({tag_type:a},d)}};exports.Parser=e;exports.DefaultHandler=g;exports.ElementType=c;exports.DomUtils=j})();
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+***********************************************/
+/* v1.5.0 */
+(function(){function e(a){this.validateHandler(a);this._handler=a;this.reset()}function g(a,d){this.reset();this._options=d?d:{};if(this._options.ignoreWhitespace==undefined)this._options.ignoreWhitespace=false;if(this._options.verbose==undefined)this._options.verbose=true;if(this._options.enforceEmptyTags==undefined)this._options.enforceEmptyTags=true;if(typeof a=="function")this._callback=a}if(!(typeof require=="function"&&typeof exports=="object"&&typeof module=="object"&&typeof __filename=="string"&& typeof __dirname=="string")){if(this.Tautologistics){if(this.Tautologistics.NodeHtmlParser)return}else this.Tautologistics={};this.Tautologistics.NodeHtmlParser={};exports=this.Tautologistics.NodeHtmlParser}var c={Text:"text",Directive:"directive",Comment:"comment",Script:"script",Style:"style",Tag:"tag"};e._reTrim=/(^\s+|\s+$)/g;e._reTrimComment=/(^\!--|--$)/g;e._reWhitespace=/\s/g;e._reTagName=/^\s*(\/?)\s*([^\s\/]+)/;e._reAttrib=/([^=<>\"\'\s]+)\s*=\s*"([^"]*)"|([^=<>\"\'\s]+)\s*=\s*'([^']*)'|([^=<>\"\'\s]+)\s*=\s*([^'"\s]+)|([^=<>\"\'\s\/]+)/g; e._reTags=/[\<\>]/g;e.prototype.parseComplete=function(a){this.reset();this.parseChunk(a);this.done()};e.prototype.parseChunk=function(a){this._done&&this.handleError(new Error("Attempted to parse chunk after parsing already done"));this._buffer+=a;this.parseTags()};e.prototype.done=function(){if(!this._done){this._done=true;if(this._buffer.length){var a=this._buffer;this._buffer="";a={raw:a,data:this._parseState==c.Text?a:a.replace(e._reTrim,""),type:this._parseState};if(this._parseState==c.Tag|| this._parseState==c.Script||this._parseState==c.Style)a.name=this.parseTagName(a.data);this.parseAttribs(a);this._elements.push(a)}this.writeHandler();this._handler.done()}};e.prototype.reset=function(){this._buffer="";this._done=false;this._elements=[];this._next=this._current=this._elementsCurrent=0;this._parseState=c.Text;this._prevTagSep="";this._tagStack=[];this._handler.reset()};e.prototype._handler=null;e.prototype._buffer=null;e.prototype._done=false;e.prototype._elements=null;e.prototype._elementsCurrent= 0;e.prototype._current=0;e.prototype._next=0;e.prototype._parseState=c.Text;e.prototype._prevTagSep="";e.prototype._tagStack=null;e.prototype.parseTagAttribs=function(a){for(var d=a.length,b=0;b<d;){var h=a[b++];if(h.type==c.Tag||h.type==c.Script||h.type==c.style)this.parseAttribs(h)}return a};e.prototype.parseAttribs=function(a){if(!(a.type!=c.Script&&a.type!=c.Style&&a.type!=c.Tag)){var d=a.data.split(e._reWhitespace,1)[0];d=a.data.substring(d.length);if(!(d.length<1)){var b;for(e._reAttrib.lastIndex= 0;b=e._reAttrib.exec(d);){if(a.attribs==undefined)a.attribs={};if(typeof b[1]=="string"&&b[1].length)a.attribs[b[1]]=b[2];else if(typeof b[3]=="string"&&b[3].length)a.attribs[b[3].toString()]=b[4].toString();else if(typeof b[5]=="string"&&b[5].length)a.attribs[b[5]]=b[6];else if(typeof b[7]=="string"&&b[7].length)a.attribs[b[7]]=b[7]}}}};e.prototype.parseTagName=function(a){if(a==null||a=="")return"";a=e._reTagName.exec(a);if(!a)return"";return(a[1]?"/":"")+a[2]};e.prototype.parseTags=function(){for(var a= this._buffer.length-1;e._reTags.test(this._buffer);){this._next=e._reTags.lastIndex-1;var d=this._buffer.charAt(this._next),b=this._buffer.substring(this._current,this._next);b={raw:b,data:this._parseState==c.Text?b:b.replace(e._reTrim,""),type:this._parseState};var h=this.parseTagName(b.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==c.Script)if(h=="/script")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=c.Text;if(this._elements.length&&this._elements[this._elements.length- 1].type==c.Text){var f=this._elements[this._elements.length-1];f.raw=f.data=f.raw+this._prevTagSep+b.raw;b.raw=b.data=""}}}else if(this._tagStack[this._tagStack.length-1]==c.Style)if(h=="/style")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=c.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==c.Text)if(b.raw!=""){f=this._elements[this._elements.length-1];f.raw=f.data=f.raw+this._prevTagSep+b.raw;b.raw=b.data=""}else f.raw=f.data=f.raw+this._prevTagSep;else if(b.raw!= "")b.raw=b.data=b.raw}}else if(this._tagStack[this._tagStack.length-1]==c.Comment){var i=b.raw.length;if(b.raw.charAt(i-2)=="-"&&b.raw.charAt(i-1)=="-"&&d==">"){this._tagStack.pop();if(this._elements.length&&this._elements[this._elements.length-1].type==c.Comment){f=this._elements[this._elements.length-1];f.raw=f.data=(f.raw+b.raw).replace(e._reTrimComment,"");b.raw=b.data="";b.type=c.Text}else b.type=c.Comment}else{b.type=c.Comment;if(this._elements.length&&this._elements[this._elements.length-1].type== c.Comment){f=this._elements[this._elements.length-1];f.raw=f.data=f.raw+b.raw+d;b.raw=b.data="";b.type=c.Text}else b.raw=b.data=b.raw+d}}if(b.type==c.Tag){b.name=h;if(b.raw.indexOf("!--")==0){b.type=c.Comment;delete b.name;i=b.raw.length;if(b.raw.charAt(i-1)=="-"&&b.raw.charAt(i-2)=="-"&&d==">")b.raw=b.data=b.raw.replace(e._reTrimComment,"");else{b.raw+=d;this._tagStack.push(c.Comment)}}else if(b.raw.indexOf("!")==0)b.type=c.Directive;else if(b.name=="script"){b.type=c.Script;b.data.charAt(b.data.length- 1)!="/"&&this._tagStack.push(c.Script)}else if(b.name=="/script")b.type=c.Script;else if(b.name=="style"){b.type=c.Style;b.data.charAt(b.data.length-1)!="/"&&this._tagStack.push(c.Style)}else if(b.name=="/style")b.type=c.Style;if(b.name&&b.name.charAt(0)=="/")b.data=b.name}if(b.raw!=""||b.type!=c.Text){this.parseAttribs(b);this._elements.push(b);b.type!=c.Text&&b.type!=c.Comment&&b.type!=c.Directive&&b.data.charAt(b.data.length-1)=="/"&&this._elements.push({raw:"/"+b.name,data:"/"+b.name,name:"/"+ b.name,type:b.type})}this._parseState=d=="<"?c.Tag:c.Text;this._current=this._next+1;this._prevTagSep=d}this._buffer=this._current<=a?this._buffer.substring(this._current):"";this._current=0;this.writeHandler()};e.prototype.validateHandler=function(a){if(typeof a!="object")throw new Error("Handler is not an object");if(typeof a.reset!="function")throw new Error("Handler method 'reset' is invalid");if(typeof a.done!="function")throw new Error("Handler method 'done' is invalid");if(typeof a.writeTag!= "function")throw new Error("Handler method 'writeTag' is invalid");if(typeof a.writeText!="function")throw new Error("Handler method 'writeText' is invalid");if(typeof a.writeComment!="function")throw new Error("Handler method 'writeComment' is invalid");if(typeof a.writeDirective!="function")throw new Error("Handler method 'writeDirective' is invalid");};e.prototype.writeHandler=function(a){if(!(this._tagStack.length&&!!!a))for(;this._elements.length;){a=this._elements.shift();switch(a.type){case c.Comment:this._handler.writeComment(a); break;case c.Directive:this._handler.writeDirective(a);break;case c.Text:this._handler.writeText(a);break;default:this._handler.writeTag(a);break}}};e.prototype.handleError=function(a){if(typeof this._handler.error=="function")this._handler.error(a);else throw a;};g._emptyTags={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};g.reWhitespace=/^\s*$/;g.prototype.dom=null;g.prototype.reset=function(){this.dom=[];this._done=false;this._tagStack=[]; this._tagStack.last=function(){return this.length?this[this.length-1]:null}};g.prototype.done=function(){this._done=true;this.handleCallback(null)};g.prototype.writeTag=function(a){this.handleElement(a)};g.prototype.writeText=function(a){if(this._options.ignoreWhitespace)if(g.reWhitespace.test(a.data))return;this.handleElement(a)};g.prototype.writeComment=function(a){this.handleElement(a)};g.prototype.writeDirective=function(a){this.handleElement(a)};g.prototype.error=function(a){this.handleCallback(a)}; g.prototype._options=null;g.prototype._callback=null;g.prototype._done=false;g.prototype._tagStack=null;g.prototype.handleCallback=function(a){if(typeof this._callback!="function")if(a)throw a;else return;this._callback(a,this.dom)};g.prototype.handleElement=function(a){this._done&&this.handleCallback(new Error("Writing to the handler after done() called is not allowed without a reset()"));if(!this._options.verbose){delete a.raw;if(a.type=="tag"||a.type=="script"||a.type=="style")delete a.data}if(this._tagStack.last())if(a.type!= c.Text&&a.type!=c.Comment&&a.type!=c.Directive)if(a.name.charAt(0)=="/"){a=a.name.substring(1);if(!this._options.enforceEmptyTags||!g._emptyTags[a]){for(var d=this._tagStack.length-1;d>-1&&this._tagStack[d--].name!=a;);if(d>-1||this._tagStack[0].name==a)for(;d<this._tagStack.length-1;)this._tagStack.pop()}}else{if(!this._tagStack.last().children)this._tagStack.last().children=[];this._tagStack.last().children.push(a);if(!this._options.enforceEmptyTags||!g._emptyTags[a.name])this._tagStack.push(a)}else{if(!this._tagStack.last().children)this._tagStack.last().children= [];this._tagStack.last().children.push(a)}else if(a.type!=c.Text&&a.type!=c.Comment&&a.type!=c.Directive){if(a.name.charAt(0)!="/"){this.dom.push(a);if(!this._options.enforceEmptyTags||!g._emptyTags[a.name])this._tagStack.push(a)}}else this.dom.push(a)};var j={testElement:function(a,d){if(!d)return false;for(var b in a)if(b=="tag_name"){if(d.type!="tag"&&d.type!="script"&&d.type!="style")return false;return a.tag_name(d.name)}else if(b=="tag_type")return a.tag_type(d.type);else if(b=="tag_contains"){if(d.type!= "text"&&d.type!="comment"&&d.type!="directive")return false;return a.tag_contains(d.data)}else return d.attribs&&a[b](d.attribs[b]);return true},getElements:function(a,d){function b(l){return typeof a[i]=="function"?l:function(m){return m==l}}if(!d)return[];var h=[],f;for(var i in a)a[i]=b(a[i]);j.testElement(a,d)&&h.push(d);if(d.children)f=d.children;else if(d instanceof Array)f=d;else return h;for(var k=0;k<f.length;k++)h=h.concat(j.getElements(a,f[k]));return h},getElementById:function(a,d){var b= j.getElements({id:a},d);return b.length?b[0]:null},getElementsByTagName:function(a,d){return j.getElements({tag_name:a},d)},getElementsByTagType:function(a,d){return j.getElements({tag_type:a},d)}};exports.Parser=e;exports.DefaultHandler=g;exports.ElementType=c;exports.DomUtils=j})();
View
2  runtests.min.html
@@ -39,6 +39,8 @@
<script language="JavaScript" src="tests/15-non-verbose.js"></script>
<script language="JavaScript" src="tests/16-ignore_whitespace.js"></script>
<script language="JavaScript" src="tests/17-xml_namespace.js"></script>
+ <script language="JavaScript" src="tests/18-enforce_empty_tags.js"></script>
+ <script language="JavaScript" src="tests/19-ignore_empty_tags.js"></script>
<!-- //TODO: dynamic loading of test files -->
</head>
<body style="font-size: small; font-family:Arial, Helvetica, sans-serif;">
View
6 snippet.js
@@ -3,13 +3,13 @@
var sys = require("sys");
var htmlparser = require("./node-htmlparser");
-var html = "<a href=\"test.html\">xxx</a>";
+var html = "<link>text</link>";
var handler = new htmlparser.DefaultHandler(function(err, dom) {
if (err)
sys.debug("Error: " + err);
else
sys.debug(sys.inspect(dom, false, null));
-}, { verbose: false });
+}, { enforceEmptyTags: true });
var parser = new htmlparser.Parser(handler);
-parser.ParseComplete(html);
+parser.parseComplete(html);
Please sign in to comment.
Something went wrong with that request. Please try again.