-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Core:Manipulation: Add basic TrustedHTML support #4927
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| import toType from "./toType.js"; | ||
| import isWindow from "../var/isWindow.js"; | ||
|
|
||
| function isArrayLike( obj ) { | ||
|
|
||
| var length = !!obj && obj.length, | ||
| type = toType( obj ); | ||
|
|
||
| if ( typeof obj === "function" || isWindow( obj ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return type === "array" || length === 0 || | ||
| typeof length === "number" && length > 0 && ( length - 1 ) in obj; | ||
| } | ||
|
|
||
| export default isArrayLike; |
This file contains hidden or 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,7 @@ | ||
| function isObviousHtml( input ) { | ||
| return input[ 0 ] === "<" && | ||
| input[ input.length - 1 ] === ">" && | ||
| input.length >= 3; | ||
| } | ||
|
|
||
| export default isObviousHtml; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset=utf-8 /> | ||
| <title>body</title> | ||
| </head> | ||
| <body> | ||
| <div id="qunit-fixture"></div> | ||
| <script src="../../dist/jquery.min.js"></script> | ||
| <script src="iframeTest.js"></script> | ||
| <script> | ||
| var i, input, elem, tags, policy, | ||
| results = [], | ||
| inputs = [ | ||
| [ "<div></div>", "<div class='test'></div>", [ "div" ] ], | ||
| [ "<div></div>", "<div class='test'></div><span class='test'></span>", | ||
| [ "div", "span" ] ], | ||
| [ "<table></table>", "<td class='test'></td>", [ "td" ] ], | ||
| [ "<select></select>", "<option class='test'></option>", [ "option" ] ] | ||
| ]; | ||
|
|
||
| function runTests( messagePrefix, getHtmlWrapper ) { | ||
| for ( i = 0; i < inputs.length; i++ ) { | ||
| input = inputs[ i ]; | ||
| elem = jQuery( getHtmlWrapper( input[ 0 ] ) ); | ||
| elem.append( getHtmlWrapper( input[ 1 ] ) ); | ||
| tags = elem.find( ".test" ).toArray().map( function( node ) { | ||
| return node.nodeName.toLowerCase(); | ||
| } ); | ||
| results.push( { | ||
| actual: tags, | ||
| expected: input[ 2 ], | ||
| message: messagePrefix + ": " + input[ 2 ].join( ", " ) | ||
| } ); | ||
| } | ||
|
|
||
| elem = jQuery( getHtmlWrapper( "<div></div>" ) ); | ||
| elem.append( getHtmlWrapper( "text content" ) ); | ||
| results.push( { | ||
| actual: elem.html(), | ||
| expected: "text content", | ||
| message: messagePrefix + ": text content properly appended" | ||
| } ); | ||
| } | ||
|
|
||
| if ( typeof trustedTypes !== "undefined" ) { | ||
| policy = trustedTypes.createPolicy( "jquery-test-policy", { | ||
| createHTML: function( html ) { | ||
| return html; | ||
| } | ||
| } ); | ||
|
|
||
| runTests( "TrustedHTML", function wrapInTrustedHtml( input ) { | ||
| return policy.createHTML( input ); | ||
| } ); | ||
| } else { | ||
|
|
||
| // No TrustedHTML support so let's at least run tests with object wrappers | ||
| // with a proper `toString` function. This also shows that jQuery support | ||
| // of TrustedHTML is generic and would work with similar APIs out of the box | ||
| // as well. Ideally, we'd run these tests in browsers with TrustedHTML support | ||
| // as well but due to the CSP TrustedHTML enforcement these tests would fail. | ||
| runTests( "Object wrapper", function( input ) { | ||
| return { | ||
| toString: function toString() { | ||
| return input; | ||
| } | ||
| }; | ||
| } ); | ||
| } | ||
|
|
||
| startIframeTest( results ); | ||
| </script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or 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 hidden or 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
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.
Does it make sense to also add a test for
TrustedHTMLs that wrap over a non-obvious HTML, e.g. just some text? From what I can tell, different branches are used then.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.
Yes, good point. That will only apply to the second array items as the first ones are passed directly to
jQuerywhich also supports selectors - and since for TrustedHTML we cannot carve out a part of an input string to later parse, I opted to restrict support for TrustedHTML-wrapped strings to ones that are start with<and end with>- i.e. what was already going through the fast path skipping the regex match.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.
So, I assume that
TrustedHTMLwrappingsomethingwill just get stringified somewhere and be treated as a selector? That's OK, I don't think there would be expectations for jQuery to do anything else. I think it just makes sense to have an explicit test for that behavior.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.
@koto It would actually just create a jQuery wrapper with a single element being this TrustedHTML instance, i.e. it would treat it like any other non-DOM object. I just didn't handle it in any special way and let it go where it goes naturally in the current flow.
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.
To be clear, this limitation only applies to (pseudo-code)
jQuery(TrustedHTML('foo'));jQuery(document.body).append(TrustedHTML('foo'))should append the textfoojust as without wrapping in TrustedHTML; I'll add a test for that.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.
Oh, I forgot about https://api.jquery.com/jQuery/#jQuery-object behavior :) Yeah, that's fine, thanks for clarifying.
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.
I added one test case for text content. I haven't added one for the object wrapper as I'm not sure if we want this to be a part of the contract.
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.
Sounds good!