Socket
Socket
Sign inDemoInstall

domhandler

Package Overview
Dependencies
0
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

.travis.yml

79

lib/handler.js

@@ -1,6 +0,8 @@

var ElementType = require("./ElementType.js");
var ElementType = require("./ElementType.js"),
DomUtils = require("./utils.js");
function DomHandler(callback, options){
function DomHandler(callback, options, elementCB){
this._options = typeof callback === "object" ? callback : options || defaultOpts;
this._callback = typeof callback === "object" ? null : callback;
this._elementCB = elementCB;
this.dom = [];

@@ -13,4 +15,3 @@ this._done = false;

var defaultOpts = {
ignoreWhitespace: false, //Keep whitespace-only text nodes
refParent: false //add a reference to the elements parent node
ignoreWhitespace: false //Keep whitespace-only text nodes
};

@@ -20,3 +21,3 @@

DomHandler.prototype.onreset = function(){
DomHandler.call(this, this._callback, this._options);
DomHandler.call(this, this._callback, this._options, this._elementCB);
};

@@ -42,27 +43,31 @@

//if(this._tagStack.pop().name !== name) this._handleCallback(Error("Tagname didn't match!"));
this._tagStack.pop();
var elem = this._tagStack.pop();
if(this._elementCB) this._elementCB(elem);
};
DomHandler.prototype._addDomElement = function(element){
var lastChild,
lastTag = this._tagStack[this._tagStack.length - 1];
if(lastTag){ //There are parent elements
if(this._options.refParent){
element.parent = lastTag;
var lastTag = element.parent = this._tagStack[this._tagStack.length - 1];
if(!lastTag){ //There aren't parent elements
this.dom.push(element);
return;
}
if(lastTag.children === null){
lastTag.children = [element];
return;
}
if(DomUtils.isTag(element)){
var idx = lastTag.children.length;
while(idx > 0){
if(DomUtils.isTag(lastTag.children[--idx])){
element.prev = lastTag.children[idx];
lastTag.children[idx].next = element;
break;
}
}
if(lastTag.children === null){
lastTag.children = [element];
return;
}
lastChild = lastTag.children[lastTag.children.length - 1];
if(element.type === ElementType.Text && lastChild.type === ElementType.Text){
lastChild.data += element.data;
} else {
lastTag.children.push(element);
}
}
else {
this.dom.push(element);
}
lastTag.children.push(element);
};

@@ -75,3 +80,5 @@

attribs: attribs,
children: null
children: null,
prev: null,
next: null
};

@@ -84,2 +91,15 @@ this._addDomElement(element);

if(this._options.ignoreWhitespace && data.trim() === "") return;
var lastTag;
if(
(lastTag = this._tagStack[this._tagStack.length - 1]) &&
(lastTag = lastTag.children) &&
(lastTag = lastTag[lastTag.length - 1]) &&
lastTag.type === ElementType.Text
){
lastTag.data += data;
return;
}
this._addDomElement({

@@ -101,9 +121,6 @@ data: data,

data: data,
type: ElementType.Comment
type: ElementType.Comment
};
if(!lastTag) this.dom.push(element);
else if(!lastTag.children) lastTag.children = [element];
else lastTag.children.push(element);
this._addDomElement(element);
this._tagStack.push(element);

@@ -110,0 +127,0 @@ };

@@ -104,2 +104,9 @@ var ElementType = require("./ElementType.js"),

DomUtils.removeElement = function(elem){
if(elem.prev) elem.prev.next = elem.next;
if(elem.next) elem.next.prev = elem.prev;
elem.parent.children.splice(elem.parent.children.indexOf(elem), 1);
};
DomUtils.getInnerHTML = function(elem){

@@ -113,3 +120,3 @@ if(!elem.children) return "";

for(var i = 0; i < childNum; i++){
ret += this.getOuterHTML(childs[i]);
ret += DomUtils.getOuterHTML(childs[i]);
}

@@ -121,4 +128,3 @@

DomUtils.getOuterHTML = function(elem){
var type = elem.type,
name = elem.name;
var type = elem.type;

@@ -129,4 +135,4 @@ if(type === ElementType.Text) return elem.data;

var ret = "<" + name;
if(elem.hasOwnProperty("attribs")){
var ret = "<" + elem.name;
if("attribs" in elem){
for(var attr in elem.attribs){

@@ -143,3 +149,3 @@ if(elem.attribs.hasOwnProperty(attr)){

return ret + this.getInnerHTML(elem) + "</" + name + ">";
return ret + DomUtils.getInnerHTML(elem) + "</" + elem.name + ">";
};
{
"name": "domhandler",
"version": "1.1.0",
"version": "1.2.0",
"description": "htmlparser2's dom as a separate module",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -1,2 +0,2 @@

#DOMHandler
#DOMHandler [![Build Status](https://secure.travis-ci.org/fb55/DomHandler.png)](http://travis-ci.org/fb55/DomHandler)

@@ -3,0 +3,0 @@ The DOM handler (formally known as DefaultHandler) creates a tree containing all nodes of a page. The tree may be manipulated using the DOMUtils library.

@@ -7,2 +7,19 @@ var fs = require("fs"),

function compare(expected, result){
if(typeof expected !== typeof result){
throw Error("types didn't match");
}
if(typeof expected !== "object" || expected === null){
if(expected !== result){
throw Error("result doesn't equal expected");
}
return;
}
for(var prop in expected){
if(!(prop in result)) throw Error("result didn't contain property " + prop);
compare(expected[prop], result[prop]);
}
}
function runTests(test){

@@ -27,3 +44,3 @@ //read files, load them, run them

assert.ifError(err);
assert.deepEqual(file.expected, dom, "didn't get expected output");
compare(file.expected, dom);

@@ -30,0 +47,0 @@ if(second){

//generate a dom
var handler = new (require("../lib/Handler.js"))();
var handler = new (require("../lib/handler.js"))();

@@ -4,0 +4,0 @@ (new (require("htmlparser2").Parser)(handler)).parseComplete(

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc