browser_dbg_parser-03.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Check that JS inside HTML can be separated and parsed correctly.
  6. */
  7. function test() {
  8. let { Parser } = Cu.import("resource://devtools/shared/Parser.jsm", {});
  9. let source = [
  10. "<!doctype html>",
  11. "<head>",
  12. "<script>",
  13. "let a = 42;",
  14. "</script>",
  15. "<script type='text/javascript'>",
  16. "let b = 42;",
  17. "</script>",
  18. "<script type='text/javascript;version=1.8'>",
  19. "let c = 42;",
  20. "</script>",
  21. "</head>"
  22. ].join("\n");
  23. let parser = new Parser();
  24. let parsed = parser.get(source);
  25. ok(parsed,
  26. "HTML code should be parsed correctly.");
  27. is(parser.errors.length, 0,
  28. "There should be no errors logged when parsing.");
  29. is(parsed.scriptCount, 3,
  30. "There should be 3 scripts parsed in the parent HTML source.");
  31. is(parsed.getScriptInfo(0).toSource(), "({start:-1, length:-1, index:-1})",
  32. "There is no script at the beginning of the parent source.");
  33. is(parsed.getScriptInfo(source.length - 1).toSource(), "({start:-1, length:-1, index:-1})",
  34. "There is no script at the end of the parent source.");
  35. is(parsed.getScriptInfo(source.indexOf("let a")).toSource(), "({start:31, length:13, index:0})",
  36. "The first script was located correctly.");
  37. is(parsed.getScriptInfo(source.indexOf("let b")).toSource(), "({start:85, length:13, index:1})",
  38. "The second script was located correctly.");
  39. is(parsed.getScriptInfo(source.indexOf("let c")).toSource(), "({start:151, length:13, index:2})",
  40. "The third script was located correctly.");
  41. is(parsed.getScriptInfo(source.indexOf("let a") - 1).toSource(), "({start:31, length:13, index:0})",
  42. "The left edge of the first script was interpreted correctly.");
  43. is(parsed.getScriptInfo(source.indexOf("let b") - 1).toSource(), "({start:85, length:13, index:1})",
  44. "The left edge of the second script was interpreted correctly.");
  45. is(parsed.getScriptInfo(source.indexOf("let c") - 1).toSource(), "({start:151, length:13, index:2})",
  46. "The left edge of the third script was interpreted correctly.");
  47. is(parsed.getScriptInfo(source.indexOf("let a") - 2).toSource(), "({start:-1, length:-1, index:-1})",
  48. "The left outside of the first script was interpreted correctly.");
  49. is(parsed.getScriptInfo(source.indexOf("let b") - 2).toSource(), "({start:-1, length:-1, index:-1})",
  50. "The left outside of the second script was interpreted correctly.");
  51. is(parsed.getScriptInfo(source.indexOf("let c") - 2).toSource(), "({start:-1, length:-1, index:-1})",
  52. "The left outside of the third script was interpreted correctly.");
  53. is(parsed.getScriptInfo(source.indexOf("let a") + 12).toSource(), "({start:31, length:13, index:0})",
  54. "The right edge of the first script was interpreted correctly.");
  55. is(parsed.getScriptInfo(source.indexOf("let b") + 12).toSource(), "({start:85, length:13, index:1})",
  56. "The right edge of the second script was interpreted correctly.");
  57. is(parsed.getScriptInfo(source.indexOf("let c") + 12).toSource(), "({start:151, length:13, index:2})",
  58. "The right edge of the third script was interpreted correctly.");
  59. is(parsed.getScriptInfo(source.indexOf("let a") + 13).toSource(), "({start:-1, length:-1, index:-1})",
  60. "The right outside of the first script was interpreted correctly.");
  61. is(parsed.getScriptInfo(source.indexOf("let b") + 13).toSource(), "({start:-1, length:-1, index:-1})",
  62. "The right outside of the second script was interpreted correctly.");
  63. is(parsed.getScriptInfo(source.indexOf("let c") + 13).toSource(), "({start:-1, length:-1, index:-1})",
  64. "The right outside of the third script was interpreted correctly.");
  65. finish();
  66. }