Skip to content

Commit

Permalink
capricorn86#633@patch: Fix getElementByTagName doesn't support wildca…
Browse files Browse the repository at this point in the history
…rd searches (improves jQuery support).
  • Loading branch information
IGx89 committed Oct 23, 2022
1 parent 42c92a6 commit 1e3d0df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
Expand Up @@ -97,9 +97,10 @@ export default class ParentNodeUtility {
): IHTMLCollection<IElement> {
const upperTagName = tagName.toUpperCase();
const matches = HTMLCollectionFactory.create();
const includeAll = tagName === '*';

for (const child of parentNode.children) {
if (child.tagName === upperTagName) {
if (includeAll || child.tagName === upperTagName) {
matches.push(child);
}
for (const match of this.getElementsByTagName(<IElement>child, tagName)) {
Expand All @@ -125,9 +126,10 @@ export default class ParentNodeUtility {
): IHTMLCollection<IElement> {
const upperTagName = tagName.toUpperCase();
const matches = HTMLCollectionFactory.create();
const includeAll = tagName === '*';

for (const child of parentNode.children) {
if (child.tagName === upperTagName && child.namespaceURI === namespaceURI) {
if ((includeAll || child.tagName === upperTagName) && child.namespaceURI === namespaceURI) {
matches.push(child);
}
for (const match of this.getElementsByTagNameNS(<IElement>child, namespaceURI, tagName)) {
Expand Down
Expand Up @@ -155,6 +155,27 @@ describe('ParentNodeUtility', () => {
div4
]);
});

it('Returns all elements when tag name is *.', () => {
const parent = document.createElement('div');
const div1 = document.createElement('div');
const div2 = document.createElement('div');
const div3 = document.createElement('div');
const div4 = document.createElement('div');
const span1 = document.createElement('span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

parent.appendChild(div1);
div1.appendChild(div2);
div2.appendChild(span1);
span1.appendChild(div3);
div3.appendChild(span2);
div3.appendChild(span3);
span3.appendChild(div4);

expect(ParentNodeUtility.getElementsByTagName(parent, '*').length).toEqual(7);
});
});

describe('getElementsByTagNameNS()', () => {
Expand All @@ -164,7 +185,7 @@ describe('ParentNodeUtility', () => {
const div2 = document.createElement('div');
const div3 = document.createElementNS(NamespaceURI.svg, 'div');
const div4 = document.createElement('div');
const span1 = document.createElement('span');
const span1 = document.createElementNS(NamespaceURI.svg, 'span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

Expand All @@ -181,6 +202,29 @@ describe('ParentNodeUtility', () => {
div3
]);
});

it('Returns all elements when tag name is *.', () => {
const parent = document.createElement('div');
const div1 = document.createElementNS(NamespaceURI.svg, 'div');
const div2 = document.createElement('div');
const div3 = document.createElementNS(NamespaceURI.svg, 'div');
const div4 = document.createElement('div');
const span1 = document.createElementNS(NamespaceURI.svg, 'span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

parent.appendChild(div1);
div1.appendChild(div2);
div2.appendChild(span1);
span1.appendChild(div3);
div3.appendChild(span2);
div3.appendChild(span3);
span3.appendChild(div4);

expect(
ParentNodeUtility.getElementsByTagNameNS(parent, NamespaceURI.svg, '*').length
).toEqual(3);
});
});

describe('getElementByTagName()', () => {
Expand Down

0 comments on commit 1e3d0df

Please sign in to comment.