@wordpress/style-engine 0.13.0 → 0.15.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 +4 -0
- package/README.md +206 -42
- package/build/index.js +7 -5
- package/build/index.js.map +1 -1
- package/build/styles/typography/index.js +7 -1
- package/build/styles/typography/index.js.map +1 -1
- package/build/styles/utils.js +5 -5
- package/build/styles/utils.js.map +1 -1
- package/build-module/index.js +6 -4
- package/build-module/index.js.map +1 -1
- package/build-module/styles/typography/index.js +7 -1
- package/build-module/styles/typography/index.js.map +1 -1
- package/build-module/styles/utils.js +5 -5
- package/build-module/styles/utils.js.map +1 -1
- package/build-types/index.d.ts +4 -4
- package/build-types/index.d.ts.map +1 -1
- package/build-types/styles/color/background.d.ts +1 -1
- package/build-types/styles/color/gradient.d.ts +1 -1
- package/build-types/styles/color/index.d.ts +1 -1
- package/build-types/styles/color/text.d.ts +1 -1
- package/build-types/styles/typography/index.d.ts +1 -1
- package/build-types/styles/typography/index.d.ts.map +1 -1
- package/build-types/styles/utils.d.ts +6 -6
- package/build-types/types.d.ts +3 -4
- package/build-types/types.d.ts.map +1 -1
- package/class-wp-style-engine-css-declarations.php +42 -15
- package/class-wp-style-engine-css-rule.php +20 -10
- package/class-wp-style-engine-css-rules-store.php +53 -3
- package/class-wp-style-engine-processor.php +76 -26
- package/class-wp-style-engine.php +260 -140
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +87 -7
- package/phpunit/class-wp-style-engine-css-rule-test.php +38 -7
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +58 -2
- package/phpunit/class-wp-style-engine-processor-test.php +170 -31
- package/phpunit/class-wp-style-engine-test.php +223 -22
- package/src/index.ts +4 -4
- package/src/styles/typography/index.ts +13 -0
- package/src/styles/utils.ts +5 -5
- package/src/test/index.js +24 -10
- package/src/types.ts +3 -4
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
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;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASK,gBAAT,CACNT,KADM,EAENC,OAFM,EAGNC,IAHM,EAINQ,QAJM,EAMe;AAAA,MADrBC,oBACqB,uEADY,CAAE,KAAF,EAAS,OAAT,EAAkB,QAAlB,EAA4B,MAA5B,CACZ;AACrB,QAAMC,QAAkC,GAAGjB,GAAG,CAAEK,KAAF,EAASE,IAAT,CAA9C;;AACA,MAAK,CAAEU,QAAP,EAAkB;AACjB,WAAO,EAAP;AACA;;AAED,QAAMC,KAAyB,GAA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,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
|
|
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;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASK,gBAAT,CACNT,KADM,EAENC,OAFM,EAGNC,IAHM,EAINQ,QAJM,EAMe;AAAA,MADrBC,oBACqB,uEADY,CAAE,KAAF,EAAS,OAAT,EAAkB,QAAlB,EAA4B,MAA5B,CACZ;AACrB,QAAMC,QAAkC,GAAGjB,GAAG,CAAEK,KAAF,EAASE,IAAT,CAA9C;;AACA,MAAK,CAAEU,QAAP,EAAkB;AACjB,WAAO,EAAP;AACA;;AAED,QAAMC,KAAyB,GAAG,EAAlC;;AACA,MAAK,OAAOD,QAAP,KAAoB,QAAzB,EAAoC;AACnCC,IAAAA,KAAK,CAACC,IAAN,CAAY;AACXT,MAAAA,QAAQ,EAAEJ,OAAF,aAAEA,OAAF,uBAAEA,OAAO,CAAEI,QADR;AAEXC,MA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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"]}
|
package/build-types/index.d.ts
CHANGED
|
@@ -5,19 +5,19 @@ import type { Style, StyleOptions, GeneratedCSSRule } from './types';
|
|
|
5
5
|
/**
|
|
6
6
|
* Generates a stylesheet for a given style object and selector.
|
|
7
7
|
*
|
|
8
|
-
* @param style Style object.
|
|
8
|
+
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
|
|
9
9
|
* @param options Options object with settings to adjust how the styles are generated.
|
|
10
10
|
*
|
|
11
11
|
* @return generated stylesheet.
|
|
12
12
|
*/
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function compileCSS(style: Style, options?: StyleOptions): string;
|
|
14
14
|
/**
|
|
15
15
|
* Returns a JSON representation of the generated CSS rules.
|
|
16
16
|
*
|
|
17
|
-
* @param style Style object.
|
|
17
|
+
* @param style Style object, for example, the value of a block's attributes.style object or the top level styles in theme.json
|
|
18
18
|
* @param options Options object with settings to adjust how the styles are generated.
|
|
19
19
|
*
|
|
20
20
|
* @return generated styles.
|
|
21
21
|
*/
|
|
22
|
-
export declare function getCSSRules(style: Style, options
|
|
22
|
+
export declare function getCSSRules(style: Style, options?: StyleOptions): GeneratedCSSRule[];
|
|
23
23
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EACX,KAAK,EACL,YAAY,EACZ,gBAAgB,EAEhB,MAAM,SAAS,CAAC;AAGjB;;;;;;;GAOG;AACH,wBAAgB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EACX,KAAK,EACL,YAAY,EACZ,gBAAgB,EAEhB,MAAM,SAAS,CAAC;AAGjB;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAE,KAAK,EAAE,KAAK,EAAE,OAAO,GAAE,YAAiB,GAAI,MAAM,CA6B7E;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,KAAK,EACZ,OAAO,GAAE,YAAiB,GACxB,gBAAgB,EAAE,CASpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/styles/typography/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/styles/typography/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;;;;;;;;;AAmGvD,wBASE"}
|
|
@@ -13,18 +13,18 @@ import type { CssRulesKeys, GeneratedCSSRule, Style, StyleOptions } from '../typ
|
|
|
13
13
|
* @return GeneratedCSSRule[] CSS rules.
|
|
14
14
|
*/
|
|
15
15
|
export declare function generateRule(style: Style, options: StyleOptions, path: string[], ruleKey: string): {
|
|
16
|
-
selector: string;
|
|
16
|
+
selector: string | undefined;
|
|
17
17
|
key: string;
|
|
18
18
|
value: string;
|
|
19
19
|
}[];
|
|
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
|
|
27
|
-
* @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.
|
|
28
28
|
*
|
|
29
29
|
* @return GeneratedCSSRule[] CSS rules.
|
|
30
30
|
*/
|
package/build-types/types.d.ts
CHANGED
|
@@ -66,10 +66,10 @@ export declare type StyleOptions = {
|
|
|
66
66
|
/**
|
|
67
67
|
* CSS selector for the generated style.
|
|
68
68
|
*/
|
|
69
|
-
selector
|
|
69
|
+
selector?: string;
|
|
70
70
|
};
|
|
71
71
|
export declare type GeneratedCSSRule = {
|
|
72
|
-
selector
|
|
72
|
+
selector?: string;
|
|
73
73
|
value: string;
|
|
74
74
|
/**
|
|
75
75
|
* The CSS key in JS style attribute format, compatible with React.
|
|
@@ -79,8 +79,7 @@ export declare type GeneratedCSSRule = {
|
|
|
79
79
|
};
|
|
80
80
|
export interface StyleDefinition {
|
|
81
81
|
name: string;
|
|
82
|
-
generate?: (style: Style, options: StyleOptions, path?: string[], ruleKey?: string) => GeneratedCSSRule[];
|
|
83
|
-
getClassNames?: (style: Style) => string[];
|
|
82
|
+
generate?: (style: Style, options: StyleOptions | {}, path?: string[], ruleKey?: string) => GeneratedCSSRule[];
|
|
84
83
|
}
|
|
85
84
|
export {};
|
|
86
85
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -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,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AY,CAAE,CAAC;QAC3C,cAAc,CAAC,EAAE,aAAa,CAAE,gBAAgB,CAAE,CAAC;QACnD,aAAa,CAAC,EAAE,aAAa,CAAE,eAAe,CAAE,CAAC;KACjD,CAAC;IACF,KAAK,CAAC,EAAE;QACP,IAAI,CAAC,EAAE,aAAa,CAAE,OAAO,CAAE,CAAC;QAChC,UAAU,CAAC,EAAE,aAAa,CAAE,iBAAiB,CAAE,CAAC;QAChD,QAAQ,CAAC,EAAE,aAAa,CAAE,YAAY,CAAE,CAAC;KACzC,CAAC;IACF,QAAQ,CAAC,EAAE;QACV,IAAI,CAAC,EAAE;YACN,KAAK,CAAC,EAAE;gBACP,IAAI,CAAC,EAAE,aAAa,CAAE,OAAO,CAAE,CAAC;aAChC,CAAC;SACF,CAAC;KACF,CAAC;CACF;AAED,oBAAY,YAAY,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnE,oBAAY,YAAY,GAAG;IAC1B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,oBAAY,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,CACV,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,YAAY,GAAG,EAAE,EAC1B,IAAI,CAAC,EAAE,MAAM,EAAE,EACf,OAAO,CAAC,EAAE,MAAM,KACZ,gBAAgB,EAAE,CAAC;CACxB"}
|
|
@@ -34,9 +34,6 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
34
34
|
* @param array $declarations An array of declarations (property => value pairs).
|
|
35
35
|
*/
|
|
36
36
|
public function __construct( $declarations = array() ) {
|
|
37
|
-
if ( empty( $declarations ) ) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
37
|
$this->add_declarations( $declarations );
|
|
41
38
|
}
|
|
42
39
|
|
|
@@ -46,7 +43,7 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
46
43
|
* @param string $property The CSS property.
|
|
47
44
|
* @param string $value The CSS value.
|
|
48
45
|
*
|
|
49
|
-
* @return
|
|
46
|
+
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
|
|
50
47
|
*/
|
|
51
48
|
public function add_declaration( $property, $value ) {
|
|
52
49
|
|
|
@@ -54,17 +51,19 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
54
51
|
$property = $this->sanitize_property( $property );
|
|
55
52
|
// Bail early if the property is empty.
|
|
56
53
|
if ( empty( $property ) ) {
|
|
57
|
-
return;
|
|
54
|
+
return $this;
|
|
58
55
|
}
|
|
59
56
|
|
|
60
57
|
// Trim the value. If empty, bail early.
|
|
61
58
|
$value = trim( $value );
|
|
62
59
|
if ( '' === $value ) {
|
|
63
|
-
return;
|
|
60
|
+
return $this;
|
|
64
61
|
}
|
|
65
62
|
|
|
66
63
|
// Add the declaration property/value pair.
|
|
67
64
|
$this->declarations[ $property ] = $value;
|
|
65
|
+
|
|
66
|
+
return $this;
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
/**
|
|
@@ -72,10 +71,11 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
72
71
|
*
|
|
73
72
|
* @param string $property The CSS property.
|
|
74
73
|
*
|
|
75
|
-
* @return
|
|
74
|
+
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
|
|
76
75
|
*/
|
|
77
76
|
public function remove_declaration( $property ) {
|
|
78
77
|
unset( $this->declarations[ $property ] );
|
|
78
|
+
return $this;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
@@ -83,25 +83,27 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
83
83
|
*
|
|
84
84
|
* @param array $declarations An array of declarations.
|
|
85
85
|
*
|
|
86
|
-
* @return
|
|
86
|
+
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
|
|
87
87
|
*/
|
|
88
88
|
public function add_declarations( $declarations ) {
|
|
89
89
|
foreach ( $declarations as $property => $value ) {
|
|
90
90
|
$this->add_declaration( $property, $value );
|
|
91
91
|
}
|
|
92
|
+
return $this;
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
/**
|
|
95
96
|
* Remove multiple declarations.
|
|
96
97
|
*
|
|
97
|
-
* @param array $
|
|
98
|
+
* @param array $properties An array of properties.
|
|
98
99
|
*
|
|
99
|
-
* @return
|
|
100
|
+
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
|
|
100
101
|
*/
|
|
101
|
-
public function remove_declarations( $
|
|
102
|
-
foreach ( $
|
|
102
|
+
public function remove_declarations( $properties = array() ) {
|
|
103
|
+
foreach ( $properties as $property ) {
|
|
103
104
|
$this->remove_declaration( $property );
|
|
104
105
|
}
|
|
106
|
+
return $this;
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
/**
|
|
@@ -113,18 +115,43 @@ class WP_Style_Engine_CSS_Declarations {
|
|
|
113
115
|
return $this->declarations;
|
|
114
116
|
}
|
|
115
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Filters a CSS property + value pair.
|
|
120
|
+
*
|
|
121
|
+
* @param string $property The CSS property.
|
|
122
|
+
* @param string $value The value to be filtered.
|
|
123
|
+
* @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
|
|
124
|
+
*
|
|
125
|
+
* @return string The filtered declaration as a single string.
|
|
126
|
+
*/
|
|
127
|
+
protected static function filter_declaration( $property, $value, $spacer = '' ) {
|
|
128
|
+
$filtered_value = wp_strip_all_tags( $value, true );
|
|
129
|
+
if ( '' !== $filtered_value ) {
|
|
130
|
+
return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
|
|
131
|
+
}
|
|
132
|
+
return '';
|
|
133
|
+
}
|
|
134
|
+
|
|
116
135
|
/**
|
|
117
136
|
* Filters and compiles the CSS declarations.
|
|
118
137
|
*
|
|
138
|
+
* @param boolean $should_prettify Whether to add spacing, new lines and indents.
|
|
139
|
+
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
|
|
140
|
+
*
|
|
119
141
|
* @return string The CSS declarations.
|
|
120
142
|
*/
|
|
121
|
-
public function get_declarations_string() {
|
|
143
|
+
public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) {
|
|
122
144
|
$declarations_array = $this->get_declarations();
|
|
123
145
|
$declarations_output = '';
|
|
146
|
+
$indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
|
|
147
|
+
$suffix = $should_prettify ? ' ' : '';
|
|
148
|
+
$suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix;
|
|
149
|
+
$spacer = $should_prettify ? ' ' : '';
|
|
150
|
+
|
|
124
151
|
foreach ( $declarations_array as $property => $value ) {
|
|
125
|
-
$filtered_declaration =
|
|
152
|
+
$filtered_declaration = static::filter_declaration( $property, $value, $spacer );
|
|
126
153
|
if ( $filtered_declaration ) {
|
|
127
|
-
$declarations_output .= $filtered_declaration
|
|
154
|
+
$declarations_output .= "{$indent}{$filtered_declaration};$suffix";
|
|
128
155
|
}
|
|
129
156
|
}
|
|
130
157
|
return rtrim( $declarations_output );
|
|
@@ -54,11 +54,7 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
54
54
|
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
|
|
55
55
|
*/
|
|
56
56
|
public function set_selector( $selector ) {
|
|
57
|
-
if ( empty( $selector ) ) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
57
|
$this->selector = $selector;
|
|
61
|
-
|
|
62
58
|
return $this;
|
|
63
59
|
}
|
|
64
60
|
|
|
@@ -74,11 +70,11 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
74
70
|
$is_declarations_object = ! is_array( $declarations );
|
|
75
71
|
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;
|
|
76
72
|
|
|
77
|
-
if ( null === $this->declarations && $is_declarations_object ) {
|
|
78
|
-
$this->declarations = $declarations;
|
|
79
|
-
return $this;
|
|
80
|
-
}
|
|
81
73
|
if ( null === $this->declarations ) {
|
|
74
|
+
if ( $is_declarations_object ) {
|
|
75
|
+
$this->declarations = $declarations;
|
|
76
|
+
return $this;
|
|
77
|
+
}
|
|
82
78
|
$this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
|
|
83
79
|
}
|
|
84
80
|
$this->declarations->add_declarations( $declarations_array );
|
|
@@ -107,9 +103,23 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
107
103
|
/**
|
|
108
104
|
* Get the CSS.
|
|
109
105
|
*
|
|
106
|
+
* @param boolean $should_prettify Whether to add spacing, new lines and indents.
|
|
107
|
+
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
|
|
108
|
+
*
|
|
110
109
|
* @return string
|
|
111
110
|
*/
|
|
112
|
-
public function get_css() {
|
|
113
|
-
|
|
111
|
+
public function get_css( $should_prettify = false, $indent_count = 0 ) {
|
|
112
|
+
$rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
|
|
113
|
+
$declarations_indent = $should_prettify ? $indent_count + 1 : 0;
|
|
114
|
+
$new_line = $should_prettify ? "\n" : '';
|
|
115
|
+
$space = $should_prettify ? ' ' : '';
|
|
116
|
+
$selector = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
|
|
117
|
+
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
|
|
118
|
+
|
|
119
|
+
if ( empty( $css_declarations ) ) {
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return "{$rule_indent}{$selector}{$space}{{$new_line}{$css_declarations}{$new_line}{$rule_indent}}";
|
|
114
124
|
}
|
|
115
125
|
}
|
|
@@ -27,6 +27,13 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
27
27
|
*/
|
|
28
28
|
protected static $stores = array();
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The store name.
|
|
32
|
+
*
|
|
33
|
+
* @var string
|
|
34
|
+
*/
|
|
35
|
+
protected $name = '';
|
|
36
|
+
|
|
30
37
|
/**
|
|
31
38
|
* An array of CSS Rules objects assigned to the store.
|
|
32
39
|
*
|
|
@@ -39,14 +46,58 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
39
46
|
*
|
|
40
47
|
* @param string $store_name The name of the store.
|
|
41
48
|
*
|
|
42
|
-
* @return WP_Style_Engine_CSS_Rules_Store
|
|
49
|
+
* @return WP_Style_Engine_CSS_Rules_Store|void
|
|
43
50
|
*/
|
|
44
51
|
public static function get_store( $store_name = 'default' ) {
|
|
52
|
+
if ( ! is_string( $store_name ) || empty( $store_name ) ) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
45
55
|
if ( ! isset( static::$stores[ $store_name ] ) ) {
|
|
46
56
|
static::$stores[ $store_name ] = new static();
|
|
57
|
+
// Set the store name.
|
|
58
|
+
static::$stores[ $store_name ]->set_name( $store_name );
|
|
47
59
|
}
|
|
48
60
|
return static::$stores[ $store_name ];
|
|
49
61
|
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get an array of all available stores.
|
|
65
|
+
*
|
|
66
|
+
* @return WP_Style_Engine_CSS_Rules_Store[]
|
|
67
|
+
*/
|
|
68
|
+
public static function get_stores() {
|
|
69
|
+
return static::$stores;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Clears all stores from static::$stores.
|
|
74
|
+
*
|
|
75
|
+
* @return void
|
|
76
|
+
*/
|
|
77
|
+
public static function remove_all_stores() {
|
|
78
|
+
static::$stores = array();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Set the store name.
|
|
83
|
+
*
|
|
84
|
+
* @param string $name The store name.
|
|
85
|
+
*
|
|
86
|
+
* @return void
|
|
87
|
+
*/
|
|
88
|
+
public function set_name( $name ) {
|
|
89
|
+
$this->name = $name;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get the store name.
|
|
94
|
+
*
|
|
95
|
+
* @return string
|
|
96
|
+
*/
|
|
97
|
+
public function get_name() {
|
|
98
|
+
return $this->name;
|
|
99
|
+
}
|
|
100
|
+
|
|
50
101
|
/**
|
|
51
102
|
* Get an array of all rules.
|
|
52
103
|
*
|
|
@@ -62,10 +113,9 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
62
113
|
*
|
|
63
114
|
* @param string $selector The CSS selector.
|
|
64
115
|
*
|
|
65
|
-
* @return WP_Style_Engine_CSS_Rule|
|
|
116
|
+
* @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
|
|
66
117
|
*/
|
|
67
118
|
public function add_rule( $selector ) {
|
|
68
|
-
|
|
69
119
|
$selector = trim( $selector );
|
|
70
120
|
|
|
71
121
|
// Bail early if there is no selector.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* WP_Style_Engine_Processor
|
|
4
4
|
*
|
|
5
|
-
* Compiles styles from
|
|
5
|
+
* Compiles styles from stores or collection of CSS rules.
|
|
6
6
|
*
|
|
7
7
|
* @package Gutenberg
|
|
8
8
|
*/
|
|
@@ -12,42 +12,94 @@ if ( class_exists( 'WP_Style_Engine_Processor' ) ) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Compiles styles from
|
|
15
|
+
* Compiles styles from stores or collection of CSS rules.
|
|
16
16
|
*
|
|
17
17
|
* @access private
|
|
18
18
|
*/
|
|
19
19
|
class WP_Style_Engine_Processor {
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* The Style-Engine Store
|
|
22
|
+
* The Style-Engine Store objects
|
|
23
23
|
*
|
|
24
|
-
* @var WP_Style_Engine_CSS_Rules_Store
|
|
24
|
+
* @var WP_Style_Engine_CSS_Rules_Store[]
|
|
25
25
|
*/
|
|
26
|
-
protected $
|
|
26
|
+
protected $stores = array();
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* The set of CSS rules that this processor will work on.
|
|
30
30
|
*
|
|
31
|
-
* @
|
|
31
|
+
* @var WP_Style_Engine_CSS_Rule[]
|
|
32
32
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
protected $css_rules = array();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Add a store to the processor.
|
|
37
|
+
*
|
|
38
|
+
* @param WP_Style_Engine_CSS_Rules_Store $store The store to add.
|
|
39
|
+
*
|
|
40
|
+
* @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
|
|
41
|
+
*/
|
|
42
|
+
public function add_store( WP_Style_Engine_CSS_Rules_Store $store ) {
|
|
43
|
+
$this->stores[ $store->get_name() ] = $store;
|
|
44
|
+
|
|
45
|
+
return $this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Adds rules to be processed.
|
|
50
|
+
*
|
|
51
|
+
* @param WP_Style_Engine_CSS_Rule|WP_Style_Engine_CSS_Rule[] $css_rules A single, or an array of, WP_Style_Engine_CSS_Rule objects from a store or otherwise.
|
|
52
|
+
*
|
|
53
|
+
* @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
|
|
54
|
+
*/
|
|
55
|
+
public function add_rules( $css_rules ) {
|
|
56
|
+
if ( ! is_array( $css_rules ) ) {
|
|
57
|
+
$css_rules = array( $css_rules );
|
|
58
|
+
}
|
|
59
|
+
foreach ( $css_rules as $rule ) {
|
|
60
|
+
$selector = $rule->get_selector();
|
|
61
|
+
if ( isset( $this->css_rules[ $selector ] ) ) {
|
|
62
|
+
$this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() );
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
$this->css_rules[ $rule->get_selector() ] = $rule;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return $this;
|
|
35
69
|
}
|
|
36
70
|
|
|
37
71
|
/**
|
|
38
72
|
* Get the CSS rules as a string.
|
|
39
73
|
*
|
|
74
|
+
* @param array $options array(
|
|
75
|
+
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
|
|
76
|
+
* 'prettify' => (boolean) Whether to add new lines to output.
|
|
77
|
+
* );.
|
|
78
|
+
*
|
|
40
79
|
* @return string The computed CSS.
|
|
41
80
|
*/
|
|
42
|
-
public function get_css() {
|
|
81
|
+
public function get_css( $options = array() ) {
|
|
82
|
+
$defaults = array(
|
|
83
|
+
'optimize' => true,
|
|
84
|
+
'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
|
|
85
|
+
);
|
|
86
|
+
$options = wp_parse_args( $options, $defaults );
|
|
87
|
+
|
|
88
|
+
// If we have stores, get the rules from them.
|
|
89
|
+
foreach ( $this->stores as $store ) {
|
|
90
|
+
$this->add_rules( $store->get_all_rules() );
|
|
91
|
+
}
|
|
92
|
+
|
|
43
93
|
// Combine CSS selectors that have identical declarations.
|
|
44
|
-
$
|
|
94
|
+
if ( true === $options['optimize'] ) {
|
|
95
|
+
$this->combine_rules_selectors();
|
|
96
|
+
}
|
|
45
97
|
|
|
46
98
|
// Build the CSS.
|
|
47
|
-
$css
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
$css .= $
|
|
99
|
+
$css = '';
|
|
100
|
+
foreach ( $this->css_rules as $rule ) {
|
|
101
|
+
$css .= $rule->get_css( $options['prettify'] );
|
|
102
|
+
$css .= $options['prettify'] ? "\n" : '';
|
|
51
103
|
}
|
|
52
104
|
return $css;
|
|
53
105
|
}
|
|
@@ -58,14 +110,12 @@ class WP_Style_Engine_Processor {
|
|
|
58
110
|
* @return void
|
|
59
111
|
*/
|
|
60
112
|
private function combine_rules_selectors() {
|
|
61
|
-
$rules = $this->store->get_all_rules();
|
|
62
|
-
|
|
63
113
|
// Build an array of selectors along with the JSON-ified styles to make comparisons easier.
|
|
64
114
|
$selectors_json = array();
|
|
65
|
-
foreach ( $
|
|
115
|
+
foreach ( $this->css_rules as $rule ) {
|
|
66
116
|
$declarations = $rule->get_declarations()->get_declarations();
|
|
67
117
|
ksort( $declarations );
|
|
68
|
-
$selectors_json[ $
|
|
118
|
+
$selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
|
|
69
119
|
}
|
|
70
120
|
|
|
71
121
|
// Combine selectors that have the same styles.
|
|
@@ -76,18 +126,18 @@ class WP_Style_Engine_Processor {
|
|
|
76
126
|
if ( 1 >= count( $duplicates ) ) {
|
|
77
127
|
continue;
|
|
78
128
|
}
|
|
129
|
+
|
|
130
|
+
$declarations = $this->css_rules[ $selector ]->get_declarations();
|
|
131
|
+
|
|
79
132
|
foreach ( $duplicates as $key ) {
|
|
80
133
|
// Unset the duplicates from the $selectors_json array to avoid looping through them as well.
|
|
81
134
|
unset( $selectors_json[ $key ] );
|
|
82
|
-
// Remove the rules from the
|
|
83
|
-
$this->
|
|
135
|
+
// Remove the rules from the rules collection.
|
|
136
|
+
unset( $this->css_rules[ $key ] );
|
|
84
137
|
}
|
|
85
138
|
// Create a new rule with the combined selectors.
|
|
86
|
-
$
|
|
87
|
-
|
|
88
|
-
if ( $new_rule ) {
|
|
89
|
-
$new_rule->add_declarations( $rules[ $selector ]->get_declarations() );
|
|
90
|
-
}
|
|
139
|
+
$duplicate_selectors = implode( ',', $duplicates );
|
|
140
|
+
$this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule( $duplicate_selectors, $declarations );
|
|
91
141
|
}
|
|
92
142
|
}
|
|
93
143
|
}
|