@wordpress/style-engine 0.11.0 → 0.14.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +162 -4
  3. package/build/styles/utils.js +1 -1
  4. package/build/styles/utils.js.map +1 -1
  5. package/build-module/styles/utils.js +1 -1
  6. package/build-module/styles/utils.js.map +1 -1
  7. package/class-wp-style-engine-css-declarations.php +167 -0
  8. package/class-wp-style-engine-css-rule.php +129 -0
  9. package/class-wp-style-engine-css-rules-store.php +143 -0
  10. package/class-wp-style-engine-processor.php +135 -0
  11. package/class-wp-style-engine.php +290 -152
  12. package/package.json +2 -2
  13. package/phpunit/class-wp-style-engine-css-declarations-test.php +232 -0
  14. package/phpunit/class-wp-style-engine-css-rule-test.php +127 -0
  15. package/phpunit/class-wp-style-engine-css-rules-store-test.php +157 -0
  16. package/phpunit/class-wp-style-engine-processor-test.php +266 -0
  17. package/phpunit/class-wp-style-engine-test.php +360 -27
  18. package/src/styles/utils.ts +3 -1
  19. package/src/test/index.js +41 -2
  20. package/build-types/index.d.ts +0 -23
  21. package/build-types/index.d.ts.map +0 -1
  22. package/build-types/styles/border/index.d.ts +0 -10
  23. package/build-types/styles/border/index.d.ts.map +0 -1
  24. package/build-types/styles/color/background.d.ts +0 -14
  25. package/build-types/styles/color/background.d.ts.map +0 -1
  26. package/build-types/styles/color/gradient.d.ts +0 -14
  27. package/build-types/styles/color/gradient.d.ts.map +0 -1
  28. package/build-types/styles/color/index.d.ts +0 -10
  29. package/build-types/styles/color/index.d.ts.map +0 -1
  30. package/build-types/styles/color/text.d.ts +0 -14
  31. package/build-types/styles/color/text.d.ts.map +0 -1
  32. package/build-types/styles/constants.d.ts +0 -4
  33. package/build-types/styles/constants.d.ts.map +0 -1
  34. package/build-types/styles/index.d.ts +0 -5
  35. package/build-types/styles/index.d.ts.map +0 -1
  36. package/build-types/styles/spacing/index.d.ts +0 -6
  37. package/build-types/styles/spacing/index.d.ts.map +0 -1
  38. package/build-types/styles/spacing/margin.d.ts +0 -10
  39. package/build-types/styles/spacing/margin.d.ts.map +0 -1
  40. package/build-types/styles/spacing/padding.d.ts +0 -10
  41. package/build-types/styles/spacing/padding.d.ts.map +0 -1
  42. package/build-types/styles/typography/index.d.ts +0 -14
  43. package/build-types/styles/typography/index.d.ts.map +0 -1
  44. package/build-types/styles/utils.d.ts +0 -48
  45. package/build-types/styles/utils.d.ts.map +0 -1
  46. package/build-types/types.d.ts +0 -86
  47. package/build-types/types.d.ts.map +0 -1
  48. package/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.14.0 (2022-08-10)
6
+
7
+ ## 0.13.0 (2022-07-27)
8
+
9
+ ## 0.12.0 (2022-07-13)
10
+
5
11
  ## 0.11.0 (2022-06-29)
6
12
 
7
13
  ## 0.10.0 (2022-06-15)
package/README.md CHANGED
@@ -2,6 +2,158 @@
2
2
 
3
3
  The Style Engine powering global styles and block customizations.
4
4
 
5
+ ## Backend API
6
+
7
+ ### wp_style_engine_get_styles()
8
+
9
+ Global public function to generate styles from a single style object, e.g., the value of
10
+ a [block's attributes.style object](https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles)
11
+ or
12
+ the [top level styles in theme.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/)
13
+ .
14
+
15
+ _Parameters_
16
+
17
+ - _$block_styles_ `array` A block's `attributes.style` object or the top level styles in theme.json
18
+ - _$options_ `array<string|boolean>` An array of options to determine the output.
19
+ - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or '
20
+ global-styles'. Default is 'block-supports'.
21
+ - _enqueue_ `boolean` When `true` will attempt to store and enqueue for rendering in a `style` tag on the site frontend.
22
+ - _convert_vars_to_classnames_ `boolean` Whether to skip converting CSS var:? values to var( --wp--preset--\* )
23
+ values. Default is `false`.
24
+ - _selector_ `string` When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`,
25
+ otherwise a concatenated string of properties and values.
26
+
27
+ _Returns_
28
+ `array<string|array>|null`
29
+
30
+ ```php
31
+ array(
32
+ 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
33
+ 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
34
+ 'classnames' => (string) Classnames separated by a space.
35
+ );
36
+ ```
37
+
38
+ It will return compiled CSS declarations for inline styles, or, where a selector is provided, a complete CSS rule.
39
+
40
+ To enqueue a style for rendering in the site's frontend, the `$options` array requires the following:
41
+
42
+ 1. **selector (string)** - this is the CSS selector for your block style CSS declarations.
43
+ 2. **context (string)** - this tells the style engine where to store the styles. Styles in the same context will be
44
+ batched together and printed in the same HTML style tag. The default is `'block-supports'`.
45
+ 3. **enqueue (boolean)** - tells the style engine to store the styles.
46
+
47
+ `wp_style_engine_get_styles` will return the compiled CSS and CSS declarations array.
48
+
49
+ #### Usage
50
+
51
+ ```php
52
+ $block_styles = array(
53
+ 'spacing' => array( 'padding' => '100px' )
54
+ );
55
+ $styles = wp_style_engine_get_styles(
56
+ $block_styles,
57
+ array(
58
+ 'selector' => '.a-selector',
59
+ 'context' => 'block-supports',
60
+ 'enqueue' => true,
61
+ )
62
+ );
63
+ print_r( $styles );
64
+
65
+ /*
66
+ array(
67
+ 'css' => '.a-selector{padding:10px}'
68
+ 'declarations' => array( 'padding' => '100px' )
69
+ )
70
+ */
71
+ ```
72
+
73
+ ### wp_style_engine_get_stylesheet_from_css_rules()
74
+
75
+ Use this function to compile and return a stylesheet for any CSS rules. The style engine will automatically merge declarations and combine selectors.
76
+
77
+ This function acts as a CSS compiler, but will also enqueue styles for rendering where `enqueue` and `context` strings are passed in the options.
78
+
79
+ _Parameters_
80
+
81
+ - _$css_rules_ `array<array>`
82
+ - _$options_ `array<string|boolean>` An array of options to determine the output.
83
+ - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or '
84
+ global-styles'. Default is 'block-supports'.
85
+ - _enqueue_ `boolean` When `true` will store using the `context` value as a key.
86
+
87
+ _Returns_
88
+ `string` A compiled CSS string based on `$css_rules`.
89
+
90
+ #### Usage
91
+
92
+ ```php
93
+ $styles = array(
94
+ array(
95
+ 'selector'. => '.wp-pumpkin',
96
+ 'declarations' => array( 'color' => 'orange' )
97
+ ),
98
+ array(
99
+ 'selector'. => '.wp-tomato',
100
+ 'declarations' => array( 'color' => 'red' )
101
+ ),
102
+ array(
103
+ 'selector'. => '.wp-tomato',
104
+ 'declarations' => array( 'padding' => '100px' )
105
+ ),
106
+ array(
107
+ 'selector'. => '.wp-kumquat',
108
+ 'declarations' => array( 'color' => 'orange' )
109
+ ),
110
+ );
111
+
112
+ $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
113
+ $styles,
114
+ array(
115
+ 'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
116
+ 'enqueue' => true, // Render the styles for output.
117
+ )
118
+ );
119
+ print_r( $stylesheet ); // .wp-pumpkin, .wp-kumquat {color:orange}.wp-tomato{color:red;padding:100px}
120
+ ```
121
+
122
+ ### wp_style_engine_get_stylesheet_from_store()
123
+
124
+ Returns compiled CSS from a store, if found.
125
+
126
+ _Parameters_
127
+
128
+ - _$store_key_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or ' global-styles'. Default is 'block-supports'.
129
+
130
+ _Returns_
131
+ `string` A compiled CSS string from the stored CSS rules.
132
+
133
+ #### Usage
134
+
135
+ ```php
136
+ // First register some styles.
137
+ $styles = array(
138
+ array(
139
+ 'selector'. => '.wp-apple',
140
+ 'declarations' => array( 'color' => 'green' )
141
+ ),
142
+ );
143
+
144
+ $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
145
+ $styles,
146
+ array(
147
+ 'context' => 'fruit-styles',
148
+ 'enqueue' => true,
149
+ )
150
+ );
151
+
152
+ // Later, fetch compiled rules from store.
153
+ $stylesheet = gutenberg_style_engine_get_stylesheet_from_store( 'fruit-styles' );
154
+ print_r( $stylesheet ); // .wp-apple{color:green;}
155
+ ```
156
+
5
157
  ## Installation (JS only)
6
158
 
7
159
  Install the module
@@ -10,13 +162,18 @@ Install the module
10
162
  npm install @wordpress/style-engine --save
11
163
  ```
