aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-04-18 13:48:51 -0700
committerValerio Virgillito2012-04-18 13:48:51 -0700
commit2e04af953463643791f6362bd8ef4c6ba190abfa (patch)
treed07aaf646091ddf9dad5b030a7905055fd323490 /node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js
parent616a8532099fec2a15855eac97cd85cb60c4451c (diff)
downloadninja-2e04af953463643791f6362bd8ef4c6ba190abfa.tar.gz
Squashed commit of the following:
commit 2054551bfb01a0f4ca2e138b9d724835462d45cd Merge: 765c2da 616a853 Author: Valerio Virgillito <valerio@motorola.com> Date: Wed Apr 18 13:48:21 2012 -0700 Merge branch 'refs/heads/master' into integration commit 765c2da8e1aa03550caf42b2bd5f367555ad2843 Author: Valerio Virgillito <valerio@motorola.com> Date: Tue Apr 17 15:29:41 2012 -0700 updating the picasa carousel Signed-off-by: Valerio Virgillito <valerio@motorola.com> commit 9484f1c82b81e27edf2dc0a1bcc1fa3b12077406 Merge: d27f2df cacb4a2 Author: Valerio Virgillito <valerio@motorola.com> Date: Tue Apr 17 15:03:50 2012 -0700 Merge branch 'refs/heads/master' into integration commit d27f2df4d846064444263d7832d213535962abe7 Author: Valerio Virgillito <valerio@motorola.com> Date: Wed Apr 11 10:39:36 2012 -0700 integrating new picasa carousel component Signed-off-by: Valerio Virgillito <valerio@motorola.com> commit 6f98384c9ecbc8abe55ccfe1fc25a0c7ce22c493 Author: Valerio Virgillito <valerio@motorola.com> Date: Tue Apr 10 14:33:00 2012 -0700 fixed the text area case issue Text area was renamed from TextArea to Textarea Signed-off-by: Valerio Virgillito <valerio@motorola.com> commit 1e83e26652266136802bc7af930379c1ecd631a6 Author: Valerio Virgillito <valerio@motorola.com> Date: Mon Apr 9 22:10:45 2012 -0700 integrating montage v0.8 into ninja. Signed-off-by: Valerio Virgillito <valerio@motorola.com> Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js')
-rw-r--r--node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js132
1 files changed, 132 insertions, 0 deletions
diff --git a/node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js b/node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js
new file mode 100644
index 00000000..993b4fc6
--- /dev/null
+++ b/node_modules/montage/ui/rich-text-editor/rich-text-editor.reel/rich-text-sanitizer.js
@@ -0,0 +1,132 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6/**
7 @module "montage/ui/rich-text-sanitizer.js"
8 @requires montage/core/core
9*/
10var Montage = require("montage").Montage;
11
12/**
13 @class module:"montage/ui/rich-text-sanitizer.js".Sanitizer
14 @extends module:montage/core/core.Montage
15*/
16exports.Sanitizer = Montage.create(Montage,/** @lends module:"montage/ui/rich-text-sanitizer.js".Sanitizer# */ {
17
18 willSetValue: {
19 value: function(value, uniqueId) {
20 return this._scopeCSS(value, uniqueId);
21 }
22 },
23
24 didGetValue: {
25 value: function(value, uniqueId) {
26 return this._unscopeCSS(value, uniqueId);
27 }
28 },
29
30 willInsertHtmlData: {
31 value: function(data, uniqueId) {
32 return this._scopeCSS(this._removeScript(data), uniqueId);
33 }
34 },
35
36 _scopeCSS: {
37 enumerable: true,
38 value: function(htmlFragment, uniqueId) {
39 var identifierSelector = ".editor-" + uniqueId+ " ";
40
41 if (typeof htmlFragment == "string") {
42 // Extract the style tag and its content
43 htmlFragment = htmlFragment.replace(/(<style ?[^>]*>)([^<]*)(<\/style>)/ig, function(match, pre, style, post) {
44 // Remove any newlines and tab for easier processing
45 style = style.replace(/\t|\n|\r/g, function(char) {if (char == "\t") return " "; return ""});
46
47 // Cleanup any potential leftover from a previous scoping
48 style = style.replace(/\*\.editor-[^ ] +/g, "body");
49 style = style.replace(/\.editor-[^ ]+ /g, "");
50
51 // Extract the selectors for each css block
52 style = style.replace(/([^{]+)({[^}]*})/ig, function(match, selectors, rules) {
53
54 // Split the selectors and add the identifierSelector
55 selectors = selectors.replace(/ *([^,]+)/g, function(match, selector) {
56 // convert body selector
57 if (selector.toLowerCase() == "body") {
58 return "*" + identifierSelector;
59 } else {
60 return identifierSelector + selector;
61 }
62 })
63 return selectors + rules;
64 })
65 return pre + style + post;
66 });
67 }
68
69 return htmlFragment;
70 }
71 },
72
73 _unscopeCSS: {
74 enumerable: true,
75 value: function(htmlFragment, uniqueId) {
76
77 if (typeof htmlFragment == "string") {
78 // Extract the style tag and its content
79 htmlFragment = htmlFragment.replace(/(<style ?[^>]*>)([^<]*)(<\/style>)/ig, function(match, pre, style, post) {
80 style = style.replace(/\*\.editor-[^ ] +/g, "body");
81 style = style.replace(/\.editor-[^ ]+ /g, "");
82 return pre + style + post;
83 });
84 }
85 return htmlFragment;
86 }
87 },
88
89 _removeScript: {
90 enumerable: true,
91 value: function(htmlFragment) {
92 /*
93 Will remove any script tag, onXXX handlers and javascript URLs
94 */
95 var div = document.createElement("div"),
96 _removeScript = function(element) {
97 var children = element.children,
98 child,
99 nbrChildren = children.length,
100 attributes = element.attributes,
101 attribute,
102 nbrAttributes = attributes.length,
103 i;
104
105 for (i = 0; i < nbrAttributes; i ++) {
106 attribute = attributes[i];
107 if (attribute.name.match(/^on[a-z]+/i) || attribute.value.match(/^javascript:/)) {
108 element.removeAttribute(attribute.name);
109 i --;
110 nbrAttributes --;
111 }
112 }
113
114 for (i = 0; i < nbrChildren; i ++) {
115 child = children[i];
116 if (child.tagName == "SCRIPT") {
117 child.parentNode.removeChild(child);
118 i --;
119 nbrChildren --;
120 } else {
121 _removeScript(child);
122 }
123 }
124 };
125
126 div.innerHTML = htmlFragment;
127 _removeScript(div);
128
129 return div.innerHTML;
130 }
131 }
132});