-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: user script extraction and execution (#100)
- Loading branch information
1 parent
cf39f9f
commit 3eeeee9
Showing
8 changed files
with
93 additions
and
59 deletions.
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,33 @@ | ||
const { mount } = require('enzyme'); | ||
const React = require('react'); | ||
const { renderToString } = require('react-dom/server'); | ||
|
||
const sanitize = require('../../sanitize.schema'); | ||
const HTMLBlock = require('../../components/HTMLBlock')(sanitize); | ||
|
||
describe('HTML Block', () => { | ||
global.window = true; | ||
global.mockFn = jest.fn(() => console.log('custom script ran')); | ||
|
||
it("doesn't run user scripts by default", () => { | ||
mount(<HTMLBlock html="<script>mockFn()</script>" runScripts={false} />); | ||
expect(global.mockFn).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('runs user scripts in compat mode', () => { | ||
mount(<HTMLBlock html="<script>mockFn()</script>" runScripts={true} />); | ||
expect(global.mockFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("doesn't run scripts on the server (even in compat mode)", () => { | ||
const html = ` | ||
<h1>Hello World</h1> | ||
<script>mockFn()</script> | ||
`; | ||
const elem = <HTMLBlock html={html} runScripts={true} />; | ||
const ssr = renderToString(elem); | ||
expect(elem.props.runScripts).toBe(true); | ||
expect(ssr.indexOf('<script>')).toBeLessThan(0); | ||
expect(ssr.indexOf('<h1>')).toBeGreaterThanOrEqual(0); | ||
}); | ||
}); |
File renamed without changes.
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
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
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,40 @@ | ||
const sanitize = require('hast-util-sanitize/lib/github.json'); | ||
|
||
// Sanitization Schema Defaults | ||
sanitize.clobberPrefix = ''; | ||
|
||
sanitize.tagNames.push('span', 'style'); | ||
sanitize.attributes['*'].push('class', 'className', 'align', 'style'); | ||
|
||
/** | ||
* @todo don't manually whitelist custom component attributes | ||
* within the engine! | ||
* @todo change `link` to `href` | ||
*/ | ||
sanitize.attributes['tutorial-tile'] = ['backgroundColor', 'emoji', 'link']; | ||
|
||
sanitize.tagNames.push('rdme-pin'); | ||
|
||
sanitize.tagNames.push('rdme-embed'); | ||
sanitize.attributes['rdme-embed'] = [ | ||
'url', | ||
'provider', | ||
'html', | ||
'title', | ||
'href', | ||
'iframe', | ||
'width', | ||
'height', | ||
'image', | ||
'favicon', | ||
]; | ||
|
||
sanitize.attributes.a = ['href', 'title', 'class', 'className', 'download']; | ||
|
||
sanitize.tagNames.push('figure'); | ||
sanitize.tagNames.push('figcaption'); | ||
|
||
sanitize.tagNames.push('input'); // allow GitHub-style todo lists | ||
sanitize.ancestors.input = ['li']; | ||
|
||
module.exports = sanitize; |