FlexitimeTracker/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/www/js/htmlparser.min.js

22 lines
14 KiB
JavaScript

/***********************************************
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})();