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

var 
re_whitespace = /s+/g;

function 
DomHandler(callbackoptionselementCB){
    if(
typeof callback === "object"){
        
elementCB options;
        
options callback;
        
callback null;
    } else if(
typeof options === "function"){
        
elementCB options;
        
options defaultOpts;
    }
    
this._callback callback;
    
this._options options || defaultOpts;
    
this._elementCB elementCB;
    
this.dom = [];
    
this._done false;
    
this._tagStack = [];
}

//default options
var defaultOpts = {
    
normalizeWhitespacefalse //Replace all whitespace with single spaces
};

//Resets the handler back to starting state
DomHandler.prototype.onreset = function(){
    
DomHandler.call(thisthis._callbackthis._optionsthis._elementCB);
};

//Signals the handler that parsing is done
DomHandler.prototype.onend = function(){
    if(
this._done) return;
    
this._done true;
    
this._handleCallback(null);
};

DomHandler.prototype._handleCallback =
DomHandler.prototype.onerror = function(error){
    if(
typeof this._callback === "function"){
        
this._callback(errorthis.dom);
    } else {
        if(
error) throw error;
    }
};

DomHandler.prototype.onclosetag = function(){
    
//if(this._tagStack.pop().name !== name) this._handleCallback(Error("Tagname didn't match!"));
    
var elem this._tagStack.pop();
    if(
this._elementCBthis._elementCB(elem);
};

DomHandler.prototype._addDomElement = function(element){
    var 
parent this._tagStack[this._tagStack.length 1];
    var 
siblings parent parent.children this.dom;
    var 
previousSibling siblings[siblings.length 1];

    
element.next null;

    if (
this._options.withDomLvl1) {
        
element.__proto__ NodePrototype;
    }

    if(
previousSibling){
        
element.prev previousSibling;
        
previousSibling.next element;
    } else {
        
element.prev null;
    }

    
siblings.push(element);
    
element.parent parent || null;
};

// This object will be used as the prototype for Nodes when creating a
// DOM-Level-1-compliant structure.
var NodePrototype = {
    
get firstChild() {
        var 
children this.children;
        return 
children && children[0] || null;
    },
    
get lastChild() {
        var 
children this.children;
        return 
children && children[children.length 1] || null;
    },
    
get nodeType() {
        return 
nodeTypes[this.type] || nodeTypes.element;
    }
};
var 
domLvl1 = {
    
tagName"name",
    
childNodes"children",
    
parentNode"parent",
    
previousSibling"prev",
    
nextSibling"next",
    
nodeValue"data"
};
var 
nodeTypes = {
    
element1,
    
text3,
    
cdata4,
    
comment8
};
Object.keys(domLvl1).forEach(function(key) {
    var 
shorthand domLvl1[key];
    
Object.defineProperty(NodePrototypekey, {
        
get: function() {
            return 
this[shorthand] || null;
        },
        
set: function(val) {
            
this[shorthand] = val;
            return 
val;
        }
    });
});

DomHandler.prototype.onopentag = function(nameattribs){
    var 
element = {
        
typename === "script" ElementType.Script name === "style" ElementType.Style ElementType.Tag,
        
namename,
        
attribsattribs,
        
children: []
    };

    
this._addDomElement(element);

    
this._tagStack.push(element);
};

DomHandler.prototype.ontext = function(data){
    
//the ignoreWhitespace is officially dropped, but for now,
    //it's an alias for normalizeWhitespace
    
var normalize this._options.normalizeWhitespace || this._options.ignoreWhitespace;

    var 
lastTag;

    if(!
this._tagStack.length && this.dom.length && (lastTag this.dom[this.dom.length-1]).type === ElementType.Text){
        if(
normalize){
            
lastTag.data = (lastTag.data data).replace(re_whitespace" ");
        } else {
            
lastTag.data += data;
        }
    } else {
        if(
            
this._tagStack.length &&
            (
lastTag this._tagStack[this._tagStack.length 1]) &&
            (
lastTag lastTag.children[lastTag.children.length 1]) &&
            
lastTag.type === ElementType.Text
        
){
            if(
normalize){
                
lastTag.data = (lastTag.data data).replace(re_whitespace" ");
            } else {
                
lastTag.data += data;
            }
        } else {
            if(
normalize){
                
data data.replace(re_whitespace" ");
            }

            
this._addDomElement({
                
datadata,
                
typeElementType.Text
            
});
        }
    }
};

DomHandler.prototype.oncomment = function(data){
    var 
lastTag this._tagStack[this._tagStack.length 1];

    if(
lastTag && lastTag.type === ElementType.Comment){
        
lastTag.data += data;
        return;
    }

    var 
element = {
        
datadata,
        
typeElementType.Comment
    
};

    
this._addDomElement(element);
    
this._tagStack.push(element);
};

DomHandler.prototype.oncdatastart = function(){
    var 
element = {
        
children: [{
            
data"",
            
typeElementType.Text
        
}],
        
typeElementType.CDATA
    
};

    
this._addDomElement(element);
    
this._tagStack.push(element);
};

DomHandler.prototype.oncommentend DomHandler.prototype.oncdataend = function(){
    
this._tagStack.pop();
};

DomHandler.prototype.onprocessinginstruction = function(namedata){
    
this._addDomElement({
        
namename,
        
datadata,
        
typeElementType.Directive
    
});
};

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