Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear previous children when SVG node doesn't have innerHTML #11108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>');
});
});
});