Initial commit
This commit is contained in:
11
node_modules/domhandler/LICENSE
generated
vendored
Normal file
11
node_modules/domhandler/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
Copyright (c) Felix Böhm
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
217
node_modules/domhandler/index.js
generated
vendored
Normal file
217
node_modules/domhandler/index.js
generated
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
var ElementType = require("domelementtype");
|
||||
|
||||
var re_whitespace = /\s+/g;
|
||||
var NodePrototype = require("./lib/node");
|
||||
var ElementPrototype = require("./lib/element");
|
||||
|
||||
function DomHandler(callback, options, elementCB){
|
||||
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 = [];
|
||||
this._parser = this._parser || null;
|
||||
}
|
||||
|
||||
//default options
|
||||
var defaultOpts = {
|
||||
normalizeWhitespace: false, //Replace all whitespace with single spaces
|
||||
withStartIndices: false, //Add startIndex properties to nodes
|
||||
withEndIndices: false, //Add endIndex properties to nodes
|
||||
};
|
||||
|
||||
DomHandler.prototype.onparserinit = function(parser){
|
||||
this._parser = parser;
|
||||
};
|
||||
|
||||
//Resets the handler back to starting state
|
||||
DomHandler.prototype.onreset = function(){
|
||||
DomHandler.call(this, this._callback, this._options, this._elementCB);
|
||||
};
|
||||
|
||||
//Signals the handler that parsing is done
|
||||
DomHandler.prototype.onend = function(){
|
||||
if(this._done) return;
|
||||
this._done = true;
|
||||
this._parser = null;
|
||||
this._handleCallback(null);
|
||||
};
|
||||
|
||||
DomHandler.prototype._handleCallback =
|
||||
DomHandler.prototype.onerror = function(error){
|
||||
if(typeof this._callback === "function"){
|
||||
this._callback(error, this.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._options.withEndIndices && elem){
|
||||
elem.endIndex = this._parser.endIndex;
|
||||
}
|
||||
|
||||
if(this._elementCB) this._elementCB(elem);
|
||||
};
|
||||
|
||||
DomHandler.prototype._createDomElement = function(properties){
|
||||
if (!this._options.withDomLvl1) return properties;
|
||||
|
||||
var element;
|
||||
if (properties.type === "tag") {
|
||||
element = Object.create(ElementPrototype);
|
||||
} else {
|
||||
element = Object.create(NodePrototype);
|
||||
}
|
||||
|
||||
for (var key in properties) {
|
||||
if (properties.hasOwnProperty(key)) {
|
||||
element[key] = properties[key];
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
};
|
||||
|
||||
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.withStartIndices){
|
||||
element.startIndex = this._parser.startIndex;
|
||||
}
|
||||
if(this._options.withEndIndices){
|
||||
element.endIndex = this._parser.endIndex;
|
||||
}
|
||||
|
||||
if(previousSibling){
|
||||
element.prev = previousSibling;
|
||||
previousSibling.next = element;
|
||||
} else {
|
||||
element.prev = null;
|
||||
}
|
||||
|
||||
siblings.push(element);
|
||||
element.parent = parent || null;
|
||||
};
|
||||
|
||||
DomHandler.prototype.onopentag = function(name, attribs){
|
||||
var properties = {
|
||||
type: name === "script" ? ElementType.Script : name === "style" ? ElementType.Style : ElementType.Tag,
|
||||
name: name,
|
||||
attribs: attribs,
|
||||
children: []
|
||||
};
|
||||
|
||||
var element = this._createDomElement(properties);
|
||||
|
||||
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, " ");
|
||||
}
|
||||
|
||||
var element = this._createDomElement({
|
||||
data: data,
|
||||
type: ElementType.Text
|
||||
});
|
||||
|
||||
this._addDomElement(element);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
DomHandler.prototype.oncomment = function(data){
|
||||
var lastTag = this._tagStack[this._tagStack.length - 1];
|
||||
|
||||
if(lastTag && lastTag.type === ElementType.Comment){
|
||||
lastTag.data += data;
|
||||
return;
|
||||
}
|
||||
|
||||
var properties = {
|
||||
data: data,
|
||||
type: ElementType.Comment
|
||||
};
|
||||
|
||||
var element = this._createDomElement(properties);
|
||||
|
||||
this._addDomElement(element);
|
||||
this._tagStack.push(element);
|
||||
};
|
||||
|
||||
DomHandler.prototype.oncdatastart = function(){
|
||||
var properties = {
|
||||
children: [{
|
||||
data: "",
|
||||
type: ElementType.Text
|
||||
}],
|
||||
type: ElementType.CDATA
|
||||
};
|
||||
|
||||
var element = this._createDomElement(properties);
|
||||
|
||||
this._addDomElement(element);
|
||||
this._tagStack.push(element);
|
||||
};
|
||||
|
||||
DomHandler.prototype.oncommentend = DomHandler.prototype.oncdataend = function(){
|
||||
this._tagStack.pop();
|
||||
};
|
||||
|
||||
DomHandler.prototype.onprocessinginstruction = function(name, data){
|
||||
var element = this._createDomElement({
|
||||
name: name,
|
||||
data: data,
|
||||
type: ElementType.Directive
|
||||
});
|
||||
|
||||
this._addDomElement(element);
|
||||
};
|
||||
|
||||
module.exports = DomHandler;
|
||||
20
node_modules/domhandler/lib/element.js
generated
vendored
Normal file
20
node_modules/domhandler/lib/element.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
// DOM-Level-1-compliant structure
|
||||
var NodePrototype = require('./node');
|
||||
var ElementPrototype = module.exports = Object.create(NodePrototype);
|
||||
|
||||
var domLvl1 = {
|
||||
tagName: "name"
|
||||
};
|
||||
|
||||
Object.keys(domLvl1).forEach(function(key) {
|
||||
var shorthand = domLvl1[key];
|
||||
Object.defineProperty(ElementPrototype, key, {
|
||||
get: function() {
|
||||
return this[shorthand] || null;
|
||||
},
|
||||
set: function(val) {
|
||||
this[shorthand] = val;
|
||||
return val;
|
||||
}
|
||||
});
|
||||
});
|
||||
44
node_modules/domhandler/lib/node.js
generated
vendored
Normal file
44
node_modules/domhandler/lib/node.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
// This object will be used as the prototype for Nodes when creating a
|
||||
// DOM-Level-1-compliant structure.
|
||||
var NodePrototype = module.exports = {
|
||||
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 = {
|
||||
element: 1,
|
||||
text: 3,
|
||||
cdata: 4,
|
||||
comment: 8
|
||||
};
|
||||
|
||||
Object.keys(domLvl1).forEach(function(key) {
|
||||
var shorthand = domLvl1[key];
|
||||
Object.defineProperty(NodePrototype, key, {
|
||||
get: function() {
|
||||
return this[shorthand] || null;
|
||||
},
|
||||
set: function(val) {
|
||||
this[shorthand] = val;
|
||||
return val;
|
||||
}
|
||||
});
|
||||
});
|
||||
34
node_modules/domhandler/package.json
generated
vendored
Normal file
34
node_modules/domhandler/package.json
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "domhandler",
|
||||
"version": "2.4.2",
|
||||
"description": "handler for htmlparser2 that turns pages into a dom",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "tests"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/fb55/DomHandler.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"domelementtype": "1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"htmlparser2": "^3.9.0",
|
||||
"mocha": "^3.0.2",
|
||||
"jshint": "^2.9.1"
|
||||
},
|
||||
"author": "Felix Boehm <me@feedic.com>",
|
||||
"license": "BSD-2-Clause",
|
||||
"jshintConfig": {
|
||||
"quotmark": "double",
|
||||
"trailing": true,
|
||||
"unused": true,
|
||||
"undef": true,
|
||||
"node": true,
|
||||
"proto": true,
|
||||
"globals": {
|
||||
"it": true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user