forked from jhollingworth/bootstrap-wysihtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserRules_test.js
131 lines (95 loc) · 4.8 KB
/
parserRules_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
if (wysihtml5.browser.supported()) {
var colors = { silver: 1, gray: 1, white: 1, maroon: 1, red: 1, purple: 1,
fuchsia: 1, green: 1, lime: 1, olive: 1, yellow: 1, navy: 1,
blue: 1, teal: 1, aqua: 1, orange: 1 };
var colorText = '';
for(c in colors) {
colorText = colorText + '<span class="wysiwig-color-' + c + '">' + c + '</span>';
}
module("wysihtml5.bootstrap.parserRules", {
setup: function() {
wysihtml5.dom.insertCSS([
"#wysihtml5-test-textarea { width: 200px; height: 100px; margin-top: 5px; font-style: italic; border: 2px solid red; border-radius: 2px; }",
"#wysihtml5-test-textarea:focus { margin-top: 10px; }"
]).into(document);
this.textareaElement = document.createElement("textarea");
this.textareaElement.id = "wysihtml5-test-textarea";
this.textareaElement.title = "Please enter your foo";
this.textareaElement.value = "hey tiff, what's up?";
this.form = document.createElement("form");
this.form.onsubmit = function() { return false; };
this.form.appendChild(this.textareaElement);
this.originalBodyClassName = document.body.className;
document.body.appendChild(this.form);
this.colors = { silver: 1, gray: 1, white: 1, maroon: 1, red: 1, purple: 1,
fuchsia: 1, green: 1, lime: 1, olive: 1, yellow: 1, navy: 1,
blue: 1, teal: 1, aqua: 1, orange: 1 };
this.colorText = '';
for(c in this.colors) {
this.colorText = this.colorText + '<span class="wysiwyg-color-' + c + '">' + c + '</span>';
};
},
teardown: function() {
var leftover;
while (leftover = document.querySelector("iframe.wysihtml5-sandbox, input[name='_wysihtml5_mode']")) {
leftover.parentNode.removeChild(leftover);
}
this.form.parentNode.removeChild(this.form);
document.body.className = this.originalBodyClassName;
},
getComposerElement: function() {
return this.getIframeElement().contentWindow.document.body;
},
getIframeElement: function() {
var iframes = document.querySelectorAll("iframe.wysihtml5-sandbox");
return iframes[iframes.length - 1];
}
});
asyncTest("check that he default parser rules work as expected", function() {
expect(6);
var that = this;
$(this.textareaElement).wysihtml5();
var editor = $(this.textareaElement).data('wysihtml5').editor;
editor.observe("load", function() {
equal(editor.parse("hello <b>foo</b>!").toLowerCase(), "hello <b>foo</b>!", "<b></b> tags are allowed");
equal(editor.parse(that.colorText).toLowerCase(), that.colorText, 'classes used for font colors are allowed');
equal(editor.parse("hello <strong>foo</strong>!").toLowerCase(), "hello foo!", "<strong></strong> tags are stripped out");
equal(editor.parse("hello <faketag>foo</faketag>!").toLowerCase(), "hello foo!", "unrecognized tags are stripped out");
equal(editor.parse('hello <a href="http://seospammer.com">spam</a>!').toLowerCase(),
'hello <a target="_blank" rel="nofollow" href="http://seospammer.com">spam</a>!',
'<a></a> tags have target="_blank" and rel="nofollow" attributes added');
equal(editor.parse('standard internet image: <img width="foo" height="50px" src="cdn0.sbnation.com/imported_assets/155249/kitties.jpg">').toLowerCase(),
'standard internet image: <img alt="" height="50">',
'<img> tags have alt attribute added, strip out non-numeric characters in width/height, require fully-qualified url in src');
start();
});
});
asyncTest("check that parserRules passed in as options work properly", function() {
expect(6);
var that = this;
$(this.textareaElement).wysihtml5('deepExtend', {
parserRules: {
classes: {
bar: 1,
spanner: 1
},
tags: {
strong: 1,
blink: 1,
em: { rename_tag: "blink" },
span: {}
}
}
});
var editor = $(this.textareaElement).data('wysihtml5').editor;
editor.observe("load", function() {
equal(editor.parse("hello <b>foo</b>!").toLowerCase(), "hello <b>foo</b>!", "<b></b> tags are still allowed");
equal(editor.parse('hello <span class="spanner">foo</span>!').toLowerCase(), 'hello <span class="spanner">foo</span>!', "<span></span> tags are allowed")
equal(editor.parse(that.colorText).toLowerCase(), that.colorText, 'classes used for font color are still allowed');
equal(editor.parse("hello <strong>foo</strong>!").toLowerCase(), "hello <strong>foo</strong>!", "<strong></strong> tags are allowed");
equal(editor.parse('hello <i class="bar">foo</i>!').toLowerCase(), 'hello <i class="bar">foo</i>!', '<i class="bar"> survives');
equal(editor.parse("hello <em>foo</em>!").toLowerCase(), "hello <blink>foo</blink>!", "<em></em> tags are converted to <blink></blink>");
start();
});
});
}