Skip to content

Truly independent web browser

LibWeb: Implement SpeechRecognitionPhrase in Rust

Aliaksandr Kalenik8c72e8596a2b
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index 3c6e6545fd7..009153a4d0e 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -33,6 +33,11 @@ pub(crate) mod bindings;
allow(dead_code, reason = "only the generated bindings use the interfaces")
)]
pub(crate) mod encoding;
+#[cfg_attr(
+ not(web_idl_bindings),
+ allow(dead_code, reason = "only the generated bindings use the interfaces")
+)]
+pub(crate) mod speech;
// NB: Only C++ calls into libweb_rust, so nothing in this crate names it. Linking it keeps its extern "C" functions in
// the static library.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs
new file mode 100644
index 00000000000..f87350c0e87
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/mod.rs
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The Web Speech API, https://webaudio.github.io/web-speech-api/
+
+#[cfg(not(test))]
+pub(crate) mod speech_recognition_phrase;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition_phrase.rs b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition_phrase.rs
new file mode 100644
index 00000000000..c5d2ade5be2
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/speech/speech_recognition_phrase.rs
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! SpeechRecognitionPhrase, https://wicg.github.io/speech-api/#speechrecognitionphrase
+
+use ak::Utf16String;
+use libjs_rust::api::Trace;
+
+use crate::bindings::{CallContext, WebIdlResult};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct SpeechRecognitionPhrase {
+ phrase: Utf16String,
+ boost: f32,
+}
+
+#[expect(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every method of an implementation for its WebIDL result, whether or not it can fail"
+)]
+impl SpeechRecognitionPhrase {
+ // https://wicg.github.io/speech-api/#dom-speechrecognitionphrase-speechrecognitionphrase
+ pub(crate) fn construct(_: &mut CallContext, phrase: Utf16String, boost: f32) -> WebIdlResult<Self> {
+ Ok(Self { phrase, boost })
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognitionphrase-phrase
+ pub(crate) fn phrase(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
+ Ok(self.phrase.clone())
+ }
+
+ // https://wicg.github.io/speech-api/#dom-speechrecognitionphrase-boost
+ pub(crate) fn boost(&self, _: &mut CallContext) -> WebIdlResult<f32> {
+ Ok(self.boost)
+ }
+}
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index a98413f02d7..2b8c8d9b3d3 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -837,7 +837,6 @@ set(SOURCES
Speech/SpeechRecognition.cpp
Speech/SpeechRecognitionAlternative.cpp
Speech/SpeechRecognitionEvent.cpp
- Speech/SpeechRecognitionPhrase.cpp
Speech/SpeechRecognitionResult.cpp
Speech/SpeechRecognitionResultList.cpp
Speech/SpeechSynthesis.cpp
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 367d109267e..678124b788c 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -1079,7 +1079,6 @@ class SpeechGrammarList;
class SpeechRecognition;
class SpeechRecognitionAlternative;
class SpeechRecognitionEvent;
-class SpeechRecognitionPhrase;
class SpeechRecognitionResult;
class SpeechRecognitionResultList;
class SpeechSynthesis;
diff --git a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.cpp b/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.cpp
deleted file mode 100644
index 69928c82dd6..00000000000
--- a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/Speech/SpeechRecognitionPhrase.h>
-
-namespace Web::Speech {
-
-GC_DEFINE_ALLOCATOR(SpeechRecognitionPhrase);
-
-GC::Ref<SpeechRecognitionPhrase> SpeechRecognitionPhrase::create(Utf16String const& phrase, float boost)
-{
- return GC::Heap::the().allocate<SpeechRecognitionPhrase>(phrase, boost);
-}
-
-SpeechRecognitionPhrase::SpeechRecognitionPhrase(Utf16String const& phrase, float boost)
- : m_phrase(phrase)
- , m_boost(boost)
-{
-}
-
-SpeechRecognitionPhrase::~SpeechRecognitionPhrase() = default;
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.h b/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.h
deleted file mode 100644
index fe510ed5aca..00000000000
--- a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.h
+++ /dev/null
@@ -1,39 +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 <LibJS/Forward.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/Forward.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::Speech {
-
-class SpeechRecognitionPhrase final : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(SpeechRecognitionPhrase, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(SpeechRecognitionPhrase);
-
-public:
- static GC::Ref<SpeechRecognitionPhrase> create(Utf16String const& phrase, float boost = 1.f);
- virtual ~SpeechRecognitionPhrase() override;
-
- // https://wicg.github.io/speech-api/#dom-speechrecognitionphrase-phrase
- Utf16String const& phrase() const { return m_phrase; }
-
- // https://wicg.github.io/speech-api/#dom-speechrecognitionphrase-boost
- float boost() const { return m_boost; }
-
-private:
- SpeechRecognitionPhrase(Utf16String const& phrase, float boost);
-
- Utf16String m_phrase;
- float m_boost { 1.f };
-};
-
-}
diff --git a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.idl b/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.idl
index eeff395bf5e..c8fbabaf360 100644
--- a/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.idl
+++ b/Libraries/LibWeb/Speech/SpeechRecognitionPhrase.idl
@@ -1,5 +1,5 @@
// https://wicg.github.io/speech-api/#speechrecognitionphrase
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface SpeechRecognitionPhrase {
constructor(DOMString phrase, optional float boost = 1.0);
readonly attribute DOMString phrase;