mirror of
https://github.com/danbulant/node-html-parser
synced 2026-05-24 12:35:10 +00:00
🐛 issue #35
This commit is contained in:
parent
9a48e032f1
commit
30a5701544
3 changed files with 44517 additions and 4 deletions
|
|
@ -286,11 +286,16 @@ export default class HTMLElement extends Node {
|
||||||
}
|
}
|
||||||
matcher = new Matcher(selector);
|
matcher = new Matcher(selector);
|
||||||
}
|
}
|
||||||
const stack = [] as { 0: Node; 1: 0 | 1; 2: boolean }[];
|
interface IStack {
|
||||||
|
0: Node; // node
|
||||||
|
1: number; // children
|
||||||
|
2: boolean; // found flag
|
||||||
|
}
|
||||||
|
const stack = [] as IStack[];
|
||||||
return this.childNodes.reduce((res, cur) => {
|
return this.childNodes.reduce((res, cur) => {
|
||||||
stack.push([cur, 0, false]);
|
stack.push([cur, 0, false]);
|
||||||
while (stack.length) {
|
while (stack.length) {
|
||||||
const state = arr_back(stack);
|
const state = arr_back(stack); // get last element
|
||||||
const el = state[0];
|
const el = state[0];
|
||||||
if (state[1] === 0) {
|
if (state[1] === 0) {
|
||||||
// Seen for first time.
|
// Seen for first time.
|
||||||
|
|
@ -298,10 +303,12 @@ export default class HTMLElement extends Node {
|
||||||
stack.pop();
|
stack.pop();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
state[2] = matcher.advance(el as HTMLElement);
|
const html_el = el as HTMLElement;
|
||||||
|
state[2] = matcher.advance(html_el);
|
||||||
if (state[2]) {
|
if (state[2]) {
|
||||||
if (matcher.matched) {
|
if (matcher.matched) {
|
||||||
res.push(el as HTMLElement);
|
res.push(html_el);
|
||||||
|
res.push(...(html_el.querySelectorAll(selector)));
|
||||||
// no need to go further.
|
// no need to go further.
|
||||||
matcher.rewind();
|
matcher.rewind();
|
||||||
stack.pop();
|
stack.pop();
|
||||||
|
|
|
||||||
|
|
@ -207,6 +207,13 @@ describe('HTML Parser', function () {
|
||||||
const root = parseHTML(fs.readFileSync(__dirname + '/html/incomplete-script').toString());
|
const root = parseHTML(fs.readFileSync(__dirname + '/html/incomplete-script').toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should parse talble currect', function () {
|
||||||
|
const root = parseHTML(fs.readFileSync(__dirname + '/html/tables.html').toString(), {
|
||||||
|
script: true
|
||||||
|
});
|
||||||
|
const tables = root.querySelectorAll('table');
|
||||||
|
tables.length.should.eql(3176);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('parseWithValidation', function () {
|
describe('parseWithValidation', function () {
|
||||||
|
|
|
||||||
44499
test/html/tables.html
Normal file
44499
test/html/tables.html
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue