node-html-parser
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -457,11 +457,27 @@ "use strict"; | ||
const classes = parts.slice(1).sort(); | ||
let source = ''; | ||
let source = '"use strict";'; | ||
if (tagName && tagName != '*') { | ||
if (tagName[0] == '#') | ||
let matcher; | ||
if (tagName[0] == '#') { | ||
source += 'if (el.id != ' + JSON.stringify(tagName.substr(1)) + ') return false;'; | ||
else | ||
} | ||
else if (matcher = tagName.match(/^\[\s*(\S+)\s*(=|!=)\s*((((["'])([^\6]*)\6))|(\S*?))\]\s*/)) { | ||
const attr_key = matcher[1]; | ||
let method = matcher[2]; | ||
if (method !== '=' && method !== '!=') { | ||
throw new Error('Selector not supported, Expect [key${op}value].op must be =,!='); | ||
} | ||
if (method === '=') { | ||
method = '=='; | ||
} | ||
const value = matcher[7] || matcher[8]; | ||
source += `const attrs = el.attributes;for (const key in attrs){const val = attrs[key]; if (key == "${attr_key}" && val ${method} "${value}"){return true;}} return false;`; | ||
} | ||
else { | ||
source += 'if (el.tagName != ' + JSON.stringify(tagName) + ') return false;'; | ||
} | ||
} | ||
if (classes.length > 0) | ||
if (classes.length > 0) { | ||
source += 'for (var cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;'; | ||
} | ||
source += 'return true;'; | ||
@@ -513,3 +529,3 @@ return pMatchFunctionCache[matcher] = new Function('el', source); | ||
const kMarkupPattern = /<!--[^]*?(?=-->)-->|<(\/?)([a-z][a-z0-9]*)\s*([^>]*?)(\/?)>/ig; | ||
const kAttributePattern = /\b(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig; | ||
const kAttributePattern = /(^|\s)(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig; | ||
const kSelfClosingElements = { | ||
@@ -577,3 +593,3 @@ meta: true, | ||
for (var attMatch; attMatch = kAttributePattern.exec(match[3]);) | ||
attrs[attMatch[1]] = attMatch[3] || attMatch[4] || attMatch[5]; | ||
attrs[attMatch[2]] = attMatch[4] || attMatch[5] || attMatch[6]; | ||
// console.log(attrs); | ||
@@ -580,0 +596,0 @@ if (!match[4] && kElementsClosedByOpening[currentParent.tagName]) { |
@@ -466,11 +466,27 @@ (function (factory) { | ||
const classes = parts.slice(1).sort(); | ||
let source = ''; | ||
let source = '"use strict";'; | ||
if (tagName && tagName != '*') { | ||
if (tagName[0] == '#') | ||
let matcher; | ||
if (tagName[0] == '#') { | ||
source += 'if (el.id != ' + JSON.stringify(tagName.substr(1)) + ') return false;'; | ||
else | ||
} | ||
else if (matcher = tagName.match(/^\[\s*(\S+)\s*(=|!=)\s*((((["'])([^\6]*)\6))|(\S*?))\]\s*/)) { | ||
const attr_key = matcher[1]; | ||
let method = matcher[2]; | ||
if (method !== '=' && method !== '!=') { | ||
throw new Error('Selector not supported, Expect [key${op}value].op must be =,!='); | ||
} | ||
if (method === '=') { | ||
method = '=='; | ||
} | ||
const value = matcher[7] || matcher[8]; | ||
source += `const attrs = el.attributes;for (const key in attrs){const val = attrs[key]; if (key == "${attr_key}" && val ${method} "${value}"){return true;}} return false;`; | ||
} | ||
else { | ||
source += 'if (el.tagName != ' + JSON.stringify(tagName) + ') return false;'; | ||
} | ||
} | ||
if (classes.length > 0) | ||
if (classes.length > 0) { | ||
source += 'for (var cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;'; | ||
} | ||
source += 'return true;'; | ||
@@ -522,3 +538,3 @@ return pMatchFunctionCache[matcher] = new Function('el', source); | ||
const kMarkupPattern = /<!--[^]*?(?=-->)-->|<(\/?)([a-z][a-z0-9]*)\s*([^>]*?)(\/?)>/ig; | ||
const kAttributePattern = /\b(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig; | ||
const kAttributePattern = /(^|\s)(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig; | ||
const kSelfClosingElements = { | ||
@@ -586,3 +602,3 @@ meta: true, | ||
for (var attMatch; attMatch = kAttributePattern.exec(match[3]);) | ||
attrs[attMatch[1]] = attMatch[3] || attMatch[4] || attMatch[5]; | ||
attrs[attMatch[2]] = attMatch[4] || attMatch[5] || attMatch[6]; | ||
// console.log(attrs); | ||
@@ -589,0 +605,0 @@ if (!match[4] && kElementsClosedByOpening[currentParent.tagName]) { |
{ | ||
"name": "node-html-parser", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.", | ||
@@ -11,3 +11,3 @@ "main": "dist/index.js", | ||
"pretest": "gulp", | ||
"prepublish": "gulp" | ||
"prepare": "gulp" | ||
}, | ||
@@ -14,0 +14,0 @@ "keywords": [ |
@@ -14,5 +14,3 @@ var should = require('should'); | ||
describe('Matcher', function () { | ||
it('should match corrent elements', function () { | ||
var matcher = new Matcher('#id .a a.b *.a.b .a.b * a'); | ||
@@ -59,5 +57,3 @@ var MatchesNothingButStarEl = new HTMLElement('_', {}); | ||
matcher.matched.should.be.ok; | ||
}); | ||
}); | ||
@@ -68,3 +64,2 @@ | ||
describe('parse()', function () { | ||
it('should parse "<p id=\\"id\\"><a class=\'cls\'>Hello</a><ul><li><li></ul><span></span></p>" and return root element', function () { | ||
@@ -83,3 +78,2 @@ | ||
root.firstChild.should.eql(p); | ||
}); | ||
@@ -167,3 +161,2 @@ | ||
describe('TextNode', function () { | ||
describe('#isWhitespace', function () { | ||
@@ -177,3 +170,2 @@ var node = new TextNode(''); | ||
}); | ||
}); | ||
@@ -184,5 +176,3 @@ | ||
describe('#removeWhitespace()', function () { | ||
it('should remove whitespaces while preserving nodes with content', function () { | ||
var root = parseHTML('<p> \r \n \t <h5> 123 </h5></p>'); | ||
@@ -195,13 +185,8 @@ | ||
root.firstChild.removeWhitespace().should.eql(p); | ||
}); | ||
}); | ||
describe('#rawAttributes', function () { | ||
it('should return escaped attributes of the element', function () { | ||
var root = parseHTML('<p a=12 data-id="!$$&" yAz=\'1\'></p>'); | ||
root.firstChild.rawAttributes.should.eql({ | ||
@@ -212,13 +197,8 @@ 'a': '12', | ||
}); | ||
}); | ||
}); | ||
describe('#attributes', function () { | ||
it('should return attributes of the element', function () { | ||
var root = parseHTML('<p a=12 data-id="!$$&" yAz=\'1\'></p>'); | ||
root.firstChild.attributes.should.eql({ | ||
@@ -229,13 +209,22 @@ 'a': '12', | ||
}); | ||
}); | ||
}); | ||
describe('#querySelector()', function () { | ||
it('should return correct elements in DOM tree', function () { | ||
var root = parseHTML('<a id="id" data-id="myid"><div><span class="a b"></span><span></span><span></span></div></a>'); | ||
root.querySelector('#id').should.eql(root.firstChild); | ||
root.querySelector('span.a').should.eql(root.firstChild.firstChild.firstChild); | ||
root.querySelector('span.b').should.eql(root.firstChild.firstChild.firstChild); | ||
root.querySelector('span.a.b').should.eql(root.firstChild.firstChild.firstChild); | ||
root.querySelector('#id .b').should.eql(root.firstChild.firstChild.firstChild); | ||
root.querySelector('#id span').should.eql(root.firstChild.firstChild.firstChild); | ||
root.querySelector('[data-id=myid]').should.eql(root.firstChild); | ||
root.querySelector('[data-id="myid"]').should.eql(root.firstChild); | ||
}); | ||
}); | ||
describe('#querySelectorAll()', function () { | ||
it('should return correct elements in DOM tree', function () { | ||
var root = parseHTML('<a id="id"><div><span class="a b"></span><span></span><span></span></div></a>'); | ||
root.querySelectorAll('#id').should.eql([root.firstChild]); | ||
@@ -247,19 +236,12 @@ root.querySelectorAll('span.a').should.eql([root.firstChild.firstChild.firstChild]); | ||
root.querySelectorAll('#id span').should.eql(root.firstChild.firstChild.childNodes); | ||
}); | ||
}); | ||
describe('#structuredText', function () { | ||
it('should return correct structured text', function () { | ||
var root = parseHTML('<span>o<p>a</p><p>b</p>c</span>'); | ||
root.structuredText.should.eql('o\na\nb\nc'); | ||
}); | ||
}); | ||
describe('#set_content', function () { | ||
it('set content string', function () { | ||
@@ -285,5 +267,3 @@ var root = parseHTML('<div></div>'); | ||
}); | ||
}); | ||
}); | ||
@@ -290,0 +270,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151205
1932