You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sj...@apache.org on 2014/09/30 11:35:58 UTC

[09/12] git commit: jsgui: Replace _.template with custom function that strips comments

jsgui: Replace _.template with custom function that strips comments


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/136abd8a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/136abd8a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/136abd8a

Branch: refs/heads/master
Commit: 136abd8ad41ae9df5be88912377a538978e4ad44
Parents: 34d61f5
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Tue Sep 30 10:06:59 2014 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Tue Sep 30 10:06:59 2014 +0100

----------------------------------------------------------------------
 .../src/main/webapp/assets/js/util/brooklyn.js  | 21 ++++++++-
 .../src/test/javascript/specs/brooklyn-spec.js  | 49 ++++++++++++++++++++
 2 files changed, 68 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/136abd8a/usage/jsgui/src/main/webapp/assets/js/util/brooklyn.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/util/brooklyn.js b/usage/jsgui/src/main/webapp/assets/js/util/brooklyn.js
index 40aa344..702b59b 100644
--- a/usage/jsgui/src/main/webapp/assets/js/util/brooklyn.js
+++ b/usage/jsgui/src/main/webapp/assets/js/util/brooklyn.js
@@ -21,8 +21,8 @@
 /** brooklyn extension to make console methods available and simplify access to other utils */
 
 define([
-    "brooklyn-view", "brooklyn-utils"
-], function (BrooklynViews, BrooklynUtils) {
+    "underscore", "brooklyn-view", "brooklyn-utils"
+], function (_, BrooklynViews, BrooklynUtils) {
 
     /**
      * Makes the console API safe to use:
@@ -60,6 +60,23 @@ define([
         }
     })();
 
+    var template = _.template;
+    _.mixin({
+        /**
+         * @param {string} text
+         * @return string The text with HTML comments removed.
+         */
+        stripComments: function (text) {
+            return text.replace(/<!--(.|[\n\r\t])*?-->\r?\n?/g, "");
+        },
+        /**
+         * As the real _.template, calling stripComments on text.
+         */
+        template: function (text, data, settings) {
+            return template(_.stripComments(text), data, settings);
+        }
+    });
+
     var Brooklyn = {
         view: BrooklynViews,
         util: BrooklynUtils

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/136abd8a/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js b/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
index e953cb0..3fcfcc2 100644
--- a/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
+++ b/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
@@ -75,6 +75,55 @@ define([
 
             });
         });
+    });
+
+    describe("_.stripComments", function () {
+        it("should strip a basic comment", function () {
+            var text = "<p>abc</p>\n <!-- comment-here --> <p>cba</p>";
+            expect(_.stripComments(text)).toBe("<p>abc</p>\n  <p>cba</p>");
+        });
+
+        it("should return an empty string for an empty comment", function () {
+            expect(_.stripComments("<!---->")).toBe("");
+            expect(_.stripComments("<!-- -->")).toBe("");
+        });
+
+        it("should strip multiple comments", function () {
+            var text = "a<!-- one -->b<!--two-->c<!-- three  -->";
+            expect(_.stripComments(text)).toBe("abc");
+        });
+
+        it("should strip trailing newlines", function () {
+            expect(_.stripComments("<!-- a -->\nb")).toBe("b");
+            expect(_.stripComments("<!-- a -->\rb")).toBe("b");
+        });
 
+        it("should leave text with no comments untouched", function () {
+            var text = "<p>abc</p>";
+            expect(_.stripComments(text)).toBe(text);
+        });
+
+        it("should remove the Apache license header from an HTML template", function () {
+            var text = "<!--\n" +
+                    "Licensed to the Apache Software Foundation (ASF) under one\n" +
+                    "or more contributor license agreements.  See the NOTICE file\n" +
+                    "distributed with this work for additional information\n" +
+                    "regarding copyright ownership.  The ASF licenses this file\n" +
+                    "to you under the Apache License, Version 2.0 (the\n" +
+                    "\"License\"); you may not use this file except in compliance\n" +
+                    "with the License.  You may obtain a copy of the License at\n" +
+                    "\n" +
+                     "http://www.apache.org/licenses/LICENSE-2.0\n" +
+                    "\n" +
+                    "Unless required by applicable law or agreed to in writing,\n" +
+                    "software distributed under the License is distributed on an\n" +
+                    "\"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" +
+                    "KIND, either express or implied.  See the License for the\n" +
+                    "specific language governing permissions and limitations\n" +
+                    "under the License.\n" +
+                    "-->\n" +
+                    "real content";
+            expect(_.stripComments(text)).toBe("real content");
+        });
     });
 });
\ No newline at end of file