Вход Регистрация
Файл: error-kitty/node_modules/cheerio/node_modules/htmlparser2/lib/Parser.js
Строк: 377
<?php
var Tokenizer = require("./Tokenizer.js");

/*
    Options:

    xmlMode: Special behavior for script/style tags (true by default)
    lowerCaseAttributeNames: call .toLowerCase for each attribute name (true if xmlMode is `false`)
    lowerCaseTags: call .toLowerCase for each tag name (true if xmlMode is `false`)
*/

/*
    Callbacks:

    oncdataend,
    oncdatastart,
    onclosetag,
    oncomment,
    oncommentend,
    onerror,
    onopentag,
    onprocessinginstruction,
    onreset,
    ontext
*/

var formTags = {
    
inputtrue,
    
optiontrue,
    
optgrouptrue,
    
selecttrue,
    
buttontrue,
    
datalisttrue,
    
textareatrue
};

var 
openImpliesClose = {
    
tr      : { tr:trueth:truetd:true },
    
th      : { th:true },
    
td      : { thead:truetd:true },
    
body    : { head:truelink:truescript:true },
    
li      : { li:true },
    
p       : { p:true },
    
select  formTags,
    
input   formTags,
    
output  formTags,
    
button  formTags,
    
datalistformTags,
    
textareaformTags,
    
option  : { option:true },
    
optgroup: { optgroup:true }
};

var 
voidElements = {
    
__proto__null,
    
areatrue,
    
basetrue,
    
basefonttrue,
    
brtrue,
    
coltrue,
    
commandtrue,
    
embedtrue,
    
frametrue,
    
hrtrue,
    
imgtrue,
    
inputtrue,
    
isindextrue,
    
keygentrue,
    
linktrue,
    
metatrue,
    
paramtrue,
    
sourcetrue,
    
tracktrue,
    
wbrtrue,

    
//common self closing svg elements
    
pathtrue,
    
circletrue,
    
ellipsetrue,
    
linetrue,
    
recttrue,
    use: 
true
};

var 
re_nameEnd = /s|//;

function Parser(cbsoptions){
    
this._options options || {};
    
this._cbs cbs || {};

    
this._tagname "";
    
this._attribname "";
    
this._attribvalue "";
    
this._attribs null;
    
this._stack = [];

    
this.startIndex 0;
    
this.endIndex null;

    
this._lowerCaseTagNames "lowerCaseTags" in this._options ?
                                    !!
this._options.lowerCaseTags :
                                    !
this._options.xmlMode;
    
this._lowerCaseAttributeNames "lowerCaseAttributeNames" in this._options ?
                                    !!
this._options.lowerCaseAttributeNames :
                                    !
this._options.xmlMode;

    
this._tokenizer = new Tokenizer(this._optionsthis);
}

require(
"util").inherits(Parser, require("events").EventEmitter);

Parser.prototype._updatePosition = function(initialOffset){
    if(
this.endIndex === null){
        if(
this._tokenizer._sectionStart <= initialOffset){
            
this.startIndex 0;
        } else {
            
this.startIndex this._tokenizer._sectionStart initialOffset;
        }
    }
    else 
this.startIndex this.endIndex 1;
    
this.endIndex this._tokenizer._index;
};

