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
This commit is contained in:
Ori Riner
2017-10-10 13:54:47 +03:00
committed by Dan Abramov
parent 3019210df2
commit 6ad6dcd112
2 changed files with 32 additions and 8 deletions
+3
View File
@@ -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);
}
@@ -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>',
@@ -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>');
});
});
});