Skip to content

Commit c0b8f47

Browse files
committed
LibWeb/HTML: Move connectedness check for script post_connection()
Corresponds to: whatwg/html@905384d Also import the test added along with that change.
1 parent 5179c57 commit c0b8f47

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

Libraries/LibWeb/HTML/HTMLScriptElement.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void HTMLScriptElement::attribute_changed(FlyString const& name, Optional<String
6363
} else if (name == HTML::AttributeNames::referrerpolicy) {
6464
m_referrer_policy = ReferrerPolicy::from_string(value.value_or(""_string)).value_or(ReferrerPolicy::ReferrerPolicy::EmptyString);
6565
} else if (name == HTML::AttributeNames::src) {
66-
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-steps-6
66+
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:concept-element-attributes-change-ext
6767
// 1. If namespace is not null, then return.
6868
if (namespace_.has_value())
6969
return;
@@ -73,8 +73,9 @@ void HTMLScriptElement::attribute_changed(FlyString const& name, Optional<String
7373
if (!value.has_value())
7474
return;
7575

76-
// 2. If localName is src, then run the script HTML element post-connection steps, given element.
77-
post_connection();
76+
// 2. If localName is src and element is connected, then run the script HTML element post-connection steps, given element.
77+
if (is_connected())
78+
post_connection();
7879
} else if (name == HTML::AttributeNames::async) {
7980
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:script-force-async
8081
// When an async attribute is added to a script element el, the user agent must set el's force async to false.
@@ -631,27 +632,27 @@ void HTMLScriptElement::prepare_script()
631632
}
632633
}
633634

634-
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-steps-4
635+
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:children-changed-steps
635636
void HTMLScriptElement::children_changed(ChildrenChangedMetadata const* metadata)
636637
{
637638
Base::children_changed(metadata);
638639

639-
// 1. Run the script HTML element post-connection steps, given the script element.
640+
// 1. If the script element is not connected, then return.
641+
if (!is_connected())
642+
return;
643+
644+
// 2. Run the script HTML element post-connection steps, given the script element.
640645
post_connection();
641646
}
642647

643-
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:prepare-the-script-element-5
648+
// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-steps
644649
void HTMLScriptElement::post_connection()
645650
{
646-
// 1. If insertedNode is not connected, then return.
647-
if (!is_connected())
648-
return;
649-
650-
// 2. If insertedNode is parser-inserted, then return.
651+
// 1. If insertedNode is parser-inserted, then return.
651652
if (is_parser_inserted())
652653
return;
653654

654-
// 3. Prepare the script element given insertedNode.
655+
// 2. Prepare the script element given insertedNode.
655656
prepare_script();
656657
}
657658

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Harness status: OK
2+
3+
Found 2 tests
4+
5+
2 Pass
6+
Pass A later-inserted script removed by an earlier-inserted script in the same document fragment should not run
7+
Pass A later-inserted script removed by an earlier-inserted script in the same append() call should not run
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<meta charset=utf-8>
3+
<title>A later-inserted script removed by an earlier-inserted script does not run</title>
4+
<script src=../../../resources/testharness.js></script>
5+
<script src=../../../resources/testharnessreport.js></script>
6+
<body>
7+
<script>
8+
9+
test(t => {
10+
const fragment = document.createDocumentFragment();
11+
// Global so they can be more easily accessed by the inner script blocks.
12+
window.script1 = fragment.appendChild(document.createElement("script"));
13+
window.script2 = fragment.appendChild(document.createElement("script"));
14+
15+
window.script2Run = false;
16+
t.add_cleanup(() => { window.script2Run = false; });
17+
script1.textContent = `
18+
assert_true(script2.isConnected, 'script2 is connected when script2 runs');
19+
script2.remove();
20+
`;
21+
script2.textContent = "window.script2Run = true;";
22+
23+
document.body.append(fragment);
24+
assert_false(window.script2Run, "script2 did not run");
25+
}, "A later-inserted script removed by an earlier-inserted script in the " +
26+
"same document fragment should not run");
27+
28+
test(t => {
29+
window.script1 = document.createElement("script");
30+
window.script2 = document.createElement("script");
31+
32+
window.script2Run = false;
33+
t.add_cleanup(() => { window.script2Run = false; });
34+
script1.textContent = `
35+
assert_true(script2.isConnected, 'script2 is connected when script2 runs');
36+
script2.remove();
37+
`;
38+
script2.textContent = "window.script2Run = true;";
39+
40+
document.body.append(script1, script2);
41+
assert_false(window.script2Run, "script2 did not run");
42+
}, "A later-inserted script removed by an earlier-inserted script in the " +
43+
"same append() call should not run");
44+
</script>
45+
</body>

0 commit comments

Comments
 (0)