@wordpress/style-engine 0.16.0 → 1.0.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.
@@ -6,16 +6,26 @@
6
6
  * @subpackage style-engine
7
7
  */
8
8
 
9
- require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
9
+ // Check for the existence of Style Engine classes and methods.
10
+ // Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
11
+ // Testing new features from the Gutenberg package may require
12
+ // testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
13
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
14
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
15
+ }
10
16
 
11
17
  /**
12
- * Tests for registering, storing and generating CSS declarations.
18
+ * Tests registering, storing and generating CSS declarations.
19
+ *
20
+ * @coversDefaultClass WP_Style_Engine_CSS_Declarations
13
21
  */
14
22
  class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
15
23
  /**
16
- * Should set declarations on instantiation.
24
+ * Tests setting declarations on instantiation.
25
+ *
26
+ * @covers ::__construct
17
27
  */
18
- public function test_instantiate_with_declarations() {
28
+ public function test_should_instantiate_with_declarations() {
19
29
  $input_declarations = array(
20
30
  'margin-top' => '10px',
21
31
  'font-size' => '2rem',
@@ -25,22 +35,29 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
25
35
  }
26
36
 
27
37
  /**
28
- * Should add declarations.
38
+ * Tests that declarations are added.
39
+ *
40
+ * @covers ::add_declarations
41
+ * @covers ::add_declaration
29
42
  */
30
- public function test_add_declarations() {
43
+ public function test_should_add_declarations() {
31
44
  $input_declarations = array(
32
45
  'padding' => '20px',
33
46
  'color' => 'var(--wp--preset--elbow-patches)',
34
47
  );
35
48
  $css_declarations = new WP_Style_Engine_CSS_Declarations();
36
49
  $css_declarations->add_declarations( $input_declarations );
50
+
37
51
  $this->assertSame( $input_declarations, $css_declarations->get_declarations() );
38
52
  }
39
53
 
40
54
  /**
41
- * Should add declarations.
55
+ * Tests that new declarations are added to existing declarations.
56
+ *
57
+ * @covers ::add_declarations
58
+ * @covers ::add_declaration
42
59
  */
43
- public function test_add_a_single_declaration() {
60
+ public function test_should_add_new_declarations_to_existing() {
44
61
  $input_declarations = array(
45
62
  'border-width' => '1%',
46
63
  'background-color' => 'var(--wp--preset--english-mustard)',
@@ -50,13 +67,17 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
50
67
  'letter-spacing' => '1.5px',
51
68
  );
52
69
  $css_declarations->add_declarations( $extra_declaration );
70
+
53
71
  $this->assertSame( array_merge( $input_declarations, $extra_declaration ), $css_declarations->get_declarations() );
54
72
  }
55
73
 
56
74
  /**
57
- * Should sanitize properties before storing.
75
+ * Tests that properties are sanitized before storing.
76
+ *
77
+ * @covers ::filter_declaration
78
+ * @covers ::sanitize_property
58
79
  */
59
- public function test_sanitize_properties() {
80
+ public function test_should_sanitize_properties() {
60
81
  $input_declarations = array(
61
82
  '^--wp--style--sleepy-potato$' => '40px',
62
83
  '<background-//color>' => 'var(--wp--preset--english-mustard)',
@@ -73,123 +94,141 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
73
94
  }
74
95
 
75
96
  /**
76
- * Should compile css declarations into a css declarations block string.
97
+ * Tests that values with HTML tags are escaped, and CSS properties are run through safecss_filter_attr().
98
+ *
99
+ * @covers ::get_declarations_string
100
+ * @covers ::filter_declaration
77
101
  */
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',
102
+ public function test_should_strip_html_tags_and_remove_unsafe_css_properties() {
103
+ $input_declarations = array(
104
+ 'font-size' => '<red/>',
105
+ 'padding' => '</style>',
106
+ 'potato' => 'uppercase',
107
+ 'cheese' => '10px',
108
+ 'margin-right' => '10em',
83
109
  );
84
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
110
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
111
+ $safe_style_css_mock_action = new MockAction();
85
112
 
86
- $this->assertSame(
87
- 'color:red;border-top-left-radius:99px;text-decoration:underline;',
88
- $css_declarations->get_declarations_string()
89
- );
90
- }
113
+ // filter_declaration() is called in get_declarations_string().
114
+ add_filter( 'safe_style_css', array( $safe_style_css_mock_action, 'filter' ) );
115
+ $css_declarations_string = $css_declarations->get_declarations_string();
91
116
 
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',
117
+ $this->assertSame(
118
+ 3, // Values with HTML tags are removed first by wp_strip_all_tags().
119
+ $safe_style_css_mock_action->get_call_count(),
120
+ '"safe_style_css" filters were not applied to CSS declaration properties.'
100
121
  );
101
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
102
122
 
103
123
  $this->assertSame(
104
- 'color: red; border-top-left-radius: 99px; text-decoration: underline;',
105
- $css_declarations->get_declarations_string( true )
124
+ 'margin-right:10em;',
125
+ $css_declarations_string,
126
+ 'Unallowed CSS properties or values with HTML tags were not removed.'
106
127
  );
107
128
  }
108
129
 
130
+
109
131
  /**
110
- * Should compile css declarations into a prettified and indented css declarations block string.
132
+ * Tests that calc, clamp, min, max, and minmax CSS functions are allowed.
133
+ *
134
+ * @covers ::get_declarations_string
135
+ * @covers ::filter_declaration
111
136
  */
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',
137
+ public function test_should_allow_css_functions_and_strip_unsafe_css_values() {
138
+ $input_declarations = array(
139
+ 'background' => 'var(--wp--preset--color--primary, 10px)', // Simple var().
140
+ 'font-size' => 'clamp(36.00rem, calc(32.00rem + 10.00vw), 40.00rem)', // Nested clamp().
141
+ 'width' => 'min(150vw, 100px)',
142
+ 'min-width' => 'max(150vw, 100px)',
143
+ 'max-width' => 'minmax(400px, 50%)',
144
+ 'padding' => 'calc(80px * -1)',
145
+ 'background-image' => 'url("https://wordpress.org")',
146
+ 'line-height' => 'url("https://wordpress.org")',
147
+ 'margin' => 'illegalfunction(30px)',
117
148
  );
118
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
149
+ $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
150
+ $safecss_filter_attr_allow_css_mock_action = new MockAction();
119
151
 
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
- }
152
+ // filter_declaration() is called in get_declarations_string().
153
+ add_filter( 'safecss_filter_attr_allow_css', array( $safecss_filter_attr_allow_css_mock_action, 'filter' ) );
154
+ $css_declarations_string = $css_declarations->get_declarations_string();
127
155
 
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',
156
+ $this->assertSame(
157
+ 9,
158
+ $safecss_filter_attr_allow_css_mock_action->get_call_count(),
159
+ '"safecss_filter_attr_allow_css" filters were not applied to CSS declaration values.'
136
160
  );
137
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
138
161
 
139
162
  $this->assertSame(
140
- ' color: red;
141
- border-top-left-radius: 99px;
142
- text-decoration: underline;',
143
- $css_declarations->get_declarations_string( true, 2 )
163
+ '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");',
164
+ $css_declarations_string,
165
+ 'Unsafe values were not removed'
144
166
  );
145
167
  }
146
168
 
147
169
  /**
148
- * Should escape values and run the CSS through safecss_filter_attr.
170
+ * Tests that CSS declarations are compiled into a CSS declarations block string.
171
+ *
172
+ * @covers ::get_declarations_string
173
+ *
174
+ * @dataProvider data_should_compile_css_declarations_to_css_declarations_string
175
+ *
176
+ * @param string $expected The expected declarations block string.
177
+ * @param bool $should_prettify Optional. Whether to pretty the string. Default false.
178
+ * @param int $indent_count Optional. The number of tab indents. Default false.
149
179
  */
150
- public function test_remove_unsafe_properties_and_values() {
180
+ public function test_should_compile_css_declarations_to_css_declarations_string( $expected, $should_prettify = false, $indent_count = 0 ) {
151
181
  $input_declarations = array(
152
- 'color' => 'url("https://wordpress.org")',
153
- 'font-size' => '<red/>',
154
- 'margin-right' => '10em',
155
- 'padding' => '</style>',
156
- 'potato' => 'uppercase',
182
+ 'color' => 'red',
183
+ 'border-top-left-radius' => '99px',
184
+ 'text-decoration' => 'underline',
157
185
  );
158
186
  $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
159
187
 
160
188
  $this->assertSame(
161
- 'margin-right:10em;',
162
- $css_declarations->get_declarations_string()
189
+ $expected,
190
+ $css_declarations->get_declarations_string( $should_prettify, $indent_count )
163
191
  );
164
192
  }
165
193
 
166
194
  /**
167
- * Should allow calc, clamp, min, max, and minmax CSS functions.
195
+ * Data provider for test_should_compile_css_declarations_to_css_declarations_string().
196
+ *
197
+ * @return array
168
198
  */
169
- public function test_allow_particular_css_functions() {
170
- $input_declarations = array(
171
- 'background' => 'var(--wp--preset--color--primary, 10px)', // Simple var().
172
- 'font-size' => 'clamp(36.00rem, calc(32.00rem + 10.00vw), 40.00rem)', // Nested clamp().
173
- 'width' => 'min(150vw, 100px)',
174
- 'min-width' => 'max(150vw, 100px)',
175
- 'max-width' => 'minmax(400px, 50%)',
176
- 'padding' => 'calc(80px * -1)',
177
- 'background-image' => 'url("https://wordpress.org")',
178
- 'line-height' => 'url("https://wordpress.org")',
179
- 'margin' => 'illegalfunction(30px)',
180
- );
181
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
182
-
183
- $this->assertSame(
184
- '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");',
185
- $css_declarations->get_declarations_string()
199
+ public function data_should_compile_css_declarations_to_css_declarations_string() {
200
+ return array(
201
+ 'unprettified, no indent' => array(
202
+ 'expected' => 'color:red;border-top-left-radius:99px;text-decoration:underline;',
203
+ ),
204
+ 'unprettified, one indent' => array(
205
+ 'expected' => 'color:red;border-top-left-radius:99px;text-decoration:underline;',
206
+ 'should_prettify' => false,
207
+ 'indent_count' => 1,
208
+ ),
209
+ 'prettified, no indent' => array(
210
+ 'expected' => 'color: red; border-top-left-radius: 99px; text-decoration: underline;',
211
+ 'should_prettify' => true,
212
+ ),
213
+ 'prettified, one indent' => array(
214
+ 'expected' => "\tcolor: red;\n\tborder-top-left-radius: 99px;\n\ttext-decoration: underline;",
215
+ 'should_prettify' => true,
216
+ 'indent_count' => 1,
217
+ ),
218
+ 'prettified, two indents' => array(
219
+ 'expected' => "\t\tcolor: red;\n\t\tborder-top-left-radius: 99px;\n\t\ttext-decoration: underline;",
220
+ 'should_prettify' => true,
221
+ 'indent_count' => 2,
222
+ ),
186
223
  );
187
224
  }
188
225
 
189
226
  /**
190
- * Should remove a declaration
227
+ * Tests removing a single declaration.
228
+ *
229
+ * @covers ::remove_declaration
191
230
  */
192
- public function test_remove_declaration() {
231
+ public function test_should_remove_single_declaration() {
193
232
  $input_declarations = array(
194
233
  'color' => 'tomato',
195
234
  'margin' => '10em 10em 20em 1px',
@@ -199,20 +238,25 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
199
238
 
200
239
  $this->assertSame(
201
240
  'color:tomato;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
202
- $css_declarations->get_declarations_string()
241
+ $css_declarations->get_declarations_string(),
242
+ 'CSS declarations string does not match the values of `$declarations` passed to the constructor.'
203
243
  );
204
244
 
205
245
  $css_declarations->remove_declaration( 'color' );
246
+
206
247
  $this->assertSame(
207
248
  'margin:10em 10em 20em 1px;font-family:Happy Font serif;',
208
- $css_declarations->get_declarations_string()
249
+ $css_declarations->get_declarations_string(),
250
+ 'Output after removing "color" declaration via `remove_declaration()` does not match expectations'
209
251
  );
210
252
  }
211
253
 
212
254
  /**
213
- * Should remove declarations
255
+ * Tests that multiple declarations are removed.
256
+ *
257
+ * @covers ::remove_declarations
214
258
  */
215
- public function test_remove_declarations() {
259
+ public function test_should_remove_multiple_declarations() {
216
260
  $input_declarations = array(
217
261
  'color' => 'cucumber',
218
262
  'margin' => '10em 10em 20em 1px',
@@ -222,13 +266,16 @@ class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
222
266
 
223
267
  $this->assertSame(
224
268
  'color:cucumber;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
225
- $css_declarations->get_declarations_string()
269
+ $css_declarations->get_declarations_string(),
270
+ 'CSS declarations string does not match the values of `$declarations` passed to the constructor.'
226
271
  );
227
272
 
228
273
  $css_declarations->remove_declarations( array( 'color', 'margin' ) );
274
+
229
275
  $this->assertSame(
230
276
  'font-family:Happy Font serif;',
231
- $css_declarations->get_declarations_string()
277
+ $css_declarations->get_declarations_string(),
278
+ 'Output after removing "color" and "margin" declarations via `remove_declarations()` does not match expectations'
232
279
  );
233
280
  }
234
281
  }
@@ -6,17 +6,30 @@
6
6
  * @subpackage style-engine
7
7
  */
8
8
 
9
- require __DIR__ . '/../class-wp-style-engine-css-rule.php';
10
- require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
9
+ // Check for the existence of Style Engine classes and methods.
10
+ // Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
11
+ // Testing new features from the Gutenberg package may require
12
+ // testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
13
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
14
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
15
+ }
16
+
17
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
18
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
19
+ }
11
20
 
12
21
  /**
13
- * Tests for registering, storing and generating CSS declarations.
22
+ * Tests for registering, storing and generating CSS rules.
23
+ *
24
+ * @coversDefaultClass WP_Style_Engine_CSS_Rule
14
25
  */
15
26
  class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
16
27
  /**
17
- * Should set declarations on instantiation.
28
+ * Tests that declarations are set on instantiation.
29
+ *
30
+ * @covers ::__construct
18
31
  */
19
- public function test_instantiate_with_selector_and_rules() {
32
+ public function test_should_instantiate_with_selector_and_rules() {
20
33
  $selector = '.law-and-order';
21
34
  $input_declarations = array(
22
35
  'margin-top' => '10px',
@@ -25,16 +38,20 @@ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
25
38
  $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
26
39
  $css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
27
40
 
28
- $this->assertSame( $selector, $css_rule->get_selector() );
41
+ $this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );
29
42
 
30
43
  $expected = "$selector{{$css_declarations->get_declarations_string()}}";
31
- $this->assertSame( $expected, $css_rule->get_css() );
44
+
45
+ $this->assertSame( $expected, $css_rule->get_css(), 'Value returned by get_css() does not match expected declarations string.' );
32
46
  }
33
47
 
34
48
  /**
35
- * Test dedupe declaration properties.
49
+ * Tests that declaration properties are deduplicated.
50
+ *
51
+ * @covers ::add_declarations
52
+ * @covers ::get_css
36
53
  */
37
- public function test_dedupe_properties_in_rules() {
54
+ public function test_should_dedupe_properties_in_rules() {
38
55
  $selector = '.taggart';
39
56
  $first_declaration = array(
40
57
  'font-size' => '2rem',
@@ -50,9 +67,12 @@ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
50
67
  }
51
68
 
52
69
  /**
53
- * Should add declarations.
70
+ * Tests that declarations can be added to existing rules.
71
+ *
72
+ * @covers ::add_declarations
73
+ * @covers ::get_css
54
74
  */
55
- public function test_add_declarations() {
75
+ public function test_should_add_declarations_to_existing_rules() {
56
76
  // Declarations using a WP_Style_Engine_CSS_Declarations object.
57
77
  $some_css_declarations = new WP_Style_Engine_CSS_Declarations( array( 'margin-top' => '10px' ) );
58
78
  // Declarations using a property => value array.
@@ -61,27 +81,32 @@ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
61
81
  $css_rule->add_declarations( $some_more_css_declarations );
62
82
 
63
83
  $expected = '.hill-street-blues{margin-top:10px;font-size:1rem;}';
84
+
64
85
  $this->assertSame( $expected, $css_rule->get_css() );
65
86
  }
66
87
 
67
88
  /**
68
- * Should set selector.
89
+ * Tests setting a selector to a rule.
90
+ *
91
+ * @covers ::set_selector
69
92
  */
70
- public function test_set_selector() {
93
+ public function test_should_set_selector() {
71
94
  $selector = '.taggart';
72
95
  $css_rule = new WP_Style_Engine_CSS_Rule( $selector );
73
96
 
74
- $this->assertSame( $selector, $css_rule->get_selector() );
97
+ $this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );
75
98
 
76
99
  $css_rule->set_selector( '.law-and-order' );
77
100
 
78
- $this->assertSame( '.law-and-order', $css_rule->get_selector() );
101
+ $this->assertSame( '.law-and-order', $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to set_selector().' );
79
102
  }
80
103
 
81
104
  /**
82
- * Should generate CSS rules.
105
+ * Tests generating a CSS rule string.
106
+ *
107
+ * @covers ::get_css
83
108
  */
84
- public function test_get_css() {
109
+ public function test_should_generate_css_rule_string() {
85
110
  $selector = '.chips';
86
111
  $input_declarations = array(
87
112
  'margin-top' => '10px',
@@ -95,9 +120,11 @@ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
95
120
  }
96
121
 
97
122
  /**
98
- * Should return empty string with no declarations.
123
+ * Tests that an empty string will be returned where there are no declarations in a CSS rule.
124
+ *
125
+ * @covers ::get_css
99
126
  */
100
- public function test_get_css_no_declarations() {
127
+ public function test_should_return_empty_string_with_no_declarations() {
101
128
  $selector = '.holmes';
102
129
  $input_declarations = array();
103
130
  $css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
@@ -107,9 +134,11 @@ class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
107
134
  }
108
135
 
109
136
  /**
110
- * Should generate prettified CSS rules.
137
+ * Tests that CSS rules are prettified.
138
+ *
139
+ * @covers ::get_css
111
140
  */
112
- public function test_get_prettified_css() {
141
+ public function test_should_prettify_css_rule_output() {
113
142
  $selector = '.baptiste';
114
143
  $input_declarations = array(
115
144
  'margin-left' => '0',