Skip to content

Commit 6ad6dcd

Browse files
OriRgaearon
authored andcommitted
Clear previous children when SVG node doesn't have innerHTML (#11108)
* clear previous children when SVG node doesn't have innerHTML * remove 'get' handler for appendChild in test node proxy
1 parent 3019210 commit 6ad6dcd

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/renderers/dom/shared/setInnerHTML.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var setInnerHTML = createMicrosoftUnsafeLocalFunction(function(node, html) {
3131
reusableSVGContainer || document.createElement('div');
3232
reusableSVGContainer.innerHTML = '<svg>' + html + '</svg>';
3333
var svgNode = reusableSVGContainer.firstChild;
34+
while (node.firstChild) {
35+
node.removeChild(node.firstChild);
36+
}
3437
while (svgNode.firstChild) {
3538
node.appendChild(svgNode.firstChild);
3639
}

src/renderers/dom/shared/utils/__tests__/setInnerHTML-test.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,25 @@ describe('setInnerHTML', () => {
2424
});
2525

2626
describe('when the node does not have an innerHTML property', () => {
27-
// Disabled. JSDOM doesn't seem to remove nodes when using appendChild to
28-
// move existing nodes.
29-
xit('sets innerHTML on it', () => {
27+
var node;
28+
var nodeProxy;
29+
beforeEach(() => {
3030
// Create a mock node that looks like an SVG in IE (without innerHTML)
31-
var node = {
32-
namespaceURI: Namespaces.svg,
33-
appendChild: jasmine.createSpy(),
34-
};
31+
node = document.createElementNS(Namespaces.svg, 'svg');
3532

33+
nodeProxy = new Proxy(node, {
34+
has: (target, prop) => {
35+
return prop === 'innerHTML' ? false : prop in target;
36+
},
37+
});
38+
39+
spyOn(node, 'appendChild').and.callThrough();
40+
spyOn(node, 'removeChild').and.callThrough();
41+
});
42+
43+
it('sets innerHTML on it', () => {
3644
var html = '<circle></circle><rect></rect>';
37-
setInnerHTML(node, html);
45+
setInnerHTML(nodeProxy, html);
3846

3947
expect(node.appendChild.calls.argsFor(0)[0].outerHTML).toBe(
4048
'<circle></circle>',
@@ -43,5 +51,18 @@ describe('setInnerHTML', () => {
4351
'<rect></rect>',
4452
);
4553
});
54+
55+
it('clears previous children', () => {
56+
var firstHtml = '<rect></rect>';
57+
var secondHtml = '<circle></circle>';
58+
setInnerHTML(nodeProxy, firstHtml);
59+
60+
setInnerHTML(nodeProxy, secondHtml);
61+
62+
expect(node.removeChild.calls.argsFor(0)[0].outerHTML).toBe(
63+
'<rect></rect>',
64+
);
65+
expect(node.innerHTML).toBe('<circle></circle>');
66+
});
4667
});
4768
});

0 commit comments

Comments
 (0)