safestylesheet_test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * @fileoverview Unit tests for goog.html.SafeStyleSheet and its builders.
  16. */
  17. goog.provide('goog.html.safeStyleSheetTest');
  18. goog.require('goog.html.SafeStyle');
  19. goog.require('goog.html.SafeStyleSheet');
  20. goog.require('goog.object');
  21. goog.require('goog.string.Const');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.html.safeStyleSheetTest');
  24. function testSafeStyleSheet() {
  25. var styleSheet = 'P.special { color:red ; }';
  26. var safeStyleSheet =
  27. goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from(styleSheet));
  28. var extracted = goog.html.SafeStyleSheet.unwrap(safeStyleSheet);
  29. assertEquals(styleSheet, extracted);
  30. assertEquals(styleSheet, safeStyleSheet.getTypedStringValue());
  31. assertEquals('SafeStyleSheet{' + styleSheet + '}', String(safeStyleSheet));
  32. // Interface marker is present.
  33. assertTrue(safeStyleSheet.implementsGoogStringTypedString);
  34. }
  35. /** @suppress {checkTypes} */
  36. function testUnwrap() {
  37. var privateFieldName = 'privateDoNotAccessOrElseSafeStyleSheetWrappedValue_';
  38. var markerFieldName =
  39. 'SAFE_STYLE_SHEET_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_';
  40. var propNames = goog.object.getKeys(
  41. goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('')));
  42. assertContains(privateFieldName, propNames);
  43. assertContains(markerFieldName, propNames);
  44. var evil = {};
  45. evil[privateFieldName] = 'P.special { color:expression(evil) ; }';
  46. evil[markerFieldName] = {};
  47. var exception =
  48. assertThrows(function() { goog.html.SafeStyleSheet.unwrap(evil); });
  49. assertContains('expected object of type SafeStyleSheet', exception.message);
  50. }
  51. /**
  52. * @param {string} expected
  53. * @param {string} selector
  54. * @param {!goog.html.SafeStyle.PropertyMap|!goog.html.SafeStyle} style
  55. */
  56. function assertCreateRuleEquals(expected, selector, style) {
  57. var actual = goog.html.SafeStyleSheet.createRule(selector, style);
  58. assertEquals(expected, goog.html.SafeStyleSheet.unwrap(actual));
  59. }
  60. function testCreateRule() {
  61. assertCreateRuleEquals(
  62. '#id{top:0;left:0;}', '#id', {'top': '0', 'left': '0'});
  63. assertCreateRuleEquals(
  64. '.class{margin-left:5px;}',
  65. '.class', goog.html.SafeStyle.create({'margin-left': '5px'}));
  66. assertCreateRuleEquals(
  67. 'tag #id, .class{color:black !important;}',
  68. 'tag #id, .class', {'color': 'black !important'});
  69. assertCreateRuleEquals('[title=\'son\\\'s\']{}', '[title=\'son\\\'s\']', {});
  70. assertCreateRuleEquals('[title="{"]{}', '[title="{"]', {});
  71. assertCreateRuleEquals(':nth-child(1){}', ':nth-child(1)', {});
  72. assertThrows(function() {
  73. goog.html.SafeStyleSheet.createRule('tag{color:black;}', {});
  74. });
  75. assertThrows(function() {
  76. goog.html.SafeStyleSheet.createRule('[title', {});
  77. });
  78. assertThrows(function() {
  79. goog.html.SafeStyleSheet.createRule('[foo)bar]', {});
  80. });
  81. assertThrows(function() {
  82. goog.html.SafeStyleSheet.createRule('[foo[bar]', {});
  83. });
  84. assertThrows(function() {
  85. goog.html.SafeStyleSheet.createRule('foo(bar(baz)', {});
  86. });
  87. assertThrows(function() {
  88. goog.html.SafeStyleSheet.createRule(':nth-child(1', {});
  89. });
  90. assertThrows(function() {
  91. goog.html.SafeStyleSheet.createRule('[type="a]', {});
  92. });
  93. assertThrows(function() {
  94. goog.html.SafeStyleSheet.createRule('[type=\'a]', {});
  95. });
  96. assertThrows(function() {
  97. goog.html.SafeStyleSheet.createRule('<', {});
  98. });
  99. assertThrows(function() {
  100. goog.html.SafeStyleSheet.createRule('@import "foo";#id', {});
  101. });
  102. }
  103. function testFromConstant_allowsEmptyString() {
  104. assertEquals(
  105. goog.html.SafeStyleSheet.EMPTY,
  106. goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('')));
  107. }
  108. function testFromConstant_throwsOnLessThanCharacter() {
  109. assertThrows(function() {
  110. goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from('x<x'));
  111. });
  112. }
  113. function testConcat() {
  114. var styleSheet1 = goog.html.SafeStyleSheet.fromConstant(
  115. goog.string.Const.from('P.special { color:red ; }'));
  116. var styleSheet2 = goog.html.SafeStyleSheet.fromConstant(
  117. goog.string.Const.from('P.regular { color:blue ; }'));
  118. var expected = 'P.special { color:red ; }P.special { color:red ; }' +
  119. 'P.regular { color:blue ; }P.regular { color:blue ; }';
  120. var concatStyleSheet = goog.html.SafeStyleSheet.concat(
  121. styleSheet1, [styleSheet1, styleSheet2], styleSheet2);
  122. assertEquals(expected, goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
  123. // Empty.
  124. concatStyleSheet = goog.html.SafeStyleSheet.concat();
  125. assertEquals('', goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
  126. concatStyleSheet = goog.html.SafeStyleSheet.concat([]);
  127. assertEquals('', goog.html.SafeStyleSheet.unwrap(concatStyleSheet));
  128. }
  129. function testEmpty() {
  130. assertEquals(
  131. '', goog.html.SafeStyleSheet.unwrap(goog.html.SafeStyleSheet.EMPTY));
  132. }