Skip to content

Truly independent web browser

LibWeb: Implement speech recognition and grammars in Rust

Shannon Booth7205ee1e6ac9
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs
index a8d12086e7b..cb60cc23e36 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs
@@ -6,6 +6,12 @@
//! The Web Speech API, https://webaudio.github.io/web-speech-api/
+#[cfg(not(test))]
+pub(crate) mod speech_grammar;
+#[cfg(not(test))]
+pub(crate) mod speech_grammar_list;
+#[cfg(not(test))]
+pub(crate) mod speech_recognition;
#[cfg(not(test))]
pub(crate) mod speech_recognition_alternative;
#[cfg(not(test))]
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar.rs
new file mode 100644
index 00000000000..65a9edd7ac2
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar.rs
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! SpeechGrammar, https://wicg.github.io/speech-api/#speechgrammar
+
+use core::cell::{Cell, RefCell};
+
+use ak::Utf16String;
+use libjs_rust::api::Trace;
+
+use crate::bindings::interface::Wrappable;
+use crate::bindings::{CallContext, WebIdlResult};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct SpeechGrammar {
+ #[gc(untraced)]
+ src: RefCell<Utf16String>,
+ weight: Cell<f32>,
+}
+
+impl Wrappable for SpeechGrammar {}
+
+#[expect(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every method of an implementation for its WebIDL result, whether or not it can fail"
+)]
+impl SpeechGrammar {
+ pub(crate) fn construct(_: &mut CallContext) -> WebIdlResult<Self> {
+ Ok(Self {
+ src: RefCell::new(Utf16String::default()),
+ weight: Cell::new(1.0),
+ })
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechgrammar-src
+ pub(crate) fn src(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
+ Ok(self.src.borrow().clone())
+ }
+
+ pub(crate) fn set_src(&self, _: &mut CallContext, src: Utf16String) -> WebIdlResult<()> {
+ *self.src.borrow_mut() = src;
+ Ok(())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechgrammar-weight
+ pub(crate) fn weight(&self, _: &mut CallContext) -> WebIdlResult<f32> {
+ Ok(self.weight.get())
+ }
+
+ pub(crate) fn set_weight(&self, _: &mut CallContext, weight: f32) -> WebIdlResult<()> {
+ self.weight.set(weight);
+ Ok(())
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar_list.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar_list.rs
new file mode 100644
index 00000000000..905938f0b93
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_grammar_list.rs
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! SpeechGrammarList, https://wicg.github.io/speech-api/#speechgrammarlist
+
+use libjs_rust::api::{DeferGc, Trace};
+
+use super::speech_grammar::SpeechGrammar;
+use crate::bindings::interface::{Ref, Wrappable, allocate};
+use crate::bindings::{CallContext, WebIdlResult};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct SpeechGrammarList {
+ grammars: Vec<Ref<SpeechGrammar>>,
+}
+
+impl Wrappable for SpeechGrammarList {}
+
+#[expect(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every method of an implementation for its WebIDL result, whether or not it can fail"
+)]
+impl SpeechGrammarList {
+ pub(crate) fn new(_: &DeferGc<'_>) -> Self {
+ Self { grammars: Vec::new() }
+ }
+
+ pub(crate) fn construct(cx: &mut CallContext) -> WebIdlResult<Ref<Self>> {
+ Ok(allocate(cx.vm(), Self::new))
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechgrammarlist-length
+ pub(crate) fn length(&self, _: &mut CallContext) -> WebIdlResult<u32> {
+ Ok(u32::try_from(self.grammars.len()).expect("a list has fewer than 2^32 grammars"))
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechgrammarlist-item
+ pub(crate) fn item(&self, _: &mut CallContext, index: u32) -> WebIdlResult<Option<Ref<SpeechGrammar>>> {
+ Ok(self.grammars.get(index as usize).copied())
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition.rs
new file mode 100644
index 00000000000..51cc090fdf2
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition.rs
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! SpeechRecognition, https://wicg.github.io/speech-api/#speechrecognition
+
+use core::cell::{Cell, RefCell};
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::Trace;
+
+use super::speech_grammar_list::SpeechGrammarList;
+use crate::bindings::event_target::{CppEventTarget, GetTheParent};
+use crate::bindings::interface::{Ref, Wrappable, allocate};
+use crate::bindings::{CallContext, WebIdlResult};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct SpeechRecognition {
+ event_target: CppEventTarget,
+ grammars: Cell<Ref<SpeechGrammarList>>,
+ #[gc(untraced)]
+ lang: RefCell<Utf16String>,
+ continuous: Cell<bool>,
+ interim_results: Cell<bool>,
+ max_alternatives: Cell<u32>,
+}
+
+impl Wrappable for SpeechRecognition {
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event_target.as_cpp_parent()
+ }
+}
+
+impl GetTheParent for SpeechRecognition {
+ fn get_the_parent(&self) -> Option<CppEventTarget> {
+ None
+ }
+}
+
+#[expect(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every method of an implementation for its WebIDL result, whether or not it can fail"
+)]
+impl SpeechRecognition {
+ pub(crate) fn construct(cx: &mut CallContext) -> WebIdlResult<Ref<Self>> {
+ let grammars = allocate(cx.vm(), SpeechGrammarList::new);
+ let recognition = allocate(cx.vm(), |_| Self {
+ event_target: CppEventTarget::new(),
+ grammars: Cell::new(grammars),
+ lang: RefCell::new(Utf16String::default()),
+ continuous: Cell::new(false),
+ interim_results: Cell::new(false),
+ max_alternatives: Cell::new(1),
+ });
+ recognition.event_target.set_owner(recognition);
+ Ok(recognition)
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognition-grammars
+ pub(crate) fn grammars(&self, _: &mut CallContext) -> WebIdlResult<Ref<SpeechGrammarList>> {
+ Ok(self.grammars.get())
+ }
+
+ pub(crate) fn set_grammars(&self, _: &mut CallContext, grammars: Ref<SpeechGrammarList>) -> WebIdlResult<()> {
+ self.grammars.set(grammars);
+ Ok(())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognition-lang
+ pub(crate) fn lang(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
+ Ok(self.lang.borrow().clone())
+ }
+
+ pub(crate) fn set_lang(&self, _: &mut CallContext, lang: Utf16String) -> WebIdlResult<()> {
+ *self.lang.borrow_mut() = lang;
+ Ok(())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognition-continuous
+ pub(crate) fn continuous(&self, _: &mut CallContext) -> WebIdlResult<bool> {
+ Ok(self.continuous.get())
+ }
+
+ pub(crate) fn set_continuous(&self, _: &mut CallContext, continuous: bool) -> WebIdlResult<()> {
+ self.continuous.set(continuous);
+ Ok(())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognition-interimresults
+ pub(crate) fn interim_results(&self, _: &mut CallContext) -> WebIdlResult<bool> {
+ Ok(self.interim_results.get())
+ }
+
+ pub(crate) fn set_interim_results(&self, _: &mut CallContext, interim_results: bool) -> WebIdlResult<()> {
+ self.interim_results.set(interim_results);
+ Ok(())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognition-maxalternatives
+ pub(crate) fn max_alternatives(&self, _: &mut CallContext) -> WebIdlResult<u32> {
+ Ok(self.max_alternatives.get())
+ }
+
+ pub(crate) fn set_max_alternatives(&self, _: &mut CallContext, max_alternatives: u32) -> WebIdlResult<()> {
+ self.max_alternatives.set(max_alternatives);
+ Ok(())
+ }
+}
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index f6116e41a22..110593aa76f 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -760,9 +760,6 @@ set(SOURCES
ServiceWorker/ServiceWorkerGlobalScope.cpp
ServiceWorker/ServiceWorkerRecord.cpp
ServiceWorker/ServiceWorkerRegistration.cpp
- Speech/SpeechGrammar.cpp
- Speech/SpeechGrammarList.cpp
- Speech/SpeechRecognition.cpp
Speech/SpeechSynthesis.cpp
Speech/SpeechSynthesisUtterance.cpp
Speech/SpeechSynthesisVoice.cpp
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index a641e0fb2e5..edb5c8853de 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -1081,9 +1081,6 @@ using WorkerType = Bindings::WorkerType;
namespace Web::Speech {
-class SpeechGrammar;
-class SpeechGrammarList;
-class SpeechRecognition;
class SpeechSynthesis;
class SpeechSynthesisUtterance;
class SpeechSynthesisVoice;
diff --git a/Libraries/LibWeb/Speech/SpeechGrammar.cpp b/Libraries/LibWeb/Speech/SpeechGrammar.cpp
deleted file mode 100644
index d381fdd3f00..00000000000
--- a/Libraries/LibWeb/Speech/SpeechGrammar.cpp
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/Speech/SpeechGrammar.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-GC_DEFINE_ALLOCATOR(SpeechGrammar);
-
-GC::Ref<SpeechGrammar> SpeechGrammar::create()
-{
- return GC::Heap::the().allocate<SpeechGrammar>();
-}
-
-SpeechGrammar::SpeechGrammar()
-{
-}
-
-SpeechGrammar::~SpeechGrammar() = default;
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechGrammar.h b/Libraries/LibWeb/Speech/SpeechGrammar.h
deleted file mode 100644
index f25e9da4a6f..00000000000
--- a/Libraries/LibWeb/Speech/SpeechGrammar.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Utf16String.h>
-#include <LibGC/Ptr.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/Forward.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-class SpeechGrammar final : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(SpeechGrammar, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(SpeechGrammar);
-
-public:
- static GC::Ref<SpeechGrammar> create();
- virtual ~SpeechGrammar() override;
-
- // https://wicg.github.io/speech-api/#dom-speechgrammar-src
- Utf16String const& src() const { return m_src; }
- void set_src(Utf16String const& src) { m_src = src; }
-
- // https://wicg.github.io/speech-api/#dom-speechgrammar-weight
- float weight() const { return m_weight; }
- void set_weight(float weight) { m_weight = weight; }
-
-private:
- explicit SpeechGrammar();
-
- Utf16String m_src;
- float m_weight { 1.f };
-};
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechGrammar.idl b/Libraries/LibWeb/Speech/SpeechGrammar.idl
index 0e4579bad4f..9bf287cf247 100644
--- a/Libraries/LibWeb/Speech/SpeechGrammar.idl
+++ b/Libraries/LibWeb/Speech/SpeechGrammar.idl
@@ -1,5 +1,5 @@
// https://wicg.github.io/speech-api/#speechgrammar
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface SpeechGrammar {
constructor();
diff --git a/Libraries/LibWeb/Speech/SpeechGrammarList.cpp b/Libraries/LibWeb/Speech/SpeechGrammarList.cpp
deleted file mode 100644
index 172af66d477..00000000000
--- a/Libraries/LibWeb/Speech/SpeechGrammarList.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/Speech/SpeechGrammarList.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-GC_DEFINE_ALLOCATOR(SpeechGrammarList);
-
-GC::Ref<SpeechGrammarList> SpeechGrammarList::create()
-{
- return GC::Heap::the().allocate<SpeechGrammarList>();
-}
-
-SpeechGrammarList::SpeechGrammarList()
-{
-}
-
-SpeechGrammarList::~SpeechGrammarList() = default;
-
-void SpeechGrammarList::visit_edges(GC::Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_grammars);
-}
-
-// https://wicg.github.io/speech-api/#dom-speechgrammarlist-item
-GC::Ptr<SpeechGrammar> SpeechGrammarList::item(size_t index) const
-{
- if (index >= m_grammars.size())
- return nullptr;
- return m_grammars[index];
-}
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechGrammarList.h b/Libraries/LibWeb/Speech/SpeechGrammarList.h
deleted file mode 100644
index 529d0d0a034..00000000000
--- a/Libraries/LibWeb/Speech/SpeechGrammarList.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Vector.h>
-#include <LibGC/Ptr.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/Forward.h>
-#include <LibWeb/Speech/SpeechGrammar.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-class SpeechGrammarList final : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(SpeechGrammarList, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(SpeechGrammarList);
-
-public:
- static GC::Ref<SpeechGrammarList> create();
- virtual ~SpeechGrammarList() override;
-
- // https://wicg.github.io/speech-api/#dom-speechgrammarlist-length
- size_t length() const { return m_grammars.size(); }
-
- // https://wicg.github.io/speech-api/#dom-speechgrammarlist-item
- GC::Ptr<SpeechGrammar> item(size_t index) const;
-
-private:
- explicit SpeechGrammarList();
-
- virtual void visit_edges(GC::Cell::Visitor&) override;
-
- Vector<GC::Ref<SpeechGrammar>> m_grammars;
-};
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechGrammarList.idl b/Libraries/LibWeb/Speech/SpeechGrammarList.idl
index 68805eaa207..f5876b55c3d 100644
--- a/Libraries/LibWeb/Speech/SpeechGrammarList.idl
+++ b/Libraries/LibWeb/Speech/SpeechGrammarList.idl
@@ -1,10 +1,10 @@
// https://wicg.github.io/speech-api/#speechgrammarlist
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface SpeechGrammarList {
constructor();
readonly attribute unsigned long length;
- getter SpeechGrammar item(unsigned long index);
+ getter SpeechGrammar? item(unsigned long index);
[FIXME] undefined addFromURI(DOMString src, optional float weight = 1.0);
[FIXME] undefined addFromString(DOMString string, optional float weight = 1.0);
};
diff --git a/Libraries/LibWeb/Speech/SpeechRecognition.cpp b/Libraries/LibWeb/Speech/SpeechRecognition.cpp
deleted file mode 100644
index a1f980545ec..00000000000
--- a/Libraries/LibWeb/Speech/SpeechRecognition.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/Speech/SpeechRecognition.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-GC_DEFINE_ALLOCATOR(SpeechRecognition);
-
-GC::Ref<SpeechRecognition> SpeechRecognition::create()
-{
- return GC::Heap::the().allocate<SpeechRecognition>();
-}
-
-SpeechRecognition::SpeechRecognition()
- : DOM::EventTarget()
- , m_grammars(SpeechGrammarList::create())
-{
-}
-
-SpeechRecognition::~SpeechRecognition() = default;
-
-void SpeechRecognition::visit_edges(Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_grammars);
-}
-
-#undef __ENUMERATE
-#define __ENUMERATE(attribute_name, event_name) \
- void SpeechRecognition::set_##attribute_name(GC::Ptr<WebIDL::CallbackType> value) \
- { \
- set_event_handler_attribute(event_name, value); \
- } \
- GC::Ptr<WebIDL::CallbackType> SpeechRecognition::attribute_name() \
- { \
- return event_handler_attribute(event_name); \
- }
-ENUMERATE_SPEECH_RECOGNITION_EVENT_HANDLERS(__ENUMERATE)
-#undef __ENUMERATE
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechRecognition.h b/Libraries/LibWeb/Speech/SpeechRecognition.h
deleted file mode 100644
index 32c72dfd890..00000000000
--- a/Libraries/LibWeb/Speech/SpeechRecognition.h
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Utf16String.h>
-#include <LibGC/Ptr.h>
-#include <LibWeb/DOM/EventTarget.h>
-#include <LibWeb/Forward.h>
-#include <LibWeb/HTML/EventNames.h>
-#include <LibWeb/Speech/SpeechGrammarList.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-#include <LibWebCommon/WebIDL/Types.h>
-
-#define ENUMERATE_SPEECH_RECOGNITION_EVENT_HANDLERS(E) \
- E(onaudiostart, HTML::EventNames::audiostart) \
- E(onsoundstart, HTML::EventNames::soundstart) \
- E(onspeechstart, HTML::EventNames::speechstart) \
- E(onspeechend, HTML::EventNames::speechend) \
- E(onsoundend, HTML::EventNames::soundend) \
- E(onaudioend, HTML::EventNames::audioend) \
- E(onresult, HTML::EventNames::result) \
- E(onnomatch, HTML::EventNames::nomatch) \
- E(onerror, HTML::EventNames::error) \
- E(onstart, HTML::EventNames::start) \
- E(onend, HTML::EventNames::end)
-
-namespace Web::Speech {
-
-class SpeechRecognition final : public DOM::EventTarget {
- WEB_WRAPPABLE(SpeechRecognition, DOM::EventTarget);
- GC_DECLARE_ALLOCATOR(SpeechRecognition);
-
-public:
- static constexpr size_t grammars_offset() { return offsetof(SpeechRecognition, m_grammars); }
- static GC::Ref<SpeechRecognition> create();
- virtual ~SpeechRecognition() override;
-
- // https://wicg.github.io/speech-api/#dom-speechrecognition-grammars
- GC::Ref<SpeechGrammarList> grammars() const { return *m_grammars; }
- void set_grammars(GC::Ref<SpeechGrammarList> grammars) { m_grammars = grammars; }
-
- // https://wicg.github.io/speech-api/#dom-speechrecognition-lang
- Utf16String const& lang() const { return m_lang; }
- void set_lang(Utf16String const& lang) { m_lang = lang; }
-
- // https://wicg.github.io/speech-api/#dom-speechrecognition-continuous
- bool continuous() const { return m_continuous; }
- void set_continuous(bool continuous) { m_continuous = continuous; }
-
- // https://wicg.github.io/speech-api/#dom-speechrecognition-interimresults
- bool interim_results() const { return m_interim_results; }
- void set_interim_results(bool interim_results) { m_interim_results = interim_results; }
-
- // https://wicg.github.io/speech-api/#dom-speechrecognition-maxalternatives
- WebIDL::UnsignedLong max_alternatives() const { return m_max_alternatives; }
- void set_max_alternatives(WebIDL::UnsignedLong max_alternatives) { m_max_alternatives = max_alternatives; }
-
-#undef __ENUMERATE
-#define __ENUMERATE(attribute_name, event_name) \
- void set_##attribute_name(GC::Ptr<WebIDL::CallbackType>); \
- GC::Ptr<WebIDL::CallbackType> attribute_name();
- ENUMERATE_SPEECH_RECOGNITION_EVENT_HANDLERS(__ENUMERATE)
-#undef __ENUMERATE
-
-private:
- explicit SpeechRecognition();
- virtual void visit_edges(Cell::Visitor&) override;
-
- GC::Ptr<SpeechGrammarList> m_grammars;
- Utf16String m_lang;
- bool m_continuous { false };
- bool m_interim_results { false };
- WebIDL::UnsignedLong m_max_alternatives { 1 };
-};
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechRecognition.idl b/Libraries/LibWeb/Speech/SpeechRecognition.idl
index 750609374f0..1c2573fb54a 100644
--- a/Libraries/LibWeb/Speech/SpeechRecognition.idl
+++ b/Libraries/LibWeb/Speech/SpeechRecognition.idl
@@ -1,9 +1,9 @@
// https://wicg.github.io/speech-api/#speechrecognition
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface SpeechRecognition : EventTarget {
constructor();
- [DirectGetter] attribute SpeechGrammarList grammars;
+ attribute SpeechGrammarList grammars;
attribute DOMString lang;
attribute boolean continuous;
attribute boolean interimResults;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/speech-api/SpeechGrammarList-item.https.txt b/Tests/LibWeb/Text/expected/wpt-import/speech-api/SpeechGrammarList-item.https.txt
new file mode 100644
index 00000000000..5b1370c5eb9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/speech-api/SpeechGrammarList-item.https.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 2 tests
+
+1 Pass
+1 Fail
+Pass item() returns null for an out-of-range index
+Fail item() returns the grammar at an in-range index
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/speech-api/historical.txt b/Tests/LibWeb/Text/expected/wpt-import/speech-api/historical.txt
new file mode 100644
index 00000000000..06ab8bd67f5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/speech-api/historical.txt
@@ -0,0 +1,14 @@
+Harness status: OK
+
+Found 9 tests
+
+9 Pass
+Pass SpeechRecognitionError interface should not exist
+Pass webkitSpeechGrammar interface should not exist
+Pass webkitSpeechGrammarList interface should not exist
+Pass webkitSpeechRecognition interface should not exist
+Pass webkitSpeechRecognitionError interface should not exist
+Pass webkitSpeechRecognitionEvent interface should not exist
+Pass SpeechRecognition's serviceURI attribute should not exist
+Pass SpeechRecognitionEvent's interpretation attribute should not exist
+Pass SpeechRecognitionEvent's emma attribute should not exist
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/speech-api/SpeechGrammarList-item.https.html b/Tests/LibWeb/Text/input/wpt-import/speech-api/SpeechGrammarList-item.https.html
new file mode 100644
index 00000000000..d8ef75920a0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/speech-api/SpeechGrammarList-item.https.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<title>SpeechGrammarList item()</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script>
+window.SpeechGrammarList = window.SpeechGrammarList || window.webkitSpeechGrammarList;
+
+test(() => {
+ const list = new SpeechGrammarList();
+ assert_equals(list.length, 0, 'length of a new list');
+ assert_equals(list.item(0), null, 'item(0)');
+ assert_equals(list.item(255), null, 'item(255)');
+ assert_equals(list[255], undefined, 'list[255]');
+}, 'item() returns null for an out-of-range index');
+
+test(() => {
+ const list = new SpeechGrammarList();
+ list.addFromString('#ABNF 1.0 UTF-8; language en-US; root $hello;');
+ assert_equals(list.length, 1, 'length after addFromString()');
+ assert_true(list.item(0) instanceof SpeechGrammar, 'item(0)');
+ assert_equals(list.item(1), null, 'item(1)');
+}, 'item() returns the grammar at an in-range index');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/speech-api/historical.html b/Tests/LibWeb/Text/input/wpt-import/speech-api/historical.html
new file mode 100644
index 00000000000..d37e2302abb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/speech-api/historical.html
@@ -0,0 +1,34 @@
+<!doctype html>
+<title>Historical Speech API features</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<div id="log"></div>
+<script>
+[
+ "SpeechRecognitionError",
+ "webkitSpeechGrammar",
+ "webkitSpeechGrammarList",
+ "webkitSpeechRecognition",
+ "webkitSpeechRecognitionError",
+ "webkitSpeechRecognitionEvent",
+].forEach(name => {
+ test(() => {
+ assert_false(name in window);
+ }, `${name} interface should not exist`);
+});
+
+test(() => {
+ assert_implements('SpeechRecognition' in window, 'SpeechRecognition exposed');
+ assert_false("serviceURI" in SpeechRecognition.prototype);
+}, "SpeechRecognition's serviceURI attribute should not exist");
+
+[
+ "interpretation",
+ "emma",
+].forEach(name => {
+ test(() => {
+ assert_implements('SpeechRecognitionEvent' in window, 'SpeechRecognitionEvent exposed');
+ assert_false(name in SpeechRecognitionEvent.prototype);
+ }, `SpeechRecognitionEvent's ${name} attribute should not exist`);
+});
+</script>