mirror of
https://github.com/danbulant/node-html-parser
synced 2026-05-19 20:38:46 +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
15
src/index.ts
15
src/index.ts
|
|
@ -219,7 +219,7 @@ export class HTMLElement extends Node {
|
|||
currentBlock.push(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dfs(this);
|
||||
return blocks
|
||||
.map(function (block) {
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
12
test/html.js
12
test/html.js
|
|
@ -129,7 +129,7 @@ describe('HTML Parser', function () {
|
|||
it('should parse picture element', function () {
|
||||
|
||||
var root = parseHTML('<picture><source srcset="/images/example-1.jpg 1200w, /images/example-2.jpg 1600w" sizes="100vw"><img src="/images/example.jpg" alt="Example"/></picture>');
|
||||
|
||||
|
||||
var picture = new HTMLElement('picture', {}, '');
|
||||
var source = picture.appendChild(new HTMLElement('source', {}, 'srcset="/images/example-1.jpg 1200w, /images/example-2.jpg 1600w" sizes="100vw"'));
|
||||
var img = picture.appendChild(new HTMLElement('img', {}, 'src="/images/example.jpg" alt="Example"'));
|
||||
|
|
@ -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