- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 64
feat: add support for mixed html/js content #128
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
Changes from 1 commit
77058f9
ce93e10
3db3273
cdf21f6
97909a9
2e86442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import React, { useState, useEffect, useRef, useMemo } from 'react'; | ||
import Scrollable from './Scrollable'; | ||
import PreviewHint from './PreviewHint'; | ||
import AddHtml from './AddHtml'; | ||
|
@@ -34,6 +34,36 @@ function Preview({ markup, accessibleRoles, elements, dispatch }) { | |
|
||
// TestingLibraryDom?.getSuggestedQuery(highlighted, 'get').toString() : null | ||
|
||
const scripts = useMemo(() => { | ||
const container = document.createElement('div'); | ||
container.innerHTML = markup; | ||
const scriptsCollections = container.getElementsByTagName('script'); | ||
return Array.from(scriptsCollections).map((script) => script.innerHTML); | ||
}, [markup]); | ||
|
||
const actualMarkup = useMemo( | ||
() => | ||
scripts.length | ||
? scripts.reduce( | ||
(html, script) => html.replace(`<script>${script}</script>`, ''), | ||
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. This doesn't remove 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. Yes, totally right! I am going to fix it! |
||
markup, | ||
) | ||
: markup, | ||
[scripts, markup], | ||
); | ||
|
||
useEffect(() => { | ||
if (scripts.length) { | ||
try { | ||
scripts.forEach((script) => { | ||
window.eval(script); | ||
}); | ||
} catch { | ||
return; | ||
} | ||
} | ||
}, [scripts]); | ||
|
||
useEffect(() => { | ||
setRoles(Object.keys(accessibleRoles || {}).sort()); | ||
}, [accessibleRoles]); | ||
|
@@ -98,7 +128,9 @@ function Preview({ markup, accessibleRoles, elements, dispatch }) { | |
onClick={handleClick} | ||
onMouseMove={handleMove} | ||
ref={htmlRoot} | ||
dangerouslySetInnerHTML={{ __html: markup }} | ||
dangerouslySetInnerHTML={{ | ||
__html: actualMarkup, | ||
}} | ||
/> | ||
</Scrollable> | ||
</div> | ||
|
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.
This also parses
script
elements of other types than javascript. For example<script type="text/css">
. Type attributes forscript
elements default totext/javascript
, so we should only extractscript
elements without atype
attribute, or with atype === "text/javascript"
See also this repl.
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.
Got it! I am going to fix it again!