Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

htmldom

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

htmldom - npm Package Compare versions

Comparing version 0.1.17 to 0.1.18

lib/scanner.js

224

htmldom.js

@@ -1,4 +0,3 @@

var REG = require('./lib/reg');
var Scanner = require('./lib/scanner');
var Parser = require('./lib/parser');
var VOID_ELEMENTS = require('./lib/elements').VOID_ELEMENTS;
var $ = require('./selector/index');

@@ -22,5 +21,9 @@ var esc = require('./lib/escape');

this._str = str.trimRight();
this.dom = [];
this._scanner();
// Scanner html code
var scanner = new Scanner(str, this._escape);
// Parser html dom
var parser = new Parser(scanner.dom);
this.dom = parser.dom;
/**

@@ -37,213 +40,2 @@ * @example

HtmlDom.prototype._scanner = function() {
this._documentType();
while (this._str) {
if (this._comment()) {
continue;
}
if (this._opentag()) {
continue;
}
if (this._closetag()) {
continue;
}
this._text();
}
var parser = new Parser(this.dom);
this.dom = parser.result;
};
// match and update position
HtmlDom.prototype._match = function(reg) {
var match = this._str.match(reg);
if (!match) {
return false;
}
this._str = this._str.slice(match[0].length);
return match;
};
// match comment
HtmlDom.prototype._comment = function() {
var match = this._match(REG.COMMENT_START);
var value;
if (match) {
match = this._match(REG.COMMENT_END);
if (match) {
value = match[1];
} else {
value = this._str;
this._str = '';
}
this.dom.push({
type: 'comment',
value: value,
isIEHack: value.slice(value.length- REG.IE_HACK.length) === REG.IE_HACK
});
return true;
} else {
return false;
}
};
// DOCTYPE Declaration
HtmlDom.prototype._documentType = function() {
while (this._str) {
this._match(REG.WHITESPACE);
if (this._comment()) {
continue;
}
var match = this._match(REG.DOCTYPE);
if (match) {
this.dom.push({
type: 'documentType',
value: match[0]
});
}
return;
}
};
HtmlDom.prototype._attrs = function(name) {
var match = this._match(REG.TAG_CONTENT);
var self = this;
if (this._str[0] === '>') {
this._str = this._str.slice(1);
var attrs = {};
var count = 0;
/**
* attributes syntax
* @example
* Empty attribute: <input disabled>
* Unquoted attribute value: <input value=yes>
* Single-quoted attribute value: <input type='checkbox'>
* Double-quoted attribute value: <input name="be evil">
*/
match[0].replace(REG.ATTR, function(match, key, value) {
if (!self._isServerCode(key)) {
key = key.toLowerCase();
}
if (attrs.hasOwnProperty(key)) {
key += '__' + count++;
}
value = value ? value.replace(REG.TRIM_QUOTES, '') : null;
attrs[key] = value;
});
var dom = {
name: name.toLowerCase(),
attributes: attrs
};
if (VOID_ELEMENTS.indexOf(dom.name) !== -1) {
dom.type = 'voidtag';
} else {
dom.type = 'opentag';
}
this.dom.push(dom);
}
};
// Match open tag
HtmlDom.prototype._opentag = function() {
var match = this._match(REG.RAW_TAG);
if (match) {
var name = match[1];
this._attrs(name);
match = this._match(REG.RAW_TAG_END(name));
if (match) {
match = match[0];
} else {
match = this._str;
this._str = '';
}
this.dom.push({
type: 'text',
value: match
});
return true;
} else if (match = this._match(REG.OPEN_TAG)) {
this._attrs(match[1]);
return true;
} else {
return false;
}
};
HtmlDom.prototype._closetag = function() {
var match = this._match(/^<\/([\w\s]+)>/);
if (match) {
var tagname = match[1];
this.dom.push({
type: 'closetag',
name: tagname.toLowerCase()
});
return true;
} else {
return false;
}
};
HtmlDom.prototype._text = function() {
var match = this._match(REG.WHITESPACE);
var value;
if (match) {
value = match[0];
} else {
value = this._str[0];
this._str = this._str.slice(1);
}
var last = this.dom[this.dom.length - 1];
if (last) {
if (last.type === 'text') {
last.value += value;
} else {
this.dom.push({
type: 'text',
value: value
});
}
} else {
this.dom = [{
type: 'text',
value: value
}];
}
};
HtmlDom.prototype._isServerCode = function(code) {
for (var i = 0; i < this._escape.length; i++) {
var reg = new RegExp(this._escape[i]);
if (reg.test(code)) {
return true;
}
}
return false;
};
HtmlDom.prototype._unescape = function(html, callback) {

@@ -250,0 +42,0 @@ function replace(match) {

@@ -57,3 +57,3 @@ var CHILD = require('./elements').CHILD;

this.result = doc;
this.dom = doc;
};

@@ -127,2 +127,3 @@

}
while (prev) {

@@ -129,0 +130,0 @@ var optional = OPTIONAL_TAGS[prev.name];

{
"name": "htmldom",
"version": "0.1.17",
"version": "0.1.18",
"description": "Simplified html handle in nodejs",

@@ -5,0 +5,0 @@ "main": "htmldom.js",

@@ -12,5 +12,5 @@ var Parser = require('../../lib/parser');

assert.equal(parser.result.children[0].type, 'text');
assert.equal(parser.result.children[1].type, 'comment');
assert.equal(parser.result.children[2].type, 'tag');
assert.equal(parser.dom.children[0].type, 'text');
assert.equal(parser.dom.children[1].type, 'comment');
assert.equal(parser.dom.children[2].type, 'tag');
});

@@ -27,3 +27,3 @@

assert.equal(parser.result.children[0].children[0].children[0].name, 'div');
assert.equal(parser.dom.children[0].children[0].children[0].name, 'div');
});

@@ -38,6 +38,6 @@

assert.equal(parser.result.children[0].name, 'img');
assert.equal(parser.result.children[1].name, 'input');
assert.equal(parser.result.children[2].name, 'h3');
assert.equal(parser.dom.children[0].name, 'img');
assert.equal(parser.dom.children[1].name, 'input');
assert.equal(parser.dom.children[2].name, 'h3');
});
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc