medusa-ui/medusa

Script tags with custom scripts get HTML encoded

kevindeyne opened this issue · 2 comments

An HTML with the following:

<script>
    //redirect script once loaded - should eventually be a Medusa feature
    var _M = _M || {};
    _M.postRender = function (incomingMessage) {
        if(incomingMessage !== null && typeof incomingMessage !== "undefined" && incomingMessage.length === 1) {
            if("redirect" === incomingMessage[0]['f']) {
                window.location.href = incomingMessage[0]['v'];
            }
        }
    };
</script>

Turns into

<script>
    //redirect script once loaded - should eventually be a Medusa feature
    var _M = _M || {};
    _M.postRender = function (incomingMessage) {
        if(incomingMessage !== null &amp;&amp; typeof incomingMessage !== "undefined" &amp;&amp; incomingMessage.length === 1) {
            if("redirect" === incomingMessage[0]['f']) {
                window.location.href = incomingMessage[0]['v'];
            }
        }
    };
</script>

An easy workaround is to mix JS-comment // and CDATA.
The XML-Parser treats the CDATA-section as regular text, and JS just sees the extra lines as comments.

<script>
    // <![CDATA[
    //redirect script once loaded - should eventually be a Medusa feature
    var _M = _M || {};
    _M.postRender = function (incomingMessage) {
        if(incomingMessage !== null && typeof incomingMessage !== "undefined" && incomingMessage.length === 1) {
            if("redirect" === incomingMessage[0]['f']) {
                window.location.href = incomingMessage[0]['v'];
            }
        }
    };
    // ]]-->
</script>

Can be automatized by surrounding the script-tag-content with /* <![CDATA[ */ and /* ]]--> */ in HTMLCache
see PR #177