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
11
src/index.ts
11
src/index.ts
|
|
@ -353,11 +353,12 @@ export class HTMLElement extends Node {
|
||||||
} else {
|
} else {
|
||||||
if (selector.includes(',')) {
|
if (selector.includes(',')) {
|
||||||
const selectors = selector.split(',') as string[];
|
const selectors = selector.split(',') as string[];
|
||||||
let result = [] as HTMLElement[];
|
return Array.from(selectors.reduce((pre, cur) => {
|
||||||
selectors.forEach((s) => {
|
const result = this.querySelectorAll(cur.trim()) as HTMLElement[];
|
||||||
result = result.concat(this.querySelectorAll(s.trim()));
|
return result.reduce((p, c) => {
|
||||||
});
|
return p.add(c);
|
||||||
return result;
|
}, pre);
|
||||||
|
}, new Set<HTMLElement>()));
|
||||||
}
|
}
|
||||||
matcher = new Matcher(selector);
|
matcher = new Matcher(selector);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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 span').should.eql(root.firstChild.firstChild.childNodes);
|
||||||
root.querySelectorAll('#id, #id .b').should.eql([root.firstChild, root.firstChild.firstChild.firstChild]);
|
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 () {
|
describe('#structuredText', function () {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue