@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
@@ -0,0 +1,232 @@
1
+ <?php
2
+ /**
3
+ * Tests the Style Engine CSS declarations class.
4
+ *
5
+ * @package Gutenberg
6
+ * @subpackage style-engine
7
+ */
8
+
9
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
10
+
11
+ /**
12
+ * Tests for registering, storing and generating CSS declarations.
13
+ */
14
+ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
15
+ /**
16
+ * Should set declarations on instantiation.
17
+ */
18
+ public function test_instantiate_with_declarations() {
19
+ $input_declarations = array(
20
+ 'margin-top' => '10px',
21
+ 'font-size' => '2rem',
22
+ );
23
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
24
+ $this->assertSame( $input_declarations, $css_declarations->get_declarations() );
25
+ }
26
+
27
+ /**
28
+ * Should add declarations.
29
+ */
30
+ public function test_add_declarations() {
31
+ $input_declarations = array(
32
+ 'padding' => '20px',
33
+ 'color' => 'var(--wp--preset--elbow-patches)',
34
+ );
35
+ $css_declarations = new WP_Style_Engine_CSS_Declarations();
36
+ $css_declarations->add_declarations( $input_declarations );
37
+ $this->assertSame( $input_declarations, $css_declarations->get_declarations() );
38
+ }
39
+
40
+ /**
41
+ * Should add declarations.
42
+ */
43
+ public function test_add_a_single_declaration() {
44
+ $input_declarations = array(
45
+ 'border-width' => '1%',
46
+ 'background-color' => 'var(--wp--preset--english-mustard)',
47
+ );
48
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
49
+ $extra_declaration = array(
50
+ 'letter-spacing' => '1.5px',
51
+ );
52
+ $css_declarations->add_declarations( $extra_declaration );
53
+ $this->assertSame( array_merge( $input_declarations, $extra_declaration ), $css_declarations->get_declarations() );
54
+ }
55
+
56
+ /**
57
+ * Should sanitize properties before storing.
58
+ */
59
+ public function test_sanitize_properties() {
60
+ $input_declarations = array(
61
+ '^--wp--style--sleepy-potato$' => '40px',
62
+ '<background-//color>' => 'var(--wp--preset--english-mustard)',
63
+ );
64
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
65
+
66
+ $this->assertSame(
67
+ array(
68
+ '--wp--style--sleepy-potato' => '40px',
69
+ 'background-color' => 'var(--wp--preset--english-mustard)',
70
+ ),
71
+ $css_declarations->get_declarations()
72
+ );
73
+ }
74
+
75
+ /**
76
+ * Should compile css declarations into a css declarations block string.
77
+ */
78
+ public function test_generate_css_declarations_string() {
79
+ $input_declarations = array(
80
+ 'color' => 'red',
81
+ 'border-top-left-radius' => '99px',
82
+ 'text-decoration' => 'underline',
83
+ );
84
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
85
+
86
+ $this->assertSame(
87
+ 'color:red;border-top-left-radius:99px;text-decoration:underline;',
88
+ $css_declarations->get_declarations_string()
89
+ );
90
+ }
91
+
92
+ /**
93
+ * Should compile css declarations into a prettified css declarations block string.
94
+ */
95
+ public function test_generate_prettified_css_declarations_string() {
96
+ $input_declarations = array(
97
+ 'color' => 'red',
98
+ 'border-top-left-radius' => '99px',
99
+ 'text-decoration' => 'underline',
100
+ );
101
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
102
+
103
+ $this->assertSame(
104
+ 'color: red; border-top-left-radius: 99px; text-decoration: underline;',
105
+ $css_declarations->get_declarations_string( true )
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Should compile css declarations into a prettified and indented css declarations block string.
111
+ */
112
+ public function test_generate_prettified_with_indent_css_declarations_string() {
113
+ $input_declarations = array(
114
+ 'color' => 'red',
115
+ 'border-top-left-radius' => '99px',
116
+ 'text-decoration' => 'underline',
117
+ );
118
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
119
+
120
+ $this->assertSame(
121
+ ' color: red;
122
+ border-top-left-radius: 99px;
123
+ text-decoration: underline;',
124
+ $css_declarations->get_declarations_string( true, 1 )
125
+ );
126
+ }
127
+
128
+ /**
129
+ * Should compile css declarations into a css declarations block string.
130
+ */
131
+ public function test_generate_prettified_with_more_indents_css_declarations_string() {
132
+ $input_declarations = array(
133
+ 'color' => 'red',
134
+ 'border-top-left-radius' => '99px',
135
+ 'text-decoration' => 'underline',
136
+ );
137
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
138
+
139
+ $this->assertSame(
140
+ ' color: red;
141
+ border-top-left-radius: 99px;
142
+ text-decoration: underline;',
143
+ $css_declarations->get_declarations_string( true, 2 )
144
+ );
145
+ }
146
+
147
+ /**
148
+ * Should escape values and run the CSS through safecss_filter_attr.
149
+ */
150
+ public function test_remove_unsafe_properties_and_values() {
151
+ $input_declarations = array(
152
+ 'color' => 'url("https://wordpress.org")',
153
+ 'margin-right' => '10em',
154
+ 'potato' => 'uppercase',
155
+ );
156
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
157
+
158
+ $this->assertSame(
159
+ 'margin-right:10em;',
160
+ $css_declarations->get_declarations_string()
161
+ );
162
+ }
163
+
164
+ /**
165
+ * Should allow calc, clamp, min, max, and minmax CSS functions.
166
+ */
167
+ public function test_allow_particular_css_functions() {
168
+ $input_declarations = array(
169
+ 'background' => 'var(--wp--preset--color--primary, 10px)', // Simple var().
170
+ 'font-size' => 'clamp(36.00rem, calc(32.00rem + 10.00vw), 40.00rem)', // Nested clamp().
171
+ 'width' => 'min(150vw, 100px)',
172
+ 'min-width' => 'max(150vw, 100px)',
173
+ 'max-width' => 'minmax(400px, 50%)',
174
+ 'padding' => 'calc(80px * -1)',
175
+ 'background-image' => 'url("https://wordpress.org")',
176
+ 'line-height' => 'url("https://wordpress.org")',
177
+ 'margin' => 'illegalfunction(30px)',
178
+ );
179
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
180
+
181
+ $this->assertSame(
182
+ 'background:var(--wp--preset--color--primary, 10px);font-size:clamp(36.00rem, calc(32.00rem + 10.00vw), 40.00rem);width:min(150vw, 100px);min-width:max(150vw, 100px);max-width:minmax(400px, 50%);padding:calc(80px * -1);background-image:url("https://wordpress.org");',
183
+ $css_declarations->get_declarations_string()
184
+ );
185
+ }
186
+
187
+ /**
188
+ * Should remove a declaration
189
+ */
190
+ public function test_remove_declaration() {
191
+ $input_declarations = array(
192
+ 'color' => 'tomato',
193
+ 'margin' => '10em 10em 20em 1px',
194
+ 'font-family' => 'Happy Font serif',
195
+ );
196
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
197
+
198
+ $this->assertSame(
199
+ 'color:tomato;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
200
+ $css_declarations->get_declarations_string()
201
+ );
202
+
203
+ $css_declarations->remove_declaration( 'color' );
204
+ $this->assertSame(
205
+ 'margin:10em 10em 20em 1px;font-family:Happy Font serif;',
206
+ $css_declarations->get_declarations_string()
207
+ );
208
+ }
209
+
210
+ /**
211
+ * Should remove declarations
212
+ */
213
+ public function test_remove_declarations() {
214
+ $input_declarations = array(
215
+ 'color' => 'cucumber',
216
+ 'margin' => '10em 10em 20em 1px',
217
+ 'font-family' => 'Happy Font serif',
218
+ );
219
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
220
+
221
+ $this->assertSame(
222
+ 'color:cucumber;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
223
+ $css_declarations->get_declarations_string()
224
+ );
225
+
226
+ $css_declarations->remove_declarations( array( 'color', 'margin' ) );
227
+ $this->assertSame(
228
+ 'font-family:Happy Font serif;',
229
+ $css_declarations->get_declarations_string()
230
+ );
231
+ }
232
+ }
@@ -0,0 +1,127 @@
1
+ <?php
2
+ /**
3
+ * Tests the Style Engine CSS Rule class.
4
+ *
5
+ * @package Gutenberg
6
+ * @subpackage style-engine
7
+ */
8
+
9
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
10
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
11
+
12
+ /**
13
+ * Tests for registering, storing and generating CSS declarations.
14
+ */
15
+ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
16
+ /**
17
+ * Should set declarations on instantiation.
18
+ */
19
+ public function test_instantiate_with_selector_and_rules() {
20
+ $selector = '.law-and-order';
21
+ $input_declarations = array(
22
+ 'margin-top' => '10px',
23
+ 'font-size' => '2rem',
24
+ );
25
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
26
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
27
+
28
+ $this->assertSame( $selector, $css_rule->get_selector() );
29
+
30
+ $expected = "$selector{{$css_declarations->get_declarations_string()}}";
31
+ $this->assertSame( $expected, $css_rule->get_css() );
32
+ }
33
+
34
+ /**
35
+ * Test dedupe declaration properties.
36
+ */
37
+ public function test_dedupe_properties_in_rules() {
38
+ $selector = '.taggart';
39
+ $first_declaration = array(
40
+ 'font-size' => '2rem',
41
+ );
42
+ $overwrite_first_declaration = array(
43
+ 'font-size' => '4px',
44
+ );
45
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $first_declaration );
46
+ $css_rule->add_declarations( new WP_Style_Engine_CSS_Declarations( $overwrite_first_declaration ) );
47
+
48
+ $expected = '.taggart{font-size:4px;}';
49
+ $this->assertSame( $expected, $css_rule->get_css() );
50
+ }
51
+
52
+ /**
53
+ * Should add declarations.
54
+ */
55
+ public function test_add_declarations() {
56
+ // Declarations using a WP_Style_Engine_CSS_Declarations object.
57
+ $some_css_declarations = new WP_Style_Engine_CSS_Declarations( array( 'margin-top' => '10px' ) );
58
+ // Declarations using a property => value array.
59
+ $some_more_css_declarations = array( 'font-size' => '1rem' );
60
+ $css_rule = new WP_Style_Engine_CSS_Rule( '.hill-street-blues', $some_css_declarations );
61
+ $css_rule->add_declarations( $some_more_css_declarations );
62
+
63
+ $expected = '.hill-street-blues{margin-top:10px;font-size:1rem;}';
64
+ $this->assertSame( $expected, $css_rule->get_css() );
65
+ }
66
+
67
+ /**
68
+ * Should set selector.
69
+ */
70
+ public function test_set_selector() {
71
+ $selector = '.taggart';
72
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector );
73
+
74
+ $this->assertSame( $selector, $css_rule->get_selector() );
75
+
76
+ $css_rule->set_selector( '.law-and-order' );
77
+
78
+ $this->assertSame( '.law-and-order', $css_rule->get_selector() );
79
+ }
80
+
81
+ /**
82
+ * Should generate CSS rules.
83
+ */
84
+ public function test_get_css() {
85
+ $selector = '.chips';
86
+ $input_declarations = array(
87
+ 'margin-top' => '10px',
88
+ 'font-size' => '2rem',
89
+ );
90
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
91
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
92
+ $expected = "$selector{{$css_declarations->get_declarations_string()}}";
93
+
94
+ $this->assertSame( $expected, $css_rule->get_css() );
95
+ }
96
+
97
+ /**
98
+ * Should return empty string with no declarations.
99
+ */
100
+ public function test_get_css_no_declarations() {
101
+ $selector = '.holmes';
102
+ $input_declarations = array();
103
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
104
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
105
+
106
+ $this->assertSame( '', $css_rule->get_css() );
107
+ }
108
+
109
+ /**
110
+ * Should generate prettified CSS rules.
111
+ */
112
+ public function test_get_prettified_css() {
113
+ $selector = '.baptiste';
114
+ $input_declarations = array(
115
+ 'margin-left' => '0',
116
+ 'font-family' => 'Detective Sans',
117
+ );
118
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
119
+ $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
120
+ $expected = '.baptiste {
121
+ margin-left: 0;
122
+ font-family: Detective Sans;
123
+ }';
124
+
125
+ $this->assertSame( $expected, $css_rule->get_css( true ) );
126
+ }
127
+ }
@@ -0,0 +1,157 @@
1
+ <?php
2
+ /**
3
+ * Tests the Style Engine CSS Rules Store class.
4
+ *
5
+ * @package Gutenberg
6
+ * @subpackage style-engine
7
+ */
8
+
9
+ require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
10
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
11
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
12
+
13
+ /**
14
+ * Tests for registering, storing and retrieving CSS Rules.
15
+ */
16
+ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
17
+ /**
18
+ * Tear down after each test.
19
+ */
20
+ public function tear_down() {
21
+ parent::tear_down();
22
+ WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
23
+ }
24
+ /**
25
+ * Should create a new store.
26
+ */
27
+ public function test_create_new_store() {
28
+ $new_pancakes_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pancakes-with-strawberries' );
29
+ $this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $new_pancakes_store );
30
+ }
31
+
32
+ /**
33
+ * Should return previously created store when the same selector key is passed.
34
+ */
35
+ public function test_get_store() {
36
+ $new_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
37
+ $selector = '.haddock';
38
+
39
+ $new_fish_store->add_rule( $selector )->get_selector();
40
+ $this->assertEquals( $selector, $new_fish_store->add_rule( $selector )->get_selector() );
41
+
42
+ $the_same_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
43
+ $this->assertEquals( $selector, $the_same_fish_store->add_rule( $selector )->get_selector() );
44
+ }
45
+
46
+ /**
47
+ * Should return all previously created stores.
48
+ */
49
+ public function test_get_stores() {
50
+ $burrito_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'burrito' );
51
+ $quesadilla_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'quesadilla' );
52
+ $this->assertEquals(
53
+ array(
54
+ 'burrito' => $burrito_store,
55
+ 'quesadilla' => $quesadilla_store,
56
+ ),
57
+ WP_Style_Engine_CSS_Rules_Store::get_stores()
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Should delete all previously created stores.
63
+ */
64
+ public function test_remove_all_stores() {
65
+ $dolmades_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'dolmades' );
66
+ $tzatziki_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'tzatziki' );
67
+ $this->assertEquals(
68
+ array(
69
+ 'dolmades' => $dolmades_store,
70
+ 'tzatziki' => $tzatziki_store,
71
+ ),
72
+ WP_Style_Engine_CSS_Rules_Store::get_stores()
73
+ );
74
+ WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
75
+ $this->assertEquals(
76
+ array(),
77
+ WP_Style_Engine_CSS_Rules_Store::get_stores()
78
+ );
79
+ }
80
+
81
+ /**
82
+ * Should return a stored rule.
83
+ */
84
+ public function test_add_rule() {
85
+ $new_pie_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'meat-pie' );
86
+ $selector = '.wp-block-sauce a:hover';
87
+ $store_rule = $new_pie_store->add_rule( $selector );
88
+ $expected = '';
89
+ $this->assertEquals( $expected, $store_rule->get_css() );
90
+
91
+ $pie_declarations = array(
92
+ 'color' => 'brown',
93
+ 'border-color' => 'yellow',
94
+ 'border-radius' => '10rem',
95
+ );
96
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $pie_declarations );
97
+ $store_rule->add_declarations( $css_declarations );
98
+
99
+ $store_rule = $new_pie_store->add_rule( $selector );
100
+ $expected = "$selector{{$css_declarations->get_declarations_string()}}";
101
+ $this->assertEquals( $expected, $store_rule->get_css() );
102
+ }
103
+
104
+ /**
105
+ * Should return all stored rules.
106
+ */
107
+ public function test_get_all_rules() {
108
+ $new_pizza_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pizza-with-mozzarella' );
109
+ $selector = '.wp-block-anchovies a:hover';
110
+ $store_rule = $new_pizza_store->add_rule( $selector );
111
+ $expected = array(
112
+ $selector => $store_rule,
113
+ );
114
+
115
+ $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
116
+
117
+ $pizza_declarations = array(
118
+ 'color' => 'red',
119
+ 'border-color' => 'yellow',
120
+ 'border-radius' => '10rem',
121
+ );
122
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $pizza_declarations );
123
+ $store_rule->add_declarations( array( $css_declarations ) );
124
+
125
+ $expected = array(
126
+ $selector => $store_rule,
127
+ );
128
+ $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
129
+
130
+ $new_pizza_declarations = array(
131
+ 'color' => 'red',
132
+ 'border-color' => 'red',
133
+ 'font-size' => '10rem',
134
+ );
135
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $new_pizza_declarations );
136
+ $store_rule->add_declarations( array( $css_declarations ) );
137
+
138
+ $expected = array(
139
+ $selector => $store_rule,
140
+ );
141
+ $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
142
+
143
+ $new_selector = '.wp-block-mushroom a:hover';
144
+ $newer_pizza_declarations = array(
145
+ 'padding' => '100px',
146
+ );
147
+ $new_store_rule = $new_pizza_store->add_rule( $new_selector );
148
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $newer_pizza_declarations );
149
+ $new_store_rule->add_declarations( array( $css_declarations ) );
150
+
151
+ $expected = array(
152
+ $selector => $store_rule,
153
+ $new_selector => $new_store_rule,
154
+ );
155
+ $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
156
+ }
157
+ }