From e5fbaa5d69d1d11aa50ed80fd497685b05c58909 Mon Sep 17 00:00:00 2001 From: Nicolas Chevobbe Date: Thu, 15 Feb 2024 14:04:32 +0000 Subject: [PATCH] Bug 1247751 - [devtools] Add InspectorUtils.getRuleIndex. r=emilio,devtools-reviewers,ochameau. Differential Revision: https://phabricator.services.mozilla.com/D201383 --- devtools/server/actors/style-rule.js | 31 +------ dom/chrome-webidl/InspectorUtils.webidl | 1 + layout/inspector/InspectorUtils.cpp | 45 ++++++++++ layout/inspector/InspectorUtils.h | 3 + layout/inspector/tests/mochitest.toml | 2 + layout/inspector/tests/test_getRuleIndex.html | 88 +++++++++++++++++++ 6 files changed, 141 insertions(+), 29 deletions(-) create mode 100644 layout/inspector/tests/test_getRuleIndex.html diff --git a/devtools/server/actors/style-rule.js b/devtools/server/actors/style-rule.js index 512279c17cfe..e9f39fa3d067 100644 --- a/devtools/server/actors/style-rule.js +++ b/devtools/server/actors/style-rule.js @@ -611,35 +611,8 @@ class StyleRuleActor extends Actor { * nested rules. */ _computeRuleIndex() { - let rule = this.rawRule; - const result = []; - - while (rule) { - let cssRules = []; - if (rule.parentRule) { - cssRules = rule.parentRule.cssRules; - } else if (rule.parentStyleSheet) { - cssRules = rule.parentStyleSheet.cssRules; - } - - let found = false; - for (let i = 0; i < cssRules.length; i++) { - if (rule === cssRules.item(i)) { - found = true; - result.unshift(i); - break; - } - } - - if (!found) { - this._ruleIndex = null; - return; - } - - rule = rule.parentRule; - } - - this._ruleIndex = result; + const index = InspectorUtils.getRuleIndex(this.rawRule); + this._ruleIndex = index.length ? index : null; } /** diff --git a/dom/chrome-webidl/InspectorUtils.webidl b/dom/chrome-webidl/InspectorUtils.webidl index 5d785e8051b2..e3ca8a3b51a3 100644 --- a/dom/chrome-webidl/InspectorUtils.webidl +++ b/dom/chrome-webidl/InspectorUtils.webidl @@ -21,6 +21,7 @@ namespace InspectorUtils { unsigned long getRuleLine(CSSRule rule); unsigned long getRuleColumn(CSSRule rule); unsigned long getRelativeRuleLine(CSSRule rule); + sequence getRuleIndex(CSSRule rule); boolean hasRulesModifiedByCSSOM(CSSStyleSheet sheet); // Get a flat list of all rules (including nested ones) of a given stylesheet. // Useful for DevTools as this is faster than in JS where we'd have a lot of diff --git a/layout/inspector/InspectorUtils.cpp b/layout/inspector/InspectorUtils.cpp index acebb863066c..e2111aa9bd04 100644 --- a/layout/inspector/InspectorUtils.cpp +++ b/layout/inspector/InspectorUtils.cpp @@ -32,6 +32,7 @@ #include "mozilla/dom/CSSBinding.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/CSSStyleRule.h" +#include "mozilla/dom/CSSKeyframesRule.h" #include "mozilla/dom/Highlight.h" #include "mozilla/dom/HighlightRegistry.h" #include "mozilla/dom/InspectorUtilsBinding.h" @@ -350,6 +351,50 @@ uint32_t InspectorUtils::GetRelativeRuleLine(GlobalObject& aGlobal, return aRule.GetLineNumber() + 1; } + +void InspectorUtils::GetRuleIndex(GlobalObject& aGlobal, + css::Rule& aRule, + nsTArray& aResult) { + css::Rule* currentRule = &aRule; + + do { + css::Rule* parentRule = currentRule->GetParentRule(); + dom::CSSRuleList* ruleList = nullptr; + + if (parentRule) { + if (parentRule->IsGroupRule()) { + ruleList = static_cast(parentRule)->CssRules(); + } else if (parentRule->Type() == StyleCssRuleType::Keyframes) { + ruleList = static_cast(parentRule)->CssRules(); + } else { + MOZ_ASSERT_UNREACHABLE("Unknown parent rule type?"); + } + } else if (StyleSheet* sheet = currentRule->GetStyleSheet()) { + ruleList = sheet->GetCssRulesInternal(); + } + + if (!ruleList) { + return; + } + + bool found = false; + for (uint32_t i = 0, len = ruleList->Length(); i < len; ++i) { + css::Rule* rule = ruleList->Item(i); + if (currentRule == rule) { + found = true; + aResult.InsertElementAt(0, i); + break; + } + } + + if (!found) { + return; + } + + currentRule = parentRule; + } while (currentRule); +} + /* static */ bool InspectorUtils::HasRulesModifiedByCSSOM(GlobalObject& aGlobal, StyleSheet& aSheet) { diff --git a/layout/inspector/InspectorUtils.h b/layout/inspector/InspectorUtils.h index ef841eefdf0f..06bc0ff5fce5 100644 --- a/layout/inspector/InspectorUtils.h +++ b/layout/inspector/InspectorUtils.h @@ -72,6 +72,9 @@ class InspectorUtils { */ static uint32_t GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule); + static void GetRuleIndex(GlobalObject& aGlobal, css::Rule& aRule, + nsTArray& aResult); + static bool HasRulesModifiedByCSSOM(GlobalObject& aGlobal, StyleSheet& aSheet); diff --git a/layout/inspector/tests/mochitest.toml b/layout/inspector/tests/mochitest.toml index 9c9ba8e60a57..3db9693c2073 100644 --- a/layout/inspector/tests/mochitest.toml +++ b/layout/inspector/tests/mochitest.toml @@ -58,6 +58,8 @@ support-files = [ ["test_getRelativeRuleLine.html"] +["test_getRuleIndex.html"] + ["test_get_all_style_sheets.html"] ["test_is_element_themed.html"] diff --git a/layout/inspector/tests/test_getRuleIndex.html b/layout/inspector/tests/test_getRuleIndex.html new file mode 100644 index 000000000000..0eb9dba27b65 --- /dev/null +++ b/layout/inspector/tests/test_getRuleIndex.html @@ -0,0 +1,88 @@ + + + + Test InspectorUtils.getRuleIndex + + + + + +InspectorUtils.getRuleIndex + + + + +