From a2e6abe1593423140a2806f6c9dff77a762eac58 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 4 Mar 2022 18:49:30 +0100 Subject: [PATCH] LibWeb: Add HTMLMediaElement.canPlayType() For the time being, the answer is no, or rather: "" :^) --- Userland/Libraries/LibWeb/Forward.h | 1 + .../Libraries/LibWeb/HTML/HTMLMediaElement.cpp | 18 ++++++++++++++++++ .../Libraries/LibWeb/HTML/HTMLMediaElement.h | 2 ++ .../Libraries/LibWeb/HTML/HTMLMediaElement.idl | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index ceba11476a..b6411825f6 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -525,6 +525,7 @@ class XMLHttpRequestConstructor; class XMLHttpRequestEventTargetWrapper; class XMLHttpRequestPrototype; class XMLHttpRequestWrapper; +enum class CanPlayTypeResult; enum class DOMParserSupportedType; enum class XMLHttpRequestResponseType; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index 24f77b793d..14965147ac 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include namespace Web::HTML { @@ -17,4 +18,21 @@ HTMLMediaElement::~HTMLMediaElement() { } +// https://html.spec.whatwg.org/multipage/media.html#dom-navigator-canplaytype +Bindings::CanPlayTypeResult HTMLMediaElement::can_play_type(String const& type) const +{ + // The canPlayType(type) method must: + // - return the empty string if type is a type that the user agent knows it cannot render or is the type "application/octet-stream" + // - return "probably" if the user agent is confident that the type represents a media resource that it can render if used in with this audio or video element + // - return "maybe" otherwise. Implementers are encouraged to return "maybe" unless the type can be confidently established as being supported or not + // Generally, a user agent should never return "probably" for a type that allows the codecs parameter if that parameter is not present. + if (type == "application/octet-stream"sv) + return Bindings::CanPlayTypeResult::Empty; + // FIXME: Eventually we should return `Maybe` here, but for now `Empty` is our best bet :^) + // Being honest here leads to some apps and frameworks skipping things like audio loading, + // which for the time being would create more issues than it solves - e.g. endless waiting + // for audio that will never load. + return Bindings::CanPlayTypeResult::Empty; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h index d8cefc19c6..ed5bb41b5b 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h @@ -16,6 +16,8 @@ public: HTMLMediaElement(DOM::Document&, DOM::QualifiedName); virtual ~HTMLMediaElement() override; + + Bindings::CanPlayTypeResult can_play_type(String const& type) const; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.idl index 6b0c8012bb..a97e1e29ba 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.idl @@ -1,5 +1,11 @@ #import +enum CanPlayTypeResult { + "", + "maybe", + "probably" +}; + interface HTMLMediaElement : HTMLElement { [Reflect] attribute DOMString src; @@ -9,4 +15,5 @@ interface HTMLMediaElement : HTMLElement { [Reflect] attribute boolean controls; + CanPlayTypeResult canPlayType(DOMString type); };