mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] styles: fix selector generation
This commit fixes the css selector generation for nested rules
Before this fix, a selector like:
.parent {
.child-a, .child-b {
color: red;
}
}
was generated as:
.parent .child-a, .child-b {
color: red;
}
Now it generates:
.parent .child-a, .parent .child-b {
color: red;
}
This commit is contained in:
+15
-4
@@ -7,14 +7,25 @@
|
|||||||
|
|
||||||
export const STYLESHEETS: { [id: string]: HTMLStyleElement } = {};
|
export const STYLESHEETS: { [id: string]: HTMLStyleElement } = {};
|
||||||
|
|
||||||
function processSheet(str: string): string {
|
export function processSheet(str: string): string {
|
||||||
const tokens = str.split(/(\{|\}|;)/).map(s => s.trim());
|
const tokens = str.split(/(\{|\}|;)/).map(s => s.trim());
|
||||||
const selectorStack: string[] = [];
|
const selectorStack: string[][] = [];
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
let rules: string[] = [];
|
let rules: string[] = [];
|
||||||
|
function generateSelector(stackIndex: number, parentSelector?: string) {
|
||||||
|
const parts: string[] = [];
|
||||||
|
for (const selector of selectorStack[stackIndex]) {
|
||||||
|
let part = parentSelector && parentSelector + ' ' + selector || selector;
|
||||||
|
if (stackIndex < selectorStack.length - 1) {
|
||||||
|
part = generateSelector(stackIndex + 1, part);
|
||||||
|
}
|
||||||
|
parts.push(part);
|
||||||
|
}
|
||||||
|
return parts.join(', ');
|
||||||
|
}
|
||||||
function generateRules() {
|
function generateRules() {
|
||||||
if (rules.length) {
|
if (rules.length) {
|
||||||
parts.push(selectorStack.join(" ") + " {");
|
parts.push(generateSelector(0) + " {");
|
||||||
parts.push(...rules);
|
parts.push(...rules);
|
||||||
parts.push("}");
|
parts.push("}");
|
||||||
rules = [];
|
rules = [];
|
||||||
@@ -28,7 +39,7 @@ function processSheet(str: string): string {
|
|||||||
} else {
|
} else {
|
||||||
if (tokens[0] === "{") {
|
if (tokens[0] === "{") {
|
||||||
generateRules();
|
generateRules();
|
||||||
selectorStack.push(token);
|
selectorStack.push(token.split(/\s*,\s*/));
|
||||||
tokens.shift();
|
tokens.shift();
|
||||||
}
|
}
|
||||||
if (tokens[0] === ";") {
|
if (tokens[0] === ";") {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Component, Env } from "../../src/component/component";
|
import { Component, Env } from "../../src/component/component";
|
||||||
|
import { processSheet } from "../../src/component/styles";
|
||||||
import { xml, css } from "../../src/tags";
|
import { xml, css } from "../../src/tags";
|
||||||
import { makeTestFixture, makeTestEnv } from "../helpers";
|
import { makeTestFixture, makeTestEnv } from "../helpers";
|
||||||
|
|
||||||
@@ -127,6 +128,18 @@ describe("styles and component", () => {
|
|||||||
}
|
}
|
||||||
.app {
|
.app {
|
||||||
display: block;
|
display: block;
|
||||||
|
}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("properly handle rules with commas", async () => {
|
||||||
|
const sheet = processSheet(`.parent-a, .parent-b {
|
||||||
|
.child-a, .child-b {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
}`);
|
||||||
|
|
||||||
|
expect(sheet).toBe(`.parent-a .child-a, .parent-a .child-b, .parent-b .child-a, .parent-b .child-b {
|
||||||
|
color: red;
|
||||||
}`);
|
}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user