Skip to content

Instantly share code, notes, and snippets.

@Daylon
Created September 26, 2016 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daylon/cb80b252a912875224eeaedfa560ee63 to your computer and use it in GitHub Desktop.
Save Daylon/cb80b252a912875224eeaedfa560ee63 to your computer and use it in GitHub Desktop.
MJML postrender fix
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var removeCDATA = exports.removeCDATA = function removeCDATA(str) {
return str.replace(/(?:<!-{0,2}\[CDATA\[){1,2}([^]*?)(?:\]\]-{0,2}>?){1,2}/gm, '$1');
};
var fixLegacyAttrs = exports.fixLegacyAttrs = function fixLegacyAttrs($) {
var legacyAttrs = ['align', 'valign', 'bgcolor', 'border', 'background'];
// https://github.com/facebook/react/issues/140 ...
// server side workaround to allow custom tags.
legacyAttrs.forEach(function (attr) {
var dataAttr = 'data-legacy-' + attr;
$('[' + dataAttr + ']').each(function () {
$(this).attr(attr, $(this).attr(dataAttr));
$(this).removeAttr(dataAttr);
});
});
return $;
};
@Daylon
Copy link
Author

Daylon commented Sep 26, 2016

Some component may be parsed twice, seeing their content twice embedded between CDATA tags, e.g.

<!--[CDATA[<![CDATA[
    <script></script>
    <style>
@media all {
  a.link:hover {
    text-decoration: underline !important;
  }
}
</style>
        <script type="application/ld+json">
            {
                "@context": "http://schema.org",
                "@type": "EmailMessage",
                "potentialAction": {
                    "@type": "ViewAction",
                    "name": "{{saveto.name}}",
                    "target": "{{saveto.link}}",
                    "url": "{{saveto.link}}"
                },
                "description": "{{saveto.description}}"
            }
        </script>    ]]-->]]

… Which, once it gets through postrender will still have one remaining CDATA or ]] artefact.
The key is to collect both regular CDATA tags and multiple concatenation. At the time of writing, I didn't find any case with more than two consecutive CDATAs.

@Daylon
Copy link
Author

Daylon commented Sep 26, 2016

Before/after

/<!--\[CDATA\[([^]*?)\]\]-->/gm

/(?:<!-{0,2}\[CDATA\[){1,2}([^]*?)(?:\]\]-{0,2}>?){1,2}/gm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment