1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/debugger/test/browser_dbg_parser-03.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Check that JS inside HTML can be separated and parsed correctly. 1.9 + */ 1.10 + 1.11 +function test() { 1.12 + let { Parser } = Cu.import("resource:///modules/devtools/Parser.jsm", {}); 1.13 + 1.14 + let source = [ 1.15 + "<!doctype html>", 1.16 + "<head>", 1.17 + "<script>", 1.18 + "let a = 42;", 1.19 + "</script>", 1.20 + "<script type='text/javascript'>", 1.21 + "let b = 42;", 1.22 + "</script>", 1.23 + "<script type='text/javascript;version=1.8'>", 1.24 + "let c = 42;", 1.25 + "</script>", 1.26 + "</head>" 1.27 + ].join("\n"); 1.28 + let parser = new Parser(); 1.29 + let parsed = parser.get(source); 1.30 + 1.31 + ok(parsed, 1.32 + "HTML code should be parsed correctly."); 1.33 + is(parser.errors.length, 0, 1.34 + "There should be no errors logged when parsing."); 1.35 + 1.36 + is(parsed.scriptCount, 3, 1.37 + "There should be 3 scripts parsed in the parent HTML source."); 1.38 + 1.39 + is(parsed.getScriptInfo(0).toSource(), "({start:-1, length:-1, index:-1})", 1.40 + "There is no script at the beginning of the parent source."); 1.41 + is(parsed.getScriptInfo(source.length - 1).toSource(), "({start:-1, length:-1, index:-1})", 1.42 + "There is no script at the end of the parent source."); 1.43 + 1.44 + is(parsed.getScriptInfo(source.indexOf("let a")).toSource(), "({start:31, length:13, index:0})", 1.45 + "The first script was located correctly."); 1.46 + is(parsed.getScriptInfo(source.indexOf("let b")).toSource(), "({start:85, length:13, index:1})", 1.47 + "The second script was located correctly."); 1.48 + is(parsed.getScriptInfo(source.indexOf("let c")).toSource(), "({start:151, length:13, index:2})", 1.49 + "The third script was located correctly."); 1.50 + 1.51 + is(parsed.getScriptInfo(source.indexOf("let a") - 1).toSource(), "({start:31, length:13, index:0})", 1.52 + "The left edge of the first script was interpreted correctly."); 1.53 + is(parsed.getScriptInfo(source.indexOf("let b") - 1).toSource(), "({start:85, length:13, index:1})", 1.54 + "The left edge of the second script was interpreted correctly."); 1.55 + is(parsed.getScriptInfo(source.indexOf("let c") - 1).toSource(), "({start:151, length:13, index:2})", 1.56 + "The left edge of the third script was interpreted correctly."); 1.57 + 1.58 + is(parsed.getScriptInfo(source.indexOf("let a") - 2).toSource(), "({start:-1, length:-1, index:-1})", 1.59 + "The left outside of the first script was interpreted correctly."); 1.60 + is(parsed.getScriptInfo(source.indexOf("let b") - 2).toSource(), "({start:-1, length:-1, index:-1})", 1.61 + "The left outside of the second script was interpreted correctly."); 1.62 + is(parsed.getScriptInfo(source.indexOf("let c") - 2).toSource(), "({start:-1, length:-1, index:-1})", 1.63 + "The left outside of the third script was interpreted correctly."); 1.64 + 1.65 + is(parsed.getScriptInfo(source.indexOf("let a") + 12).toSource(), "({start:31, length:13, index:0})", 1.66 + "The right edge of the first script was interpreted correctly."); 1.67 + is(parsed.getScriptInfo(source.indexOf("let b") + 12).toSource(), "({start:85, length:13, index:1})", 1.68 + "The right edge of the second script was interpreted correctly."); 1.69 + is(parsed.getScriptInfo(source.indexOf("let c") + 12).toSource(), "({start:151, length:13, index:2})", 1.70 + "The right edge of the third script was interpreted correctly."); 1.71 + 1.72 + is(parsed.getScriptInfo(source.indexOf("let a") + 13).toSource(), "({start:-1, length:-1, index:-1})", 1.73 + "The right outside of the first script was interpreted correctly."); 1.74 + is(parsed.getScriptInfo(source.indexOf("let b") + 13).toSource(), "({start:-1, length:-1, index:-1})", 1.75 + "The right outside of the second script was interpreted correctly."); 1.76 + is(parsed.getScriptInfo(source.indexOf("let c") + 13).toSource(), "({start:-1, length:-1, index:-1})", 1.77 + "The right outside of the third script was interpreted correctly."); 1.78 + 1.79 + finish(); 1.80 +}