Skip to content

Commit

Permalink
Allow speculative parser to see inside DSD templates
Browse files Browse the repository at this point in the history
Prior to this CL, the speculative parser would bail out at the sight
of any <template> element. However, if that template was a DSD
shadow root, like <template shadowrootmode=open>, it should be
scanned.

Note that DSD shadow roots inside "normal" template elements
should still be skipped, since everything in the <template>
is inert.

I also updated the speculative parser script to not blow away *all*
files, but instead just remove the .html files.

Bug: 1379513
Change-Id: I0ee8d64d9c33905f9fb1c5a0f5b4f428858c403a
Fixed: 1410933
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4200836
Commit-Queue: Mason Freed <masonf@chromium.org>
Reviewed-by: David Baron <dbaron@chromium.org>
Auto-Submit: Mason Freed <masonf@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1100556}
  • Loading branch information
Mason Freed authored and Chromium LUCI CQ committed Feb 2, 2023
1 parent a083ed5 commit 1318b9f
Show file tree
Hide file tree
Showing 30 changed files with 558 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,9 @@ void TokenPreloadScanner::ScanCommon(
--template_count_;
return;
}
if (template_count_) {
return;
}
if (Match(tag_impl, html_names::kStyleTag)) {
if (in_style_)
css_scanner_.Reset();
Expand All @@ -935,8 +938,21 @@ void TokenPreloadScanner::ScanCommon(
case HTMLToken::kStartTag: {
const StringImpl* tag_impl = TagImplFor(token.Data());
if (Match(tag_impl, html_names::kTemplateTag)) {
++template_count_;
return;
bool is_declarative_shadow_root = false;
const HTMLToken::Attribute* shadowrootmode_attribute =
token.GetAttributeItem(html_names::kShadowrootmodeAttr);
if (shadowrootmode_attribute) {
String shadowrootmode_value(shadowrootmode_attribute->Value());
is_declarative_shadow_root =
EqualIgnoringASCIICase(shadowrootmode_value, "open") ||
EqualIgnoringASCIICase(shadowrootmode_value, "closed");
}
// If this is a declarative shadow root <template shadowrootmode>
// element *and* we're not already inside a non-DSD <template> element,
// then we leave the template count at zero. Otherwise, increment it.
if (!(is_declarative_shadow_root && !template_count_)) {
++template_count_;
}
}
if (template_count_)
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): nested-template-shadowrootmode-1</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'nested-template-shadowrootmode-1', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<template><div><template shadowrootmode="closed"><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"><\/script></template></div></template>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): nested-template-shadowrootmode-2</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'nested-template-shadowrootmode-2', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<div><template shadowrootmode="closed"><template><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"><\/script></template></template></div>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): template-img-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'template-img-src', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<template><img src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"></template>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): template-link-stylesheet</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'template-link-stylesheet', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<template><link rel=stylesheet href="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"></template>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): template-shadowrootmode-img-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, true)
.then(compare_with_nonspeculative(uuid, 'template-shadowrootmode-img-src', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<div><template shadowrootmode="closed"><img src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"></template></div>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): template-shadowrootmode-link-stylesheet</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, true)
.then(compare_with_nonspeculative(uuid, 'template-shadowrootmode-link-stylesheet', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<div><template shadowrootmode="closed"><link rel=stylesheet href="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"></template></div>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, document.write(): template-shadowrootmode-script-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, true)
.then(compare_with_nonspeculative(uuid, 'template-shadowrootmode-script-src', true))
.then(done);
document.write(`
<script src="/common/slow.py?delay=1500"><\/script>
<script>
document.write('<plaintext>');
<\/script>
<\!-- speculative case in document.write -->
<div><template shadowrootmode="closed"><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid=${uuid}&amp;encodingcheck=&Gbreve;"><\/script></template></div>
`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load: nested-template-shadowrootmode-1</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/nested-template-shadowrootmode-1-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'nested-template-shadowrootmode-1', true))
.then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load: nested-template-shadowrootmode-2</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/nested-template-shadowrootmode-2-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'nested-template-shadowrootmode-2', true))
.then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): nested-template-shadowrootmode-1</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<template><div><template shadowrootmode="closed"><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></script></template></div></template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): nested-template-shadowrootmode-2</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<div><template shadowrootmode="closed"><template><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></script></template></template></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): template-img-src</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<template><img src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): template-link-stylesheet</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<template><link rel=stylesheet href="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): template-shadowrootmode-img-src</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<div><template shadowrootmode="closed"><img src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></template></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): template-shadowrootmode-link-stylesheet</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<div><template shadowrootmode="closed"><link rel=stylesheet href="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></template></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load (helper file): template-shadowrootmode-script-src</title>
<script src="/common/slow.py?delay=1500"></script>
<script>
document.write('<plaintext>');
</script>
<!-- speculative case -->
<div><template shadowrootmode="closed"><script src="/html/syntax/speculative-parsing/resources/stash.py?action=put&amp;uuid={{GET[uuid]}}&amp;encodingcheck=&Gbreve;"></script></template></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load: template-img-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/template-img-src-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'template-img-src', true))
.then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<!-- DO NOT EDIT. This file has been generated. Source:
/html/syntax/speculative-parsing/tools/generate.py
-->
<meta charset=utf-8>
<title>Speculative parsing, page load: template-link-stylesheet</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/speculative-parsing/resources/speculative-parsing-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/template-link-stylesheet-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false)
.then(compare_with_nonspeculative(uuid, 'template-link-stylesheet', true))
.then(done);
</script>

0 comments on commit 1318b9f

Please sign in to comment.