File tree Expand file tree Collapse file tree 3 files changed +39
-16
lines changed Expand file tree Collapse file tree 3 files changed +39
-16
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,15 @@ function domToReact(nodes) {
26
26
27
27
children = null ;
28
28
29
- if ( node . type === 'tag' ) {
29
+ // node type for script is "script" not "tag"
30
+ if ( node . name === 'script' && node . children [ 0 ] ) {
31
+ // prevent text in script tag from being escaped
32
+ // https://facebook.github.io/react/tips/dangerously-set-inner-html.html
33
+ node . attribs . dangerouslySetInnerHTML = {
34
+ __html : node . children [ 0 ] . data
35
+ } ;
36
+
37
+ } else if ( node . type === 'tag' ) {
30
38
// setting textarea value in children is an antipattern in React
31
39
// https://facebook.github.io/react/docs/forms.html#why-textarea-value
32
40
if ( node . name === 'textarea' && node . children [ 0 ] ) {
@@ -36,21 +44,21 @@ function domToReact(nodes) {
36
44
// continue recursion of creating React elements
37
45
children = domToReact ( node . children ) ;
38
46
}
47
+ }
39
48
40
- // specify a `key` prop if returning an array
41
- // https://fb.me/react-warning-keys
42
- if ( len > 1 ) {
43
- node . attribs . key = i ;
44
- }
45
-
46
- result . push (
47
- React . createElement (
48
- node . name ,
49
- node . attribs ,
50
- children
51
- )
52
- ) ;
49
+ // specify a `key` prop if returning an array
50
+ // https://fb.me/react-warning-keys
51
+ if ( len > 1 ) {
52
+ node . attribs . key = i ;
53
53
}
54
+
55
+ result . push (
56
+ React . createElement (
57
+ node . name ,
58
+ node . attribs ,
59
+ children
60
+ )
61
+ ) ;
54
62
}
55
63
56
64
if ( result . length === 1 ) {
Original file line number Diff line number Diff line change 3
3
"single" : " <p>foo</p>" ,
4
4
"multiple" : " <p>foo</p><p>bar</p>" ,
5
5
"complex" : " <html><head><title>Title</title></head><body><header id=\" header\" >Header</header><h1>Heading</h1><p>Paragraph</p><div class=\" class1 class2\" >Some <em>text</em>.</div><script>alert();</script></body></html>" ,
6
- "textarea" : " <textarea>foo</textarea>"
6
+ "textarea" : " <textarea>foo</textarea>" ,
7
+ "script" : " <script>alert(1 < 2);</script>"
7
8
}
8
9
}
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ describe('dom-to-react parser', function() {
41
41
} ) ;
42
42
43
43
// https://facebook.github.io/react/docs/forms.html#why-textarea-value
44
- it ( 'converts textarea correctly' , function ( ) {
44
+ it ( 'converts < textarea> correctly' , function ( ) {
45
45
var html = data . html . textarea ;
46
46
var reactElement = domToReact ( htmlToDOM ( html ) ) ;
47
47
assert ( React . isValidElement ( reactElement ) ) ;
@@ -53,4 +53,18 @@ describe('dom-to-react parser', function() {
53
53
) ;
54
54
} ) ;
55
55
56
+ it ( 'converts <script> correctly' , function ( ) {
57
+ var html = data . html . script ;
58
+ var reactElement = domToReact ( htmlToDOM ( html ) ) ;
59
+ assert ( React . isValidElement ( reactElement ) ) ;
60
+ assert . deepEqual (
61
+ reactElement ,
62
+ React . createElement ( 'script' , {
63
+ dangerouslySetInnerHTML : {
64
+ __html : 'alert(1 < 2);'
65
+ }
66
+ } , null )
67
+ ) ;
68
+ } ) ;
69
+
56
70
} ) ;
You can’t perform that action at this time.
0 commit comments