| 1 |
// Copyright 2008 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 |
goog.provide('goog.cssomTest'); |
| 16 |
goog.setTestOnly('goog.cssomTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.cssom'); |
| 20 |
goog.require('goog.cssom.CssRuleType'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.userAgent'); |
| 23 |
|
| 24 |
// Since sheet cssom_test1.css's first line is to import |
| 25 |
// cssom_test2.css, we should get 2 before one in the string. |
| 26 |
var cssText = '.css-link-1 { display: block; } ' + |
| 27 |
'.css-import-2 { display: block; } ' + |
| 28 |
'.css-import-1 { display: block; } ' + |
| 29 |
'.css-style-1 { display: block; } ' + |
| 30 |
'.css-style-2 { display: block; } ' + |
| 31 |
'.css-style-3 { display: block; }'; |
| 32 |
|
| 33 |
var replacementCssText = '.css-repl-1 { display: block; }'; |
| 34 |
|
| 35 |
var isIe7 = goog.userAgent.IE && |
| 36 |
(goog.userAgent.compare(goog.userAgent.VERSION, '7.0') == 0); |
| 37 |
|
| 38 |
// We're going to toLowerCase cssText before testing, because IE returns |
| 39 |
// CSS property names in UPPERCASE, and the function shouldn't |
| 40 |
// "fix" the text as it would be expensive and rarely of use. |
| 41 |
// Same goes for the trailing whitespace in IE. |
| 42 |
// Same goes for fixing the optimized removal of trailing ; in rules. |
| 43 |
// Also needed for Opera. |
| 44 |
function fixCssTextForIe(cssText) { |
| 45 |
cssText = cssText.toLowerCase().replace(/\s*$/, ''); |
| 46 |
if (cssText.match(/[^;] \}/)) { |
| 47 |
cssText = cssText.replace(/([^;]) \}/g, '$1; }'); |
| 48 |
} |
| 49 |
return cssText; |
| 50 |
} |
| 51 |
|
| 52 |
function testGetFileNameFromStyleSheet() { |
| 53 |
var styleSheet = {'href': 'http://foo.com/something/filename.css'}; |
| 54 |
assertEquals( |
| 55 |
'filename.css', goog.cssom.getFileNameFromStyleSheet(styleSheet)); |
| 56 |
|
| 57 |
styleSheet = {'href': 'https://foo.com:123/something/filename.css'}; |
| 58 |
assertEquals( |
| 59 |
'filename.css', goog.cssom.getFileNameFromStyleSheet(styleSheet)); |
| 60 |
|
| 61 |
styleSheet = {'href': 'http://foo.com/something/filename.css?bar=bas'}; |
| 62 |
assertEquals( |
| 63 |
'filename.css', goog.cssom.getFileNameFromStyleSheet(styleSheet)); |
| 64 |
|
| 65 |
styleSheet = {'href': 'filename.css?bar=bas'}; |
| 66 |
assertEquals( |
| 67 |
'filename.css', goog.cssom.getFileNameFromStyleSheet(styleSheet)); |
| 68 |
|
| 69 |
styleSheet = {'href': 'filename.css'}; |
| 70 |
assertEquals( |
| 71 |
'filename.css', goog.cssom.getFileNameFromStyleSheet(styleSheet)); |
| 72 |
} |
| 73 |
|
| 74 |
function testGetAllCssStyleSheets() { |
| 75 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 76 |
assertEquals(4, styleSheets.length); |
| 77 |
// Makes sure they're in the right cascade order. |
| 78 |
assertEquals( |
| 79 |
'cssom_test_link_1.css', |
| 80 |
goog.cssom.getFileNameFromStyleSheet(styleSheets[0])); |
| 81 |
assertEquals( |
| 82 |
'cssom_test_import_2.css', |
| 83 |
goog.cssom.getFileNameFromStyleSheet(styleSheets[1])); |
| 84 |
assertEquals( |
| 85 |
'cssom_test_import_1.css', |
| 86 |
goog.cssom.getFileNameFromStyleSheet(styleSheets[2])); |
| 87 |
// Not an external styleSheet |
| 88 |
assertNull(goog.cssom.getFileNameFromStyleSheet(styleSheets[3])); |
| 89 |
} |
| 90 |
|
| 91 |
function testGetAllCssText() { |
| 92 |
var allCssText = goog.cssom.getAllCssText(); |
| 93 |
// In IE7, a CSSRule object gets included twice and replaces another |
| 94 |
// existing CSSRule object. We aren't using |
| 95 |
// goog.testing.ExpectedFailures since it brings in additional CSS |
| 96 |
// which breaks a lot of our expectations about the number of rules |
| 97 |
// present in a style sheet. |
| 98 |
if (!isIe7) { |
| 99 |
assertEquals(cssText, fixCssTextForIe(allCssText)); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
function testGetAllCssStyleRules() { |
| 104 |
var allCssRules = goog.cssom.getAllCssStyleRules(); |
| 105 |
assertEquals(6, allCssRules.length); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
function testAddCssText() { |
| 110 |
var newCssText = '.css-add-1 { display: block; }'; |
| 111 |
var newCssNode = goog.cssom.addCssText(newCssText); |
| 112 |
|
| 113 |
assertEquals(document.styleSheets.length, 3); |
| 114 |
|
| 115 |
var allCssText = goog.cssom.getAllCssText(); |
| 116 |
|
| 117 |
// In IE7, a CSSRule object gets included twice and replaces another |
| 118 |
// existing CSSRule object. We aren't using |
| 119 |
// goog.testing.ExpectedFailures since it brings in additional CSS |
| 120 |
// which breaks a lot of our expectations about the number of rules |
| 121 |
// present in a style sheet. |
| 122 |
if (!isIe7) { |
| 123 |
// Opera inserts the CSSRule to the first position. And fixCssText |
| 124 |
// is also needed to clean up whitespace. |
| 125 |
if (goog.userAgent.OPERA) { |
| 126 |
assertEquals(newCssText + ' ' + cssText, fixCssTextForIe(allCssText)); |
| 127 |
} else { |
| 128 |
assertEquals(cssText + ' ' + newCssText, fixCssTextForIe(allCssText)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 133 |
assertEquals(7, cssRules.length); |
| 134 |
|
| 135 |
// Remove the new stylesheet now so it doesn't interfere with other |
| 136 |
// tests. |
| 137 |
newCssNode.parentNode.removeChild(newCssNode); |
| 138 |
// Sanity check. |
| 139 |
cssRules = goog.cssom.getAllCssStyleRules(); |
| 140 |
assertEquals(6, cssRules.length); |
| 141 |
} |
| 142 |
|
| 143 |
function testAddCssRule() { |
| 144 |
// test that addCssRule correctly adds the rule to the style |
| 145 |
// sheet. |
| 146 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 147 |
var styleSheet = styleSheets[3]; |
| 148 |
var newCssRule = '.css-addCssRule { display: block; }'; |
| 149 |
var rules = styleSheet.rules || styleSheet.cssRules; |
| 150 |
var origNumberOfRules = rules.length; |
| 151 |
|
| 152 |
goog.cssom.addCssRule(styleSheet, newCssRule, 1); |
| 153 |
|
| 154 |
rules = styleSheet.rules || styleSheet.cssRules; |
| 155 |
var newNumberOfRules = rules.length; |
| 156 |
assertEquals(newNumberOfRules, origNumberOfRules + 1); |
| 157 |
|
| 158 |
// Remove the added rule so we don't mess up other tests. |
| 159 |
goog.cssom.removeCssRule(styleSheet, 1); |
| 160 |
} |
| 161 |
|
| 162 |
function testAddCssRuleAtPos() { |
| 163 |
// test that addCssRule correctly adds the rule to the style |
| 164 |
// sheet at the specified position. |
| 165 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 166 |
var styleSheet = styleSheets[3]; |
| 167 |
var newCssRule = '.css-addCssRulePos { display: block; }'; |
| 168 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 169 |
var origNumberOfRules = rules.length; |
| 170 |
|
| 171 |
// Firefox croaks if we try to insert a CSSRule at an index that |
| 172 |
// contains a CSSImport Rule. Since we deal only with CSSStyleRule |
| 173 |
// objects, we find the first CSSStyleRule and return its index. |
| 174 |
// |
| 175 |
// NOTE(user): We could have unified the code block below for all |
| 176 |
// browsers but IE6 horribly mangled up the stylesheet by creating |
| 177 |
// duplicate instances of a rule when removeCssRule was invoked |
| 178 |
// just after addCssRule with the looping construct in. This is |
| 179 |
// perfectly fine since IE's styleSheet.rules does not contain |
| 180 |
// references to anything but CSSStyleRules. |
| 181 |
var pos = 0; |
| 182 |
if (styleSheet.cssRules) { |
| 183 |
pos = goog.array.findIndex(rules, function(rule) { |
| 184 |
return rule.type == goog.cssom.CssRuleType.STYLE; |
| 185 |
}); |
| 186 |
} |
| 187 |
goog.cssom.addCssRule(styleSheet, newCssRule, pos); |
| 188 |
|
| 189 |
rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 190 |
var newNumberOfRules = rules.length; |
| 191 |
assertEquals(newNumberOfRules, origNumberOfRules + 1); |
| 192 |
|
| 193 |
// Remove the added rule so we don't mess up other tests. |
| 194 |
goog.cssom.removeCssRule(styleSheet, pos); |
| 195 |
|
| 196 |
rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 197 |
assertEquals(origNumberOfRules, rules.length); |
| 198 |
} |
| 199 |
|
| 200 |
function testAddCssRuleNoIndex() { |
| 201 |
// How well do we handle cases where the optional index is |
| 202 |
// not passed in? |
| 203 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 204 |
var styleSheet = styleSheets[3]; |
| 205 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 206 |
var origNumberOfRules = rules.length; |
| 207 |
var newCssRule = '.css-addCssRuleNoIndex { display: block; }'; |
| 208 |
|
| 209 |
// Try inserting the rule without specifying an index. |
| 210 |
// Make sure we don't throw an exception, and that we added |
| 211 |
// the entry. |
| 212 |
goog.cssom.addCssRule(styleSheet, newCssRule); |
| 213 |
|
| 214 |
rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 215 |
var newNumberOfRules = rules.length; |
| 216 |
assertEquals(newNumberOfRules, origNumberOfRules + 1); |
| 217 |
|
| 218 |
// Remove the added rule so we don't mess up the other tests. |
| 219 |
goog.cssom.removeCssRule(styleSheet, newNumberOfRules - 1); |
| 220 |
|
| 221 |
rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 222 |
assertEquals(origNumberOfRules, rules.length); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
function testGetParentStyleSheetAfterGetAllCssStyleRules() { |
| 227 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 228 |
var cssRule = cssRules[4]; |
| 229 |
var parentStyleSheet = goog.cssom.getParentStyleSheet(cssRule); |
| 230 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 231 |
var styleSheet = styleSheets[3]; |
| 232 |
assertEquals(styleSheet, parentStyleSheet); |
| 233 |
} |
| 234 |
|
| 235 |
function testGetCssRuleIndexInParentStyleSheetAfterGetAllCssStyleRules() { |
| 236 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 237 |
var cssRule = cssRules[4]; |
| 238 |
// Note here that this is correct - IE's styleSheet.rules does not |
| 239 |
// contain references to anything but CSSStyleRules while FF and others |
| 240 |
// include anything that inherits from the CSSRule interface. |
| 241 |
// See http://dev.w3.org/csswg/cssom/#cssrule. |
| 242 |
var parentStyleSheet = goog.cssom.getParentStyleSheet(cssRule); |
| 243 |
var ruleIndex = goog.isDefAndNotNull(parentStyleSheet.cssRules) ? 2 : 1; |
| 244 |
assertEquals( |
| 245 |
ruleIndex, goog.cssom.getCssRuleIndexInParentStyleSheet(cssRule)); |
| 246 |
} |
| 247 |
|
| 248 |
function testGetCssRuleIndexInParentStyleSheetNonStyleRule() { |
| 249 |
// IE's styleSheet.rules only contain CSSStyleRules. |
| 250 |
if (!goog.userAgent.IE) { |
| 251 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 252 |
var styleSheet = styleSheets[3]; |
| 253 |
var newCssRule = '@media print { .css-nonStyle { display: block; } }'; |
| 254 |
goog.cssom.addCssRule(styleSheet, newCssRule); |
| 255 |
var rules = styleSheet.rules || styleSheet.cssRules; |
| 256 |
var cssRule = rules[rules.length - 1]; |
| 257 |
assertEquals(goog.cssom.CssRuleType.MEDIA, cssRule.type); |
| 258 |
// Make sure we don't throw an exception. |
| 259 |
goog.cssom.getCssRuleIndexInParentStyleSheet(cssRule, styleSheet); |
| 260 |
// Remove the added rule. |
| 261 |
goog.cssom.removeCssRule(styleSheet, rules.length - 1); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// Tests the scenario where we have a known stylesheet and index. |
| 266 |
function testReplaceCssRuleWithStyleSheetAndIndex() { |
| 267 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 268 |
var styleSheet = styleSheets[3]; |
| 269 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 270 |
var index = 2; |
| 271 |
var origCssRule = rules[index]; |
| 272 |
var origCssText = |
| 273 |
fixCssTextForIe(goog.cssom.getCssTextFromCssRule(origCssRule)); |
| 274 |
|
| 275 |
goog.cssom.replaceCssRule(origCssRule, replacementCssText, styleSheet, index); |
| 276 |
|
| 277 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 278 |
var newCssRule = rules[index]; |
| 279 |
var newCssText = goog.cssom.getCssTextFromCssRule(newCssRule); |
| 280 |
assertEquals(replacementCssText, fixCssTextForIe(newCssText)); |
| 281 |
|
| 282 |
// Now we need to re-replace our rule, to preserve parity for the other |
| 283 |
// tests. |
| 284 |
goog.cssom.replaceCssRule(newCssRule, origCssText, styleSheet, index); |
| 285 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 286 |
var nowCssRule = rules[index]; |
| 287 |
var nowCssText = goog.cssom.getCssTextFromCssRule(nowCssRule); |
| 288 |
assertEquals(origCssText, fixCssTextForIe(nowCssText)); |
| 289 |
} |
| 290 |
|
| 291 |
function testReplaceCssRuleUsingGetAllCssStyleRules() { |
| 292 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 293 |
var origCssRule = cssRules[4]; |
| 294 |
var origCssText = |
| 295 |
fixCssTextForIe(goog.cssom.getCssTextFromCssRule(origCssRule)); |
| 296 |
// notice we don't pass in the stylesheet or index. |
| 297 |
goog.cssom.replaceCssRule(origCssRule, replacementCssText); |
| 298 |
|
| 299 |
var styleSheets = goog.cssom.getAllCssStyleSheets(); |
| 300 |
var styleSheet = styleSheets[3]; |
| 301 |
var rules = goog.cssom.getCssRulesFromStyleSheet(styleSheet); |
| 302 |
var index = goog.isDefAndNotNull(styleSheet.cssRules) ? 2 : 1; |
| 303 |
var newCssRule = rules[index]; |
| 304 |
var newCssText = |
| 305 |
fixCssTextForIe(goog.cssom.getCssTextFromCssRule(newCssRule)); |
| 306 |
assertEquals(replacementCssText, newCssText); |
| 307 |
|
| 308 |
// try getting it the other way around too. |
| 309 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 310 |
var newCssRule = cssRules[4]; |
| 311 |
var newCssText = |
| 312 |
fixCssTextForIe(goog.cssom.getCssTextFromCssRule(newCssRule)); |
| 313 |
assertEquals(replacementCssText, newCssText); |
| 314 |
|
| 315 |
// Now we need to re-replace our rule, to preserve parity for the other |
| 316 |
// tests. |
| 317 |
goog.cssom.replaceCssRule(newCssRule, origCssText); |
| 318 |
var cssRules = goog.cssom.getAllCssStyleRules(); |
| 319 |
var nowCssRule = cssRules[4]; |
| 320 |
var nowCssText = |
| 321 |
fixCssTextForIe(goog.cssom.getCssTextFromCssRule(nowCssRule)); |
| 322 |
assertEquals(origCssText, nowCssText); |
| 323 |
} |