You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
innerHTML on a script tag escapes tag characters #14975
The parent() operation in the html5ever serializer gets the last element in the element stack. In the outerHTML case this is the element that is pushed in start_elem. In the innerHTML case this is the dummy initial value added in the serializer's constructor, so it's not considered to have a script element parent.
To fix this, in the case where the serializer is created with ChildrenOnly we should make the initial element contain the data for the actual element parent. We should add a unit test in html5ever for this case, as well as create a new WPT test for Servo.
Actually, there's a useful tests/wpt/web-platform-tests/html/syntax/serializing-html-fragments.html test that already exists, and contains a relevant case: <span><script type="test"><&></script></span>. However, it only checks innerHTML and outerHTML of the span, rather than the script directly. Let's add another case to this that gets rid of the outer span.
Can I claim this? I have no experience with Servo (except testing) and little with Rust, so if it needs to be done quickly then I'm probably not the correct person...
Hmm... This bug is tagged with "C - has test", but when I run "cargo test" all the tests pass. Is the test wrong? And the code says that the contents of a script tag should not be escaped (i.e. let script = false;), while this thread and the title says they are. What am I missing?
I am actually a little unclear on where (which repository) to make this change. I have tried the following:
I tried making the fix by changing the Serializer::new method in html5ever. I am not sure how to get the tagname for the html_name field, without importing types from servo. It seems like html5ever is a more general library that should not depend on servo, correct?
Also took a look at pushing to the stack in the serialize method when the traversal scope is ChildrenOnly (implemented in servo repo). Thought I need parent method (to check if the None value should replaced by the correct thing). parent is private. I tried to make it public (would this be undesirable?) but ran into an issue of "can't leak private types" (have done some googling but am still new to rust and don't quite understand why this occurs).
Not sure if I've misunderstood how to implement this.
Sorry for the delay in responding. Good investigation! You're right that html5ever cannot depend on Servo (especially since Servo depends on html5ever). The error about private types occurs because a public function would now have a private type in its public interface; the private type in this case is ElemInfo which is defined in the same file.
As for the optimal solution, I think I have a useful idea for you. Let's make the ChildrenOnly variant of TraversalScope have a Option<LocalName> field. Serializer::new can look at the traversal_scope member of the options that receives and extract the name if it's present, and use that for the initial ElemInfo value. Am I making sense?
I think what I actually need is a function that's part of the Serializer trait, which will push/pop elements to the stack, without doing any of the writes that start_elem or end_elem do.
I believe (and don't have a great grasp of Rust yet) that this function should have a signature like
// i don't know what to name this fn yetfnstart_elem_without_write<T>(&mutself,elem:T);
and that the implementation of this function for HtmlSerializer should be something like
but this is evidently not right. The error I am getting is: expected struct 'serialize::ElemInfo', found type parameter. I guess this type parameter is ElemInfo? (Based on a compiler note: expected type serialize::ElemInfo found type ElemInfo) I don't understand why this is a type parameter rather than a thing that is as struct.
Activity
jdm commentedon Jan 12, 2017
The cause is this match which says that the contents of script tags should be escaped.
jdm commentedon Jan 12, 2017
That code matches what is written at https://html.spec.whatwg.org/multipage/syntax.html#serialising-html-fragments for "If current node is a Text node". Interestingly, outerHTML works fine, and innerHTML does not.
jdm commentedon Jan 12, 2017
The
parent()operation in the html5ever serializer gets the last element in the element stack. In the outerHTML case this is the element that is pushed instart_elem. In the innerHTML case this is the dummy initial value added in the serializer's constructor, so it's not considered to have a script element parent.highfive commentedon Jan 12, 2017
Please make a comment here if you intend to work on this issue. Thank you!
jdm commentedon Jan 12, 2017
To fix this, in the case where the serializer is created with ChildrenOnly we should make the initial element contain the data for the actual element parent. We should add a unit test in html5ever for this case, as well as create a new WPT test for Servo.
[-]innerHTML on a non-JS script tag escapes tag characters[/-][+]innerHTML on a script tag escapes tag characters[/+]jdm commentedon Jan 12, 2017
Actually, there's a useful
tests/wpt/web-platform-tests/html/syntax/serializing-html-fragments.htmltest that already exists, and contains a relevant case:<span><script type="test"><&></script></span>. However, it only checks innerHTML and outerHTML of the span, rather than the script directly. Let's add another case to this that gets rid of the outer span.tormeh commentedon Jan 12, 2017
Can I claim this? I have no experience with Servo (except testing) and little with Rust, so if it needs to be done quickly then I'm probably not the correct person...
jdm commentedon Jan 12, 2017
Please do! There's no rush, and the actual code changes will happen in the html5ever repository.
jdm commentedon Jan 12, 2017
To see how Servo uses the html5ever serializer, look at Element::GetInnerHTML and related code in the HTML parser serialize implementation.
tormeh commentedon Jan 15, 2017
Hmm... This bug is tagged with "C - has test", but when I run "cargo test" all the tests pass. Is the test wrong? And the code says that the contents of a script tag should not be escaped (i.e. let script = false;), while this thread and the title says they are. What am I missing?
38 remaining items
jdm commentedon May 23, 2017
@moonlightdrive Are you still working on this?
moonlightdrive commentedon May 25, 2017
I am actually a little unclear on where (which repository) to make this change. I have tried the following:
I tried making the fix by changing the
Serializer::newmethod in html5ever. I am not sure how to get the tagname for thehtml_namefield, without importing types from servo. It seems like html5ever is a more general library that should not depend on servo, correct?Also took a look at pushing to the stack in the
serializemethod when the traversal scope isChildrenOnly(implemented in servo repo). Thought I need parent method (to check if theNonevalue should replaced by the correct thing).parentis private. I tried to make it public (would this be undesirable?) but ran into an issue of "can't leak private types" (have done some googling but am still new to rust and don't quite understand why this occurs).Not sure if I've misunderstood how to implement this.
Thank you
jdm commentedon May 29, 2017
Sorry for the delay in responding. Good investigation! You're right that html5ever cannot depend on Servo (especially since Servo depends on html5ever). The error about private types occurs because a public function would now have a private type in its public interface; the private type in this case is
ElemInfowhich is defined in the same file.As for the optimal solution, I think I have a useful idea for you. Let's make the
ChildrenOnlyvariant ofTraversalScopehave aOption<LocalName>field.Serializer::newcan look at thetraversal_scopemember of the options that receives and extract the name if it's present, and use that for the initial ElemInfo value. Am I making sense?moonlightdrive commentedon May 31, 2017
Yeah, that makes sense! I'll take a look at implementing that solution. Thanks :)
moonlightdrive commentedon Jun 9, 2017
I think what I actually need is a function that's part of the Serializer trait, which will push/pop elements to the stack, without doing any of the writes that
start_elemorend_elemdo.I believe (and don't have a great grasp of Rust yet) that this function should have a signature like
and that the implementation of this function for
HtmlSerializershould be something likebut this is evidently not right. The error I am getting is: expected struct 'serialize::ElemInfo', found type parameter. I guess this type parameter is
ElemInfo? (Based on a compiler note: expected typeserialize::ElemInfofound typeElemInfo) I don't understand why this is a type parameter rather than a thing that is as struct.extend Serializer trait with additional functions
extend Serializer trait with additional functions
Auto merge of #277 - moonlightdrive:innerhtml-escape, r=jdm
Auto merge of #18747 - jdm:serializing-childrenonly, r=jdm
Auto merge of #18747 - jdm:serializing-childrenonly, r=jdm
Auto merge of #18747 - jdm:serializing-childrenonly, r=jdm
Auto merge of #18747 - jdm:serializing-childrenonly, r=jdm
Auto merge of #277 - moonlightdrive:innerhtml-escape, r=jdm