remove duplicated items in querySelectAll

This commit is contained in:
taoqf 2020-01-26 21:57:27 +08:00
parent c145442179
commit 881d708e72
2 changed files with 19 additions and 8 deletions

View file

@ -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);
} }

View file

@ -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 () {