12
164
 
13
- _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
165
+ _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has
166
+ limited or no support for such language features and APIs, you should
167
+ include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill)
168
+ in your code._
14
169
 
15
170
  ## Important
16
171
 
17
- This Package is considered experimental at the moment. The idea is to have a package used to generate styles based on a style object that is consistent between: backend, frontend, block style object and theme.json.
172
+ This Package is considered experimental at the moment. The idea is to have a package used to generate styles based on a
173
+ style object that is consistent between: backend, frontend, block style object and theme.json.
18
174
 
19
- Because this package is experimental and still in development it does not yet generate a `wp.styleEngine` global. To get there, the following tasks need to be completed:
175
+ Because this package is experimental and still in development it does not yet generate a `wp.styleEngine` global. To get
176
+ there, the following tasks need to be completed:
20
177
 
21
178
  **TODO List:**
22
179
 
@@ -26,7 +183,8 @@ Because this package is experimental and still in development it does not yet ge
26
183
  - Support generating styles in the backend (block supports and theme.json stylesheet). (Ongoing)
27
184
  - Refactor all block styles to use the style engine server side. (Ongoing)
28
185
  - Consolidate global and block style rendering and enqueuing
29
- - Refactor all blocks to consistently use the "style" attribute for all customizations (get rid of the preset specific attributes).
186
+ - Refactor all blocks to consistently use the "style" attribute for all customizations (get rid of the preset specific
187
+ attributes).
30
188
 
31
189
  See [Tracking: Add a Style Engine to manage rendering block styles #38167](https://github.com/WordPress/gutenberg/issues/38167)
32
190
 
@@ -65,7 +65,7 @@ function generateBoxRules(style, options, path, ruleKeys) {
65
65
  });
