Skip to content

<script> tag escapes Javascript code #13

@defaultxr

Description

@defaultxr

With Flute and Parenscript, something like this doesn't work:

(h (script (ps (dotimes (n 3)
                 (alert "test")))))

because the result looks like this:

<script>
  (function () {
    for (var n = 0; n &lt; 3; n += 1) {
        alert('test');
    };
})();
</script>

Note that the < in the Javascript is escaped to &lt;, which seems to be invalid Javascript, as it causes the browser's JS parser to get confused and throw a syntax error. But the following, with < instead of &lt;, does run:

<script>
  (function () {
    for (var n = 0; n < 3; n += 1) {
        alert('test');
    };
})();
</script>

Since the contents of a <script> tag are parsed differently by the browser (i.e. HTML escape codes are not interpreted), it seems like maybe the default behavior of flute:script should be to not HTML-escape the content.

In the meantime, a workaround seems to be manually setting flute:*escape-html* to nil:

(h (let ((flute:*escape-html* nil))
     (script (ps (dotimes (n 3)
                   (alert "test"))))))

I'm not an HTML/JS pro though, nor a Flute pro, so if I should be doing this a different way instead, let me know.

(And yes, I know that <script src="..."></script> can also be used, but it seems more ergonomic that both use-cases would be handled correctly by Flute. IMO anyway)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions