Skip to content

Commit

Permalink
Clear previous children when SVG node doesn't have innerHTML (#11108)
Browse files Browse the repository at this point in the history
* clear previous children when SVG node doesn't have innerHTML

* remove 'get' handler for appendChild in test node proxy
  • Loading branch information
OriR authored and gaearon committed Oct 10, 2017
1 parent 3019210 commit 6ad6dcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/renderers/dom/shared/setInnerHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var setInnerHTML = createMicrosoftUnsafeLocalFunction(function(node, html) {
reusableSVGContainer || document.createElement('div');
reusableSVGContainer.innerHTML = '<svg>' + html + '</svg>';
var svgNode = reusableSVGContainer.firstChild;
while (node.firstChild) {
node.removeChild(node.firstChild);
}
while (svgNode.firstChild) {
node.appendChild(svgNode.firstChild);
}
Expand Down
37 changes: 29 additions & 8 deletions src/renderers/dom/shared/utils/__tests__/setInnerHTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ describe('setInnerHTML', () => {
});

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

nodeProxy = new Proxy(node, {
has: (target, prop) => {
return prop === 'innerHTML' ? false : prop in target;
},
});

spyOn(node, 'appendChild').and.callThrough();
spyOn(node, 'removeChild').and.callThrough();
});

it('sets innerHTML on it', () => {
var html = '<circle></circle><rect></rect>';
setInnerHTML(node, html);
setInnerHTML(nodeProxy, html);

expect(node.appendChild.calls.argsFor(0)[0].outerHTML).toBe(
'<circle></circle>',
Expand All @@ -43,5 +51,18 @@ describe('setInnerHTML', () => {
'<rect></rect>',
);
});

it('clears previous children', () => {
var firstHtml = '<rect></rect>';
var secondHtml = '<circle></circle>';
setInnerHTML(nodeProxy, firstHtml);

setInnerHTML(nodeProxy, secondHtml);

expect(node.removeChild.calls.argsFor(0)[0].outerHTML).toBe(
'<rect></rect>',
);
expect(node.innerHTML).toBe('<circle></circle>');
});
});
});

0 comments on commit 6ad6dcd

Please sign in to comment.