Skip to content

Correctly handle HTML entities inside <py-script> source code #735

Closed
@antocuni

Description

@antocuni

Follow up of this conversation: #684 (comment)

This is best explained by a simple example:

<script type="text/javascript">
console.log("js: a &amp; b");
</script>
<py-script>
import js
js.console.log("py: a &amp; b");
</py-script>

This produces the following:

js: a &amp; b
py: a & b

This is even worse if we use &quot; or &apos;, because currently they are interpreted as quotes and thus they can easily trigger Python syntax errors; e.g. the following trigger a SyntaxError:

<py-script>
import js
js.console.log("py: a &quot; b");
</py-script>

By reading the code, it seems that the interpretation of HTML entities happens here:

function escape(str: string): string {
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;")
}
function htmlDecode(input: string): string {
const doc = new DOMParser().parseFromString(ltrim(escape(input)), 'text/html');
return doc.documentElement.textContent;
}

I don't really understand what's going on: inside htmlDecode input seems to be the raw string of text that we want; then we put it inside DOMParser(), only to extract the text back.
PR #684 mitigates the issue by escaping angle brackets, but why do we need to call DOMParser in the first place?

/cc @philippjfr who worked on this recently and @fpliger who might remember what was the original idea

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogissue has been triaged but has not been earmarked for any upcoming release

    Type

    No type

    Projects

    Status

    Next

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy