Skip to content

innerHTML on a script tag escapes tag characters #14975

Closed

Description

@jdm
<script type="text/template"><!-- hi --></script><script>console.log(document.querySelector('script').innerHTML)</script>

In Firefox, this prints <!-- hi -->. In Servo, it prints &lt;-- hi --&gt;.

Activity

  1. added
    A-content/parsers
    C-has-manual-testcase
    on Jan 12, 2017
  2. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    The cause is this match which says that the contents of script tags should be escaped.

  3. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    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.

  4. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    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.

  5. added
    E-less-complex
    on Jan 12, 2017
  6. highfive commented on Jan 12, 2017

    @highfive

    Please make a comment here if you intend to work on this issue. Thank you!

  7. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    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.

  8. changed the title [-]innerHTML on a non-JS script tag escapes tag characters[/-] [+]innerHTML on a script tag escapes tag characters[/+] on Jan 12, 2017
  9. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    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.

  10. tormeh commented on Jan 12, 2017

    @tormeh

    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...

  11. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    Please do! There's no rush, and the actual code changes will happen in the html5ever repository.

  12. added
    C-assigned
    on Jan 12, 2017
  13. jdm commented on Jan 12, 2017

    @jdm
    MemberAuthor

    To see how Servo uses the html5ever serializer, look at Element::GetInnerHTML and related code in the HTML parser serialize implementation.

  14. tormeh commented on Jan 15, 2017

    @tormeh

    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?

  15. 38 remaining items

  16. jdm commented on May 23, 2017

    @jdm
    MemberAuthor

    @moonlightdrive Are you still working on this?

  17. moonlightdrive commented on May 25, 2017

    @moonlightdrive
    Contributor

    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.

    Thank you

  18. jdm commented on May 29, 2017

    @jdm
    MemberAuthor

    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?

  19. moonlightdrive commented on May 31, 2017

    @moonlightdrive
    Contributor

    Yeah, that makes sense! I'll take a look at implementing that solution. Thanks :)

  20. moonlightdrive commented on Jun 9, 2017

    @moonlightdrive
    Contributor

    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 yet
    fn start_elem_without_write<T>(&mut self, elem: T);

    and that the implementation of this function for HtmlSerializer should be something like

    fn start_elem_without_write<ElemInfo>(&mut self, elem: ElemInfo) {
      self.stack.push(elem);
    }

    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.

  21. added
    C-has-open-pr
    on Jun 20, 2017
  22. added a commit that references this issue on Aug 23, 2017
  23. added 4 commits that reference this issue on Oct 6, 2017
    6129d6e
    a1b4b55
    39c84af
    e3624e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/parsersC-assignedC-has-manual-testcaseC-has-open-prE-less-complex

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions