Skip to content

Commit

Permalink
Implement HTMLScriptElement.supports(type) method
Browse files Browse the repository at this point in the history
Bug: 1245528, whatwg/html#6472

Change-Id: I9a902504cf692caa73ae7e49fd65895156bbf197
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3133553
Commit-Queue: Tsuyoshi Horo <horo@chromium.org>
Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/main@{#918474}
NOKEYCHECK=True
GitOrigin-RevId: b8b24648e5716bd4e7f7cb77cb792163e8599149
  • Loading branch information
horo-t authored and copybara-github committed Sep 6, 2021
1 parent 469cc53 commit 9f181a3
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
19 changes: 19 additions & 0 deletions blink/renderer/core/html/html_script_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/script/script_loader.h"
#include "third_party/blink/renderer/core/script/script_runner.h"
#include "third_party/blink/renderer/core/script_type_names.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_types_util.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
Expand Down Expand Up @@ -316,6 +317,24 @@ Element& HTMLScriptElement::CloneWithoutAttributesAndChildren(
return *factory.CreateElement(TagQName(), flags, IsValue());
}

// static
bool HTMLScriptElement::supports(ExecutionContext* execution_context,
const AtomicString& type) {
if (type == script_type_names::kClassic)
return true;
if (type == script_type_names::kModule)
return true;
if (type == script_type_names::kImportmap)
return true;

if ((type == script_type_names::kSpeculationrules) &&
RuntimeEnabledFeatures::SpeculationRulesEnabled(execution_context)) {
return true;
}

return false;
}

void HTMLScriptElement::Trace(Visitor* visitor) const {
visitor->Trace(loader_);
HTMLElement::Trace(visitor);
Expand Down
2 changes: 2 additions & 0 deletions blink/renderer/core/html/html_script_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CORE_EXPORT HTMLScriptElement final : public HTMLElement,
DEFINE_WRAPPERTYPEINFO();

public:
static bool supports(ExecutionContext*, const AtomicString&);

HTMLScriptElement(Document&, const CreateElementFlags);

// Returns attributes that should be checked against Trusted Types
Expand Down
3 changes: 3 additions & 0 deletions blink/renderer/core/html/html_script_element.idl
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@
// Subresource Integrity
// https://w3c.github.io/webappsec-subresource-integrity/#HTMLScriptElement
[Reflect] attribute DOMString integrity;

// https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
[RuntimeEnabled=ScriptElementSupports, CallWith=ExecutionContext] static boolean supports(DOMString type);
};
4 changes: 4 additions & 0 deletions blink/renderer/platform/runtime_enabled_features.json5
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,10 @@
name: "ScriptedSpeechSynthesis",
status: "stable",
},
{
name: "ScriptElementSupports",
status: "experimental",
},
{
name: "ScrollbarGutter",
status: "stable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>
<meta charset=utf-8>
<title>HTMLScriptElement.supports</title>
<link rel=help href="https://html.spec.whatwg.org/#dom-script-supports">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(function() {
assert_equals(typeof HTMLScriptElement.supports, 'function');
}, 'Type of HTMLScriptElement.supports is function');

test(function() {
assert_true(HTMLScriptElement.supports('classic'));
}, 'HTMLScriptElement.supports resurns true for \'classic\'');

test(function() {
assert_true(HTMLScriptElement.supports('module'));
}, 'HTMLScriptElement.supports resurns true for \'module\'');

test(function() {
assert_false(HTMLScriptElement.supports('application/ecmascript'));
assert_false(HTMLScriptElement.supports('application/javascript'));
assert_false(HTMLScriptElement.supports('application/x-ecmascript'));
assert_false(HTMLScriptElement.supports('application/x-javascript'));
assert_false(HTMLScriptElement.supports('text/ecmascript'));
assert_false(HTMLScriptElement.supports('text/javascript'));
assert_false(HTMLScriptElement.supports('text/javascript1.0'));
assert_false(HTMLScriptElement.supports('text/javascript1.1'));
assert_false(HTMLScriptElement.supports('text/javascript1.2'));
assert_false(HTMLScriptElement.supports('text/javascript1.3'));
assert_false(HTMLScriptElement.supports('text/javascript1.4'));
assert_false(HTMLScriptElement.supports('text/javascript1.5'));
assert_false(HTMLScriptElement.supports('text/jscript'));
assert_false(HTMLScriptElement.supports('text/livescript'));
assert_false(HTMLScriptElement.supports('text/x-ecmascript'));
assert_false(HTMLScriptElement.supports('text/x-javascript'));
}, 'HTMLScriptElement.supports returns false for JavaScript MIME types');

test(function() {
assert_false(HTMLScriptElement.supports(''));
assert_false(HTMLScriptElement.supports(' '));
assert_false(HTMLScriptElement.supports('classic '));
assert_false(HTMLScriptElement.supports('module '));
assert_false(HTMLScriptElement.supports(' classic '));
assert_false(HTMLScriptElement.supports(' module '));
assert_false(HTMLScriptElement.supports('classics'));
assert_false(HTMLScriptElement.supports('modules'));
assert_false(HTMLScriptElement.supports('Classic'));
assert_false(HTMLScriptElement.supports('Module'));
assert_false(HTMLScriptElement.supports('unsupported'));
}, 'HTMLScriptElement.supports returns false for unsupported types');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<title>HTMLScriptElement.supports importmap</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(function() {
assert_true(HTMLScriptElement.supports('importmap'));
}, 'HTMLScriptElement.supports returns true for \'importmap\'');

test(function() {
assert_false(HTMLScriptElement.supports(' importmap'));
assert_false(HTMLScriptElement.supports('importmap '));
assert_false(HTMLScriptElement.supports('Importmap'));
assert_false(HTMLScriptElement.supports('ImportMap'));
assert_false(HTMLScriptElement.supports('importMap'));
assert_false(HTMLScriptElement.supports('import-map'));
assert_false(HTMLScriptElement.supports('importmaps'));
assert_false(HTMLScriptElement.supports('import-maps'));
}, 'HTMLScriptElement.supports returns false for unsupported types');

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -4137,6 +4137,7 @@ interface HTMLQuoteElement : HTMLElement
method constructor
setter cite
interface HTMLScriptElement : HTMLElement
static method supports
attribute @@toStringTag
getter async
getter charset
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<title>HTMLScriptElement.supports speculationrules</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name="timeout" content="long">
<script>
test(function() {
assert_true(HTMLScriptElement.supports('speculationrules'));
}, 'HTMLScriptElement.supports returns true for \'speculationrules\'');

test(function() {
assert_false(HTMLScriptElement.supports(' speculationrules'));
assert_false(HTMLScriptElement.supports('speculationrules '));
assert_false(HTMLScriptElement.supports('Speculationrules'));
assert_false(HTMLScriptElement.supports('SpeculationRules'));
assert_false(HTMLScriptElement.supports('speculationRules'));
assert_false(HTMLScriptElement.supports('speculation-rules'));
}, 'HTMLScriptElement.supports returns false for unsupported types');

</script>

0 comments on commit 9f181a3

Please sign in to comment.