Bug 1905569 - [devtools] Add InspectorUtils.getCSSRegisteredProperty. r=emilio.

In some cases, DevTools might want to get the property definition of a given registered property,
so we're adding a new InspectorUtils method for this.
It returns null when there is no registered property matching the passed name.
A test is added to ensure this behaves as expected.

Differential Revision: https://phabricator.services.mozilla.com/D215317
This commit is contained in:
Nicolas Chevobbe 2024-07-01 13:35:27 +00:00
Родитель a0b50ecbfc
Коммит 2e3e77edb3
6 изменённых файлов: 203 добавлений и 0 удалений

Просмотреть файл

@ -91,6 +91,7 @@ namespace InspectorUtils {
[NewObject] NodeList getOverflowingChildrenOfElement(Element element);
sequence<DOMString> getRegisteredCssHighlights(Document document, optional boolean activeOnly = false);
sequence<InspectorCSSPropertyDefinition> getCSSRegisteredProperties(Document document);
InspectorCSSPropertyDefinition? getCSSRegisteredProperty(Document document, UTF8String name);
boolean valueMatchesSyntax(Document document, UTF8String value, UTF8String syntax);
// Get the first rule body text within initialText

Просмотреть файл

@ -1044,6 +1044,37 @@ void InspectorUtils::GetCSSRegisteredProperties(
}
}
/* static */
void InspectorUtils::GetCSSRegisteredProperty(
GlobalObject& aGlobalObject, Document& aDocument, const nsACString& aName,
Nullable<InspectorCSSPropertyDefinition>& aResult) {
StylePropDef result{StyleAtom(NS_Atomize(aName))};
// Update the rules before looking up @property rules.
ServoStyleSet& styleSet = aDocument.EnsureStyleSet();
styleSet.UpdateStylistIfNeeded();
if (!Servo_GetRegisteredCustomProperty(styleSet.RawData(), &aName, &result)) {
aResult.SetNull();
return;
}
InspectorCSSPropertyDefinition& propDef = aResult.SetValue();
// Servo does not include the "--" prefix in the property definition name.
// Add it back as it's easier for DevTools to handle them _with_ "--".
propDef.mName.AssignLiteral("--");
propDef.mName.Append(nsAtomCString(result.name.AsAtom()));
propDef.mSyntax.Append(result.syntax);
propDef.mInherits = result.inherits;
if (result.has_initial_value) {
propDef.mInitialValue.Append(result.initial_value);
} else {
propDef.mInitialValue.SetIsVoid(true);
}
propDef.mFromJS = result.from_js;
}
/* static */
bool InspectorUtils::ValueMatchesSyntax(GlobalObject&, Document& aDocument,
const nsACString& aValue,

Просмотреть файл

@ -272,6 +272,13 @@ class InspectorUtils {
GlobalObject& aGlobal, Document& aDocument,
nsTArray<InspectorCSSPropertyDefinition>& aResult);
/**
* Get a single registered CSS property
*/
static void GetCSSRegisteredProperty(
GlobalObject& aGlobal, Document& aDocument, const nsACString& aName,
Nullable<InspectorCSSPropertyDefinition>& aResult);
/**
* Returns whether or not a CSS property value is valid for the passed syntax
*/

Просмотреть файл

@ -63,6 +63,8 @@ support-files = [
["test_getRegisteredCustomProperties.html"]
["test_getRegisteredCustomProperty.html"]
["test_getRelativeRuleLine.html"]
["test_getRuleIndex.html"]

Просмотреть файл

@ -0,0 +1,133 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test InspectorUtils.getCSSRegisteredProperty</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<style>
@property --color-1 {
syntax: "<color>";
inherits: true;
initial-value: blue;
}
@property --color-2 {
syntax: "*";
inherits: false;
}
</style>
</head>
<body>
<code>InspectorUtils.getCSSRegisteredProperty</code>
<script>
"use strict";
/** Test for InspectorUtils.getCSSRegisteredProperty **/
const { Assert } = SpecialPowers.ChromeUtils.importESModule(
"resource://testing-common/Assert.sys.mjs"
);
const InspectorUtils = SpecialPowers.InspectorUtils;
CSS.registerProperty({
name: "--length-1",
syntax: "<length>",
initialValue: "10px",
inherits: true,
});
CSS.registerProperty({
name: "--length-2",
syntax: "foo | <integer>+ | <percentage> | <length># | auto",
initialValue: "100vw",
inherits: true
});
CSS.registerProperty({
name: "--length-3",
syntax: "*",
inherits: false
});
CSS.registerProperty({
name: "--length-4",
syntax: "*",
initialValue: "",
inherits: false
});
is(
InspectorUtils.getCSSRegisteredProperty(document, "--unknown"),
null,
"Returns null when the name does not match a registered property"
);
is(
InspectorUtils.getCSSRegisteredProperty(document, ""),
null,
"Returns null when passed an empty string"
);
is(
InspectorUtils.getCSSRegisteredProperty(document, "color-1"),
null,
"Returns null when passed a property name without leading --"
);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--color-1"), {
name: "--color-1",
syntax: "<color>",
inherits: true,
initialValue: "blue",
fromJS: false,
}, `Got expected registered property definition for "${"--color-1"}"`);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--color-2"), {
name: "--color-2",
syntax: "*",
inherits: false,
initialValue: null,
fromJS: false,
}, `Got expected registered property definition for "${"--color-2"}"`);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--length-1"), {
name: "--length-1",
syntax: "<length>",
inherits: true,
initialValue: "10px",
fromJS: true,
}, `Got expected registered property definition for "${"--length-1"}"`);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--length-2"), {
name: "--length-2",
syntax: "foo | <integer>+ | <percentage> | <length># | auto",
inherits: true,
initialValue: "100vw",
fromJS: true,
}, `Got expected registered property definition for "${"--length-2"}"`);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--length-3"), {
name: "--length-3",
syntax: "*",
inherits: false,
initialValue: null,
fromJS: true,
}, `Got expected registered property definition for "${"--length-3"}"`);
Assert.deepEqual(
InspectorUtils.getCSSRegisteredProperty(document, "--length-4"), {
name: "--length-4",
syntax: "*",
inherits: false,
initialValue: "",
fromJS: true,
}, `Got expected registered property definition for "${"--length-4"}"`);
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -9212,6 +9212,35 @@ pub extern "C" fn Servo_GetRegisteredCustomProperties(
}
}
#[no_mangle]
pub unsafe extern "C" fn Servo_GetRegisteredCustomProperty(
per_doc_data: &PerDocumentStyleData,
name: &nsACString,
custom_property: &mut PropDef,
) -> bool {
let name = name.as_str_unchecked();
if !name.starts_with("--") {
return false;
}
// We store registered property names without the leading "--".
let name = Atom::from(&name[2..]);
let stylist = &per_doc_data.borrow().stylist;
if let Some(property_registration) = stylist.custom_property_script_registry().get(&name) {
*custom_property = PropDef::new(name, property_registration, /* from_js */ true);
return true;
}
for (cascade_data, _) in stylist.iter_origins() {
if let Some(property_registration) = cascade_data.custom_property_registrations().get(&name) {
*custom_property = PropDef::new(name, property_registration, /* from_js */ false);
return true;
}
}
false
}
#[no_mangle]
pub unsafe extern "C" fn Servo_Value_Matches_Syntax(