Bug 964709 - Updates Parser regex and tests to support self-closing script tags, r=vporof

This commit is contained in:
Hugo Arregui 2015-11-13 10:04:33 -03:00
parent 171eca078a
commit 3e1fe81622
3 changed files with 45 additions and 2 deletions

View File

@ -326,6 +326,8 @@ skip-if = e10s && debug
skip-if = e10s && debug
[browser_dbg_parser-10.js]
skip-if = e10s && debug
[browser_dbg_parser-11.js]
skip-if = e10s && debug
[browser_dbg_pause-exceptions-01.js]
skip-if = e10s && debug
[browser_dbg_pause-exceptions-02.js]

View File

@ -0,0 +1,39 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Checks if self-closing <script/> tags are parsed by Parser.jsm
*/
function test() {
let { Parser } = Cu.import("resource://devtools/shared/Parser.jsm", {});
let source = [
'<script type="text/javascript" src="chrome://foo.js"/>',
'<script type="application/javascript;version=1.8" src="chrome://baz.js"/>',
'<script async defer src="chrome://foobar.js"/>',
'<script type="application/javascript"/>"hello third"',
'<script type="application/javascript">"hello fourth"</script>',
].join("\n");
let parser = new Parser();
let parsed = parser.get(source);
is(parser.errors.length, 0,
"There should be no errors logged when parsing.");
is(parsed.scriptCount, 5,
"There should be 5 scripts parsed in the parent HTML source.");
is(parsed.getScriptInfo(source.indexOf("foo.js\"/>") + 1).toSource(), "({start:-1, length:-1, index:-1})",
"the first script is empty");
is(parsed.getScriptInfo(source.indexOf("baz.js\"/>") + 1).toSource(), "({start:-1, length:-1, index:-1})",
"the second script is empty");
is(parsed.getScriptInfo(source.indexOf("foobar.js\"/>") + 1).toSource(), "({start:-1, length:-1, index:-1})",
"the third script is empty");
is(parsed.getScriptInfo(source.indexOf("hello third!")).toSource(), "({start:-1, length:-1, index:-1})",
"Inline script on self-closing tag not considered a script");
is(parsed.getScriptInfo(source.indexOf("hello fourth")).toSource(), "({start:267, length:14, index:4})",
"The fourth script was located correctly.");
finish();
}

View File

@ -46,7 +46,7 @@ Parser.prototype = {
// all the scripts. Fastest/easiest way is with a regular expression.
// Don't worry, the rules of using a <script> tag are really strict,
// this will work.
let regexp = /<script[^>]*>([^]*?)<\/script\s*>/gim;
let regexp = /<script[^>]*?(?:>([^]*?)<\/script\s*>|\/>)/gim;
let syntaxTrees = [];
let scriptMatches = [];
let scriptMatch;
@ -54,7 +54,9 @@ Parser.prototype = {
if (aSource.match(/^\s*</)) {
// First non whitespace character is &lt, so most definitely HTML.
while (scriptMatch = regexp.exec(aSource)) {
scriptMatches.push(scriptMatch[1]); // Contents are captured at index 1.
// Contents are captured at index 1 or nothing: Self-closing scripts
// won't capture code content
scriptMatches.push(scriptMatch[1] || "");
}
}