@wordpress/style-engine 0.10.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +34 -2
- package/build/styles/border/index.js +94 -0
- package/build/styles/border/index.js.map +1 -0
- package/build/styles/index.js +3 -1
- package/build/styles/index.js.map +1 -1
- package/build/styles/spacing/margin.js +4 -1
- package/build/styles/spacing/margin.js.map +1 -1
- package/build/styles/spacing/padding.js +4 -1
- package/build/styles/spacing/padding.js.map +1 -1
- package/build/styles/utils.js +26 -10
- package/build/styles/utils.js.map +1 -1
- package/build-module/styles/border/index.js +85 -0
- package/build-module/styles/border/index.js.map +1 -0
- package/build-module/styles/index.js +2 -1
- package/build-module/styles/index.js.map +1 -1
- package/build-module/styles/spacing/margin.js +4 -1
- package/build-module/styles/spacing/margin.js.map +1 -1
- package/build-module/styles/spacing/padding.js +4 -1
- package/build-module/styles/spacing/padding.js.map +1 -1
- package/build-module/styles/utils.js +25 -11
- package/build-module/styles/utils.js.map +1 -1
- package/build-types/styles/border/index.d.ts +10 -0
- package/build-types/styles/border/index.d.ts.map +1 -0
- package/build-types/styles/index.d.ts +3 -7
- package/build-types/styles/index.d.ts.map +1 -1
- package/build-types/styles/spacing/margin.d.ts.map +1 -1
- package/build-types/styles/spacing/padding.d.ts.map +1 -1
- package/build-types/styles/utils.d.ts +16 -7
- package/build-types/styles/utils.d.ts.map +1 -1
- package/build-types/types.d.ts +26 -1
- package/build-types/types.d.ts.map +1 -1
- package/class-wp-style-engine-css-declarations.php +143 -0
- package/class-wp-style-engine-css-rule.php +115 -0
- package/class-wp-style-engine-css-rules-store.php +94 -0
- package/class-wp-style-engine-processor.php +93 -0
- package/class-wp-style-engine.php +109 -99
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +154 -0
- package/phpunit/class-wp-style-engine-css-rule-test.php +96 -0
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +115 -0
- package/phpunit/class-wp-style-engine-processor-test.php +127 -0
- package/phpunit/class-wp-style-engine-test.php +159 -23
- package/src/styles/border/index.ts +145 -0
- package/src/styles/index.ts +7 -1
- package/src/styles/spacing/margin.ts +4 -6
- package/src/styles/spacing/padding.ts +4 -6
- package/src/styles/utils.ts +36 -12
- package/src/test/index.js +92 -2
- package/src/test/utils.js +12 -0
- package/src/types.ts +33 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["get","
|
|
1
|
+
{"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["get","VARIABLE_REFERENCE_PREFIX","VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE","VARIABLE_PATH_SEPARATOR_TOKEN_STYLE","generateRule","style","options","path","ruleKey","styleValue","selector","key","value","getCSSVarFromStyleValue","generateBoxRules","ruleKeys","individualProperties","boxStyle","rules","push","default","sideRules","reduce","acc","side","individual","replace","upperFirst","startsWith","variable","slice","length","split","join","firstLetter","rest","toUpperCase"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,GAAT,QAAoB,QAApB;AAEA;AACA;AACA;;AAQA,SACCC,yBADD,EAECC,uCAFD,EAGCC,mCAHD,QAIO,aAJP;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,YAAT,CACNC,KADM,EAENC,OAFM,EAGNC,IAHM,EAINC,OAJM,EAKL;AACD,QAAMC,UAA8B,GAAGT,GAAG,CAAEK,KAAF,EAASE,IAAT,CAA1C;AAEA,SAAOE,UAAU,GACd,CACA;AACCC,IAAAA,QAAQ,EAAEJ,OAAF,aAAEA,OAAF,uBAAEA,OAAO,CAAEI,QADpB;AAECC,IAAAA,GAAG,EAAEH,OAFN;AAGCI,IAAAA,KAAK,EAAEC,uBAAuB,CAAEJ,UAAF;AAH/B,GADA,CADc,GAQd,EARH;AASA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IAAN,CAAY,GAAGE,SAAf;AACA;;AAED,SAAOH,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASL,uBAAT,CAAkCJ,UAAlC,EAA+D;AACrE,MACC,OAAOA,UAAP,KAAsB,QAAtB,IACAA,UAAU,CAACmB,UAAX,CAAuB3B,yBAAvB,CAFD,EAGE;AACD,UAAM4B,QAAQ,GAAGpB,UAAU,CACzBqB,KADe,CACR7B,yBAAyB,CAAC8B,MADlB,EAEfC,KAFe,CAER9B,uCAFQ,EAGf+B,IAHe,CAGT9B,mCAHS,CAAjB;AAIA,WAAQ,aAAa0B,QAAU,GAA/B;AACA;;AACD,SAAOpB,UAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASkB,UAAT,OAAwD;AAAA,MAAnC,CAAEO,WAAF,EAAe,GAAGC,IAAlB,CAAmC;AAC9D,SAAOD,WAAW,CAACE,WAAZ,KAA4BD,IAAI,CAACF,IAAL,CAAW,EAAX,CAAnC;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { get } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tCssRulesKeys,\n\tGeneratedCSSRule,\n\tStyle,\n\tBox,\n\tStyleOptions,\n} from '../types';\nimport {\n\tVARIABLE_REFERENCE_PREFIX,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_STYLE,\n} from './constants';\n\n/**\n * Returns a JSON representation of the generated CSS rules.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKey A CSS property key.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateRule(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKey: string\n) {\n\tconst styleValue: string | undefined = get( style, path );\n\n\treturn styleValue\n\t\t? [\n\t\t\t\t{\n\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\tkey: ruleKey,\n\t\t\t\t\tvalue: getCSSVarFromStyleValue( styleValue ),\n\t\t\t\t},\n\t\t ]\n\t\t: [];\n}\n\n/**\n * Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKeys An array of CSS property keys and patterns.\n * @param individualProperties The \"sides\" or individual properties for which to generate rules.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateBoxRules(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKeys: CssRulesKeys,\n\tindividualProperties: string[] = [ 'top', 'right', 'bottom', 'left' ]\n): GeneratedCSSRule[] {\n\tconst boxStyle: Box | string | undefined = get( style, path );\n\tif ( ! boxStyle ) {\n\t\treturn [];\n\t}\n\n\tconst rules: GeneratedCSSRule[] = [];\n\tif ( typeof boxStyle === 'string' ) {\n\t\trules.push( {\n\t\t\tselector: options?.selector,\n\t\t\tkey: ruleKeys.default,\n\t\t\tvalue: boxStyle,\n\t\t} );\n\t} else {\n\t\tconst sideRules = individualProperties.reduce(\n\t\t\t( acc: GeneratedCSSRule[], side: string ) => {\n\t\t\t\tconst value: string | undefined = getCSSVarFromStyleValue(\n\t\t\t\t\tget( boxStyle, [ side ] )\n\t\t\t\t);\n\t\t\t\tif ( value ) {\n\t\t\t\t\tacc.push( {\n\t\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\t\tkey: ruleKeys?.individual.replace(\n\t\t\t\t\t\t\t'%s',\n\t\t\t\t\t\t\tupperFirst( side )\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t},\n\t\t\t[]\n\t\t);\n\t\trules.push( ...sideRules );\n\t}\n\n\treturn rules;\n}\n\n/**\n * Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.\n *\n * @param styleValue A raw style value.\n *\n * @return string A CSS var value.\n */\nexport function getCSSVarFromStyleValue( styleValue: string ): string {\n\tif (\n\t\ttypeof styleValue === 'string' &&\n\t\tstyleValue.startsWith( VARIABLE_REFERENCE_PREFIX )\n\t) {\n\t\tconst variable = styleValue\n\t\t\t.slice( VARIABLE_REFERENCE_PREFIX.length )\n\t\t\t.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )\n\t\t\t.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );\n\t\treturn `var(--wp--${ variable })`;\n\t}\n\treturn styleValue;\n}\n\n/**\n * Capitalizes the first letter in a string.\n *\n * @param {string} str The string whose first letter the function will capitalize.\n *\n * @return string A CSS var value.\n */\nexport function upperFirst( [ firstLetter, ...rest ]: string ) {\n\treturn firstLetter.toUpperCase() + rest.join( '' );\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { GeneratedCSSRule, Style, StyleDefinition, StyleOptions } from '../../types';
|
|
5
|
+
declare const _default: (StyleDefinition | {
|
|
6
|
+
name: string;
|
|
7
|
+
generate: (style: Style, options: StyleOptions) => GeneratedCSSRule[];
|
|
8
|
+
})[];
|
|
9
|
+
export default _default;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/styles/border/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAGX,gBAAgB,EAChB,KAAK,EACL,eAAe,EACf,YAAY,EACZ,MAAM,aAAa,CAAC;;;;;AA+HrB,wBAOE"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
export declare const styleDefinitions: {
|
|
1
|
+
export declare const styleDefinitions: (import("../types").StyleDefinition | {
|
|
2
2
|
name: string;
|
|
3
|
-
generate: (style: import("../types").Style, options: import("../types").StyleOptions) =>
|
|
4
|
-
|
|
5
|
-
key: string;
|
|
6
|
-
value: string;
|
|
7
|
-
}[];
|
|
8
|
-
}[];
|
|
3
|
+
generate: (style: import("../types").Style, options: import("../types").StyleOptions) => import("../types").GeneratedCSSRule[];
|
|
4
|
+
})[];
|
|
9
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/styles/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/styles/index.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,gBAAgB;;;IAK5B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"margin.d.ts","sourceRoot":"","sources":["../../../src/styles/spacing/margin.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGvD,QAAA,MAAM,MAAM;;sBAEQ,KAAK,WAAW,YAAY;
|
|
1
|
+
{"version":3,"file":"margin.d.ts","sourceRoot":"","sources":["../../../src/styles/spacing/margin.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGvD,QAAA,MAAM,MAAM;;sBAEQ,KAAK,WAAW,YAAY;CAM/C,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"padding.d.ts","sourceRoot":"","sources":["../../../src/styles/spacing/padding.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGvD,QAAA,MAAM,OAAO;;sBAEO,KAAK,WAAW,YAAY;
|
|
1
|
+
{"version":3,"file":"padding.d.ts","sourceRoot":"","sources":["../../../src/styles/spacing/padding.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGvD,QAAA,MAAM,OAAO;;sBAEO,KAAK,WAAW,YAAY;CAM/C,CAAC;AAEF,eAAe,OAAO,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import type { GeneratedCSSRule, Style, StyleOptions } from '../types';
|
|
4
|
+
import type { CssRulesKeys, GeneratedCSSRule, Style, StyleOptions } from '../types';
|
|
5
5
|
/**
|
|
6
6
|
* Returns a JSON representation of the generated CSS rules.
|
|
7
7
|
*
|
|
@@ -20,14 +20,15 @@ export declare function generateRule(style: Style, options: StyleOptions, path:
|
|
|
20
20
|
/**
|
|
21
21
|
* Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.
|
|
22
22
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @param
|
|
25
|
-
* @param
|
|
26
|
-
* @param
|
|
23
|
+
* @param style Style object.
|
|
24
|
+
* @param options Options object with settings to adjust how the styles are generated.
|
|
25
|
+
* @param path An array of strings representing the path to the style value in the style object.
|
|
26
|
+
* @param ruleKeys An array of CSS property keys and patterns.
|
|
27
|
+
* @param individualProperties The "sides" or individual properties for which to generate rules.
|
|
27
28
|
*
|
|
28
|
-
* @return GeneratedCSSRule[]
|
|
29
|
+
* @return GeneratedCSSRule[] CSS rules.
|
|
29
30
|
*/
|
|
30
|
-
export declare function generateBoxRules(style: Style, options: StyleOptions, path: string[],
|
|
31
|
+
export declare function generateBoxRules(style: Style, options: StyleOptions, path: string[], ruleKeys: CssRulesKeys, individualProperties?: string[]): GeneratedCSSRule[];
|
|
31
32
|
/**
|
|
32
33
|
* Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.
|
|
33
34
|
*
|
|
@@ -36,4 +37,12 @@ export declare function generateBoxRules(style: Style, options: StyleOptions, pa
|
|
|
36
37
|
* @return string A CSS var value.
|
|
37
38
|
*/
|
|
38
39
|
export declare function getCSSVarFromStyleValue(styleValue: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Capitalizes the first letter in a string.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} str The string whose first letter the function will capitalize.
|
|
44
|
+
*
|
|
45
|
+
* @return string A CSS var value.
|
|
46
|
+
*/
|
|
47
|
+
export declare function upperFirst([firstLetter, ...rest]: string): string;
|
|
39
48
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/styles/utils.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/styles/utils.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EACX,YAAY,EACZ,gBAAgB,EAChB,KAAK,EAEL,YAAY,EACZ,MAAM,UAAU,CAAC;AAOlB;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC3B,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,YAAY,EACrB,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,EAAE,MAAM;;;;IAaf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,YAAY,EACrB,IAAI,EAAE,MAAM,EAAE,EACd,QAAQ,EAAE,YAAY,EACtB,oBAAoB,GAAE,MAAM,EAAyC,GACnE,gBAAgB,EAAE,CAqCpB;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAE,UAAU,EAAE,MAAM,GAAI,MAAM,CAYpE;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAE,CAAE,WAAW,EAAE,GAAG,IAAI,CAAE,EAAE,MAAM,UAE3D"}
|
package/build-types/types.d.ts
CHANGED
|
@@ -9,7 +9,28 @@ export declare type Box<T extends BoxVariants = undefined> = {
|
|
|
9
9
|
bottom?: CSSProperties[T extends undefined ? 'bottom' : `${T}Bottom`];
|
|
10
10
|
left?: CSSProperties[T extends undefined ? 'left' : `${T}Left`];
|
|
11
11
|
};
|
|
12
|
+
export declare type BorderIndividualProperty = 'top' | 'right' | 'bottom' | 'left';
|
|
13
|
+
export declare type BorderIndividualStyles<T extends BorderIndividualProperty> = {
|
|
14
|
+
color?: CSSProperties[`border${Capitalize<string & T>}Color`];
|
|
15
|
+
style?: CSSProperties[`border${Capitalize<string & T>}Style`];
|
|
16
|
+
width?: CSSProperties[`border${Capitalize<string & T>}Width`];
|
|
17
|
+
};
|
|
12
18
|
export interface Style {
|
|
19
|
+
border?: {
|
|
20
|
+
color?: CSSProperties['borderColor'];
|
|
21
|
+
radius?: CSSProperties['borderRadius'] | {
|
|
22
|
+
topLeft?: CSSProperties['borderTopLeftRadius'];
|
|
23
|
+
topRight?: CSSProperties['borderTopRightRadius'];
|
|
24
|
+
bottomLeft?: CSSProperties['borderBottomLeftRadius'];
|
|
25
|
+
bottomRight?: CSSProperties['borderBottomLeftRadius'];
|
|
26
|
+
};
|
|
27
|
+
style?: CSSProperties['borderStyle'];
|
|
28
|
+
width?: CSSProperties['borderWidth'];
|
|
29
|
+
top?: BorderIndividualStyles<'top'>;
|
|
30
|
+
right?: BorderIndividualStyles<'right'>;
|
|
31
|
+
bottom?: BorderIndividualStyles<'bottom'>;
|
|
32
|
+
left?: BorderIndividualStyles<'left'>;
|
|
33
|
+
};
|
|
13
34
|
spacing?: {
|
|
14
35
|
margin?: CSSProperties['margin'] | Box<'margin'>;
|
|
15
36
|
padding?: CSSProperties['padding'] | Box<'padding'>;
|
|
@@ -37,6 +58,10 @@ export interface Style {
|
|
|
37
58
|
};
|
|
38
59
|
};
|
|
39
60
|
}
|
|
61
|
+
export declare type CssRulesKeys = {
|
|
62
|
+
default: string;
|
|
63
|
+
individual: string;
|
|
64
|
+
};
|
|
40
65
|
export declare type StyleOptions = {
|
|
41
66
|
/**
|
|
42
67
|
* CSS selector for the generated style.
|
|
@@ -54,7 +79,7 @@ export declare type GeneratedCSSRule = {
|
|
|
54
79
|
};
|
|
55
80
|
export interface StyleDefinition {
|
|
56
81
|
name: string;
|
|
57
|
-
generate?: (style: Style, options: StyleOptions) => GeneratedCSSRule[];
|
|
82
|
+
generate?: (style: Style, options: StyleOptions, path?: string[], ruleKey?: string) => GeneratedCSSRule[];
|
|
58
83
|
getClassNames?: (style: Style) => string[];
|
|
59
84
|
}
|
|
60
85
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,aAAK,WAAW,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAC;AACpD,oBAAY,GAAG,CAAE,CAAC,SAAS,WAAW,GAAG,SAAS,IAAK;IACtD,GAAG,CAAC,EAAE,aAAa,CAAE,CAAC,SAAS,SAAS,GAAG,KAAK,GAAG,GAAI,CAAE,KAAK,CAAE,CAAC;IACjE,KAAK,CAAC,EAAE,aAAa,CAAE,CAAC,SAAS,SAAS,GAAG,OAAO,GAAG,GAAI,CAAE,OAAO,CAAE,CAAC;IACvE,MAAM,CAAC,EAAE,aAAa,CAAE,CAAC,SAAS,SAAS,GAAG,QAAQ,GAAG,GAAI,CAAE,QAAQ,CAAE,CAAC;IAC1E,IAAI,CAAC,EAAE,aAAa,CAAE,CAAC,SAAS,SAAS,GAAG,MAAM,GAAG,GAAI,CAAE,MAAM,CAAE,CAAC;CACpE,CAAC;AAEF,MAAM,WAAW,KAAK;IACrB,OAAO,CAAC,EAAE;QACT,MAAM,CAAC,EAAE,aAAa,CAAE,QAAQ,CAAE,GAAG,GAAG,CAAE,QAAQ,CAAE,CAAC;QACrD,OAAO,CAAC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@package Gutenberg
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
if ( class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Holds, sanitizes, processes and prints CSS declarations for the style engine.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_CSS_Declarations {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An array of CSS declarations (property => value pairs).
|
|
23
|
+
*
|
|
24
|
+
* @var array
|
|
25
|
+
*/
|
|
26
|
+
protected $declarations = array();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constructor for this object.
|
|
30
|
+
*
|
|
31
|
+
* If a `$declarations` array is passed, it will be used to populate
|
|
32
|
+
* the initial $declarations prop of the object by calling add_declarations().
|
|
33
|
+
*
|
|
34
|
+
* @param array $declarations An array of declarations (property => value pairs).
|
|
35
|
+
*/
|
|
36
|
+
public function __construct( $declarations = array() ) {
|
|
37
|
+
if ( empty( $declarations ) ) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
$this->add_declarations( $declarations );
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Add a single declaration.
|
|
45
|
+
*
|
|
46
|
+
* @param string $property The CSS property.
|
|
47
|
+
* @param string $value The CSS value.
|
|
48
|
+
*
|
|
49
|
+
* @return void
|
|
50
|
+
*/
|
|
51
|
+
public function add_declaration( $property, $value ) {
|
|
52
|
+
|
|
53
|
+
// Sanitize the property.
|
|
54
|
+
$property = $this->sanitize_property( $property );
|
|
55
|
+
// Bail early if the property is empty.
|
|
56
|
+
if ( empty( $property ) ) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Trim the value. If empty, bail early.
|
|
61
|
+
$value = trim( $value );
|
|
62
|
+
if ( '' === $value ) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Add the declaration property/value pair.
|
|
67
|
+
$this->declarations[ $property ] = $value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Remove a single declaration.
|
|
72
|
+
*
|
|
73
|
+
* @param string $property The CSS property.
|
|
74
|
+
*
|
|
75
|
+
* @return void
|
|
76
|
+
*/
|
|
77
|
+
public function remove_declaration( $property ) {
|
|
78
|
+
unset( $this->declarations[ $property ] );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Add multiple declarations.
|
|
83
|
+
*
|
|
84
|
+
* @param array $declarations An array of declarations.
|
|
85
|
+
*
|
|
86
|
+
* @return void
|
|
87
|
+
*/
|
|
88
|
+
public function add_declarations( $declarations ) {
|
|
89
|
+
foreach ( $declarations as $property => $value ) {
|
|
90
|
+
$this->add_declaration( $property, $value );
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Remove multiple declarations.
|
|
96
|
+
*
|
|
97
|
+
* @param array $declarations An array of properties.
|
|
98
|
+
*
|
|
99
|
+
* @return void
|
|
100
|
+
*/
|
|
101
|
+
public function remove_declarations( $declarations = array() ) {
|
|
102
|
+
foreach ( $declarations as $property ) {
|
|
103
|
+
$this->remove_declaration( $property );
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get the declarations array.
|
|
109
|
+
*
|
|
110
|
+
* @return array
|
|
111
|
+
*/
|
|
112
|
+
public function get_declarations() {
|
|
113
|
+
return $this->declarations;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Filters and compiles the CSS declarations.
|
|
118
|
+
*
|
|
119
|
+
* @return string The CSS declarations.
|
|
120
|
+
*/
|
|
121
|
+
public function get_declarations_string() {
|
|
122
|
+
$declarations_array = $this->get_declarations();
|
|
123
|
+
$declarations_output = '';
|
|
124
|
+
foreach ( $declarations_array as $property => $value ) {
|
|
125
|
+
$filtered_declaration = esc_html( safecss_filter_attr( "{$property}: {$value}" ) );
|
|
126
|
+
if ( $filtered_declaration ) {
|
|
127
|
+
$declarations_output .= $filtered_declaration . '; ';
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return rtrim( $declarations_output );
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sanitize property names.
|
|
135
|
+
*
|
|
136
|
+
* @param string $property The CSS property.
|
|
137
|
+
*
|
|
138
|
+
* @return string The sanitized property name.
|
|
139
|
+
*/
|
|
140
|
+
protected function sanitize_property( $property ) {
|
|
141
|
+
return sanitize_key( $property );
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_CSS_Rule
|
|
4
|
+
*
|
|
5
|
+
* An object for CSS rules.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
if ( class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Holds, sanitizes, processes and prints CSS declarations for the style engine.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_CSS_Rule {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The selector.
|
|
23
|
+
*
|
|
24
|
+
* @var string
|
|
25
|
+
*/
|
|
26
|
+
protected $selector;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The selector declarations.
|
|
30
|
+
*
|
|
31
|
+
* Contains a WP_Style_Engine_CSS_Declarations object.
|
|
32
|
+
*
|
|
33
|
+
* @var WP_Style_Engine_CSS_Declarations
|
|
34
|
+
*/
|
|
35
|
+
protected $declarations;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Constructor
|
|
39
|
+
*
|
|
40
|
+
* @param string $selector The CSS selector.
|
|
41
|
+
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
|
|
42
|
+
* or a WP_Style_Engine_CSS_Declarations object.
|
|
43
|
+
*/
|
|
44
|
+
public function __construct( $selector = '', $declarations = array() ) {
|
|
45
|
+
$this->set_selector( $selector );
|
|
46
|
+
$this->add_declarations( $declarations );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set the selector.
|
|
51
|
+
*
|
|
52
|
+
* @param string $selector The CSS selector.
|
|
53
|
+
*
|
|
54
|
+
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
|
|
55
|
+
*/
|
|
56
|
+
public function set_selector( $selector ) {
|
|
57
|
+
if ( empty( $selector ) ) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
$this->selector = $selector;
|
|
61
|
+
|
|
62
|
+
return $this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Set the declarations.
|
|
67
|
+
*
|
|
68
|
+
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
|
|
69
|
+
* or a WP_Style_Engine_CSS_Declarations object.
|
|
70
|
+
*
|
|
71
|
+
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
|
|
72
|
+
*/
|
|
73
|
+
public function add_declarations( $declarations ) {
|
|
74
|
+
$is_declarations_object = ! is_array( $declarations );
|
|
75
|
+
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
|
|
76
|
+
|
|
77
|
+
if ( null === $this->declarations && $is_declarations_object ) {
|
|
78
|
+
$this->declarations = $declarations;
|
|
79
|
+
return $this;
|
|
80
|
+
}
|
|
81
|
+
if ( null === $this->declarations ) {
|
|
82
|
+
$this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
|
|
83
|
+
}
|
|
84
|
+
$this->declarations->add_declarations( $declarations_array );
|
|
85
|
+
|
|
86
|
+
return $this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get the declarations object.
|
|
91
|
+
*
|
|
92
|
+
* @return WP_Style_Engine_CSS_Declarations
|
|
93
|
+
*/
|
|
94
|
+
public function get_declarations() {
|
|
95
|
+
return $this->declarations;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the full selector.
|
|
100
|
+
*
|
|
101
|
+
* @return string
|
|
102
|
+
*/
|
|
103
|
+
public function get_selector() {
|
|
104
|
+
return $this->selector;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get the CSS.
|
|
109
|
+
*
|
|
110
|
+
* @return string
|
|
111
|
+
*/
|
|
112
|
+
public function get_css() {
|
|
113
|
+
return $this->get_selector() . ' {' . $this->declarations->get_declarations_string() . '}';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_CSS_Rules_Store
|
|
4
|
+
*
|
|
5
|
+
* A store for WP_Style_Engine_CSS_Rule objects.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Holds, sanitizes, processes and prints CSS declarations for the style engine.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_CSS_Rules_Store {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An array of named WP_Style_Engine_CSS_Rules_Store objects.
|
|
23
|
+
*
|
|
24
|
+
* @static
|
|
25
|
+
*
|
|
26
|
+
* @var WP_Style_Engine_CSS_Rules_Store[]
|
|
27
|
+
*/
|
|
28
|
+
protected static $stores = array();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An array of CSS Rules objects assigned to the store.
|
|
32
|
+
*
|
|
33
|
+
* @var WP_Style_Engine_CSS_Rule[]
|
|
34
|
+
*/
|
|
35
|
+
protected $rules = array();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get an instance of the store.
|
|
39
|
+
*
|
|
40
|
+
* @param string $store_name The name of the store.
|
|
41
|
+
*
|
|
42
|
+
* @return WP_Style_Engine_CSS_Rules_Store
|
|
43
|
+
*/
|
|
44
|
+
public static function get_store( $store_name = 'default' ) {
|
|
45
|
+
if ( ! isset( static::$stores[ $store_name ] ) ) {
|
|
46
|
+
static::$stores[ $store_name ] = new static();
|
|
47
|
+
}
|
|
48
|
+
return static::$stores[ $store_name ];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get an array of all rules.
|
|
52
|
+
*
|
|
53
|
+
* @return WP_Style_Engine_CSS_Rule[]
|
|
54
|
+
*/
|
|
55
|
+
public function get_all_rules() {
|
|
56
|
+
return $this->rules;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get a WP_Style_Engine_CSS_Rule object by its selector.
|
|
61
|
+
* If the rule does not exist, it will be created.
|
|
62
|
+
*
|
|
63
|
+
* @param string $selector The CSS selector.
|
|
64
|
+
*
|
|
65
|
+
* @return WP_Style_Engine_CSS_Rule|null Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
|
|
66
|
+
*/
|
|
67
|
+
public function add_rule( $selector ) {
|
|
68
|
+
|
|
69
|
+
$selector = trim( $selector );
|
|
70
|
+
|
|
71
|
+
// Bail early if there is no selector.
|
|
72
|
+
if ( empty( $selector ) ) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Create the rule if it doesn't exist.
|
|
77
|
+
if ( empty( $this->rules[ $selector ] ) ) {
|
|
78
|
+
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return $this->rules[ $selector ];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Remove a selector from the store.
|
|
86
|
+
*
|
|
87
|
+
* @param string $selector The CSS selector.
|
|
88
|
+
*
|
|
89
|
+
* @return void
|
|
90
|
+
*/
|
|
91
|
+
public function remove_rule( $selector ) {
|
|
92
|
+
unset( $this->rules[ $selector ] );
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* WP_Style_Engine_Processor
|
|
4
|
+
*
|
|
5
|
+
* Compiles styles from a store of CSS rules.
|
|
6
|
+
*
|
|
7
|
+
* @package Gutenberg
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
if ( class_exists( 'WP_Style_Engine_Processor' ) ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Compiles styles from a store of CSS rules.
|
|
16
|
+
*
|
|
17
|
+
* @access private
|
|
18
|
+
*/
|
|
19
|
+
class WP_Style_Engine_Processor {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Style-Engine Store object.
|
|
23
|
+
*
|
|
24
|
+
* @var WP_Style_Engine_CSS_Rules_Store
|
|
25
|
+
*/
|
|
26
|
+
protected $store;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constructor.
|
|
30
|
+
*
|
|
31
|
+
* @param WP_Style_Engine_CSS_Rules_Store $store The store to render.
|
|
32
|
+
*/
|
|
33
|
+
public function __construct( WP_Style_Engine_CSS_Rules_Store $store ) {
|
|
34
|
+
$this->store = $store;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get the CSS rules as a string.
|
|
39
|
+
*
|
|
40
|
+
* @return string The computed CSS.
|
|
41
|
+
*/
|
|
42
|
+
public function get_css() {
|
|
43
|
+
// Combine CSS selectors that have identical declarations.
|
|
44
|
+
$this->combine_rules_selectors();
|
|
45
|
+
|
|
46
|
+
// Build the CSS.
|
|
47
|
+
$css = '';
|
|
48
|
+
$rules = $this->store->get_all_rules();
|
|
49
|
+
foreach ( $rules as $rule ) {
|
|
50
|
+
$css .= $rule->get_css();
|
|
51
|
+
}
|
|
52
|
+
return $css;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Combines selectors from the rules store when they have the same styles.
|
|
57
|
+
*
|
|
58
|
+
* @return void
|
|
59
|
+
*/
|
|
60
|
+
private function combine_rules_selectors() {
|
|
61
|
+
$rules = $this->store->get_all_rules();
|
|
62
|
+
|
|
63
|
+
// Build an array of selectors along with the JSON-ified styles to make comparisons easier.
|
|
64
|
+
$selectors_json = array();
|
|
65
|
+
foreach ( $rules as $selector => $rule ) {
|
|
66
|
+
$declarations = $rule->get_declarations()->get_declarations();
|
|
67
|
+
ksort( $declarations );
|
|
68
|
+
$selectors_json[ $selector ] = json_encode( $declarations );
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Combine selectors that have the same styles.
|
|
72
|
+
foreach ( $selectors_json as $selector => $json ) {
|
|
73
|
+
// Get selectors that use the same styles.
|
|
74
|
+
$duplicates = array_keys( $selectors_json, $json, true );
|
|
75
|
+
// Skip if there are no duplicates.
|
|
76
|
+
if ( 1 >= count( $duplicates ) ) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
foreach ( $duplicates as $key ) {
|
|
80
|
+
// Unset the duplicates from the $selectors_json array to avoid looping through them as well.
|
|
81
|
+
unset( $selectors_json[ $key ] );
|
|
82
|
+
// Remove the rules from the store.
|
|
83
|
+
$this->store->remove_rule( $key );
|
|
84
|
+
}
|
|
85
|
+
// Create a new rule with the combined selectors.
|
|
86
|
+
$new_rule = $this->store->add_rule( implode( ',', $duplicates ) );
|
|
87
|
+
// Set the declarations. The extra check is in place because `add_rule` in the store can return `null`.
|
|
88
|
+
if ( $new_rule ) {
|
|
89
|
+
$new_rule->add_declarations( $rules[ $selector ]->get_declarations() );
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|