[MGNLDIFF-106] Embedded JavaScript in Freemarker templates can lead to broken diff operations Created: 13/Jan/20  Updated: 29/Apr/20  Resolved: 06/Apr/20

Status: Closed
Project: Magnolia Diff Module
Component/s: None
Affects Version/s: 2.1
Fix Version/s: 2.0.1, 2.1.1, 2.2.1

Type: Bug Priority: Neutral
Reporter: Richard Gange Assignee: Jesus Alonso
Resolution: Fixed Votes: 3
Labels: maintenance, templating
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
causes MGNLDIFF-104 "Compare to previous version" does no... Closed
Relates
relation
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:
Epic Link: Support
Sprint: Maintenance 1
Story Points: 5

 Description   

If you embed JavaScript into Freemarker templates (or any templating language) then there may be occasions where you want to make use of the "&&" operator (i.e. double ampersands) This operator seems create an issue when trying to diff two versions of a page.

The same issue exists when using "<" in javascript code.



 Comments   
Comment by Richard Gange [ 13/Jan/20 ]

The diff module compares the HTML source of two pages. If you embed JavaScript into a page which uses this operator it will break the ability to Diff from the Pages app.

Possible workarounds:

  • Move the JavaScript to a file. Pass the dynamic stuff as parameters to a JS function in a linked file.
  • Refactor the code:
    // old way
    if (someCondition && someOtherCondition) {
        // do something
    }
    
    // workaround
    if (someCondition) {
        if (someOtherCondition) {
            // do something
        }
    }
    
Comment by Le Bao Duy [ 17/Jan/20 ]

We also encountered the same issue with the Diff Module. Magnolia throw org.xml.sax.SAXParseException when trying compares the HTML source of two pages with embed JavaScript.

Comment by Diana Racho [ 20/Feb/20 ]

We have same problem with "<" in javascript code. Can you exclude scripting blocks from diff merge?

Comment by Richard Gange [ 20/Feb/20 ]

Hello diana.racho-

I took a look at the class VersionDiffHtmlGenerator and unfortunately I don't see any such setting.

A couple workaround ideas:

  • Write a implementation of VersionDiffHtmlGenerator that has this feature and override the system implementation.
  • Refactor the JavaScript so that you avoid the issue all together.

HTH

Comment by Jesus Alonso [ 06/Apr/20 ]

It turns out the exception

org.xml.sax.SAXParseException; lineNumber: 391; columnNumber: 22; The entity name must immediately follow the '&' in the entity reference. com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239) com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) org.outerj.daisy.diff.helper.NekoHtmlParser.parse(NekoHtmlParser.java:57) org.outerj.daisy.diff.HtmlCleaner.cleanAndParse(HtmlCleaner.java:41) info.magnolia.module.diff.VersionDiffHtmlGenerator.diff(VersionDiffHtmlGenerator.java:92) info.magnolia.module.diff.VersionDiffHtmlGenerator.diff(VersionDiffHtmlGenerator.java:246) info.magnolia.module.diff.VersionDiffHtmlServlet.getVersionDiffHtml(VersionDiffHtmlServlet.java:259) info.magnolia.module.diff.VersionDiffHtmlServlet.doGet(VersionDiffHtmlServlet.java:146) javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
 

is caused by illegal XML (e.g. &, >, <) characters inside {{<script>}}blocks that need to be properly encoded.

Generated at Mon Feb 12 05:21:26 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.