Debian Codegraph @

closure-library/closure/goog/html/safestylesheet_test.js

raw
1 // Copyright 2014 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Unit tests for goog.html.SafeStyleSheet and its builders.
17 */
18
19 goog.provide('goog.html.safeStyleSheetTest');
20
21 goog.require('goog.html.SafeStyle');
22 goog.require('goog.html.SafeStyleSheet');
23 goog.require('goog.object');
24 goog.require('goog.string.Const');
25 goog.require('goog.testing.jsunit');
26
27 goog.setTestOnly('goog.html.safeStyleSheetTest');
28
29
30 function testSafeStyleSheet() {
31 var styleSheet = 'P.special { color:red ; }';
32 var safeStyleSheet =
33 goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from(styleSheet));
34 var extracted = goog.html.SafeStyleSheet.unwrap(safeStyleSheet);
35 assertEquals(styleSheet, extracted);
36 assertEquals(styleSheet, safeStyleSheet.getTypedStringValue());
37 assertEquals('SafeStyleSheet{' + styleSheet + '}', String(safeStyleSheet));
38
39 // Interface marker is present.
40 assertTrue(safeStyleSheet.implementsGoogStringTypedString);
41 }
42
43
44 /** @suppress {checkTypes} */
45 function testUnwrap() {
46 var privateFieldName = 'privateDoNotAccessOrElseSafeStyleSheetWrappedValue_';
47 var markerFieldName =
48 'SAFE_STYLE_SHEET_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_';
49 var propNames = goog.object.getKeys(
50 goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('')));
51 assertContains(privateFieldName, propNames);
52 assertContains(markerFieldName, propNames);
53 var evil = {};
54 evil[privateFieldName] = 'P.special { color:expression(evil) ; }';
55 evil[markerFieldName] = {};
56
57 var exception =
58 assertThrows(function() { goog.html.SafeStyleSheet.unwrap(evil); });
59 assertContains('expected object of type SafeStyleSheet', exception.message);
60 }
61
62
63 /**
64 * @param {string} expected
65 * @param {string} selector
66 * @param {!goog.html.SafeStyle.PropertyMap|!goog.html.SafeStyle} style
67 */
68 function assertCreateRuleEquals(expected, selector, style) {
69 var actual = goog.html.SafeStyleSheet.createRule(selector, style);
70 assertEquals(expected, goog.html.SafeStyleSheet.unwrap(actual));
71 }
72
73
74 function testCreateRule() {
75 assertCreateRuleEquals(
76 '#id{top:0;left:0;}', '#id', {'top': '0', 'left': '0'});
77 assertCreateRuleEquals(
78 '.class{margin-left:5px;}',
79 '.class', goog.html.SafeStyle.create({'margin-left': '5px'}));
80 assertCreateRuleEquals(
81 'tag #id, .class{color:black !important;}',
82 'tag #id, .class', {'color': 'black !important'});
83 assertCreateRuleEquals('[title=\'son\\\'s\']{}', '[title=\'son\\\'s\']', {});
84 assertCreateRuleEquals('[title="{"]{}', '[title="{"]', {});
85 assertCreateRuleEquals(':nth-child(1){}', ':nth-child(1)', {});
86
87 assertThrows(function() {
88 goog.html.SafeStyleSheet.createRule('tag{color:black;}', {});
89 });
90 assertThrows(function() {
91 goog.html.SafeStyleSheet.createRule('[title', {});
92 });
93 assertThrows(function() {
94 goog.html.SafeStyleSheet.createRule('[foo)bar]', {});
95 });
96 assertThrows(function() {
97 goog.html.SafeStyleSheet.createRule('[foo[bar]', {});
98 });
99 assertThrows(function() {
100 goog.html.SafeStyleSheet.createRule('foo(bar(baz)', {});
101 });
102 assertThrows(function() {
103 goog.html.SafeStyleSheet.createRule(':nth-child(1', {});
104 });
105 assertThrows(function() {
106 goog.html.SafeStyleSheet.createRule('[type="a]', {});
107 });
108 assertThrows(function() {
109 goog.html.SafeStyleSheet.createRule('[type=\'a]', {});
110 });
111 assertThrows(function() {
112 goog.html.SafeStyleSheet.createRule('<', {});
113 });
114 assertThrows(function() {
115 goog.html.SafeStyleSheet.createRule('@import "foo";#id', {});
116 });
117 }
118
119
120 function testFromConstant_allowsEmptyString() {
121 assertEquals(
122 goog.html.SafeStyleSheet.EMPTY,
123 goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('')));
124 }
125
126
127 function testFromConstant_throwsOnLessThanCharacter() {
128 assertThrows(function() {
129 goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('x<x'));
130 });
131 }
132
133
134 function testConcat() {
135 var styleSheet1 = goog.html.SafeStyleSheet.fromConstant(
136 goog.string.Const.from('P.special { color:red ; }'));
137 var styleSheet2 = goog.html.SafeStyleSheet.fromConstant(
138 goog.string.Const.from('P.regular { color:blue ; }'));
139 var expected = 'P.special { color:red ; }P.special { color:red ; }' +
140 'P.regular { color:blue ; }P.regular { color:blue ; }';
141
142 var concatStyleSheet = goog.html.SafeStyleSheet.concat(
143 styleSheet1, [styleSheet1, styleSheet2], styleSheet2);
144 assertEquals(expected, goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
145
146 // Empty.
147 concatStyleSheet = goog.html.SafeStyleSheet.concat();
148 assertEquals('', goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
149 concatStyleSheet = goog.html.SafeStyleSheet.concat([]);
150 assertEquals('', goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
151 }
152
153
154 function testEmpty() {
155 assertEquals(
156 '', goog.html.SafeStyleSheet.unwrap(goog.html.SafeStyleSheet.EMPTY));
157 }