-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Fix #6950, work around IE missing innerHTML on SVG nodes #6982
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f42783
Workaround IE lacking innerHTML on SVG elements
joshhunt 3589357
Add tests for setInnerHTML
joshhunt 382251b
Correctly check if node has innerHTML property
joshhunt 0472e33
Ensure tests for setInnerHTML actually tests both codepaths
joshhunt da366c6
Provide mock element for setInnerHTML tests
joshhunt 446759b
Only use SVG setInnerHTML workaround for SVG elements
joshhunt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright 2016-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var setInnerHTML = require('setInnerHTML'); | ||
var DOMNamespaces = require('DOMNamespaces'); | ||
|
||
describe('setInnerHTML', function() { | ||
describe('when the node has innerHTML property', () => { | ||
it('sets innerHTML on it', function() { | ||
var node = document.createElement('div'); | ||
var html = '<h1>hello</h1>'; | ||
setInnerHTML(node, html); | ||
expect(node.innerHTML).toBe(html); | ||
}); | ||
}); | ||
|
||
describe('when the node does not have an innerHTML property', () => { | ||
it('sets innerHTML on it', function() { | ||
// Create a mock node that looks like an SVG in IE (without innerHTML) | ||
var node = { | ||
namespaceURI: DOMNamespaces.svg, | ||
appendChild: jasmine.createSpy(), | ||
}; | ||
|
||
var html = '<circle></circle><rect></rect>'; | ||
setInnerHTML(node, html); | ||
|
||
expect(node.appendChild.calls.argsFor(0)[0].outerHTML).toBe('<circle></circle>'); | ||
expect(node.appendChild.calls.argsFor(1)[0].outerHTML).toBe('<rect></rect>'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,16 @@ | |
'use strict'; | ||
|
||
var ExecutionEnvironment = require('ExecutionEnvironment'); | ||
var DOMNamespaces = require('DOMNamespaces'); | ||
|
||
var WHITESPACE_TEST = /^[ \r\n\t\f]/; | ||
var NONVISIBLE_TEST = /<(!--|link|noscript|meta|script|style)[ \r\n\t\f\/>]/; | ||
|
||
var createMicrosoftUnsafeLocalFunction = require('createMicrosoftUnsafeLocalFunction'); | ||
|
||
// SVG temp container for IE lacking innerHTML | ||
var reusableSVGContainer; | ||
|
||
/** | ||
* Set the innerHTML property of a node, ensuring that whitespace is preserved | ||
* even in IE8. | ||
|
@@ -28,7 +32,19 @@ var createMicrosoftUnsafeLocalFunction = require('createMicrosoftUnsafeLocalFunc | |
*/ | ||
var setInnerHTML = createMicrosoftUnsafeLocalFunction( | ||
function(node, html) { | ||
node.innerHTML = html; | ||
// IE does not have innerHTML for SVG nodes, so instead we inject the | ||
// new markup in a temp node and then move the child nodes across into | ||
// the target node | ||
if (node.namespaceURI === DOMNamespaces.svg && !('innerHTML' in node)) { | ||
reusableSVGContainer = reusableSVGContainer || document.createElement('div'); | ||
reusableSVGContainer.innerHTML = '<svg>' + html + '</svg>'; | ||
var newNodes = reusableSVGContainer.firstChild.childNodes; | ||
for (var i = 0; i < newNodes.length; i++) { | ||
node.appendChild(newNodes[i]); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @jp7837 pointed out in #7358 (comment) - this is going to get every other child - childNodes is a live NodeList. This should be a while loop. I can confirm this is what is happening in IE11. |
||
} else { | ||
node.innerHTML = html; | ||
} | ||
} | ||
); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I missing something, why not create the
svg
directly and drop thediv
? EDIT: It seems to haveinnerHTML
, but perhaps it doesn't actually work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IE does not support innerHTML on SVG Elements, so we wrap the markup to make it a 'complete' svg string, pop that into a temp container node, then copy across the children of the
<svg>
.