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 1 commit
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
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') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? If you append a child to another parent, it will be removed from the current parent.

Isn't just

        get: (target, prop) => {
          return target[prop];
        },

enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the comment here it says that JSDOM doesn't remove the node from the current parent when moving nodes using appendChild, though I didn't verify this myself.

I could check and see if that's still relevant, if not then I can remove the get handler altogether.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this, yes. The comments might be very outdated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right, this was no longer necessary 🎉
Removed the get handler altogether!

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>');
});
});
});