Adding complex (and minified) JavaScript documents as Smartsite items

Release 1.0 - ...

Smartsite iXperion javaScript items are configured by default to use a Logic field which injects a separate Data field. This means that any script in the Data field must be well-formed sxml. To help the user get it right, a textfilter "includes" was introduced. This filter simply replaces <, > and & characters with the appropriate xml entities.

So far so good, but problems will occur when your JavaScript resembles viper syntax. First because vipers are ignored by textfilters (unless the filter specifically includes them). And second (if you set the filter to includevipers="true") because the item validation will attempt to validate the code as if it were a viper. So how do you know if your script resembles a viper? If it test positive for:

/\{[a-z][a-z0-9_.]{2,79}\((?:[^\)]|\)(?!\}))*\)\}/

That is:

  • an opening brace {
  • a lower case letter
  • 2 - 79 lower case letters, digits, underscores or dots
  • an opening parenthesis (
  • any number of characters that are not a closing parenthesis ) or a closing parenthesis that isn't be followed by a closing brace }
  • ending with a closing parenthesis and brace )}

When coding normally, using white space, ending your lines in semi-colons, placing linebreaks before closing braces, you won't run in to this. However, when you minify script, all non-significant white space will be removed as well as (apparently) all semi-colons deemed unnecessary (i.e. at the end of a statement). This will easily lead to viper-like syntax, causing validation errors.

Solution

Working around the validation errors is easy: don't use the Data field in these cases. Place your minified script inside a CDATA block in the Logic field. The Logic field has no textfilter configured and the CDATA block will pass it through item validation without notice.