Socket
Socket
Sign inDemoInstall

htmlparser2

Package Overview
Dependencies
0
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0 to 2.0.1

tests/HTML/21-conditional_comments.js

8

lib/_FeedHandler.js

@@ -71,2 +71,6 @@ // NOT FINISHED YET! DON'T USE IT!

var RssItemMap = {
};
var AtomFeedMap = {

@@ -81,2 +85,6 @@ id: "id",

var AtomItemMap = {
};
//TODO: make this a trully streamable handler

@@ -83,0 +91,0 @@ function FeedHandler(callback, onitem){

46

lib/DefaultHandler.js

@@ -68,5 +68,9 @@ var ElementType = require("./ElementType.js");

type: type,
name: name,
attribs: attribs
name: name
};
//for some reason, an if doesn't work
for(var i in attribs){
element.attribs = attribs;
break;
}
this._addDomElement(element);

@@ -86,24 +90,24 @@ this._tagStack.push(element);

var lastTag = this._tagStack[this._tagStack.length - 1];
var lastChild = lastTag && lastTag.children && lastTag.children[lastTag.children.length - 1];
var element;
if(!lastChild || lastChild.type !== ElementType.Comment){
element = {
data: data,
type: ElementType.Comment
};
if(!lastTag){
return this.dom.push(element);
} else if(!lastChild){
lastTag.children = [element];
} else {
if(lastChild.type !== ElementType.Comment){
lastTag.children.push(element);
}
}
} else {
lastChild.data += data;
if(lastTag && lastTag.type === ElementType.Comment){
lastTag.data += data;
return;
}
var element = {
data: data,
type: ElementType.Comment
};
if(!lastTag) this.dom.push(element);
else if(!lastTag.children) lastTag.children = [element];
else lastTag.children.push(element);
this._tagStack.push(element);
};
DefaultHandler.prototype.oncommentend = function(){
this._tagStack.pop();
};
DefaultHandler.prototype.onprocessinginstruction = function(name, data){

@@ -110,0 +114,0 @@ this._addDomElement({

//Types of elements found in the DOM
module.exports = {
Text: "text", /*Plain text*/
Directive: "directive", /*Special tag <!...>*/
Comment: "comment", /*Special tag <!--...-->*/
Script: "script", /*Special tag <script>...</script>*/
Style: "style", /*Special tag <style>...</style>*/
Tag: "tag" /*Any tag that isn't special*/
Text: "text", //Plain text
Directive: "directive", //Special tag <!...>
Comment: "comment", //Special tag <!--...-->
Script: "script", //Special tag <script>...</script>
Style: "style", //Special tag <style>...</style>
Tag: "tag" //Any tag that isn't special
};

@@ -214,7 +214,6 @@ var ElementType = require("./ElementType.js");

this._contentFlags %= SpecialTags.w;
rawData = rawData.slice(0, -2);
if(this._cbs.oncomment) this._cbs.oncomment(rawData.slice(0, -2));
if(this._cbs.oncommentend) this._cbs.oncommentend();
}
else rawData += tagSep;
if(this._cbs.oncomment) this._cbs.oncomment(rawData);
else if(this._cbs.oncomment) this._cbs.oncomment(rawData + tagSep);
};

@@ -221,0 +220,0 @@

{
"name": "htmlparser2",
"description": "Forgiving HTML/XML/RSS Parser for Node. This version is optimised and cleaned and provides a SAX interface.",
"version": "2.0.0",
"version": "2.0.1",
"author": "Felix Boehm <me@feedic.com>",

@@ -6,0 +6,0 @@ "contributors": ["Chris Winberry <chris@winberry.net>"],

@@ -23,49 +23,58 @@ #htmlparser2

var htmlparser = require("htmlparser");
var rawHtml = "Xyz <script language= javascript>var foo = '<<bar>>';< / script><!--<!-- Waah! -- -->";
var handler = new htmlparser.DefaultHandler(function (error, dom) {
if (error)
[...do something for errors...]
else
[...parsing done, do something...]
console.log(dom);
});
var parser = new htmlparser.Parser(handler);
parser.write(rawHtml);
parser.done();
```javascript
var htmlparser = require("htmlparser");
var rawHtml = "Xyz <script language= javascript>var foo = '<<bar>>';< / script><!--<!-- Waah! -- -->";
var handler = new htmlparser.DefaultHandler(function (error, dom) {
if (error)
[...do something for errors...]
else
[...parsing done, do something...]
console.log(dom);
});
var parser = new htmlparser.Parser(handler);
parser.write(rawHtml);
parser.done();
```
Output:
[{
data: 'Xyz ',
type: 'text'
}, {
type: 'script',
name: 'script',
attribs: {
language: 'javascript'
},
children: [{
data: 'var foo = \'<bar>\';<',
type: 'text'
}]
}, {
data: '<!-- Waah! -- ',
type: 'comment'
}]
```javascript
[{
data: 'Xyz ',
type: 'text'
}, {
type: 'script',
name: 'script',
attribs: {
language: 'javascript'
},
children: [{
data: 'var foo = \'<bar>\';<',
type: 'text'
}]
}, {
data: '<!-- Waah! -- ',
type: 'comment'
}]
```
##Streaming To Parser
while (...) {
...
parser.write(chunk);
}
parser.done();
```javascript
while (...) {
...
parser.write(chunk);
}
parser.done();
```
##Parsing RSS/RDF/Atom Feeds
new htmlparser.FeedHandler(function (error, dom) {
...
});
```javascript
new htmlparser.FeedHandler(function (error, feed) {
...
});
```
##Further reading
* [Parser options](https://github.com/FB55/node-htmlparser/wiki/Parser-options)
* [DefaultHandler options](https://github.com/FB55/node-htmlparser/wiki/DefaultHandler-options)
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