Browse Source

BUGFIX: HTML output which is then converted to a regular string (for a standard operation) still contained escaped HTML chars.

n1474335 8 năm trước cách đây
mục cha
commit
fc9c2d1f6e
3 tập tin đã thay đổi với 52 bổ sung1 xóa
  1. 1 1
      src/js/core/Dish.js
  2. 27 0
      src/js/core/Utils.js
  3. 24 0
      test/tests/operations/StrUtils.js

+ 1 - 1
src/js/core/Dish.js

@@ -139,7 +139,7 @@ Dish.prototype.translate = function(toType) {
             this.type = Dish.BYTE_ARRAY;
             break;
         case Dish.HTML:
-            this.value = this.value ? Utils.strToByteArray(Utils.stripHtmlTags(this.value, true)) : [];
+            this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
             this.type = Dish.BYTE_ARRAY;
             break;
         default:

+ 27 - 0
src/js/core/Utils.js

@@ -928,6 +928,33 @@ var Utils = {
     },
 
 
+    /**
+     * Unescapes HTML tags in a string to make them render again.
+     *
+     * @param {string} str
+     * @returns string
+     *
+     * @example
+     * // return "A <script> tag"
+     * Utils.unescapeHtml("A &lt;script&gt; tag");
+     */
+    unescapeHtml: function(str) {
+        var HTML_CHARS = {
+            "&amp;":  "&",
+            "&lt;":   "<",
+            "&gt;":   ">",
+            "&quot;": '"',
+            "&#x27;": "'",
+            "&#x2F;": "/",
+            "&#x60;": "`"
+        };
+
+        return str.replace(/&#?x?[a-z0-9]{2,4};/ig, function (match) {
+            return HTML_CHARS[match] || match;
+        });
+    },
+
+
     /**
      * Expresses a number of milliseconds in a human readable format.
      *

+ 24 - 0
test/tests/operations/StrUtils.js

@@ -0,0 +1,24 @@
+/**
+ * StrUtils tests.
+ *
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2017
+ * @license Apache-2.0
+ */
+TestRegister.addTests([
+    {
+        name: "Regex, non-HTML op",
+        input: "/<>",
+        expectedOutput: "/<>",
+        recipeConfig: [
+            {
+                "op": "Regular expression",
+                "args": ["User defined", "", true, true, false, "Highlight matches"]
+            },
+            {
+                "op": "Remove whitespace",
+                "args": [true, true, true, true, true, false]
+            }
+        ],
+    },
+]);