- Notifications
You must be signed in to change notification settings - Fork20.6k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -9,22 +9,8 @@ | ||
<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" ] ], | ||
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. Does it make sense to also add a test for 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, good point. That will only apply to the second array items as the first ones are passed directly to 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. So, I assume that 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. @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 commentThe reason will be displayed to describe this comment to others.Learn more. To be clear, this limitation only applies to (pseudo-code) 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. Oh, I forgot abouthttps://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 commentThe 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 commentThe reason will be displayed to describe this comment to others.Learn more. Sounds good! | ||
[ "<div></div>", "<div class='test'></div><span class='test'></span>", | ||
@@ -33,27 +19,55 @@ | ||
[ "<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> | ||