//Tokenizer event handlers
Parser.prototype.ontext = function(data){
    
this._updatePosition(1);
    
this.endIndex--;

    if(
this._cbs.ontextthis._cbs.ontext(data);
};

Parser.prototype.onopentagname = function(name){
    if(
this._lowerCaseTagNames){
        
name name.toLowerCase();
    }

    
this._tagname name;

    if (!
this._options.xmlMode && name in openImpliesClose) {
        for(
            var 
el;
            (
el this._stack[this._stack.length-1]) in openImpliesClose[name];
            
this.onclosetag(el)
        );
    }

    if(
this._options.xmlMode || !(name in voidElements)){
        
this._stack.push(name);
    }

    if(
this._cbs.onopentagnamethis._cbs.onopentagname(name);
    if(
this._cbs.onopentagthis._attribs = {};
};

Parser.prototype.onopentagend = function(){
    
this._updatePosition(1);

    if(
this._attribs){
        if(
this._cbs.onopentagthis._cbs.onopentag(this._tagnamethis._attribs);
        
this._attribs null;
    }

    if(!
this._options.xmlMode && this._cbs.onclosetag && this._tagname in voidElements){
        
this._cbs.onclosetag(this._tagname);
    }

    
this._tagname "";
};

Parser.prototype.onclosetag = function(name){
    
this._updatePosition(1);

    if(
this._lowerCaseTagNames){
        
name name.toLowerCase();
    }

    if(
this._stack.length && (!(name in voidElements) || this._options.xmlMode)){
        var 
pos this._stack.lastIndexOf(name);
        if(
pos !== -1){
            if(
this._cbs.onclosetag){
                
pos this._stack.length pos;
                while(
pos--) this._cbs.onclosetag(this._stack.pop());
            }
            else 
this._stack.length pos;
        } else if(
name === "p" && !this._options.xmlMode){
            
this.onopentagname(name);
            
this._closeCurrentTag();
        }
    } else if(!
this._options.xmlMode && (name === "br" || name === "p")){
        
this.onopentagname(name);
        
this._closeCurrentTag();
    }
};

Parser.prototype.onselfclosingtag = function(){
    if(
this._options.xmlMode || this._options.recognizeSelfClosing){
        
this._closeCurrentTag();
    } else {
        
this.onopentagend();
    }
};

Parser.prototype._closeCurrentTag = function(){
    var 
name this._tagname;

    
this.onopentagend();

    
//self-closing tags will be on the top of the stack
    //(cheaper check than in onclosetag)
    
if(this._stack[this._stack.length-1] === name){
        if(
this._cbs.onclosetag){
            
this._cbs.onclosetag(name);
        }
        
this._stack.pop();
    }
};

Parser.prototype.onattribname = function(name){
    if(
this._lowerCaseAttributeNames){
        
name name.toLowerCase();
    }
    
this._attribname name;
};

Parser.prototype.onattribdata = function(value){
    
this._attribvalue += value;
};

Parser.prototype.onattribend = function(){
    if(
this._cbs.onattributethis._cbs.onattribute(this._attribnamethis._attribvalue);
    if(
        
this._attribs &&
        !
Object.prototype.hasOwnProperty.call(this._attribsthis._attribname)
    ){
        
this._attribs[this._attribname] = this._attribvalue;
    }
    
this._attribname "";
    
this._attribvalue "";
};

Parser.prototype._getInstructionName = function(value){
    var 
idx value.search(re_nameEnd),
        
name idx value value.substr(0idx);

    if(
this._lowerCaseTagNames){
        
name name.toLowerCase();
    }

    return 
name;
};

Parser.prototype.ondeclaration = function(value){
    if(
this._cbs.onprocessinginstruction){
        var 
name this._getInstructionName(value);
        
this._cbs.onprocessinginstruction("!" name"!" value);
    }
};

Parser.prototype.onprocessinginstruction = function(value){
    if(
this._cbs.onprocessinginstruction){
        var 
name this._getInstructionName(value);
        
this._cbs.onprocessinginstruction("?" name"?" value);
    }
};

Parser.prototype.oncomment = function(value){
    
this._updatePosition(4);

    if(
this._cbs.oncommentthis._cbs.oncomment(value);
    if(
this._cbs.oncommentendthis._cbs.oncommentend();
};

Parser.prototype.oncdata = function(value){
    
this._updatePosition(1);

    if(
this._options.xmlMode || this._options.recognizeCDATA){
        if(
this._cbs.oncdatastartthis._cbs.oncdatastart();
        if(
this._cbs.ontextthis._cbs.ontext(value);
        if(
this._cbs.oncdataendthis._cbs.oncdataend();
    } else {
        
this.oncomment("[CDATA[" value "]]");
    }
};

Parser.prototype.onerror = function(err){
    if(
this._cbs.onerrorthis._cbs.onerror(err);
};

Parser.prototype.onend = function(){
    if(
this._cbs.onclosetag){
        for(
            var 
this._stack.length;
            
0;
            
this._cbs.onclosetag(this._stack[--i])
        );
    }
    if(
this._cbs.onendthis._cbs.onend();
};


//Resets the parser to a blank state, ready to parse a new HTML document
Parser.prototype.reset = function(){
    if(
this._cbs.onresetthis._cbs.onreset();
    
this._tokenizer.reset();

    
this._tagname "";
    
this._attribname "";
    
this._attribs null;
    
this._stack = [];
};

//Parses a complete HTML document and pushes it to the handler
Parser.prototype.parseComplete = function(data){
    
this.reset();
    
this.end(data);
};

Parser.prototype.write = function(chunk){
    
this._tokenizer.write(chunk);
};

Parser.prototype.end = function(chunk){
    
this._tokenizer.end(chunk);
};

Parser.prototype.pause = function(){
    
this._tokenizer.pause();
};

Parser.prototype.resume = function(){
    
this._tokenizer.resume();
};

//alias for backwards compat
Parser.prototype.parseChunk Parser.prototype.write;
Parser.prototype.done Parser.prototype.end;

module.exports Parser;
?>
Онлайн: 0
Реклама