-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1208 from basecamp/dom-purify-config
Make DOMPurify configurable
- Loading branch information
Showing
7 changed files
with
84 additions
and
3 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
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,54 @@ | ||
import { | ||
assert, | ||
test, | ||
testGroup, | ||
} from "test/test_helper" | ||
|
||
import { HTMLSanitizer } from "../../trix/models" | ||
import * as config from "../../trix/config" | ||
|
||
testGroup("HTMLSanitizer", () => { | ||
test("strips custom tags", () => { | ||
const html = "<custom-tag></custom-tag>" | ||
const expectedHTML = "" | ||
const document = HTMLSanitizer.sanitize(html).body.innerHTML | ||
assert.equal(document, expectedHTML) | ||
}) | ||
|
||
test("keeps custom tags configured for DOMPurify", () => { | ||
const config = { | ||
ADD_TAGS: [ "custom-tag" ], | ||
RETURN_DOM: true, | ||
} | ||
withDOMPurifyConfig(config, () => { | ||
const html = "<custom-tag></custom-tag>" | ||
const expectedHTML = "<custom-tag></custom-tag>" | ||
const document = HTMLSanitizer.sanitize(html).body.innerHTML | ||
assert.equal(document, expectedHTML) | ||
}) | ||
}) | ||
}) | ||
|
||
const withDOMPurifyConfig = (attrConfig = {}, fn) => { | ||
withConfig("dompurify", attrConfig, fn) | ||
} | ||
|
||
const withConfig = (section, newConfig = {}, fn) => { | ||
const originalConfig = Object.assign({}, config[section]) | ||
const copy = (section, properties) => { | ||
for (const [ key, value ] of Object.entries(properties)) { | ||
if (value) { | ||
config[section][key] = value | ||
} else { | ||
delete config[section][key] | ||
} | ||
} | ||
} | ||
|
||
try { | ||
copy(section, newConfig) | ||
fn() | ||
} finally { | ||
copy(section, originalConfig) | ||
} | ||
} |
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,4 @@ | ||
export default { | ||
ADD_ATTR: [ "language" ], | ||
RETURN_DOM: true | ||
} |
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