You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2023/04/20 14:29:19 UTC

[sling-org-apache-sling-rewriter] branch SLING-11833 created (now 5a83678)

This is an automated email from the ASF dual-hosted git repository.

dklco pushed a change to branch SLING-11833
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-rewriter.git


      at 5a83678  SLING-11833 - Fixing issue where script tags are incorrectly escaped

This branch includes the following new commits:

     new 5a83678  SLING-11833 - Fixing issue where script tags are incorrectly escaped

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-rewriter] 01/01: SLING-11833 - Fixing issue where script tags are incorrectly escaped

Posted by dk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch SLING-11833
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-rewriter.git

commit 5a83678ebd921d24778c0fb8c75298458d2e0a37
Author: Dan Klco <kl...@adobe.com>
AuthorDate: Thu Apr 20 10:29:11 2023 -0400

    SLING-11833 - Fixing issue where script tags are incorrectly escaped
---
 .../sling/rewriter/impl/components/Html5Serializer.java   | 15 +++++++++++++--
 .../rewriter/impl/components/Html5SerializerTest.java     | 15 ++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/sling/rewriter/impl/components/Html5Serializer.java b/src/main/java/org/apache/sling/rewriter/impl/components/Html5Serializer.java
index a247424..efa74d6 100644
--- a/src/main/java/org/apache/sling/rewriter/impl/components/Html5Serializer.java
+++ b/src/main/java/org/apache/sling/rewriter/impl/components/Html5Serializer.java
@@ -53,6 +53,7 @@ public class Html5Serializer implements Serializer {
                 "meta", "param", "source", "track", "wbr"));
     }
     private PrintWriter writer;
+    private boolean inScript = false;
 
     @Override
     public void characters(char[] buffer, int offset, int length) throws SAXException {
@@ -62,8 +63,12 @@ public class Html5Serializer implements Serializer {
             if (offset < 0 || offset + length > buffer.length) {
                 throw new SAXException("Offset / length out of bounds");
             }
-            writer.write(
-                    StringEscapeUtils.escapeHtml4(new String(Arrays.copyOfRange(buffer, offset, offset + length))));
+            if (inScript) {
+                writer.write(new String(Arrays.copyOfRange(buffer, offset, offset + length)));
+            } else {
+                writer.write(
+                        StringEscapeUtils.escapeHtml4(new String(Arrays.copyOfRange(buffer, offset, offset + length))));
+            }
         }
     }
 
@@ -84,6 +89,7 @@ public class Html5Serializer implements Serializer {
             writer.write(localName);
             writer.write(CHAR_GT);
         }
+        inScript = false;
     }
 
     @Override
@@ -152,6 +158,11 @@ public class Html5Serializer implements Serializer {
             writer.write("/");
         }
         writer.write(CHAR_GT);
+        if ("script".equals(localName)) {
+            inScript = true;
+        } else {
+            inScript = false;
+        }
     }
 
     private boolean shouldContinue(String localName, Attributes atts, int i) {
diff --git a/src/test/java/org/apache/sling/rewriter/impl/components/Html5SerializerTest.java b/src/test/java/org/apache/sling/rewriter/impl/components/Html5SerializerTest.java
index dda0953..2ae74ac 100644
--- a/src/test/java/org/apache/sling/rewriter/impl/components/Html5SerializerTest.java
+++ b/src/test/java/org/apache/sling/rewriter/impl/components/Html5SerializerTest.java
@@ -17,7 +17,6 @@
 package org.apache.sling.rewriter.impl.components;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
@@ -75,6 +74,20 @@ public class Html5SerializerTest {
         assertEquals("Hello &amp; Goodbye", writer.toString());
     }
 
+    @Test
+    public void testScript() throws IOException, SAXException {
+
+        testSerializer.startElement("", "script", "", new AttributesImpl());
+
+        testSerializer.characters("alert(\"Hello World!\")".toCharArray(), 0, 21);
+        assertEquals("<script>alert(\"Hello World!\")", writer.toString());
+
+        testSerializer.endElement("", "script", "");
+
+        testSerializer.characters(" & Goodbye".toCharArray(), 0, 10);
+        assertEquals("<script>alert(\"Hello World!\")</script> &amp; Goodbye", writer.toString());
+    }
+
     @Test
     public void testZeroLengthCharacters() throws IOException, SAXException {
         testSerializer.characters(new char[0], 0, 0);