1 // mocha/suite.js for normalize-selector.js
6 if (typeof require == 'function') {
7 // enable to re-use in a browser without require.js
8 normalizeSelector = require('../../lib/normalize-selector.js');
9 assert = require('assertik');
11 assert = window.assertik;
14 suite('normalizeSelector');
16 test('should be function', function () {
17 assert.equal(typeof normalizeSelector, 'function', 'wrong type');
20 test('should normalize BIG SELECTOR', function () {
21 var selector = "*~*>*.foo[ href *= \"/\" ]:hover>*[ data-foo = " +
22 "\"bar\" ]:focus+*.baz::after";
23 var expected = "* ~ * > *.foo[href*=\"/\"]:hover > *[data-foo=\"bar\"]:" +
24 "focus + *.baz::after";
25 assert.equal(normalizeSelector(selector), expected);
28 test('should return optimized selector with no change', function () {
29 assert.equal(normalizeSelector("#foo .bar"), "#foo .bar");
32 test('should trim whitespace', function () {
33 assert.equal(normalizeSelector(" #foo .bar "), "#foo .bar");
36 test('should separate between combinators', function () {
37 assert.equal(normalizeSelector("#foo>.bar+.baz"), "#foo > .bar + .baz");
40 test('should not separate concatenated classes', function () {
41 assert.equal(normalizeSelector("#foo.bar.baz"), "#foo.bar.baz");
44 test('should normalize asterisks', function () {
45 var selector = " *.class[ data * = 'data' ] ";
46 assert.equal(normalizeSelector(selector), "*.class[data*='data']");
49 test('should remove comments', function () {
50 assert.equal(normalizeSelector(".e1 /* c2 */ .e2"), ".e1 .e2");
51 assert.equal(normalizeSelector(" /*c1*/ .e1/*c2*/.e2 /*c3*/ .e3 /*c4*/ "), ".e1 .e2 .e3");
52 assert.equal(normalizeSelector(" /*c1*/ .e1/*c2*/.e2 /*c3*/ .e3 "), ".e1 .e2 .e3");
53 assert.equal(normalizeSelector("/*c1*/.e1/*c2*/.e2 /*c3*/ .e3"), ".e1 .e2 .e3");
54 assert.equal(normalizeSelector(".e1/*c2*/.e2 /*c3*/ .e3"), ".e1 .e2 .e3");
57 test('should replace comments with single whitespace', function () {
58 assert.equal(normalizeSelector("tag/* c2 */tag"), "tag tag");
61 test('should normalize parentheses', function() {
62 var selector = "((a ) (b(c ) ) d )>*[ data-foo = \"bar\" ]";
63 var expected = "((a)(b(c))d) > *[data-foo=\"bar\"]";
64 assert.equal(normalizeSelector(selector), expected);
67 test('should normalize @-rule parentheses', function () {
68 var selector = "@media screen and ( color ), projection and (color )";
69 var expected = "@media screen and (color), projection and (color)";
70 assert.equal(normalizeSelector(selector), expected);
73 test('should normalize @-rules with compound parentheses', function () {
74 var selector = "@media handheld and ( min-width : 20em ), screen " +
75 "and ( min-width: 20em )";
76 var expected = "@media handheld and (min-width:20em), screen and " +
78 assert.equal(normalizeSelector(selector), expected);
81 test('should normalize @-rules with operations', function () {
82 var selector = "@media screen and ( device-aspect-ratio : 2560 / 1440 )";
83 var expected = "@media screen and (device-aspect-ratio:2560/1440)";
84 assert.equal(normalizeSelector(selector), expected);
87 test('should normalize descriptors', function () {
88 var selector = "@counter-style triangle";
89 assert.equal(normalizeSelector(selector), "@counter-style triangle");
92 test('should normalize case-insensitivity attribute selector', function () {
93 assert.equal(normalizeSelector("[ att ~= val i ]"), "[att~=val i]");
94 assert.equal(normalizeSelector("#foo[ a = \"b\" i ]"), "#foo[a=\"b\" i]");
97 test('should normalize namespaced attribute selector', function () {
98 var selector = ' unit[ sh | quantity = "200" ] ';
99 var expected = 'unit[sh|quantity="200"]';
100 assert.equal(normalizeSelector(selector), expected);
103 test('should normalize pseudo-classes', function () {
104 var selector = " :nth-last-of-type( ) ";
105 assert.equal(normalizeSelector(selector), ":nth-last-of-type()");
108 test('should normalize pseudo-elements', function () {
109 var selector = " ::nth-fragment( ) ";
110 assert.equal(normalizeSelector(selector), "::nth-fragment()");
113 test('should normalize backslashes', function () {
114 var selector = "#foo[ a = \" b \\\" c\\\\\" ]";
115 var expected = "#foo[a=\" b \\\" c\\\\\"]";
116 assert.equal(normalizeSelector(selector), expected);