I am looking for an open-source tool that accepts JavaScript code and outputs code that is syntactically and semantically equivalent, but can be embedded in HTML and/or XHTML without changes.

This involves the following changes:

  • Replace <!-- with either
    • if in a string or regex literal, then escape one or more of the characters (say to <\x21--)
    • if not in a string or regex literal, insert a space (say to < !--)
  • Replace </script with
    • in a string literal, the sequence \x3c/script
    • in a regex literal, </ script (the / terminates the regex)
  • Replace ]]> with

    • in a string or regex literal, ]]\x3e or other equivalent
    • otherwise, with ]] > or other equivalent
  • Finally, surround the script with /*<![CDATA[*/ or similar at the start and /*]]>*/ at the end

This is non-trivial because JavaScript cannot be tokenized without parser feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.