Skip to content

Commit

Permalink
clear previous children when SVG node doesn't have innerHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
OriR committed Oct 5, 2017
1 parent fa7461e commit 3fad3b5
Show file tree
Hide file tree
Showing 2 changed files with 42 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
47 changes: 39 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,35 @@ 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;
},
get: (target, prop) => {
if (prop === 'appendChild') {
return function(child) {
child.parentElement.removeChild(child);
return target.appendChild(child);
};
} else {
return target[prop];
}
},
});

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 +61,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 3fad3b5

Please sign in to comment.