Skip to content

Commit

Permalink
Merge pull request #812 from linghaoSu/task/811-svg-ownerSVGElement
Browse files Browse the repository at this point in the history
#811@patch: Fix ownerSVGElement insteadof inifite loop.
  • Loading branch information
capricorn86 committed Apr 4, 2023
2 parents 5d35044 + 22d6fde commit fa2d574
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/nodes/svg-element/SVGElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export default class SVGElement extends Element implements ISVGElement {
* @returns Element.
*/
public get ownerSVGElement(): ISVGSVGElement {
const parent = this.parentNode;
let parent = this.parentNode;
while (parent) {
if (parent['tagName'] === 'SVG') {
return <ISVGSVGElement>parent;
}

parent = parent.parentNode;
}
return null;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/happy-dom/test/nodes/svg-element/SVGElement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Window from '../../../src/window/Window';
import Document from '../../../src/nodes/document/Document';
import SVGSVGElement from '../../../src/nodes/svg-element/SVGSVGElement';
import NamespaceURI from '../../../src/config/NamespaceURI';
import SVGElement from '../../../src/nodes/svg-element/SVGElement';

describe('SVGElement', () => {
let window: Window;
let document: Document;
let element: SVGSVGElement;
let line: SVGElement;

beforeEach(() => {
window = new Window();
document = window.document;
element = <SVGSVGElement>document.createElementNS(NamespaceURI.svg, 'svg');
line = <SVGSVGElement>document.createElementNS(NamespaceURI.svg, 'line');
});

describe('get ownerSVGElement()', () => {
it('Returns svg element when append to some svg.', () => {
element.append(line);
const ownerSVG = line.ownerSVGElement;
expect(ownerSVG).toBe(element);
});

it('Returns null when dangling.', () => {
const ownerSVG = line.ownerSVGElement;
expect(ownerSVG).toBe(null);
});
});
});

0 comments on commit fa2d574

Please sign in to comment.