Bug 1467408 - (Part 1) Skip keyword values and use computed styles when populating the font editor. r=gl
MozReview-Commit-ID: DpOjrfEudL6
--- a/devtools/client/inspector/fonts/fonts.js
+++ b/devtools/client/inspector/fonts/fonts.js
@@ -5,16 +5,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Services = require("Services");
const { gDevTools } = require("devtools/client/framework/devtools");
const { getColor } = require("devtools/client/shared/theme");
const { createFactory, createElement } = require("devtools/client/shared/vendor/react");
+const { getCssProperties } = require("devtools/shared/fronts/css-properties");
const { Provider } = require("devtools/client/shared/vendor/react-redux");
const { debounce } = require("devtools/shared/debounce");
const { ELEMENT_STYLE } = require("devtools/shared/specs/styles");
const FontsApp = createFactory(require("./components/FontsApp"));
const { LocalizationHelper } = require("devtools/shared/l10n");
const INSPECTOR_L10N =
@@ -50,18 +51,21 @@ const REGISTERED_AXES_TO_FONT_PROPERTIES
"slnt": "font-style",
"wdth": "font-stretch",
"wght": "font-weight",
};
const REGISTERED_AXES = Object.keys(REGISTERED_AXES_TO_FONT_PROPERTIES);
class FontInspector {
constructor(inspector, window) {
+ this.cssProperties = getCssProperties(inspector.toolbox);
this.document = window.document;
this.inspector = inspector;
+ // Set of unique keyword values supported by designated font properties.
+ this.keywordValues = new Set(this.getFontPropertyValueKeywords());
this.nodeComputedStyle = {};
this.pageStyle = this.inspector.pageStyle;
this.ruleView = this.inspector.getPanel("ruleview").view;
this.selectedRule = null;
this.store = this.inspector.store;
// Map CSS property names and variable font axis names to methods that write their
// corresponding values to the appropriate TextProperty from the Rule view.
// Values of variable font registered axes may be written to CSS font properties under
@@ -193,22 +197,21 @@ class FontInspector {
}
return acc;
}, []);
}
/**
* Get all expected CSS font properties and values from the node's matching rules and
- * fallback to computed style.
+ * fallback to computed style. Skip CSS Custom Properties, `calc()` and keyword values.
*
* @return {Object}
*/
getFontProperties() {
- const KEYWORD_VALUES = ["initial", "inherit", "unset", "none"];
const properties = {};
// First, get all expected font properties from computed styles, if available.
for (const prop of FONT_PROPERTIES) {
properties[prop] =
(this.nodeComputedStyle[prop] && this.nodeComputedStyle[prop].value)
? this.nodeComputedStyle[prop].value
: "";
@@ -217,29 +220,46 @@ class FontInspector {
// Then, replace with enabled font properties found on any of the rules that apply.
for (const rule of this.ruleView.rules) {
if (rule.inherited) {
continue;
}
for (const textProp of rule.textProps) {
if (FONT_PROPERTIES.includes(textProp.name) &&
- !KEYWORD_VALUES.includes(textProp.value) &&
+ !this.keywordValues.has(textProp.value) &&
!textProp.value.includes("calc(") &&
!textProp.value.includes("var(") &&
!textProp.overridden &&
textProp.enabled) {
properties[textProp.name] = textProp.value;
}
}
}
return properties;
}
+ /**
+ * Get an array of keyword values supported by the following CSS properties:
+ * - font-size
+ * - font-weight
+ * - font-stretch
+ *
+ * This list is used to filter out values when reading CSS font properties from rules.
+ * Computed styles will be used instead of any of these values.
+ *
+ * @return {Array}
+ */
+ getFontPropertyValueKeywords() {
+ return ["font-size", "font-weight", "font-stretch"].reduce((acc, property) => {
+ return acc.concat(this.cssProperties.getValues(property));
+ }, []);
+ }
+
async getFontsForNode(node, options) {
// In case we've been destroyed in the meantime
if (!this.document) {
return [];
}
const fonts =
await this.pageStyle.getUsedFontFaces(node, options).catch(console.error);