test_csslexer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. */
  5. function test_lexer(domutils, cssText, tokenTypes) {
  6. let lexer = domutils.getCSSLexer(cssText);
  7. let reconstructed = '';
  8. let lastTokenEnd = 0;
  9. let i = 0;
  10. while (true) {
  11. let token = lexer.nextToken();
  12. if (!token) {
  13. break;
  14. }
  15. let combined = token.tokenType;
  16. if (token.text)
  17. combined += ":" + token.text;
  18. equal(combined, tokenTypes[i]);
  19. ok(token.endOffset > token.startOffset);
  20. equal(token.startOffset, lastTokenEnd);
  21. lastTokenEnd = token.endOffset;
  22. reconstructed += cssText.substring(token.startOffset, token.endOffset);
  23. ++i;
  24. }
  25. // Ensure that we saw the correct number of tokens.
  26. equal(i, tokenTypes.length);
  27. // Ensure that the reported offsets cover all the text.
  28. equal(reconstructed, cssText);
  29. }
  30. var LEX_TESTS = [
  31. ["simple", ["ident:simple"]],
  32. ["simple: { hi; }",
  33. ["ident:simple", "symbol::",
  34. "whitespace", "symbol:{",
  35. "whitespace", "ident:hi",
  36. "symbol:;", "whitespace",
  37. "symbol:}"]],
  38. ["/* whatever */", ["comment"]],
  39. ["'string'", ["string:string"]],
  40. ['"string"', ["string:string"]],
  41. ["rgb(1,2,3)", ["function:rgb", "number",
  42. "symbol:,", "number",
  43. "symbol:,", "number",
  44. "symbol:)"]],
  45. ["@media", ["at:media"]],
  46. ["#hibob", ["id:hibob"]],
  47. ["#123", ["hash:123"]],
  48. ["23px", ["dimension:px"]],
  49. ["23%", ["percentage"]],
  50. ["url(http://example.com)", ["url:http://example.com"]],
  51. ["url('http://example.com')", ["url:http://example.com"]],
  52. ["url( 'http://example.com' )",
  53. ["url:http://example.com"]],
  54. // In CSS Level 3, this is an ordinary URL, not a BAD_URL.
  55. ["url(http://example.com", ["url:http://example.com"]],
  56. ["url(http://example.com @", ["bad_url:http://example.com"]],
  57. ["quo\\ting", ["ident:quoting"]],
  58. ["'bad string\n", ["bad_string:bad string", "whitespace"]],
  59. ["~=", ["includes"]],
  60. ["|=", ["dashmatch"]],
  61. ["^=", ["beginsmatch"]],
  62. ["$=", ["endsmatch"]],
  63. ["*=", ["containsmatch"]],
  64. // URANGE may be on the way out, and it isn't used by devutils, so
  65. // let's skip it.
  66. ["<!-- html comment -->", ["htmlcomment", "whitespace", "ident:html",
  67. "whitespace", "ident:comment", "whitespace",
  68. "htmlcomment"]],
  69. // earlier versions of CSS had "bad comment" tokens, but in level 3,
  70. // unterminated comments are just comments.
  71. ["/* bad comment", ["comment"]]
  72. ];
  73. function test_lexer_linecol(domutils, cssText, locations) {
  74. let lexer = domutils.getCSSLexer(cssText);
  75. let i = 0;
  76. while (true) {
  77. let token = lexer.nextToken();
  78. let startLine = lexer.lineNumber;
  79. let startColumn = lexer.columnNumber;
  80. // We do this in a bit of a funny way so that we can also test the
  81. // location of the EOF.
  82. let combined = ":" + startLine + ":" + startColumn;
  83. if (token)
  84. combined = token.tokenType + combined;
  85. equal(combined, locations[i]);
  86. ++i;
  87. if (!token) {
  88. break;
  89. }
  90. }
  91. // Ensure that we saw the correct number of tokens.
  92. equal(i, locations.length);
  93. }
  94. function test_lexer_eofchar(domutils, cssText, argText, expectedAppend,
  95. expectedNoAppend) {
  96. let lexer = domutils.getCSSLexer(cssText);
  97. while (lexer.nextToken()) {
  98. // Nothing.
  99. }
  100. do_print("EOF char test, input = " + cssText);
  101. let result = lexer.performEOFFixup(argText, true);
  102. equal(result, expectedAppend);
  103. result = lexer.performEOFFixup(argText, false);
  104. equal(result, expectedNoAppend);
  105. }
  106. var LINECOL_TESTS = [
  107. ["simple", ["ident:0:0", ":0:6"]],
  108. ["\n stuff", ["whitespace:0:0", "ident:1:4", ":1:9"]],
  109. ['"string with \\\nnewline" \r\n', ["string:0:0", "whitespace:1:8",
  110. ":2:0"]]
  111. ];
  112. var EOFCHAR_TESTS = [
  113. ["hello", "hello"],
  114. ["hello \\", "hello \\\\", "hello \\\uFFFD"],
  115. ["'hello", "'hello'"],
  116. ["\"hello", "\"hello\""],
  117. ["'hello\\", "'hello\\\\'", "'hello'"],
  118. ["\"hello\\", "\"hello\\\\\"", "\"hello\""],
  119. ["/*hello", "/*hello*/"],
  120. ["/*hello*", "/*hello*/"],
  121. ["/*hello\\", "/*hello\\*/"],
  122. ["url(hello", "url(hello)"],
  123. ["url('hello", "url('hello')"],
  124. ["url(\"hello", "url(\"hello\")"],
  125. ["url(hello\\", "url(hello\\\\)", "url(hello\\\uFFFD)"],
  126. ["url('hello\\", "url('hello\\\\')", "url('hello')"],
  127. ["url(\"hello\\", "url(\"hello\\\\\")", "url(\"hello\")"],
  128. ];
  129. function run_test()
  130. {
  131. let domutils = Components.classes["@mozilla.org/inspector/dom-utils;1"]
  132. .getService(Components.interfaces.inIDOMUtils);
  133. let text, result;
  134. for ([text, result] of LEX_TESTS) {
  135. test_lexer(domutils, text, result);
  136. }
  137. for ([text, result] of LINECOL_TESTS) {
  138. test_lexer_linecol(domutils, text, result);
  139. }
  140. for ([text, expectedAppend, expectedNoAppend] of EOFCHAR_TESTS) {
  141. if (!expectedNoAppend) {
  142. expectedNoAppend = expectedAppend;
  143. }
  144. test_lexer_eofchar(domutils, text, text, expectedAppend, expectedNoAppend);
  145. }
  146. // Ensure that passing a different inputString to performEOFFixup
  147. // doesn't cause an assertion trying to strip a backslash from the
  148. // end of an empty string.
  149. test_lexer_eofchar(domutils, "'\\", "", "\\'", "'");
  150. }