| 1 |
// Copyright 2016 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 the CSS specificity calculator. */ |
| 16 |
|
| 17 |
goog.module('goog.html.CssSpecificityTest'); |
| 18 |
goog.setTestOnly(); |
| 19 |
|
| 20 |
var CssSpecificity = goog.require('goog.html.CssSpecificity'); |
| 21 |
var testSuite = goog.require('goog.testing.testSuite'); |
| 22 |
var userAgent = goog.require('goog.userAgent'); |
| 23 |
var userAgentProduct = goog.require('goog.userAgent.product'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* @param {!Array<number>} expected |
| 28 |
* @param {string} selector |
| 29 |
*/ |
| 30 |
function assertSpecificityEquals(expected, selector) { |
| 31 |
var specificity = CssSpecificity.getSpecificity(selector); |
| 32 |
if (userAgentProduct.IE && !userAgent.isVersionOrHigher(9)) { |
| 33 |
assertArrayEquals([0, 0, 0, 0], specificity); |
| 34 |
} else { |
| 35 |
assertArrayEquals(expected, specificity); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
testSuite({ |
| 40 |
testGetSpecificity: function() { |
| 41 |
// @see http://css-tricks.com/specifics-on-css-specificity/ |
| 42 |
assertSpecificityEquals([0, 1, 1, 3], 'ul#nav li.active a'); |
| 43 |
assertSpecificityEquals([0, 0, 2, 3], 'body.ie7 .col_3 h2 ~ h2'); |
| 44 |
assertSpecificityEquals([0, 1, 0, 2], '#footer *:not(nav) li'); |
| 45 |
assertSpecificityEquals([0, 0, 0, 7], 'ul > li ul li ol li:first-letter'); |
| 46 |
|
| 47 |
// @see http://reference.sitepoint.com/css/specificity |
| 48 |
assertSpecificityEquals([0, 2, 1, 3], 'body#home div#warning p.message'); |
| 49 |
assertSpecificityEquals([0, 2, 1, 3], '* body#home>div#warning p.message'); |
| 50 |
assertSpecificityEquals([0, 2, 1, 1], '#home #warning p.message'); |
| 51 |
assertSpecificityEquals([0, 1, 1, 1], '#warning p.message'); |
| 52 |
assertSpecificityEquals([0, 1, 0, 1], '#warning p'); |
| 53 |
assertSpecificityEquals([0, 0, 1, 1], 'p.message'); |
| 54 |
assertSpecificityEquals([0, 0, 0, 1], 'p'); |
| 55 |
|
| 56 |
// Test pseudo-element with uppercase letters. |
| 57 |
assertSpecificityEquals([0, 0, 0, 2], 'li:bEfoRE'); |
| 58 |
|
| 59 |
// Pseudo-class tests. |
| 60 |
assertSpecificityEquals([0, 0, 1, 2], 'li:first-child+p'); |
| 61 |
assertSpecificityEquals([0, 0, 1, 2], 'li:nth-child(even)+p'); |
| 62 |
assertSpecificityEquals([0, 0, 1, 2], 'li:nth-child(2n+1)+p'); |
| 63 |
assertSpecificityEquals([0, 0, 1, 2], 'li:nth-child( 2n + 1 )+p'); |
| 64 |
assertSpecificityEquals([0, 0, 1, 2], 'li:nth-child(2n-1)+p'); |
| 65 |
assertSpecificityEquals([0, 0, 1, 2], 'li:nth-child(2n-1) p'); |
| 66 |
assertSpecificityEquals([0, 0, 1, 0], ':lang(nl-be)'); |
| 67 |
} |
| 68 |
}); |