66
66
  } else {
67
67
  const sideRules = individualProperties.reduce((acc, side) => {
68
- const value = (0, _lodash.get)(boxStyle, [side]);
68
+ const value = getCSSVarFromStyleValue((0, _lodash.get)(boxStyle, [side]));
69
69
 
70
70
  if (value) {
71
71
  acc.push({
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["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_REFERENCE_PREFIX","variable","slice","length","split","VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE","join","VARIABLE_PATH_SEPARATOR_TOKEN_STYLE","firstLetter","rest","toUpperCase"],"mappings":";;;;;;;;;;AAGA;;AAYA;;AAfA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,YAAT,CACNC,KADM,EAENC,OAFM,EAGNC,IAHM,EAINC,OAJM,EAKL;AACD,QAAMC,UAA8B,GAAG,iBAAKJ,KAAL,EAAYE,IAAZ,CAAvC;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;;;AACO,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,GAAG,iBAAKZ,KAAL,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AFe,CAERC,kDAFQ,EAGfC,IAHe,CAGTC,8CAHS,CAAjB;AAIA,WAAQ,aAAaN,QAAU,GAA/B;AACA;;AACD,SAAOrB,UAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASkB,UAAT,OAAwD;AAAA,MAAnC,CAAEU,WAAF,EAAe,GAAGC,IAAlB,CAAmC;AAC9D,SAAOD,WAAW,CAACE,WAAZ,KAA4BD,IAAI,CAACH,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 = get( boxStyle, [ side ] );\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"]}
1
+ {"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["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_REFERENCE_PREFIX","variable","slice","length","split","VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE","join","VARIABLE_PATH_SEPARATOR_TOKEN_STYLE","firstLetter","rest","toUpperCase"],"mappings":";;;;;;;;;;AAGA;;AAYA;;AAfA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,YAAT,CACNC,KADM,EAENC,OAFM,EAGNC,IAHM,EAINC,OAJM,EAKL;AACD,QAAMC,UAA8B,GAAG,iBAAKJ,KAAL,EAAYE,IAAZ,CAAvC;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;;;AACO,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,GAAG,iBAAKZ,KAAL,EAAYE,IAAZ,CAA3C;;AACA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KAFe,CAERC,kDAFQ,EAGfC,IAHe,CAGTC,8CAHS,CAAjB;AAIA,WAAQ,aAAaN,QAAU,GAA/B;AACA;;AACD,SAAOrB,UAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASkB,UAAT,OAAwD;AAAA,MAAnC,CAAEU,WAAF,EAAe,GAAGC,IAAlB,CAAmC;AAC9D,SAAOD,WAAW,CAACE,WAAZ,KAA4BD,IAAI,CAACH,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"]}
@@ -56,7 +56,7 @@ export function generateBoxRules(style, options, path, ruleKeys) {
56
56
  });
57
57
  } else {
58
58
  const sideRules = individualProperties.reduce((acc, side) => {
59
- const value = get(boxStyle, [side]);
59
+ const value = getCSSVarFromStyleValue(get(boxStyle, [side]));
60
60
 
61
61
  if (value) {
62
62
  acc.push({
@@ -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,KA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B,CAAlB;AAiBAL,IAAAA,KAAK,CAACC,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 = get( boxStyle, [ side ] );\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"]}
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,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ArB,CACJ,IADI,EAEJC,UAAU,CAAEH,IAAF,CAFN,CAFI;AAMTZ,UAAAA;AANS,SAAV;AAQA;;AACD,aAAOW,GAAP;AACA,KAhBgB,EAiBjB,EAjBiB,CAAlB;AAmBAL,IAAAA,KAAK,CAACC,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,167 @@
1
+ <?php
2
+ /**
3
+ * WP_Style_Engine_CSS_Declarations
4
+ *
5
+ * Holds, sanitizes and prints CSS rules declarations
6
+ *
7
+ * @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 a CSS property + value pair.
118
+ *
119
+ * @param string $property The CSS property.
120
+ * @param string $value The value to be filtered.
121
+ * @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
122
+ *
123
+ * @return string The filtered declaration as a single string.
124
+ */
125
+ protected static function filter_declaration( $property, $value, $spacer = '' ) {
126
+ if ( isset( $property ) && isset( $value ) ) {
127
+ return safecss_filter_attr( "{$property}:{$spacer}{$value}" );
128
+ }
129
+ return '';
130
+ }
131
+
132
+ /**
133
+ * Filters and compiles the CSS declarations.
134
+ *
135
+ * @param boolean $should_prettify Whether to add spacing, new lines and indents.
136
+ * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
137
+ *
138
+ * @return string The CSS declarations.
139
+ */
140
+ public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) {
141
+ $declarations_array = $this->get_declarations();
142
+ $declarations_output = '';
143
+ $indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
144
+ $suffix = $should_prettify ? ' ' : '';
145
+ $suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix;
146
+
147
+ foreach ( $declarations_array as $property => $value ) {
148
+ $spacer = $should_prettify ? ' ' : '';
149
+ $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
150
+ if ( $filtered_declaration ) {
151
+ $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
152
+ }
153
+ }
154
+ return rtrim( $declarations_output );
155
+ }
156
+
157
+ /**
158
+ * Sanitize property names.
159
+ *
160
+ * @param string $property The CSS property.
161
+ *
162
+ * @return string The sanitized property name.
163
+ */
164
+ protected function sanitize_property( $property ) {
165
+ return sanitize_key( $property );
166
+ }
167
+ }
@@ -0,0 +1,129 @@
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|void 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
+ * @param boolean $should_prettify Whether to add spacing, new lines and indents.
111
+ * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
112
+ *
113
+ * @return string
114
+ */
115
+ public function get_css( $should_prettify = false, $indent_count = 0 ) {
116
+ $rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
117
+ $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
118
+ $new_line = $should_prettify ? "\n" : '';
119
+ $space = $should_prettify ? ' ' : '';
120
+ $selector = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
121
+ $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
122
+
123
+ if ( empty( $css_declarations ) ) {
124
+ return '';
125
+ }
126
+
127
+ return "{$rule_indent}{$selector}{$space}{{$new_line}{$css_declarations}{$new_line}{$rule_indent}}";
128
+ }
129
+ }