summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Bindings
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2021-10-06 14:27:13 +0100
committerAndreas Kling <kling@serenityos.org>2021-10-08 23:02:57 +0200
commit575ce0414827cf6bc76e7c954eef762393407993 (patch)
tree12b31f046bd8216f8468ed6ddb90f825a5b4ffcb /Userland/Libraries/LibWeb/Bindings
parent3c0b55c284ba2a7e64a0c311f96cf3f3b9bcc14a (diff)
downloadserenity-575ce0414827cf6bc76e7c954eef762393407993.zip
LibWeb: Add CSS.escape() JS function
This is the `CSS` namespace defined in IDL here: https://www.w3.org/TR/cssom-1/#namespacedef-css , not to be confused with our `Web::CSS` namespace. Words are hard. `CSS.escape()` lets you escape identifiers that can then be used to create a CSS string. I've also stubbed out the `CSS.supports()` function.
Diffstat (limited to 'Userland/Libraries/LibWeb/Bindings')
-rw-r--r--Userland/Libraries/LibWeb/Bindings/CSSNamespace.cpp75
-rw-r--r--Userland/Libraries/LibWeb/Bindings/CSSNamespace.h28
-rw-r--r--Userland/Libraries/LibWeb/Bindings/WindowObject.cpp3
3 files changed, 106 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/CSSNamespace.cpp b/Userland/Libraries/LibWeb/Bindings/CSSNamespace.cpp
new file mode 100644
index 0000000000..3392d8084e
--- /dev/null
+++ b/Userland/Libraries/LibWeb/Bindings/CSSNamespace.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/ErrorTypes.h>
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/VM.h>
+#include <LibJS/Runtime/Value.h>
+#include <LibWeb/Bindings/CSSNamespace.h>
+
+namespace Web::Bindings {
+
+CSSNamespace::CSSNamespace(JS::GlobalObject& global_object)
+ : JS::Object(*global_object.object_prototype())
+{
+}
+
+CSSNamespace::~CSSNamespace()
+{
+}
+
+void CSSNamespace::initialize(JS::GlobalObject& global_object)
+{
+ Object::initialize(global_object);
+ u8 attr = JS::Attribute::Enumerable;
+ define_native_function("escape", escape, 1, attr);
+ define_native_function("supports", supports, 2, attr);
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-css-escape
+JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::escape)
+{
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.escape");
+ return {};
+ }
+
+ String result = Web::CSS::serialize_an_identifier(vm.argument(0).to_string(global_object));
+ if (vm.exception())
+ return {};
+
+ return JS::Value(JS::js_string(vm, result));
+}
+
+// https://www.w3.org/TR/css-conditional-3/#dom-css-supports
+JS_DEFINE_NATIVE_FUNCTION(CSSNamespace::supports)
+{
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "CSS.supports");
+ return {};
+ }
+
+ if (vm.argument_count() >= 2) {
+ // When the supports(property, value) method is invoked with two arguments property and value:
+ // If property is an ASCII case-insensitive match for any defined CSS property that the UA supports, and value successfully parses according to that property’s grammar, return true.
+ //
+ // Otherwise, if property is a custom property name string, return true.
+ //
+ // Otherwise, return false.
+ return JS::Value(false);
+ } else {
+ // When the supports(conditionText) method is invoked with a single conditionText argument:
+ //
+ // If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
+ //
+ // Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
+ //
+ // Otherwise, return false.
+ return JS::Value(false);
+ }
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/Bindings/CSSNamespace.h b/Userland/Libraries/LibWeb/Bindings/CSSNamespace.h
new file mode 100644
index 0000000000..2791c54580
--- /dev/null
+++ b/Userland/Libraries/LibWeb/Bindings/CSSNamespace.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibJS/Runtime/Object.h>
+#include <LibWeb/CSS/Serialize.h>
+
+namespace Web::Bindings {
+
+// The `CSS` namespace object in IDL. https://www.w3.org/TR/cssom-1/#namespacedef-css
+class CSSNamespace final : public JS::Object {
+ JS_OBJECT(CSSNamespace, JS::Object)
+
+public:
+ explicit CSSNamespace(JS::GlobalObject&);
+ virtual void initialize(JS::GlobalObject&) override;
+ virtual ~CSSNamespace() override;
+
+private:
+ JS_DECLARE_NATIVE_FUNCTION(escape);
+ JS_DECLARE_NATIVE_FUNCTION(supports);
+};
+
+}
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
index e8876a3984..d21887e170 100644
--- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -13,6 +13,7 @@
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Shape.h>
#include <LibTextCodec/Decoder.h>
+#include <LibWeb/Bindings/CSSNamespace.h>
#include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
#include <LibWeb/Bindings/CryptoWrapper.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
@@ -104,6 +105,8 @@ void WindowObject::initialize_global_object()
define_native_accessor("screenLeft", screen_left_getter, {}, attr);
define_native_accessor("screenTop", screen_top_getter, {}, attr);
+ define_direct_property("CSS", heap().allocate<CSSNamespace>(*this, *this), 0);
+
// Legacy
define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable);