browser_dbg_parser-04.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 faulty JS inside HTML can be separated and identified 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. // Don't pollute the logs with exceptions that we are going to check anyhow.
  25. parser.logExceptions = false;
  26. let parsed = parser.get(source);
  27. ok(parsed,
  28. "HTML code should be parsed correctly.");
  29. is(parser.errors.length, 2,
  30. "There should be two errors logged when parsing.");
  31. is(parser.errors[0].name, "SyntaxError",
  32. "The correct first exception was caught.");
  33. is(parser.errors[0].message, "missing ; before statement",
  34. "The correct first exception was caught.");
  35. is(parser.errors[1].name, "SyntaxError",
  36. "The correct second exception was caught.");
  37. is(parser.errors[1].message, "missing ; before statement",
  38. "The correct second exception was caught.");
  39. is(parsed.scriptCount, 1,
  40. "There should be 1 script parsed in the parent HTML source.");
  41. is(parsed.getScriptInfo(source.indexOf("let a")).toSource(), "({start:-1, length:-1, index:-1})",
  42. "The first script shouldn't be considered valid.");
  43. is(parsed.getScriptInfo(source.indexOf("let b")).toSource(), "({start:85, length:13, index:0})",
  44. "The second script was located correctly.");
  45. is(parsed.getScriptInfo(source.indexOf("let c")).toSource(), "({start:-1, length:-1, index:-1})",
  46. "The third script shouldn't be considered valid.");
  47. finish();
  48. }