Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds char/byte location information to elements. #52

Open
wants to merge 4 commits into from

3 participants

@rrjamie

This adds a character location in addition to the row and column location information.

and others added some commits
@tautologistics Added .gitignore e01ac67
Ryan J Daw Adds character position to getLocation
Location data now includes a `character` position, which is
the position (in terms of characters) in the stream processed
so far.
fc18383
Ryan J Daw Fixes bug where scripts were in the wrong location.
Note: this fix is sorta hackey. The root cause lies elsewhere.
6436064
@rrjamie

Actually, this is to be merged in V1.x branch, not master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 17, 2012
  1. Added .gitignore

    authored
Commits on Jun 14, 2012
  1. Adds character position to getLocation

    Ryan J Daw authored
    Location data now includes a `character` position, which is
    the position (in terms of characters) in the stream processed
    so far.
Commits on Jun 15, 2012
  1. Fixes bug where scripts were in the wrong location.

    Ryan J Daw authored
    Note: this fix is sorta hackey. The root cause lies elsewhere.
Commits on Jan 22, 2013
  1. @MikeKlemarewski
This page is out of date. Refresh to see the latest.
View
3  .gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+*.swp
+
View
14 lib/htmlparser.js
@@ -116,7 +116,7 @@ function Parser (handler, options) {
this._elements.push(element);
}
- this.writeHandler();
+ this.writeHandler(true);
this._handler.done();
}
@@ -133,6 +133,7 @@ function Parser (handler, options) {
, col: 0
, charOffset: 0
, inBuffer: 0
+ , character: 0
};
this._parseState = ElementType.Text;
this._prevTagSep = '';
@@ -346,7 +347,7 @@ function Parser (handler, options) {
//Add all tags and non-empty text elements to the element list
if (element.raw != "" || element.type != ElementType.Text) {
if (this._options.includeLocation && !element.location) {
- element.location = this.getLocation(element.type == ElementType.Tag);
+ element.location = this.getLocation(element.type == ElementType.Tag, element.type == ElementType.Script);
}
this.parseAttribs(element);
this._elements.push(element);
@@ -384,7 +385,7 @@ function Parser (handler, options) {
this.writeHandler();
}
- Parser.prototype.getLocation = function Parser$getLocation (startTag) {
+ Parser.prototype.getLocation = function Parser$getLocation (startTag, scriptTag) {
var c,
l = this._location,
end = this._current - (startTag ? 1 : 0),
@@ -392,16 +393,21 @@ function Parser (handler, options) {
for (; l.charOffset < end; l.charOffset++) {
c = this._buffer.charAt(l.charOffset);
+ l.character++;
+
if (c == '\n') {
l.inBuffer++;
l.col = 0;
+
} else if (c != '\r') {
l.col++;
}
}
return {
line: l.row + l.inBuffer + 1
- , col: l.col + (chunk ? 0: 1)
+ , col: l.col + (chunk ? 0: 1) - (scriptTag ? 1 : 0)
+ , character: l.character + (chunk ? 0: 1) - (scriptTag ? 1 : 0) - 1
+
};
}
View
23 lib/htmlparser.min.js
@@ -1,22 +1 @@
-/***********************************************
-Copyright 2010, 2011, 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 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.7.6 */
-(function(){function e(a,c){this._options=c?c:{};void 0==this._options.includeLocation&&(this._options.includeLocation=!1);this.validateHandler(a);this._handler=a;this.reset()}function j(a){j.super_.call(this,a,{ignoreWhitespace:!0,verbose:!1,enforceEmptyTags:!1})}function g(a,c){this.reset();this._options=c?c:{};void 0==this._options.ignoreWhitespace&&(this._options.ignoreWhitespace=!1);void 0==this._options.verbose&&(this._options.verbose=!0);void 0==this._options.enforceEmptyTags&&(this._options.enforceEmptyTags= !0);"function"==typeof a&&(this._callback=a)}if(!("function"==typeof require&&"object"==typeof exports&&"object"==typeof module&&"string"==typeof __filename&&"string"==typeof __dirname)){if(this.Tautologistics){if(this.Tautologistics.NodeHtmlParser)return}else this.Tautologistics={};this.Tautologistics.NodeHtmlParser={};exports=this.Tautologistics.NodeHtmlParser}var d={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(Error("Attempted to parse chunk after parsing already done"));this._buffer+=a;this.parseTags()};e.prototype.done=function(){if(!this._done){this._done= !0;if(this._buffer.length){var a=this._buffer;this._buffer="";a={raw:a,data:this._parseState==d.Text?a:a.replace(e._reTrim,""),type:this._parseState};if(this._parseState==d.Tag||this._parseState==d.Script||this._parseState==d.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=!1;this._elements=[];this._next=this._current=this._elementsCurrent=0;this._location={row:0,col:0, charOffset:0,inBuffer:0};this._parseState=d.Text;this._prevTagSep="";this._tagStack=[];this._handler.reset()};e.prototype._options=null;e.prototype._handler=null;e.prototype._buffer=null;e.prototype._done=!1;e.prototype._elements=null;e.prototype._elementsCurrent=0;e.prototype._current=0;e.prototype._next=0;e.prototype._location=null;e.prototype._parseState=d.Text;e.prototype._prevTagSep="";e.prototype._tagStack=null;e.prototype.parseTagAttribs=function(a){for(var c=a.length,b=0;b<c;){var e=a[b++]; (e.type==d.Tag||e.type==d.Script||e.type==d.style)&&this.parseAttribs(e)}return a};e.prototype.parseAttribs=function(a){if(!(a.type!=d.Script&&a.type!=d.Style&&a.type!=d.Tag)){var c=a.data.split(e._reWhitespace,1)[0],c=a.data.substring(c.length);if(!(1>c.length)){var b;for(e._reAttrib.lastIndex=0;b=e._reAttrib.exec(c);)void 0==a.attribs&&(a.attribs={}),"string"==typeof b[1]&&b[1].length?a.attribs[b[1]]=b[2]:"string"==typeof b[3]&&b[3].length?a.attribs[b[3].toString()]=b[4].toString():"string"==typeof b[5]&& b[5].length?a.attribs[b[5]]=b[6]:"string"==typeof b[7]&&b[7].length&&(a.attribs[b[7]]=b[7])}}};e.prototype.parseTagName=function(a){if(null==a||""==a)return"";a=e._reTagName.exec(a);return!a?"":(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 c=this._buffer.charAt(this._next),b=this._buffer.substring(this._current,this._next),b={raw:b,data:this._parseState==d.Text?b:b.replace(e._reTrim,""),type:this._parseState}, f=this.parseTagName(b.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==d.Script)if("/script"==f.toLowerCase())this._tagStack.pop();else{if(0!=b.raw.indexOf("!--")&&(b.type=d.Text,this._elements.length&&this._elements[this._elements.length-1].type==d.Text)){var h=this._elements[this._elements.length-1];h.raw=h.data=h.raw+this._prevTagSep+b.raw;b.raw=b.data=""}}else this._tagStack[this._tagStack.length-1]==d.Style?"/style"==f.toLowerCase()?this._tagStack.pop():0!=b.raw.indexOf("!--")&& (b.type=d.Text,this._elements.length&&this._elements[this._elements.length-1].type==d.Text?(h=this._elements[this._elements.length-1],""!=b.raw?(h.raw=h.data=h.raw+this._prevTagSep+b.raw,b.raw=b.data=""):h.raw=h.data=h.raw+this._prevTagSep):""!=b.raw&&(b.raw=b.data=b.raw)):this._tagStack[this._tagStack.length-1]==d.Comment&&(h=b.raw.length,"-"==b.raw.charAt(h-2)&&"-"==b.raw.charAt(h-1)&&">"==c?(this._tagStack.pop(),this._elements.length&&this._elements[this._elements.length-1].type==d.Comment?(h= this._elements[this._elements.length-1],h.raw=h.data=(h.raw+b.raw).replace(e._reTrimComment,""),b.raw=b.data="",b.type=d.Text):b.type=d.Comment):(b.type=d.Comment,this._elements.length&&this._elements[this._elements.length-1].type==d.Comment?(h=this._elements[this._elements.length-1],h.raw=h.data=h.raw+b.raw+c,b.raw=b.data="",b.type=d.Text):b.raw=b.data=b.raw+c));if(b.type==d.Tag&&(b.name=f,f=f.toLowerCase(),0==b.raw.indexOf("!--")?(b.type=d.Comment,delete b.name,h=b.raw.length,"-"==b.raw.charAt(h- 1)&&"-"==b.raw.charAt(h-2)&&">"==c?b.raw=b.data=b.raw.replace(e._reTrimComment,""):(b.raw+=c,this._tagStack.push(d.Comment))):0==b.raw.indexOf("!")||0==b.raw.indexOf("?")?b.type=d.Directive:"script"==f?(b.type=d.Script,"/"!=b.data.charAt(b.data.length-1)&&this._tagStack.push(d.Script)):"/script"==f?b.type=d.Script:"style"==f?(b.type=d.Style,"/"!=b.data.charAt(b.data.length-1)&&this._tagStack.push(d.Style)):"/style"==f&&(b.type=d.Style),b.name&&"/"==b.name.charAt(0)))b.data=b.name;if(""!=b.raw||b.type!= d.Text)this._options.includeLocation&&!b.location&&(b.location=this.getLocation(b.type==d.Tag)),this.parseAttribs(b),this._elements.push(b),b.type!=d.Text&&b.type!=d.Comment&&b.type!=d.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="<"==c?d.Tag:d.Text;this._current=this._next+1;this._prevTagSep=c}this._options.includeLocation&&(this.getLocation(),this._location.row+=this._location.inBuffer,this._location.inBuffer= 0,this._location.charOffset=0);this._buffer=this._current<=a?this._buffer.substring(this._current):"";this._current=0;this.writeHandler()};e.prototype.getLocation=function(a){for(var c=this._location,b=this._current-(a?1:0),d=a&&0==c.charOffset&&0==this._current;c.charOffset<b;c.charOffset++)a=this._buffer.charAt(c.charOffset),"\n"==a?(c.inBuffer++,c.col=0):"\r"!=a&&c.col++;return{line:c.row+c.inBuffer+1,col:c.col+(d?0:1)}};e.prototype.validateHandler=function(a){if("object"!=typeof a)throw Error("Handler is not an object"); if("function"!=typeof a.reset)throw Error("Handler method 'reset' is invalid");if("function"!=typeof a.done)throw Error("Handler method 'done' is invalid");if("function"!=typeof a.writeTag)throw Error("Handler method 'writeTag' is invalid");if("function"!=typeof a.writeText)throw Error("Handler method 'writeText' is invalid");if("function"!=typeof a.writeComment)throw Error("Handler method 'writeComment' is invalid");if("function"!=typeof a.writeDirective)throw Error("Handler method 'writeDirective' is invalid"); };e.prototype.writeHandler=function(a){if(!this._tagStack.length||a)for(;this._elements.length;)switch(a=this._elements.shift(),a.type){case d.Comment:this._handler.writeComment(a);break;case d.Directive:this._handler.writeDirective(a);break;case d.Text:this._handler.writeText(a);break;default:this._handler.writeTag(a)}};e.prototype.handleError=function(a){if("function"==typeof this._handler.error)this._handler.error(a);else throw a;};(function(a,c){var b=function(){};b.prototype=c.prototype;a.super_= c;a.prototype=new b;a.prototype.constructor=a})(j,g);j.prototype.done=function(){var a={},c,b=f.getElementsByTagName(function(a){return"rss"==a||"feed"==a},this.dom,!1);b.length&&(c=b[0]);if(c){if("rss"==c.name){a.type="rss";c=c.children[0];a.id="";try{a.title=f.getElementsByTagName("title",c.children,!1)[0].children[0].data}catch(d){}try{a.link=f.getElementsByTagName("link",c.children,!1)[0].children[0].data}catch(e){}try{a.description=f.getElementsByTagName("description",c.children,!1)[0].children[0].data}catch(g){}try{a.updated= new Date(f.getElementsByTagName("lastBuildDate",c.children,!1)[0].children[0].data)}catch(i){}try{a.author=f.getElementsByTagName("managingEditor",c.children,!1)[0].children[0].data}catch(l){}a.items=[];f.getElementsByTagName("item",c.children).forEach(function(b){var c={};try{c.id=f.getElementsByTagName("guid",b.children,!1)[0].children[0].data}catch(d){}try{c.title=f.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(e){}try{c.link=f.getElementsByTagName("link",b.children,!1)[0].children[0].data}catch(h){}try{c.description= f.getElementsByTagName("description",b.children,!1)[0].children[0].data}catch(g){}try{c.pubDate=new Date(f.getElementsByTagName("pubDate",b.children,!1)[0].children[0].data)}catch(k){}a.items.push(c)})}else{a.type="atom";try{a.id=f.getElementsByTagName("id",c.children,!1)[0].children[0].data}catch(m){}try{a.title=f.getElementsByTagName("title",c.children,!1)[0].children[0].data}catch(n){}try{a.link=f.getElementsByTagName("link",c.children,!1)[0].attribs.href}catch(o){}try{a.description=f.getElementsByTagName("subtitle", c.children,!1)[0].children[0].data}catch(p){}try{a.updated=new Date(f.getElementsByTagName("updated",c.children,!1)[0].children[0].data)}catch(q){}try{a.author=f.getElementsByTagName("email",c.children,!0)[0].children[0].data}catch(r){}a.items=[];f.getElementsByTagName("entry",c.children).forEach(function(b){var c={};try{c.id=f.getElementsByTagName("id",b.children,!1)[0].children[0].data}catch(d){}try{c.title=f.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(e){}try{c.link=f.getElementsByTagName("link", b.children,!1)[0].attribs.href}catch(h){}try{c.description=f.getElementsByTagName("summary",b.children,!1)[0].children[0].data}catch(g){}try{c.pubDate=new Date(f.getElementsByTagName("updated",b.children,!1)[0].children[0].data)}catch(k){}a.items.push(c)})}this.dom=a}j.super_.prototype.done.call(this)};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=!1;this._tagStack=[];this._tagStack.last=function(){return this.length?this[this.length-1]:null}};g.prototype.done=function(){this._done=!0;this.handleCallback(null)};g.prototype.writeTag=function(a){this.handleElement(a)};g.prototype.writeText=function(a){(!this._options.ignoreWhitespace||!g.reWhitespace.test(a.data))&&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=!1;g.prototype._tagStack=null;g.prototype.handleCallback=function(a){if("function"!=typeof this._callback){if(a)throw a;}else this._callback(a,this.dom)};g.prototype.isEmptyTag=function(a){a=a.name.toLowerCase();"/"==a.charAt(0)&&(a=a.substring(1));return this._options.enforceEmptyTags&&!!g._emptyTags[a]};g.prototype.handleElement=function(a){this._done&&this.handleCallback(Error("Writing to the handler after done() called is not allowed without a reset()")); this._options.verbose||(delete a.raw,("tag"==a.type||"script"==a.type||"style"==a.type)&&delete a.data);if(this._tagStack.last())if(a.type!=d.Text&&a.type!=d.Comment&&a.type!=d.Directive)if("/"==a.name.charAt(0)){var c=a.name.substring(1);if(!this.isEmptyTag(a)){for(a=this._tagStack.length-1;-1<a&&this._tagStack[a--].name!=c;);if(-1<a||this._tagStack[0].name==c)for(;a<this._tagStack.length-1;)this._tagStack.pop()}}else this._tagStack.last().children||(this._tagStack.last().children=[]),this._tagStack.last().children.push(a), this.isEmptyTag(a)||this._tagStack.push(a);else this._tagStack.last().children||(this._tagStack.last().children=[]),this._tagStack.last().children.push(a);else a.type!=d.Text&&a.type!=d.Comment&&a.type!=d.Directive?"/"!=a.name.charAt(0)&&(this.dom.push(a),this.isEmptyTag(a)||this._tagStack.push(a)):this.dom.push(a)};var f={testElement:function(a,c){if(!c)return!1;for(var b in a)if("tag_name"==b){if("tag"!=c.type&&"script"!=c.type&&"style"!=c.type||!a.tag_name(c.name))return!1}else if("tag_type"== b){if(!a.tag_type(c.type))return!1}else if("tag_contains"==b){if("text"!=c.type&&"comment"!=c.type&&"directive"!=c.type||!a.tag_contains(c.data))return!1}else if(!c.attribs||!a[b](c.attribs[b]))return!1;return!0},getElements:function(a,c,b,d){function e(a){return function(b){return b==a}}b=void 0===b||null===b||!!b;d=isNaN(parseInt(d))?-1:parseInt(d);if(!c)return[];var g=[],i;for(i in a)"function"!=typeof a[i]&&(a[i]=e(a[i]));f.testElement(a,c)&&g.push(c);if(0<=d&&g.length>=d)return g;if(b&&c.children)c= c.children;else if(!(c instanceof Array))return g;for(i=0;i<c.length&&!(g=g.concat(f.getElements(a,c[i],b,d)),0<=d&&g.length>=d);i++);return g},getElementById:function(a,c,b){a=f.getElements({id:a},c,b,1);return a.length?a[0]:null},getElementsByTagName:function(a,c,b,d){return f.getElements({tag_name:a},c,b,d)},getElementsByTagType:function(a,c,b,d){return f.getElements({tag_type:a},c,b,d)}};exports.Parser=e;exports.DefaultHandler=g;exports.RssHandler=j;exports.ElementType=d;exports.DomUtils=f})();
+(function(){function x(){return((typeof require)=="function"&&(typeof exports)=="object"&&(typeof module)=="object"&&(typeof __filename)=="string"&&(typeof __dirname)=="string")}if(!x()){if(!this.Tautologistics){this.Tautologistics={}}else{if(this.Tautologistics.NodeHtmlParser){return}}this.Tautologistics.NodeHtmlParser={};exports=this.Tautologistics.NodeHtmlParser}var j={Text:"text",Directive:"directive",Comment:"comment",Script:"script",Style:"style",Tag:"tag"};function a(J,I){this._options=I?I:{};if(this._options.includeLocation==undefined){this._options.includeLocation=false}this.validateHandler(J);this._handler=J;this.reset()}a._reTrim=/(^\s+|\s+$)/g;a._reTrimComment=/(^\!--|--$)/g;a._reWhitespace=/\s/g;a._reTagName=/^\s*(\/?)\s*([^\s\/]+)/;a._reAttrib=/([^=<>\"\'\s]+)\s*=\s*"([^"]*)"|([^=<>\"\'\s]+)\s*=\s*'([^']*)'|([^=<>\"\'\s]+)\s*=\s*([^'"\s]+)|([^=<>\"\'\s\/]+)/g;a._reTags=/[\<\>]/g;a.prototype.parseComplete=function d(I){this.reset();this.parseChunk(I);this.done()};a.prototype.parseChunk=function f(I){if(this._done){this.handleError(new Error("Attempted to parse chunk after parsing already done"))}this._buffer+=I;this.parseTags()};a.prototype.done=function D(){if(this._done){return}this._done=true;if(this._buffer.length){var J=this._buffer;this._buffer="";var I={raw:J,data:(this._parseState==j.Text)?J:J.replace(a._reTrim,""),type:this._parseState};if(this._parseState==j.Tag||this._parseState==j.Script||this._parseState==j.Style){I.name=this.parseTagName(I.data)}this.parseAttribs(I);this._elements.push(I)}this.writeHandler();this._handler.done()};a.prototype.reset=function c(){this._buffer="";this._done=false;this._elements=[];this._elementsCurrent=0;this._current=0;this._next=0;this._location={row:0,col:0,charOffset:0,inBuffer:0,character:0};this._parseState=j.Text;this._prevTagSep="";this._tagStack=[];this._handler.reset()};a.prototype._options=null;a.prototype._handler=null;a.prototype._buffer=null;a.prototype._done=false;a.prototype._elements=null;a.prototype._elementsCurrent=0;a.prototype._current=0;a.prototype._next=0;a.prototype._location=null;a.prototype._parseState=j.Text;a.prototype._prevTagSep="";a.prototype._tagStack=null;a.prototype.parseTagAttribs=function v(L){var J=L.length;var I=0;while(I<J){var K=L[I++];if(K.type==j.Tag||K.type==j.Script||K.type==j.style){this.parseAttribs(K)}}return(L)};a.prototype.parseAttribs=function q(L){if(L.type!=j.Script&&L.type!=j.Style&&L.type!=j.Tag){return}var K=L.data.split(a._reWhitespace,1)[0];var I=L.data.substring(K.length);if(I.length<1){return}var J;a._reAttrib.lastIndex=0;while(J=a._reAttrib.exec(I)){if(L.attribs==undefined){L.attribs={}}if(typeof J[1]=="string"&&J[1].length){L.attribs[J[1]]=J[2]}else{if(typeof J[3]=="string"&&J[3].length){L.attribs[J[3].toString()]=J[4].toString()}else{if(typeof J[5]=="string"&&J[5].length){L.attribs[J[5]]=J[6]}else{if(typeof J[7]=="string"&&J[7].length){L.attribs[J[7]]=J[7]}}}}}};a.prototype.parseTagName=function e(J){if(J==null||J==""){return("")}var I=a._reTagName.exec(J);if(!I){return("")}return((I[1]?"/":"")+I[2])};a.prototype.parseTags=function u(){var N=this._buffer.length-1;while(a._reTags.test(this._buffer)){this._next=a._reTags.lastIndex-1;var L=this._buffer.charAt(this._next);var P=this._buffer.substring(this._current,this._next);var K={raw:P,data:(this._parseState==j.Text)?P:P.replace(a._reTrim,""),type:this._parseState};var I=this.parseTagName(K.data);if(this._tagStack.length){if(this._tagStack[this._tagStack.length-1]==j.Script){if(I.toLowerCase()=="/script"){this._tagStack.pop()}else{if(K.raw.indexOf("!--")!=0){K.type=j.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==j.Text){var J=this._elements[this._elements.length-1];J.raw=J.data=J.raw+this._prevTagSep+K.raw;K.raw=K.data=""}}}}else{if(this._tagStack[this._tagStack.length-1]==j.Style){if(I.toLowerCase()=="/style"){this._tagStack.pop()}else{if(K.raw.indexOf("!--")!=0){K.type=j.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==j.Text){var J=this._elements[this._elements.length-1];if(K.raw!=""){J.raw=J.data=J.raw+this._prevTagSep+K.raw;K.raw=K.data=""}else{J.raw=J.data=J.raw+this._prevTagSep}}else{if(K.raw!=""){K.raw=K.data=K.raw}}}}}else{if(this._tagStack[this._tagStack.length-1]==j.Comment){var O=K.raw.length;if(K.raw.charAt(O-2)=="-"&&K.raw.charAt(O-1)=="-"&&L==">"){this._tagStack.pop();if(this._elements.length&&this._elements[this._elements.length-1].type==j.Comment){var J=this._elements[this._elements.length-1];J.raw=J.data=(J.raw+K.raw).replace(a._reTrimComment,"");K.raw=K.data="";K.type=j.Text}else{K.type=j.Comment}}else{K.type=j.Comment;if(this._elements.length&&this._elements[this._elements.length-1].type==j.Comment){var J=this._elements[this._elements.length-1];J.raw=J.data=J.raw+K.raw+L;K.raw=K.data="";K.type=j.Text}else{K.raw=K.data=K.raw+L}}}}}}if(K.type==j.Tag){K.name=I;var M=I.toLowerCase();if(K.raw.indexOf("!--")==0){K.type=j.Comment;delete K.name;var O=K.raw.length;if(K.raw.charAt(O-1)=="-"&&K.raw.charAt(O-2)=="-"&&L==">"){K.raw=K.data=K.raw.replace(a._reTrimComment,"")}else{K.raw+=L;this._tagStack.push(j.Comment)}}else{if(K.raw.indexOf("!")==0||K.raw.indexOf("?")==0){K.type=j.Directive}else{if(M=="script"){K.type=j.Script;if(K.data.charAt(K.data.length-1)!="/"){this._tagStack.push(j.Script)}}else{if(M=="/script"){K.type=j.Script}else{if(M=="style"){K.type=j.Style;if(K.data.charAt(K.data.length-1)!="/"){this._tagStack.push(j.Style)}}else{if(M=="/style"){K.type=j.Style}}}}}}if(K.name&&K.name.charAt(0)=="/"){K.data=K.name}}if(K.raw!=""||K.type!=j.Text){if(this._options.includeLocation&&!K.location){K.location=this.getLocation(K.type==j.Tag)}this.parseAttribs(K);this._elements.push(K);if(K.type!=j.Text&&K.type!=j.Comment&&K.type!=j.Directive&&K.data.charAt(K.data.length-1)=="/"){this._elements.push({raw:"/"+K.name,data:"/"+K.name,name:"/"+K.name,type:K.type})}}this._parseState=(L=="<")?j.Tag:j.Text;this._current=this._next+1;this._prevTagSep=L}if(this._options.includeLocation){this.getLocation();this._location.row+=this._location.inBuffer;this._location.inBuffer=0;this._location.charOffset=0}this._buffer=(this._current<=N)?this._buffer.substring(this._current):"";this._current=0;this.writeHandler()};a.prototype.getLocation=function k(L){var M,J=this._location,I=this._current-(L?1:0),K=L&&J.charOffset==0&&this._current==0;for(;J.charOffset<I;J.charOffset++){M=this._buffer.charAt(J.charOffset);J.character++;if(M=="\n"){J.inBuffer++;J.col=0}else{if(M!="\r"){J.col++}}}return{line:J.row+J.inBuffer+1,col:J.col+(K?0:1),character:J.character-(K?1:0)}};a.prototype.validateHandler=function g(I){if((typeof I)!="object"){throw new Error("Handler is not an object")}if((typeof I.reset)!="function"){throw new Error("Handler method 'reset' is invalid")}if((typeof I.done)!="function"){throw new Error("Handler method 'done' is invalid")}if((typeof I.writeTag)!="function"){throw new Error("Handler method 'writeTag' is invalid")}if((typeof I.writeText)!="function"){throw new Error("Handler method 'writeText' is invalid")}if((typeof I.writeComment)!="function"){throw new Error("Handler method 'writeComment' is invalid")}if((typeof I.writeDirective)!="function"){throw new Error("Handler method 'writeDirective' is invalid")}};a.prototype.writeHandler=function i(J){J=!!J;if(this._tagStack.length&&!J){return}while(this._elements.length){var I=this._elements.shift();switch(I.type){case j.Comment:this._handler.writeComment(I);break;case j.Directive:this._handler.writeDirective(I);break;case j.Text:this._handler.writeText(I);break;default:this._handler.writeTag(I);break}}};a.prototype.handleError=function y(I){if((typeof this._handler.error)=="function"){this._handler.error(I)}else{throw I}};function l(I){l.super_.call(this,I,{ignoreWhitespace:true,verbose:false,enforceEmptyTags:false})}m(l,h);l.prototype.done=function H(){var K={};var L;var J=z.getElementsByTagName(function(M){return(M=="rss"||M=="feed")},this.dom,false);if(J.length){L=J[0]}if(L){if(L.name=="rss"){K.type="rss";L=L.children[0];K.id="";try{K.title=z.getElementsByTagName("title",L.children,false)[0].children[0].data}catch(I){}try{K.link=z.getElementsByTagName("link",L.children,false)[0].children[0].data}catch(I){}try{K.description=z.getElementsByTagName("description",L.children,false)[0].children[0].data}catch(I){}try{K.updated=new Date(z.getElementsByTagName("lastBuildDate",L.children,false)[0].children[0].data)}catch(I){}try{K.author=z.getElementsByTagName("managingEditor",L.children,false)[0].children[0].data}catch(I){}K.items=[];z.getElementsByTagName("item",L.children).forEach(function(P,M,Q){var O={};try{O.id=z.getElementsByTagName("guid",P.children,false)[0].children[0].data}catch(N){}try{O.title=z.getElementsByTagName("title",P.children,false)[0].children[0].data}catch(N){}try{O.link=z.getElementsByTagName("link",P.children,false)[0].children[0].data}catch(N){}try{O.description=z.getElementsByTagName("description",P.children,false)[0].children[0].data}catch(N){}try{O.pubDate=new Date(z.getElementsByTagName("pubDate",P.children,false)[0].children[0].data)}catch(N){}K.items.push(O)})}else{K.type="atom";try{K.id=z.getElementsByTagName("id",L.children,false)[0].children[0].data}catch(I){}try{K.title=z.getElementsByTagName("title",L.children,false)[0].children[0].data}catch(I){}try{K.link=z.getElementsByTagName("link",L.children,false)[0].attribs.href}catch(I){}try{K.description=z.getElementsByTagName("subtitle",L.children,false)[0].children[0].data}catch(I){}try{K.updated=new Date(z.getElementsByTagName("updated",L.children,false)[0].children[0].data)}catch(I){}try{K.author=z.getElementsByTagName("email",L.children,true)[0].children[0].data}catch(I){}K.items=[];z.getElementsByTagName("entry",L.children).forEach(function(P,M,Q){var O={};try{O.id=z.getElementsByTagName("id",P.children,false)[0].children[0].data}catch(N){}try{O.title=z.getElementsByTagName("title",P.children,false)[0].children[0].data}catch(N){}try{O.link=z.getElementsByTagName("link",P.children,false)[0].attribs.href}catch(N){}try{O.description=z.getElementsByTagName("summary",P.children,false)[0].children[0].data}catch(N){}try{O.pubDate=new Date(z.getElementsByTagName("updated",P.children,false)[0].children[0].data)}catch(N){}K.items.push(O)})}this.dom=K}l.super_.prototype.done.call(this)};function h(J,I){this.reset();this._options=I?I:{};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 J)=="function"){this._callback=J}}h._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};h.reWhitespace=/^\s*$/;h.prototype.dom=null;h.prototype.reset=function E(){this.dom=[];this._done=false;this._tagStack=[];this._tagStack.last=function I(){return(this.length?this[this.length-1]:null)}};h.prototype.done=function t(){this._done=true;this.handleCallback(null)};h.prototype.writeTag=function C(I){this.handleElement(I)};h.prototype.writeText=function G(I){if(this._options.ignoreWhitespace){if(h.reWhitespace.test(I.data)){return}}this.handleElement(I)};h.prototype.writeComment=function s(I){this.handleElement(I)};h.prototype.writeDirective=function B(I){this.handleElement(I)};h.prototype.error=function r(I){this.handleCallback(I)};h.prototype._options=null;h.prototype._callback=null;h.prototype._done=false;h.prototype._tagStack=null;h.prototype.handleCallback=function A(I){if((typeof this._callback)!="function"){if(I){throw I}else{return}}this._callback(I,this.dom)};h.prototype.isEmptyTag=function(J){var I=J.name.toLowerCase();if(I.charAt(0)=="/"){I=I.substring(1)}return this._options.enforceEmptyTags&&!!h._emptyTags[I]};h.prototype.handleElement=function o(I){if(this._done){this.handleCallback(new Error("Writing to the handler after done() called is not allowed without a reset()"))}if(!this._options.verbose){delete I.raw;if(I.type=="tag"||I.type=="script"||I.type=="style"){delete I.data}}if(!this._tagStack.last()){if(I.type!=j.Text&&I.type!=j.Comment&&I.type!=j.Directive){if(I.name.charAt(0)!="/"){this.dom.push(I);if(!this.isEmptyTag(I)){this._tagStack.push(I)}}}else{this.dom.push(I)}}else{if(I.type!=j.Text&&I.type!=j.Comment&&I.type!=j.Directive){if(I.name.charAt(0)=="/"){var J=I.name.substring(1);if(!this.isEmptyTag(I)){var K=this._tagStack.length-1;while(K>-1&&this._tagStack[K--].name!=J){}if(K>-1||this._tagStack[0].name==J){while(K<this._tagStack.length-1){this._tagStack.pop()}}}}else{if(!this._tagStack.last().children){this._tagStack.last().children=[]}this._tagStack.last().children.push(I);if(!this.isEmptyTag(I)){this._tagStack.push(I)}}}else{if(!this._tagStack.last().children){this._tagStack.last().children=[]}this._tagStack.last().children.push(I)}}};var z={testElement:function n(I,K){if(!K){return false}for(var J in I){if(J=="tag_name"){if(K.type!="tag"&&K.type!="script"&&K.type!="style"){return false}if(!I.tag_name(K.name)){return false}}else{if(J=="tag_type"){if(!I.tag_type(K.type)){return false}}else{if(J=="tag_contains"){if(K.type!="text"&&K.type!="comment"&&K.type!="directive"){return false}if(!I.tag_contains(K.data)){return false}}else{if(!K.attribs||!I[J](K.attribs[J])){return false}}}}}return true},getElements:function F(Q,M,I,J){I=(I===undefined||I===null)||!!I;J=isNaN(parseInt(J))?-1:parseInt(J);if(!M){return([])}var P=[];var L;function N(R){return(function(S){return(S==R)})}for(var O in Q){if((typeof Q[O])!="function"){Q[O]=N(Q[O])}}if(z.testElement(Q,M)){P.push(M)}if(J>=0&&P.length>=J){return(P)}if(I&&M.children){L=M.children}else{if(M instanceof Array){L=M}else{return(P)}}for(var K=0;K<L.length;K++){P=P.concat(z.getElements(Q,L[K],I,J));if(J>=0&&P.length>=J){break}}return(P)},getElementById:function b(L,J,K){var I=z.getElements({id:L},J,K,1);return(I.length?I[0]:null)},getElementsByTagName:function p(K,J,L,I){return(z.getElements({tag_name:K},J,L,I))},getElementsByTagType:function w(K,J,L,I){return(z.getElements({tag_type:K},J,L,I))}};function m(J,I){var K=function(){};K.prototype=I.prototype;J.super_=I;J.prototype=new K();J.prototype.constructor=J}exports.Parser=a;exports.DefaultHandler=h;exports.RssHandler=l;exports.ElementType=j;exports.DomUtils=z})();
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "htmlparser"
, "description": "Forgiving HTML/XML/RSS Parser in JS for *both* Node and Browsers"
- , "version": "1.7.6"
+ , "version": "1.7.7"
, "author": "Chris Winberry <chris@winberry.net>"
, "contributors": []
, "repository": {
View
7 runtests.js
@@ -30,10 +30,15 @@ var testFiles = fs.readdirSync(testFolder);
var testCount = 0;
var failedCount = 0;
for (var i in testFiles) {
- testCount++;
var fileParts = testFiles[i].split(".");
fileParts.pop();
var moduleName = fileParts.join(".");
+
+ if (moduleName.charAt(0) === '.') {
+ continue;
+ }
+ testCount++;
+
var test = require(testFolder + "/" + moduleName);
var handlerCallback = function handlerCallback (error) {
if (error)
View
62 tests/22-position_data.js
@@ -30,7 +30,7 @@ exports.options = {
handler: {}
, parser: { includeLocation: true }
};
-exports.html = "<html>\r\n\n\t<title>The Title</title><body>\nHello world\r\n\n</body>\n\n</html>";
+exports.html = "<html>\r\n\n\t<title>The Title</title><body>\nHello world\r\n\n</body>\n\n<script src=\"test.js\"></script><script>var test = true;</script></html>";
exports.expected = [
{
raw: 'html',
@@ -39,7 +39,8 @@ exports.expected = [
name: 'html',
location: {
line: 1,
- col: 1
+ col: 1,
+ character: 0
},
children: [{
raw: '\r\n\n\t',
@@ -47,7 +48,8 @@ exports.expected = [
type: 'text',
location: {
line: 1,
- col: 7
+ col: 7,
+ character: 6
}
}, {
raw: 'title',
@@ -56,7 +58,8 @@ exports.expected = [
name: 'title',
location: {
line: 3,
- col: 2
+ col: 2,
+ character: 10,
},
children: [{
raw: 'The Title',
@@ -64,7 +67,8 @@ exports.expected = [
type: 'text',
location: {
line: 3,
- col: 9
+ col: 9,
+ character: 17
}
}]
}, {
@@ -74,7 +78,8 @@ exports.expected = [
name: 'body',
location: {
line: 3,
- col: 26
+ col: 26,
+ character: 34,
},
children: [{
raw: '\nHello world\r\n\n',
@@ -82,7 +87,8 @@ exports.expected = [
type: 'text',
location: {
line: 3,
- col: 32
+ col: 32,
+ character: 40,
}
}]
}, {
@@ -91,10 +97,44 @@ exports.expected = [
type: 'text',
location: {
line: 6,
- col: 8
+ col: 8,
+ character: 62
}
+ }, {
+ raw: 'script src="test.js"',
+ data: 'script src="test.js"',
+ type: 'script',
+ name: 'script',
+ location: {
+ line: 8,
+ col: 1,
+ character: 64
+ },
+ attribs: {
+ src: 'test.js'
+ }
+ }, {
+ raw: 'script',
+ data: 'script',
+ type: 'script',
+ name: 'script',
+ location: {
+ line: 8,
+ col: 32,
+ character: 95
+ },
+ children: [
+ {
+ raw: 'var test = true;',
+ data: 'var test = true;',
+ type: 'text',
+ location: {
+ line: 8,
+ col: 40,
+ character: 103
+ }
+ }
+ ]
}]
- }
- ];
-
+ }];
})();
Something went wrong with that request. Please try again.