suite.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // mocha/suite.js for normalize-selector.js
  2. var normalizeSelector;
  3. var assert;
  4. if (typeof require == 'function') {
  5. // enable to re-use in a browser without require.js
  6. normalizeSelector = require('../../lib/normalize-selector.js');
  7. assert = require('assertik');
  8. } else {
  9. assert = window.assertik;
  10. }
  11. suite('normalizeSelector');
  12. test('should be function', function () {
  13. assert.equal(typeof normalizeSelector, 'function', 'wrong type');
  14. });
  15. test('should normalize BIG SELECTOR', function () {
  16. var selector = "*~*>*.foo[ href *= \"/\" ]:hover>*[ data-foo = " +
  17. "\"bar\" ]:focus+*.baz::after";
  18. var expected = "* ~ * > *.foo[href*=\"/\"]:hover > *[data-foo=\"bar\"]:" +
  19. "focus + *.baz::after";
  20. assert.equal(normalizeSelector(selector), expected);
  21. });
  22. test('should return optimized selector with no change', function () {
  23. assert.equal(normalizeSelector("#foo .bar"), "#foo .bar");
  24. });
  25. test('should trim whitespace', function () {
  26. assert.equal(normalizeSelector(" #foo .bar "), "#foo .bar");
  27. });
  28. test('should separate between combinators', function () {
  29. assert.equal(normalizeSelector("#foo>.bar+.baz"), "#foo > .bar + .baz");
  30. });
  31. test('should not separate concatenated classes', function () {
  32. assert.equal(normalizeSelector("#foo.bar.baz"), "#foo.bar.baz");
  33. });
  34. test('should normalize asterisks', function () {
  35. var selector = " *.class[ data * = 'data' ] ";
  36. assert.equal(normalizeSelector(selector), "*.class[data*='data']");
  37. });
  38. test('should remove comments', function () {
  39. assert.equal(normalizeSelector(".e1 /* c2 */ .e2"), ".e1 .e2");
  40. assert.equal(normalizeSelector(" /*c1*/ .e1/*c2*/.e2 /*c3*/ .e3 /*c4*/ "), ".e1 .e2 .e3");
  41. assert.equal(normalizeSelector(" /*c1*/ .e1/*c2*/.e2 /*c3*/ .e3 "), ".e1 .e2 .e3");
  42. assert.equal(normalizeSelector("/*c1*/.e1/*c2*/.e2 /*c3*/ .e3"), ".e1 .e2 .e3");
  43. assert.equal(normalizeSelector(".e1/*c2*/.e2 /*c3*/ .e3"), ".e1 .e2 .e3");
  44. });
  45. test('should replace comments with single whitespace', function () {
  46. assert.equal(normalizeSelector("tag/* c2 */tag"), "tag tag");
  47. });
  48. test('should normalize parentheses', function() {
  49. var selector = "((a ) (b(c ) ) d )>*[ data-foo = \"bar\" ]";
  50. var expected = "((a)(b(c))d) > *[data-foo=\"bar\"]";
  51. assert.equal(normalizeSelector(selector), expected);
  52. });
  53. test('should normalize @-rule parentheses', function () {
  54. var selector = "@media screen and ( color ), projection and (color )";
  55. var expected = "@media screen and (color), projection and (color)";
  56. assert.equal(normalizeSelector(selector), expected);
  57. });
  58. test('should normalize @-rules with compound parentheses', function () {
  59. var selector = "@media handheld and ( min-width : 20em ), screen " +
  60. "and ( min-width: 20em )";
  61. var expected = "@media handheld and (min-width:20em), screen and " +
  62. "(min-width:20em)";
  63. assert.equal(normalizeSelector(selector), expected);
  64. });
  65. test('should normalize @-rules with operations', function () {
  66. var selector = "@media screen and ( device-aspect-ratio : 2560 / 1440 )";
  67. var expected = "@media screen and (device-aspect-ratio:2560/1440)";
  68. assert.equal(normalizeSelector(selector), expected);
  69. });
  70. test('should normalize descriptors', function () {
  71. var selector = "@counter-style triangle";
  72. assert.equal(normalizeSelector(selector), "@counter-style triangle");
  73. });
  74. test('should normalize case-insensitivity attribute selector', function () {
  75. assert.equal(normalizeSelector("[ att ~= val i ]"), "[att~=val i]");
  76. assert.equal(normalizeSelector("#foo[ a = \"b\" i ]"), "#foo[a=\"b\" i]");
  77. });
  78. test('should normalize namespaced attribute selector', function () {
  79. var selector = ' unit[ sh | quantity = "200" ] ';
  80. var expected = 'unit[sh|quantity="200"]';
  81. assert.equal(normalizeSelector(selector), expected);
  82. });
  83. test('should normalize pseudo-classes', function () {
  84. var selector = " :nth-last-of-type( ) ";
  85. assert.equal(normalizeSelector(selector), ":nth-last-of-type()");
  86. });
  87. test('should normalize pseudo-elements', function () {
  88. var selector = " ::nth-fragment( ) ";
  89. assert.equal(normalizeSelector(selector), "::nth-fragment()");
  90. });
  91. test('should normalize backslashes', function () {
  92. var selector = "#foo[ a = \" b \\\" c\\\\\" ]";
  93. var expected = "#foo[a=\" b \\\" c\\\\\"]";
  94. assert.equal(normalizeSelector(selector), expected);
  95. });