Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

experimental: support currentcolor in style object model (#3043)

Bogdan Chadkin16f165a681a9
diff --git a/apps/builder/app/shared/style-object-model.test.ts b/apps/builder/app/shared/style-object-model.test.ts
index 4ec669a81a..302623a28d 100644
--- a/apps/builder/app/shared/style-object-model.test.ts
+++ b/apps/builder/app/shared/style-object-model.test.ts
@@ -211,3 +211,115 @@ test("inherit style from ancestors", () => {
.computedValue
).toEqual({ type: "keyword", value: "auto" });
});
+
+test("support currentcolor keyword", () => {
+ const styles: StyleDecl[] = [
+ {
+ breakpointId: "base",
+ styleSourceId: "level1Local",
+ property: "color",
+ value: { type: "keyword", value: "blue" },
+ },
+ {
+ breakpointId: "base",
+ styleSourceId: "level2Local",
+ property: "borderTopColor",
+ // support lower case
+ value: { type: "keyword", value: "currentcolor" },
+ },
+ {
+ breakpointId: "base",
+ styleSourceId: "level2Local",
+ property: "backgroundColor",
+ // support camel case
+ value: { type: "keyword", value: "currentColor" },
+ },
+ ];
+ const model: StyleObjectModel = {
+ styleSourcesByInstanceId: new Map([
+ ["level2", ["level2Local"]],
+ ["level1", ["level1Local"]],
+ ]),
+ styleByStyleSourceId: getStyleByStyleSourceId(styles),
+ };
+ const styleSelector: StyleSelector = {
+ instanceSelector: ["level2", "level1"],
+ };
+ expect(
+ getComputedStyleDecl({ model, styleSelector, property: "borderTopColor" })
+ ).toEqual(
+ expect.objectContaining({
+ computedValue: { type: "keyword", value: "currentcolor" },
+ usedValue: { type: "keyword", value: "blue" },
+ })
+ );
+ expect(
+ getComputedStyleDecl({ model, styleSelector, property: "backgroundColor" })
+ ).toEqual(
+ expect.objectContaining({
+ computedValue: { type: "keyword", value: "currentColor" },
+ usedValue: { type: "keyword", value: "blue" },
+ })
+ );
+});
+
+test("in color property currentcolor is inherited", () => {
+ const styles: StyleDecl[] = [
+ {
+ breakpointId: "base",
+ styleSourceId: "level1Local",
+ property: "color",
+ value: { type: "keyword", value: "blue" },
+ },
+ {
+ breakpointId: "base",
+ styleSourceId: "level2Local",
+ property: "color",
+ value: { type: "keyword", value: "currentcolor" },
+ },
+ ];
+ const model: StyleObjectModel = {
+ styleSourcesByInstanceId: new Map([
+ ["level2", ["level2Local"]],
+ ["level1", ["level1Local"]],
+ ]),
+ styleByStyleSourceId: getStyleByStyleSourceId(styles),
+ };
+ const styleSelector: StyleSelector = {
+ instanceSelector: ["level2", "level1"],
+ };
+ expect(
+ getComputedStyleDecl({ model, styleSelector, property: "color" })
+ ).toEqual(
+ expect.objectContaining({
+ computedValue: { type: "keyword", value: "currentcolor" },
+ usedValue: { type: "keyword", value: "blue" },
+ })
+ );
+});
+
+test("in root color property currentcolor is initial", () => {
+ const styles: StyleDecl[] = [
+ {
+ breakpointId: "base",
+ styleSourceId: "bodyLocal",
+ property: "color",
+ value: { type: "keyword", value: "currentcolor" },
+ },
+ ];
+ const model: StyleObjectModel = {
+ styleSourcesByInstanceId: new Map([["body", ["bodyLocal"]]]),
+ styleByStyleSourceId: getStyleByStyleSourceId(styles),
+ };
+ const styleSelector: StyleSelector = {
+ instanceSelector: ["body"],
+ };
+ expect(
+ getComputedStyleDecl({ model, styleSelector, property: "color" })
+ ).toEqual(
+ expect.objectContaining({
+ computedValue: { type: "keyword", value: "currentcolor" },
+ usedValue: { type: "keyword", value: "black" },
+ })
+ );
+});
diff --git a/apps/builder/app/shared/style-object-model.ts b/apps/builder/app/shared/style-object-model.ts
index 8cc4429c71..38f95341fb 100644
--- a/apps/builder/app/shared/style-object-model.ts
+++ b/apps/builder/app/shared/style-object-model.ts
@@ -83,14 +83,13 @@ const getCascadedValue = ({
};
const matchKeyword = (styleValue: undefined | StyleValue, keyword: string) =>
- styleValue?.type === "keyword" && styleValue.value === keyword;
+ styleValue?.type === "keyword" && styleValue.value.toLowerCase() === keyword;
/**
* follow value processing specification
* https://drafts.csswg.org/css-cascade-5/#value-stages
*
* @todo
- * - current color
* - custom property
* - html
* - preset
@@ -108,17 +107,21 @@ export const getComputedStyleDecl = ({
model: StyleObjectModel;
styleSelector: StyleSelector;
property: Property;
-}) => {
+}): {
+ computedValue: StyleValue;
+ usedValue: StyleValue;
+} => {
const { instanceSelector } = styleSelector;
const propertyData = properties[property];
const inherited = propertyData.inherited;
const initialValue = propertyData.initial;
+ let inheritedValue: StyleValue = initialValue;
let computedValue: StyleValue = initialValue;
// start computing from the root
for (const instanceId of Array.from(instanceSelector).reverse()) {
// https://drafts.csswg.org/css-cascade-5/#inheriting
- const inheritedValue: StyleValue = computedValue;
+ inheritedValue = computedValue;
// https://drafts.csswg.org/css-cascade-5/#cascaded
const { cascadedValue } = getCascadedValue({ model, instanceId, property });
@@ -151,5 +154,21 @@ export const getComputedStyleDecl = ({
computedValue = specifiedValue;
}
- return { computedValue };
+ // https://drafts.csswg.org/css-cascade-5/#used
+ let usedValue: StyleValue = computedValue;
+ // https://drafts.csswg.org/css-color-4/#resolving-other-colors
+ if (matchKeyword(computedValue, "currentcolor")) {
+ if (property === "color") {
+ usedValue = inheritedValue;
+ } else {
+ const currentColor = getComputedStyleDecl({
+ model,
+ styleSelector,
+ property: "color",
+ });
+ usedValue = currentColor.usedValue;
+ }
+ }
+
+ return { computedValue, usedValue };
};