LibWeb: Add a simplified Notification constructor

This allows the Notification object to be created in javascript without
any additional functionalities.

It passes two wpt tests which require a call to the notification
constructor with no arguments.

https://wpt.live/notifications/constructor-basic.https.html

https://wpt.live/notifications/constructor-invalid.https.html
This commit is contained in:
Niccolo Antonelli Dziri 2025-09-17 09:14:38 +02:00 committed by Sam Atkins
parent 0aec8912c9
commit a72b0c29bb
Notes: github-actions[bot] 2025-09-24 10:54:18 +00:00
12 changed files with 263 additions and 0 deletions

View file

@ -759,6 +759,7 @@ set(SOURCES
NavigationTiming/EntryNames.cpp
NavigationTiming/PerformanceNavigation.cpp
NavigationTiming/PerformanceTiming.cpp
NotificationsAPI/Notification.cpp
Page/DragAndDropEventHandler.cpp
Page/EventHandler.cpp
Page/InputEvent.cpp

View file

@ -910,6 +910,15 @@ class PerformanceTiming;
}
namespace Web::NotificationsAPI {
struct NotificationAction;
struct NotificationOptions;
class Notification;
}
namespace Web::Painting {
class AudioPaintable;

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2025, Niccolo Antonelli-Dziri <niccolo.antonelli-dziri@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/NotificationPrototype.h>
#include <LibWeb/NotificationsAPI/Notification.h>
namespace Web::NotificationsAPI {
GC_DEFINE_ALLOCATOR(Notification);
Notification::Notification(JS::Realm& realm)
: DOM::EventTarget(realm)
{
}
// https://notifications.spec.whatwg.org/#constructors
WebIDL::ExceptionOr<GC::Ref<Notification>> Notification::construct_impl(
JS::Realm& realm,
String, // FIXME: title is unused
Optional<NotificationOptions>) // FIXME: options is unused
{
// FIXME: all of the steps specified in the spec
return realm.create<Notification>(realm);
}
void Notification::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(Notification);
Base::initialize(realm);
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2025, Niccolo Antonelli-Dziri <niccolo.antonelli-dziri@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HighResolutionTime/EpochTimeStamp.h>
namespace Web::NotificationsAPI {
struct NotificationAction {
String action;
String title;
Optional<String> navigate;
Optional<String> icon;
};
struct NotificationOptions {
Bindings::NotificationDirection dir = Bindings::NotificationDirection::Auto;
String lang = ""_string;
String body = ""_string;
Optional<String> navigate;
String tag = ""_string;
Optional<String> image;
Optional<String> icon;
Optional<String> badge;
// VibratePattern vibrate; // FIXME: properly implement vibrate pattern
Optional<HighResolutionTime::EpochTimeStamp> timestamp;
bool renotify = false;
Optional<bool> silent;
bool require_interaction = false;
JS::Value data;
Vector<NotificationAction> actions;
};
// https://notifications.spec.whatwg.org/#notifications
class WEB_API Notification final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(Notification, DOM::EventTarget);
GC_DECLARE_ALLOCATOR(Notification);
public:
[[nodiscard]] static WebIDL::ExceptionOr<GC::Ref<Notification>> construct_impl(
JS::Realm& realm,
String title,
Optional<NotificationOptions> options);
private:
Notification(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
}

View file

@ -0,0 +1,76 @@
#import <DOM/EventTarget.idl>
#import <HighResolutionTime/EpochTimeStamp.idl>
// https://notifications.spec.whatwg.org/#notification
[Exposed=(Window,Worker)]
interface Notification : EventTarget {
constructor(DOMString title, optional NotificationOptions options = {});
// FIXME: static readonly attribute NotificationPermission permission;
// FIXME: [Exposed=Window] static Promise<NotificationPermission> requestPermission(optional NotificationPermissionCallback deprecatedCallback);
// FIXME: static readonly attribute unsigned long maxActions;
[FIXME] attribute EventHandler onclick;
[FIXME] attribute EventHandler onshow;
[FIXME] attribute EventHandler onerror;
[FIXME] attribute EventHandler onclose;
[FIXME] readonly attribute DOMString title;
[FIXME] readonly attribute NotificationDirection dir;
[FIXME] readonly attribute DOMString lang;
[FIXME] readonly attribute DOMString body;
[FIXME] readonly attribute USVString navigate;
[FIXME] readonly attribute DOMString tag;
[FIXME] readonly attribute USVString image;
[FIXME] readonly attribute USVString icon;
[FIXME] readonly attribute USVString badge;
// FIXME: [SameObject] readonly attribute FrozenArray<unsigned long> vibrate;
[FIXME] readonly attribute EpochTimeStamp timestamp;
[FIXME] readonly attribute boolean renotify;
[FIXME] readonly attribute boolean? silent;
[FIXME] readonly attribute boolean requireInteraction;
// FIXME: [SameObject] readonly attribute any data;
// FIXME: [SameObject] readonly attribute FrozenArray<NotificationAction> actions;
[FIXME] undefined close();
};
dictionary NotificationOptions {
NotificationDirection dir = "auto";
DOMString lang = "";
DOMString body = "";
USVString navigate;
DOMString tag = "";
USVString image;
USVString icon;
USVString badge;
// FIXME: VibratePattern vibrate;
EpochTimeStamp timestamp;
boolean renotify = false;
boolean? silent = null;
boolean requireInteraction = false;
any data = null;
sequence<NotificationAction> actions = [];
};
enum NotificationPermission {
"default",
"denied",
"granted"
};
enum NotificationDirection {
"auto",
"ltr",
"rtl"
};
dictionary NotificationAction {
required DOMString action;
required DOMString title;
USVString navigate;
USVString icon;
};
[FIXME] callback NotificationPermissionCallback = undefined (NotificationPermission permission);

View file

@ -324,6 +324,7 @@ libweb_js_bindings(MediaSourceExtensions/SourceBuffer)
libweb_js_bindings(MediaSourceExtensions/SourceBufferList)
libweb_js_bindings(NavigationTiming/PerformanceNavigation)
libweb_js_bindings(NavigationTiming/PerformanceTiming)
libweb_js_bindings(NotificationsAPI/Notification)
libweb_js_bindings(PerformanceTimeline/PerformanceEntry)
libweb_js_bindings(PerformanceTimeline/PerformanceObserver)
libweb_js_bindings(PerformanceTimeline/PerformanceObserverEntryList)

View file

@ -4940,6 +4940,7 @@ using namespace Web::IntersectionObserver;
using namespace Web::MediaCapabilitiesAPI;
using namespace Web::MediaSourceExtensions;
using namespace Web::NavigationTiming;
using namespace Web::NotificationsAPI;
using namespace Web::PerformanceTimeline;
using namespace Web::RequestIdleCallback;
using namespace Web::ResizeObserver;

View file

@ -304,6 +304,7 @@ Node
NodeFilter
NodeIterator
NodeList
Notification
Number
Object
OfflineAudioContext

View file

@ -0,0 +1,9 @@
Harness status: OK
Found 3 tests
1 Pass
2 Fail
Pass Called the notification constructor with one argument.
Fail Constructing a notification without a NotificationOptions defaults to null.
Fail constructing a notification with a NotificationOptions dictionary correctly sets and reflects the silent attribute.

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Called the notification constructor with no arguments.

View file

@ -0,0 +1,46 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Notification constructor (basic)</title>
<link rel="author" title="Intel" href="http://www.intel.com/">
<link rel="author" title="Xin Liu" href="mailto:xinx.liu@intel.com">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
test(function() {
var notification = new Notification("New Email Received")
assert_true(notification instanceof Notification)
notification.onshow = function() {
notification.close()
}
}, "Called the notification constructor with one argument.")
test(() => {
assert_equals(
new Notification("a").silent,
null,
"Expected null by default"
);
}, "Constructing a notification without a NotificationOptions defaults to null.");
test(() => {
for (const silent of [null, undefined]) {
assert_equals(
new Notification("a", { silent }).silent,
null,
`Expected silent to be null when initialized with ${silent}.`
);
}
for (const silent of [true, 1, 100, {}, [], "a string"]) {
assert_true(
new Notification("a", { silent }).silent,
`Expected silent to be true when initialized with ${silent}.`
);
}
for (const silent of [false, 0, "", NaN]) {
assert_false(
new Notification("a", { silent }).silent,
`Expected silent to be false when initialized with ${silent}.`
);
}
}, "constructing a notification with a NotificationOptions dictionary correctly sets and reflects the silent attribute.");
</script>

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Notification constructor (invalid)</title>
<link rel="author" title="Intel" href="http://www.intel.com/">
<link rel="author" title="Xin Liu" href="mailto:xinx.liu@intel.com">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
test(function() {
assert_throws_js(TypeError, function() {
new Notification()
})
}, "Called the notification constructor with no arguments.")
</script>