mirror of
https://github.com/danbulant/node-html-parser
synced 2026-05-24 12:35:10 +00:00
remove duplicated items in querySelectAll
This commit is contained in:
parent
c145442179
commit
881d708e72
2 changed files with 19 additions and 8 deletions
13
src/index.ts
13
src/index.ts
|
|
@ -353,11 +353,12 @@ export class HTMLElement extends Node {
|
|||
} else {
|
||||
if (selector.includes(',')) {
|
||||
const selectors = selector.split(',') as string[];
|
||||
let result = [] as HTMLElement[];
|
||||
selectors.forEach((s) => {
|
||||
result = result.concat(this.querySelectorAll(s.trim()));
|
||||
});
|
||||
return result;
|
||||
return Array.from(selectors.reduce((pre, cur) => {
|
||||
const result = this.querySelectorAll(cur.trim()) as HTMLElement[];
|
||||
return result.reduce((p, c) => {
|
||||
return p.add(c);
|
||||
}, pre);
|
||||
}, new Set<HTMLElement>()));
|
||||
}
|
||||
matcher = new Matcher(selector);
|
||||
}
|
||||
|
|
@ -801,7 +802,7 @@ export function parse(data: string, options?: {
|
|||
// this is a comment
|
||||
if (options.comment) {
|
||||
// Only keep what is in between <!-- and -->
|
||||
const text = data.substring(lastTextPos - 3 , lastTextPos - match[0].length + 4);
|
||||
const text = data.substring(lastTextPos - 3, lastTextPos - match[0].length + 4);
|
||||
currentParent.appendChild(new CommentNode(text));
|
||||
}
|
||||
continue;
|
||||
|
|
|
|||
10
test/html.js
10
test/html.js
|
|
@ -341,6 +341,16 @@ describe('HTML Parser', function () {
|
|||
root.querySelectorAll('#id span').should.eql(root.firstChild.firstChild.childNodes);
|
||||
root.querySelectorAll('#id, #id .b').should.eql([root.firstChild, root.firstChild.firstChild.firstChild]);
|
||||
});
|
||||
it('should return just one element', function () {
|
||||
var root = parseHTML('<time class="date">');
|
||||
root.querySelectorAll('time,.date').should.eql([root.firstChild]);
|
||||
});
|
||||
it('should return elements in order', function () {
|
||||
var root = parseHTML('<img src=""><p>hello</p>');
|
||||
var results = root.querySelectorAll('p,img');
|
||||
results[0].rawText.should.eql('hello');
|
||||
results[1].should.eql(root.firstChild);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#structuredText', function () {
|
||||
|
|
|
|||
Loading…
Reference in a new issue