akornatskyy / wheezy.html

A lightweight html rendering library
https://wheezyhtml.readthedocs.io/en/latest/
MIT License
1 stars 0 forks source link

WhitespaceExtension breaks some embedded JavaScript code #28

Open vladimir-poghosyan opened 1 month ago

vladimir-poghosyan commented 1 month ago

When WhitespaceExtension is used with wheezy.template, which includes a JavaScript code inside script tag, the extension breaks JavaScript code in some cases. For example, JS code with only newline termination:

<script>
function getData(id, key) {
  const element = document.getElementById("missing");
  if (!element)
    return

  if (element.dataset)
    return element.dataset[key]

  return null
}

getData("missing", "target")
</script>

Should the script tag be added to ignore rules of WhitespacePreprocessor?

akornatskyy commented 1 month ago

please provide a complete example that reproduces the issue (so it can be run from CLI). also outline your expectations.

vladimir-poghosyan commented 1 month ago

When I render the following template (index.html):

@require(lang)
<!doctype html>
<html lang="@lang!h">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <script>
    window.addEventListener("DOMContentLoaded", event => {
      document.querySelectorAll("button[data-open-dialog]").forEach(button => {
        const dialog = document.getElementById(button.dataset.openDialog)
        if (!dialog)
          return

        button.addEventListener("click", event => dialog.show())
      })
    })
    </script>
  </head>
  <body>
    <dialog id="welcome_dialog"><span>Hello there<span></dialog>
    <button data-open-dialog="welcome_dialog">Open welcome dialog</button>
  </body>
</html>

with python3 -m wheezy.template.console -w index.html '{"lang": "hy"}' > ./rendered.html command, I get an error in browser console: Uncaught SyntaxError: unexpected token: keyword 'if'. Running the same command without -w argument works as expected.

If JavaScript statements are properly terminated with semicolon, the code doesn't break. But it's also possible to leave them out.

I think WhitespaceExtension must skip processing JavaScript code inside script tags. Or maybe treat it differently, which I understand can be a complex matter.