flute icon indicating copy to clipboard operation
flute copied to clipboard

`<script>` tag escapes Javascript code

Open defaultxr opened this issue 1 year ago • 0 comments

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)

defaultxr avatar Nov 23 '24 04:11 defaultxr