summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/css-properties.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /devtools/server/actors/css-properties.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/server/actors/css-properties.js')
-rw-r--r--devtools/server/actors/css-properties.js105
1 files changed, 105 insertions, 0 deletions
diff --git a/devtools/server/actors/css-properties.js b/devtools/server/actors/css-properties.js
new file mode 100644
index 0000000000..2b2633ae16
--- /dev/null
+++ b/devtools/server/actors/css-properties.js
@@ -0,0 +1,105 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Actor } = require("resource://devtools/shared/protocol.js");
+const {
+ cssPropertiesSpec,
+} = require("resource://devtools/shared/specs/css-properties.js");
+
+loader.lazyRequireGetter(
+ this,
+ "CSS_TYPES",
+ "resource://devtools/shared/css/constants.js",
+ true
+);
+
+class CssPropertiesActor extends Actor {
+ constructor(conn, targetActor) {
+ super(conn, cssPropertiesSpec);
+ this.targetActor = targetActor;
+ }
+
+ getCSSDatabase() {
+ const properties = generateCssProperties(this.targetActor.window.document);
+
+ return { properties };
+ }
+}
+exports.CssPropertiesActor = CssPropertiesActor;
+
+/**
+ * Generate the CSS properties object. Every key is the property name, while
+ * the values are objects that contain information about that property.
+ *
+ * @param {Document} doc
+ * @return {Object}
+ */
+function generateCssProperties(doc) {
+ const properties = {};
+ const propertyNames = InspectorUtils.getCSSPropertyNames({
+ includeAliases: true,
+ });
+
+ for (const name of propertyNames) {
+ // Get the list of CSS types this property supports.
+ const supports = [];
+ for (const type in CSS_TYPES) {
+ if (safeCssPropertySupportsType(name, type)) {
+ supports.push(type);
+ }
+ }
+
+ const values = InspectorUtils.getCSSValuesForProperty(name);
+ const subproperties = InspectorUtils.getSubpropertiesForCSSProperty(name);
+
+ properties[name] = {
+ isInherited: InspectorUtils.isInheritedProperty(doc, name),
+ values,
+ supports,
+ subproperties,
+ };
+ }
+
+ return properties;
+}
+exports.generateCssProperties = generateCssProperties;
+
+/**
+ * Test if a CSS is property is known using server-code.
+ *
+ * @param {string} name
+ * @return {Boolean}
+ */
+function isCssPropertyKnown(name) {
+ try {
+ // If the property name is unknown, the cssPropertyIsShorthand
+ // will throw an exception. But if it is known, no exception will
+ // be thrown; so we just ignore the return value.
+ InspectorUtils.cssPropertyIsShorthand(name);
+ return true;
+ } catch (e) {
+ return false;
+ }
+}
+
+exports.isCssPropertyKnown = isCssPropertyKnown;
+
+/**
+ * A wrapper for InspectorUtils.cssPropertySupportsType that ignores invalid
+ * properties.
+ *
+ * @param {String} name The property name.
+ * @param {number} type The type tested for support.
+ * @return {Boolean} Whether the property supports the type.
+ * If the property is unknown, false is returned.
+ */
+function safeCssPropertySupportsType(name, type) {
+ try {
+ return InspectorUtils.cssPropertySupportsType(name, type);
+ } catch (e) {
+ return false;
+ }
+}