Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

peek() method #16

Open
wants to merge 1 commit into from

2 participants

@jtwb

peek() is required for streaming parsers.

In browsers, the live DOM (document.*) must be updated with new output from the HTML parser in these situations (and possibly others):
1 Done reading HTML from a single packet
2 script tag found
3 document.write() call completed

So we need a way to query the parser's DOM state without calling done(), which prevents any further parsing.

This gist shows how peek() would be used in a simple case:
https://gist.github.com/849639/ec57b97213acb92b8a20e377d06cf1cffaf01e99

This is a more complex use case where we sync the DOM whenever a script tag appears:
https://gist.github.com/849639/dd05bcccefe82c0cc01d10c0ec54ce3f31bda4b8

@tautologistics

Thanks for the contribution! Looking through it now and will get back to you with questions or just accept the pull.

@jtwb

Sweet! By the way, this is a little unclear from those gist examples: calling peek() will trigger the handler callback function with the partial DOM, so seeing that callback no longer implies that parsing is totally finished.

@tautologistics

FYI - Working on 1.8 now and working through pull requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2011
  1. Now supports peek() method, which calls the handler callback without …

    Jason Benterou authored
    …terminating the parse.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 3 deletions.
  1. +8 −0 lib/htmlparser.js
  2. +1 −3 lib/htmlparser.min.js
View
8 lib/htmlparser.js
@@ -120,6 +120,11 @@ function Parser (handler, options) {
this._handler.done();
}
+ //Send DOM state to handlers without terminating parsing
+ Parser.prototype.peek = function Parser$peek () {
+ this._handler.peek();
+ }
+
//Resets the parser to a blank state, ready to parse a new HTML document
Parser.prototype.reset = function Parser$reset () {
this._buffer = "";
@@ -608,6 +613,9 @@ function DefaultHandler (callback, options) {
this._done = true;
this.handleCallback(null);
}
+ DefaultHandler.prototype.peek = function DefaultHandler$peek () {
+ this.handleCallback(null);
+ }
DefaultHandler.prototype.writeTag = function DefaultHandler$writeTag (element) {
this.handleElement(element);
}
View
4 lib/htmlparser.min.js
@@ -17,6 +17,4 @@ 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.2 */
-(function(){function e(a,c){this._options=c?c:{};if(this._options.includeLocation==undefined)this._options.includeLocation=false;this.validateHandler(a);this._handler=a;this.reset()}function n(a){n.super_.call(this,a,{ignoreWhitespace:true,verbose:false,enforceEmptyTags:false})}function i(a,c){this.reset();this._options=c?c:{};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 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=true;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=false;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=false;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 h=a[b++];if(h.type==d.Tag||h.type==d.Script||h.type==d.style)this.parseAttribs(h)}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(!(c.length<1)){var b;for(e._reAttrib.lastIndex=0;b=e._reAttrib.exec(c);){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 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};var h=this.parseTagName(b.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==d.Script)if(h=="/script")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=d.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==d.Text){var g=this._elements[this._elements.length-1];g.raw=g.data=g.raw+this._prevTagSep+b.raw;b.raw=b.data=""}}}else if(this._tagStack[this._tagStack.length- 1]==d.Style)if(h=="/style")this._tagStack.pop();else{if(b.raw.indexOf("!--")!=0){b.type=d.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==d.Text){g=this._elements[this._elements.length-1];if(b.raw!=""){g.raw=g.data=g.raw+this._prevTagSep+b.raw;b.raw=b.data=""}else g.raw=g.data=g.raw+this._prevTagSep}else if(b.raw!="")b.raw=b.data=b.raw}}else if(this._tagStack[this._tagStack.length-1]==d.Comment){g=b.raw.length;if(b.raw.charAt(g-2)=="-"&&b.raw.charAt(g-1)=="-"&&c==">"){this._tagStack.pop(); if(this._elements.length&&this._elements[this._elements.length-1].type==d.Comment){g=this._elements[this._elements.length-1];g.raw=g.data=(g.raw+b.raw).replace(e._reTrimComment,"");b.raw=b.data="";b.type=d.Text}else b.type=d.Comment}else{b.type=d.Comment;if(this._elements.length&&this._elements[this._elements.length-1].type==d.Comment){g=this._elements[this._elements.length-1];g.raw=g.data=g.raw+b.raw+c;b.raw=b.data="";b.type=d.Text}else b.raw=b.data=b.raw+c}}if(b.type==d.Tag){b.name=h;if(b.raw.indexOf("!--")== 0){b.type=d.Comment;delete b.name;g=b.raw.length;if(b.raw.charAt(g-1)=="-"&&b.raw.charAt(g-2)=="-"&&c==">")b.raw=b.data=b.raw.replace(e._reTrimComment,"");else{b.raw+=c;this._tagStack.push(d.Comment)}}else if(b.raw.indexOf("!")==0||b.raw.indexOf("?")==0)b.type=d.Directive;else if(b.name=="script"){b.type=d.Script;b.data.charAt(b.data.length-1)!="/"&&this._tagStack.push(d.Script)}else if(b.name=="/script")b.type=d.Script;else if(b.name=="style"){b.type=d.Style;b.data.charAt(b.data.length-1)!="/"&& this._tagStack.push(d.Style)}else if(b.name=="/style")b.type=d.Style;if(b.name&&b.name.charAt(0)=="/")b.data=b.name}if(b.raw!=""||b.type!=d.Text){if(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}if(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),h=a&&c.charOffset==0&&this._current==0;c.charOffset<b;c.charOffset++){a=this._buffer.charAt(c.charOffset);if(a=="\n"){c.inBuffer++; c.col=0}else a!="\r"&&c.col++}return{line:c.row+c.inBuffer+1,col:c.col+(h?0:1)}};e.prototype.validateHandler=function(a){if(typeof a!="object")throw Error("Handler is not an object");if(typeof a.reset!="function")throw Error("Handler method 'reset' is invalid");if(typeof a.done!="function")throw Error("Handler method 'done' is invalid");if(typeof a.writeTag!="function")throw Error("Handler method 'writeTag' is invalid");if(typeof a.writeText!="function")throw Error("Handler method 'writeText' is invalid"); if(typeof a.writeComment!="function")throw Error("Handler method 'writeComment' is invalid");if(typeof a.writeDirective!="function")throw Error("Handler method 'writeDirective' is invalid");};e.prototype.writeHandler=function(a){a=!!a;if(!(this._tagStack.length&&!a))for(;this._elements.length;){a=this._elements.shift();switch(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(typeof this._handler.error=="function")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})(n,i);n.prototype.done=function(){var a={},c,b=f.getElementsByTagName(function(j){return j=="rss"||j=="feed"},this.dom,false);if(b.length)c=b[0];if(c){if(c.name=="rss"){a.type="rss";c=c.children[0];a.id="";try{a.title=f.getElementsByTagName("title",c.children,false)[0].children[0].data}catch(h){}try{a.link= f.getElementsByTagName("link",c.children,false)[0].children[0].data}catch(g){}try{a.description=f.getElementsByTagName("description",c.children,false)[0].children[0].data}catch(l){}try{a.updated=new Date(f.getElementsByTagName("lastBuildDate",c.children,false)[0].children[0].data)}catch(m){}try{a.author=f.getElementsByTagName("managingEditor",c.children,false)[0].children[0].data}catch(o){}a.items=[];f.getElementsByTagName("item",c.children).forEach(function(j){var k={};try{k.id=f.getElementsByTagName("guid", j.children,false)[0].children[0].data}catch(q){}try{k.title=f.getElementsByTagName("title",j.children,false)[0].children[0].data}catch(r){}try{k.link=f.getElementsByTagName("link",j.children,false)[0].children[0].data}catch(s){}try{k.description=f.getElementsByTagName("description",j.children,false)[0].children[0].data}catch(t){}try{k.pubDate=new Date(f.getElementsByTagName("pubDate",j.children,false)[0].children[0].data)}catch(u){}a.items.push(k)})}else{a.type="atom";try{a.id=f.getElementsByTagName("id", c.children,false)[0].children[0].data}catch(p){}try{a.title=f.getElementsByTagName("title",c.children,false)[0].children[0].data}catch(v){}try{a.link=f.getElementsByTagName("link",c.children,false)[0].attribs.href}catch(w){}try{a.description=f.getElementsByTagName("subtitle",c.children,false)[0].children[0].data}catch(x){}try{a.updated=new Date(f.getElementsByTagName("updated",c.children,false)[0].children[0].data)}catch(y){}try{a.author=f.getElementsByTagName("email",c.children,true)[0].children[0].data}catch(z){}a.items= [];f.getElementsByTagName("entry",c.children).forEach(function(j){var k={};try{k.id=f.getElementsByTagName("id",j.children,false)[0].children[0].data}catch(q){}try{k.title=f.getElementsByTagName("title",j.children,false)[0].children[0].data}catch(r){}try{k.link=f.getElementsByTagName("link",j.children,false)[0].attribs.href}catch(s){}try{k.description=f.getElementsByTagName("summary",j.children,false)[0].children[0].data}catch(t){}try{k.pubDate=new Date(f.getElementsByTagName("updated",j.children, false)[0].children[0].data)}catch(u){}a.items.push(k)})}this.dom=a}n.super_.prototype.done.call(this)};i._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};i.reWhitespace=/^\s*$/;i.prototype.dom=null;i.prototype.reset=function(){this.dom=[];this._done=false;this._tagStack=[];this._tagStack.last=function(){return this.length?this[this.length-1]:null}};i.prototype.done=function(){this._done=true;this.handleCallback(null)};i.prototype.writeTag= function(a){this.handleElement(a)};i.prototype.writeText=function(a){if(this._options.ignoreWhitespace)if(i.reWhitespace.test(a.data))return;this.handleElement(a)};i.prototype.writeComment=function(a){this.handleElement(a)};i.prototype.writeDirective=function(a){this.handleElement(a)};i.prototype.error=function(a){this.handleCallback(a)};i.prototype._options=null;i.prototype._callback=null;i.prototype._done=false;i.prototype._tagStack=null;i.prototype.handleCallback=function(a){if(typeof this._callback!= "function")if(a)throw a;else return;this._callback(a,this.dom)};i.prototype.isEmptyTag=function(a){a=a.name.toLowerCase();if(a.charAt(0)=="/")a=a.substring(1);return this._options.enforceEmptyTags&&!!i._emptyTags[a]};i.prototype.handleElement=function(a){this._done&&this.handleCallback(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!= 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;a>-1&&this._tagStack[a--].name!=c;);if(a>-1||this._tagStack[0].name==c)for(;a<this._tagStack.length-1;)this._tagStack.pop()}}else{if(!this._tagStack.last().children)this._tagStack.last().children=[];this._tagStack.last().children.push(a);this.isEmptyTag(a)||this._tagStack.push(a)}else{if(!this._tagStack.last().children)this._tagStack.last().children= [];this._tagStack.last().children.push(a)}else if(a.type!=d.Text&&a.type!=d.Comment&&a.type!=d.Directive){if(a.name.charAt(0)!="/"){this.dom.push(a);this.isEmptyTag(a)||this._tagStack.push(a)}}else this.dom.push(a)};var f={testElement:function(a,c){if(!c)return false;for(var b in a)if(b=="tag_name"){if(c.type!="tag"&&c.type!="script"&&c.type!="style")return false;if(!a.tag_name(c.name))return false}else if(b=="tag_type"){if(!a.tag_type(c.type))return false}else if(b=="tag_contains"){if(c.type!="text"&& c.type!="comment"&&c.type!="directive")return false;if(!a.tag_contains(c.data))return false}else if(!c.attribs||!a[b](c.attribs[b]))return false;return true},getElements:function(a,c,b,h){function g(o){return function(p){return p==o}}b=b===undefined||b===null||!!b;h=isNaN(parseInt(h))?-1:parseInt(h);if(!c)return[];var l=[],m;for(m in a)if(typeof a[m]!="function")a[m]=g(a[m]);f.testElement(a,c)&&l.push(c);if(h>=0&&l.length>=h)return l;if(b&&c.children)c=c.children;else if(c instanceof Array)c=c;else return l; for(m=0;m<c.length;m++){l=l.concat(f.getElements(a,c[m],b,h));if(h>=0&&l.length>=h)break}return l},getElementById:function(a,c,b){a=f.getElements({id:a},c,b,1);return a.length?a[0]:null},getElementsByTagName:function(a,c,b,h){return f.getElements({tag_name:a},c,b,h)},getElementsByTagType:function(a,c,b,h){return f.getElements({tag_type:a},c,b,h)}};exports.Parser=e;exports.DefaultHandler=i;exports.RssHandler=n;exports.ElementType=d;exports.DomUtils=f})();
+***********************************************//* v1.7.2 */(function(){function J(a,b){var c=function(){};c.prototype=b.prototype,a.super_=b,a.prototype=new c,a.prototype.constructor=a}function s(a,b){this.reset(),this._options=b?b:{},this._options.ignoreWhitespace==undefined&&(this._options.ignoreWhitespace=!1),this._options.verbose==undefined&&(this._options.verbose=!0),this._options.enforceEmptyTags==undefined&&(this._options.enforceEmptyTags=!0),typeof a=="function"&&(this._callback=a)}function q(a){q.super_.call(this,a,{ignoreWhitespace:!0,verbose:!1,enforceEmptyTags:!1})}function c(a,b){this._options=b?b:{},this._options.includeLocation==undefined&&(this._options.includeLocation=!1),this.validateHandler(a),this._handler=a,this.reset()}function a(){return typeof require=="function"&&typeof exports=="object"&&typeof module=="object"&&typeof __filename=="string"&&typeof __dirname=="string"}if(!a()){if(!this.Tautologistics)this.Tautologistics={};else if(this.Tautologistics.NodeHtmlParser)return;this.Tautologistics.NodeHtmlParser={},exports=this.Tautologistics.NodeHtmlParser}var b={Text:"text",Directive:"directive",Comment:"comment",Script:"script",Style:"style",Tag:"tag"};c._reTrim=/(^\s+|\s+$)/g,c._reTrimComment=/(^\!--|--$)/g,c._reWhitespace=/\s/g,c._reTagName=/^\s*(\/?)\s*([^\s\/]+)/,c._reAttrib=/([^=<>\"\'\s]+)\s*=\s*"([^"]*)"|([^=<>\"\'\s]+)\s*=\s*'([^']*)'|([^=<>\"\'\s]+)\s*=\s*([^'"\s]+)|([^=<>\"\'\s\/]+)/g,c._reTags=/[\<\>]/g,c.prototype.parseComplete=function d(a){this.reset(),this.parseChunk(a),this.done()},c.prototype.parseChunk=function e(a){this._done&&this.handleError(new Error("Attempted to parse chunk after parsing already done")),this._buffer+=a,this.parseTags()},c.prototype.done=function f(){if(!this._done){this._done=!0;if(this._buffer.length){var a=this._buffer;this._buffer="";var d={raw:a,data:this._parseState==b.Text?a:a.replace(c._reTrim,""),type:this._parseState};if(this._parseState==b.Tag||this._parseState==b.Script||this._parseState==b.Style)d.name=this.parseTagName(d.data);this.parseAttribs(d),this._elements.push(d)}this.writeHandler(),this._handler.done()}},c.prototype.peek=function g(){this._handler.peek()},c.prototype.reset=function h(){this._buffer="",this._done=!1,this._elements=[],this._elementsCurrent=0,this._current=0,this._next=0,this._location={row:0,col:0,charOffset:0,inBuffer:0},this._parseState=b.Text,this._prevTagSep="",this._tagStack=[],this._handler.reset()},c.prototype._options=null,c.prototype._handler=null,c.prototype._buffer=null,c.prototype._done=!1,c.prototype._elements=null,c.prototype._elementsCurrent=0,c.prototype._current=0,c.prototype._next=0,c.prototype._location=null,c.prototype._parseState=b.Text,c.prototype._prevTagSep="",c.prototype._tagStack=null,c.prototype.parseTagAttribs=function i(a){var c=a.length,d=0;while(d<c){var e=a[d++];(e.type==b.Tag||e.type==b.Script||e.type==b.style)&&this.parseAttribs(e)}return a},c.prototype.parseAttribs=function j(a){if(a.type==b.Script||a.type==b.Style||a.type==b.Tag){var d=a.data.split(c._reWhitespace,1)[0],e=a.data.substring(d.length);if(e.length<1)return;var f;c._reAttrib.lastIndex=0;while(f=c._reAttrib.exec(e))a.attribs==undefined&&(a.attribs={}),typeof f[1]=="string"&&f[1].length?a.attribs[f[1]]=f[2]:typeof f[3]=="string"&&f[3].length?a.attribs[f[3].toString()]=f[4].toString():typeof f[5]=="string"&&f[5].length?a.attribs[f[5]]=f[6]:typeof f[7]=="string"&&f[7].length&&(a.attribs[f[7]]=f[7])}},c.prototype.parseTagName=function k(a){if(a==null||a=="")return"";var b=c._reTagName.exec(a);if(!b)return"";return(b[1]?"/":"")+b[2]},c.prototype.parseTags=function l(){var a=this._buffer.length-1;while(c._reTags.test(this._buffer)){this._next=c._reTags.lastIndex-1;var d=this._buffer.charAt(this._next),e=this._buffer.substring(this._current,this._next),f={raw:e,data:this._parseState==b.Text?e:e.replace(c._reTrim,""),type:this._parseState},g=this.parseTagName(f.data);if(this._tagStack.length)if(this._tagStack[this._tagStack.length-1]==b.Script){if(g=="/script")this._tagStack.pop();else if(f.raw.indexOf("!--")!=0){f.type=b.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==b.Text){var h=this._elements[this._elements.length-1];h.raw=h.data=h.raw+this._prevTagSep+f.raw,f.raw=f.data=""}}}else if(this._tagStack[this._tagStack.length-1]==b.Style){if(g=="/style")this._tagStack.pop();else if(f.raw.indexOf("!--")!=0){f.type=b.Text;if(this._elements.length&&this._elements[this._elements.length-1].type==b.Text){var h=this._elements[this._elements.length-1];f.raw!=""?(h.raw=h.data=h.raw+this._prevTagSep+f.raw,f.raw=f.data=""):h.raw=h.data=h.raw+this._prevTagSep}else f.raw!=""&&(f.raw=f.data=f.raw)}}else if(this._tagStack[this._tagStack.length-1]==b.Comment){var i=f.raw.length;if(f.raw.charAt(i-2)=="-"&&f.raw.charAt(i-1)=="-"&&d==">"){this._tagStack.pop();if(this._elements.length&&this._elements[this._elements.length-1].type==b.Comment){var h=this._elements[this._elements.length-1];h.raw=h.data=(h.raw+f.raw).replace(c._reTrimComment,""),f.raw=f.data="",f.type=b.Text}else f.type=b.Comment}else{f.type=b.Comment;if(this._elements.length&&this._elements[this._elements.length-1].type==b.Comment){var h=this._elements[this._elements.length-1];h.raw=h.data=h.raw+f.raw+d,f.raw=f.data="",f.type=b.Text}else f.raw=f.data=f.raw+d}}if(f.type==b.Tag){f.name=g;if(f.raw.indexOf("!--")==0){f.type=b.Comment,delete f.name;var i=f.raw.length;f.raw.charAt(i-1)=="-"&&f.raw.charAt(i-2)=="-"&&d==">"?f.raw=f.data=f.raw.replace(c._reTrimComment,""):(f.raw+=d,this._tagStack.push(b.Comment))}else f.raw.indexOf("!")==0||f.raw.indexOf("?")==0?f.type=b.Directive:f.name=="script"?(f.type=b.Script,f.data.charAt(f.data.length-1)!="/"&&this._tagStack.push(b.Script)):f.name=="/script"?f.type=b.Script:f.name=="style"?(f.type=b.Style,f.data.charAt(f.data.length-1)!="/"&&this._tagStack.push(b.Style)):f.name=="/style"&&(f.type=b.Style);f.name&&f.name.charAt(0)=="/"&&(f.data=f.name)}if(f.raw!=""||f.type!=b.Text)this._options.includeLocation&&!f.location&&(f.location=this.getLocation(f.type==b.Tag)),this.parseAttribs(f),this._elements.push(f),f.type!=b.Text&&f.type!=b.Comment&&f.type!=b.Directive&&f.data.charAt(f.data.length-1)=="/"&&this._elements.push({raw:"/"+f.name,data:"/"+f.name,name:"/"+f.name,type:f.type});this._parseState=d=="<"?b.Tag:b.Text,this._current=this._next+1,this._prevTagSep=d}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()},c.prototype.getLocation=function m(a){var b,c=this._location,d=this._current-(a?1:0),e=a&&c.charOffset==0&&this._current==0;for(;c.charOffset<d;c.charOffset++)b=this._buffer.charAt(c.charOffset),b=="\n"?(c.inBuffer++,c.col=0):b!="\r"&&c.col++;return{line:c.row+c.inBuffer+1,col:c.col+(e?0:1)}},c.prototype.validateHandler=function n(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")},c.prototype.writeHandler=function o(a){a=!!a;if(!this._tagStack.length||!!a)while(this._elements.length){var c=this._elements.shift();switch(c.type){case b.Comment:this._handler.writeComment(c);break;case b.Directive:this._handler.writeDirective(c);break;case b.Text:this._handler.writeText(c);break;default:this._handler.writeTag(c)}}},c.prototype.handleError=function p(a){if(typeof this._handler.error=="function")this._handler.error(a);else throw a},J(q,s),q.prototype.done=function r(){var a={},b,c=D.getElementsByTagName(function(a){return a=="rss"||a=="feed"},this.dom,!1);c.length&&(b=c[0]);if(b){if(b.name=="rss"){a.type="rss",b=b.children[0],a.id="";try{a.title=D.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(d){}try{a.link=D.getElementsByTagName("link",b.children,!1)[0].children[0].data}catch(d){}try{a.description=D.getElementsByTagName("description",b.children,!1)[0].children[0].data}catch(d){}try{a.updated=new Date(D.getElementsByTagName("lastBuildDate",b.children,!1)[0].children[0].data)}catch(d){}try{a.author=D.getElementsByTagName("managingEditor",b.children,!1)[0].children[0].data}catch(d){}a.items=[],D.getElementsByTagName("item",b.children).forEach(function(b,c,d){var e={};try{e.id=D.getElementsByTagName("guid",b.children,!1)[0].children[0].data}catch(f){}try{e.title=D.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(f){}try{e.link=D.getElementsByTagName("link",b.children,!1)[0].children[0].data}catch(f){}try{e.description=D.getElementsByTagName("description",b.children,!1)[0].children[0].data}catch(f){}try{e.pubDate=new Date(D.getElementsByTagName("pubDate",b.children,!1)[0].children[0].data)}catch(f){}a.items.push(e)})}else{a.type="atom";try{a.id=D.getElementsByTagName("id",b.children,!1)[0].children[0].data}catch(d){}try{a.title=D.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(d){}try{a.link=D.getElementsByTagName("link",b.children,!1)[0].attribs.href}catch(d){}try{a.description=D.getElementsByTagName("subtitle",b.children,!1)[0].children[0].data}catch(d){}try{a.updated=new Date(D.getElementsByTagName("updated",b.children,!1)[0].children[0].data)}catch(d){}try{a.author=D.getElementsByTagName("email",b.children,!0)[0].children[0].data}catch(d){}a.items=[],D.getElementsByTagName("entry",b.children).forEach(function(b,c,d){var e={};try{e.id=D.getElementsByTagName("id",b.children,!1)[0].children[0].data}catch(f){}try{e.title=D.getElementsByTagName("title",b.children,!1)[0].children[0].data}catch(f){}try{e.link=D.getElementsByTagName("link",b.children,!1)[0].attribs.href}catch(f){}try{e.description=D.getElementsByTagName("summary",b.children,!1)[0].children[0].data}catch(f){}try{e.pubDate=new Date(D.getElementsByTagName("updated",b.children,!1)[0].children[0].data)}catch(f){}a.items.push(e)})}this.dom=a}q.super_.prototype.done.call(this)},s._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},s.reWhitespace=/^\s*$/,s.prototype.dom=null,s.prototype.reset=function t(){this.dom=[],this._done=!1,this._tagStack=[],this._tagStack.last=function a(){return this.length?this[this.length-1]:null}},s.prototype.done=function u(){this._done=!0,this.handleCallback(null)},s.prototype.peek=function v(){this.handleCallback(null)},s.prototype.writeTag=function w(a){this.handleElement(a)},s.prototype.writeText=function x(a){(!this._options.ignoreWhitespace||!s.reWhitespace.test(a.data))&&this.handleElement(a)},s.prototype.writeComment=function y(a){this.handleElement(a)},s.prototype.writeDirective=function z(a){this.handleElement(a)},s.prototype.error=function A(a){this.handleCallback(a)},s.prototype._options=null,s.prototype._callback=null,s.prototype._done=!1,s.prototype._tagStack=null,s.prototype.handleCallback=function B(a){if(typeof this._callback!="function"){if(a)throw a}else this._callback(a,this.dom)},s.prototype.isEmptyTag=function(a){var b=a.name.toLowerCase();b.charAt(0)=="/"&&(b=b.substring(1));return this._options.enforceEmptyTags&&!!s._emptyTags[b]},s.prototype.handleElement=function C(a){this._done&&this.handleCallback(new Error("Writing to the handler after done() called is not allowed without a reset()")),this._options.verbose||(delete a.raw,(a.type=="tag"||a.type=="script"||a.type=="style")&&delete a.data);if(!this._tagStack.last())a.type!=b.Text&&a.type!=b.Comment&&a.type!=b.Directive?a.name.charAt(0)!="/"&&(this.dom.push(a),this.isEmptyTag(a)||this._tagStack.push(a)):this.dom.push(a);else if(a.type!=b.Text&&a.type!=b.Comment&&a.type!=b.Directive)if(a.name.charAt(0)=="/"){var c=a.name.substring(1);if(!this.isEmptyTag(a)){var d=this._tagStack.length-1;while(d>-1&&this._tagStack[d--].name!=c){}if(d>-1||this._tagStack[0].name==c)while(d<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)};var D={testElement:function E(a,b){if(!b)return!1;for(var c in a)if(c=="tag_name"){if(b.type!="tag"&&b.type!="script"&&b.type!="style")return!1;if(!a.tag_name(b.name))return!1}else if(c=="tag_type"){if(!a.tag_type(b.type))return!1}else if(c=="tag_contains"){if(b.type!="text"&&b.type!="comment"&&b.type!="directive")return!1;if(!a.tag_contains(b.data))return!1}else if(!b.attribs||!a[c](b.attribs[c]))return!1;return!0},getElements:function F(a,b,c,d){function g(a){return function(b){return b==a}}c=c===undefined||c===null||!!c,d=isNaN(parseInt(d))?-1:parseInt(d);if(!b)return[];var e=[],f;for(var h in a)typeof a[h]!="function"&&(a[h]=g(a[h]));D.testElement(a,b)&&e.push(b);if(d>=0&&e.length>=d)return e;if(c&&b.children)f=b.children;else if(b instanceof Array)f=b;else return e;for(var i=0;i<f.length;i++){e=e.concat(D.getElements(a,f[i],c,d));if(d>=0&&e.length>=d)break}return e},getElementById:function G(a,b,c){var d=D.getElements({id:a},b,c,1);return d.length?d[0]:null},getElementsByTagName:function H(a,b,c,d){return D.getElements({tag_name:a},b,c,d)},getElementsByTagType:function I(a,b,c,d){return D.getElements({tag_type:a},b,c,d)}};exports.Parser=c,exports.DefaultHandler=s,exports.RssHandler=q,exports.ElementType=b,exports.DomUtils=D})()
Something went wrong with that request. Please try again.