Repository navigation
Expand file tree
/
Copy pathclass-wp-html-style-attribute-processor.php
More file actions
1680 lines (1458 loc) · 51.8 KB
/
Copy pathclass-wp-html-style-attribute-processor.php
File metadata and controls
1680 lines (1458 loc) · 51.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* HTML API: WP_HTML_Style_Attribute_Processor class
*
* @package WordPress
* @subpackage HTML-API
* @since 7.3.0
*/
/**
* Core class used to inspect and modify CSS declarations in an HTML style attribute.
*
* The processor operates on the decoded CSS text value of a style attribute:
* the CSS declaration-list contents of a declaration block, excluding the
* delimiting braces. It does not parse HTML attribute syntax, decode character
* references, or operate on raw HTML markup.
*
* Values read from {@see WP_HTML_Tag_Processor::get_attribute()} can be passed
* to the constructor, and values returned by
* {@see WP_HTML_Style_Attribute_Processor::get_updated_style()} can be passed
* back to {@see WP_HTML_Tag_Processor::set_attribute()}. A missing (`null`) or
* boolean (`true`) style attribute gives an empty style.
*
* Unlike associative-array based style helpers, this processor preserves the
* declaration list model of CSS. Multiple declarations with the same property
* name remain separate entries and can be inspected or changed individually.
*
* ## Setting a property
*
* To set a property, remove its declarations in one `next_declaration( $name )`
* loop, then call append_declaration().
*
* Example:
*
* ```php interactive
* $styles = new WP_HTML_Style_Attribute_Processor( 'background-image: url(a.png); color: red' );
* while ( $styles->next_declaration( 'background-image' ) ) {
* $styles->remove_declaration();
* }
* $styles->append_declaration( 'background-image', WP_CSS_Builder::url( 'https://example.com/b.png' ) );
* echo $styles->get_updated_style();
* // Outputs: color: red; background-image: url("https://example.com/b.png");
* ```
*
* The cursor moves forward only. After one `next_declaration( $name )` loop
* ends, a second one finds nothing. To set several properties, remove all of
* their declarations in one `next_declaration()` loop that compares
* get_property_name() with each name, then append each declaration.
*
* Example:
*
* ```php interactive
* $values = array(
* 'color' => 'green',
* 'width' => '2px',
* );
* $styles = new WP_HTML_Style_Attribute_Processor( 'color: red; width: 1px; COLOR: blue; height: 2px' );
* while ( $styles->next_declaration() ) {
* if ( isset( $values[ $styles->get_property_name() ] ) ) {
* $styles->remove_declaration();
* }
* }
* foreach ( $values as $name => $value ) {
* $styles->append_declaration( $name, $value );
* }
* echo $styles->get_updated_style();
* // Outputs: height: 2px; color: green; width: 2px;
* ```
*
* The appended declaration is last, so it overrides earlier declarations that
* set the same thing, shorthands included:
*
* - In `background-image: url(a); background: red`, setting `background-image`
* to `url(b)` gives `background: red; background-image: url(b);`, which shows
* image b.
* - In `background-color: blue`, setting `background` to `url(c)` gives
* `background-color: blue; background: url(c);`. The shorthand resets the
* color.
*
* set_value() keeps the declaration where it is, so a later shorthand still
* overrides it.
*
* @since 7.3.0
*
* @see WP_CSS_Builder
*/
class WP_HTML_Style_Attribute_Processor {
/**
* CSS whitespace characters.
*
* @since 7.3.0
* @ignore
*
* @var string
*/
private const WHITESPACE = " \t\n\r\f";
/**
* Decoded style attribute value.
*
* @since 7.3.0
* @ignore
*
* @var string
*/
private $style;
/**
* CSS tokens of the style attribute value.
*
* @since 7.3.0
* @ignore
*
* @var WP_CSS_Token[]
*/
private $tokens = array();
/**
* Parsed declarations.
*
* Each declaration records byte offsets into the style text. The offsets
* of the second declaration in this style are:
*
* color: red; width: 1px !important; top: 0
* ╰────────────────────╯ start to after
* ╰─╯ value_start to value_end
* ╰────────╯ important_start to important_end
*
* `value_tokens` is the range of token indexes, end exclusive, of the
* value. The `important_*` offsets are null when the declaration has no
* `!important` priority.
*
* @since 7.3.0
* @ignore
*
* @var array<int, array{name:string, start:int, after:int, value_start:int, value_end:int, value_tokens:array{int,int}, important:bool, important_start:int|null, important_end:int|null}>
*/
private $declarations = array();
/**
* Whether the style attribute value has been parsed.
*
* @since 7.3.0
* @ignore
*
* @var bool
*/
private $parsed = false;
/**
* Index of the current declaration, or -1 before the first declaration.
*
* @since 7.3.0
* @ignore
*
* @var int
*/
private $current_declaration = -1;
/**
* Whether the current declaration was removed and no new declaration has been selected.
*
* @since 7.3.0
* @ignore
*
* @var bool
*/
private $current_declaration_removed = false;
/**
* Constructor.
*
* `true` and `null` give an empty style.
*
* @since 7.3.0
*
* @param string|true|null $decoded_css_text Decoded CSS text value from a style attribute.
*/
public function __construct( $decoded_css_text ) {
if ( true === $decoded_css_text || null === $decoded_css_text ) {
$decoded_css_text = '';
}
if ( ! is_string( $decoded_css_text ) ) {
throw new TypeError( __METHOD__ . '(): Argument #1 ($decoded_css_text) must be of type string|true|null' );
}
$this->style = $decoded_css_text;
}
/**
* Moves to the next declaration in the style attribute value.
*
* When a property name is provided, normal CSS properties are matched
* ASCII-case-insensitively while custom properties are matched exactly.
*
* @since 7.3.0
*
* @param string|null $property_name Optional declaration property name to match.
* @return bool Whether a matching declaration was found.
*/
public function next_declaration( ?string $property_name = null ): bool {
$this->ensure_parsed();
for ( $i = $this->current_declaration + 1; $i < count( $this->declarations ); $i++ ) {
if (
null === $property_name ||
$this->matches_property_name( $this->declarations[ $i ]['name'], $property_name )
) {
$this->current_declaration = $i;
$this->current_declaration_removed = false;
return true;
}
}
$this->current_declaration = count( $this->declarations );
$this->current_declaration_removed = false;
return false;
}
/**
* Gets the current declaration's property name.
*
* @since 7.3.0
*
* @return string|null Property name, or null when not on a declaration.
*/
public function get_property_name(): ?string {
$declaration = $this->get_current_declaration();
return null === $declaration ? null : $declaration['name'];
}
/**
* Indicates whether the current declaration has an !important priority.
*
* @since 7.3.0
*
* @return bool|null Whether the current declaration has an !important priority,
* or null when not on a declaration.
*/
public function is_important(): ?bool {
$declaration = $this->get_current_declaration();
return null === $declaration ? null : $declaration['important'];
}
/**
* Gets the current declaration's value as a list of CSS tokens.
*
* The list covers the declaration value only. Leading and trailing
* whitespace and comments and the `!important` priority are excluded;
* whitespace and comments between value tokens are included. The list is
* empty when the declaration has no value tokens, as in `color:;`.
*
* color: red blue !important;
* ╰───┬───╯
* The value tokens are `red`, a whitespace token and `blue`.
*
* Token offsets index the current style text, which
* {@see WP_HTML_Style_Attribute_Processor::get_updated_style()} returns.
* Before any mutation that is the text passed to the constructor.
*
* @since 7.3.0
*
* @return WP_CSS_Token[]|null Value tokens, or null when not on a declaration.
*/
public function get_value_tokens(): ?array {
$declaration = $this->get_current_declaration();
if ( null === $declaration ) {
return null;
}
list( $start, $end ) = $declaration['value_tokens'];
return array_slice( $this->tokens, $start, $end - $start );
}
/**
* Serializes the current declaration.
*
* The property name is escaped as an identifier, and the value is written
* from its tokens: comments are dropped, each whitespace run becomes one
* space, and blocks left open at the end of the style are closed. The
* result is `name: value;` or `name: value !important;`, as in CSSOM, and
* CSS reads it as the same declaration.
*
* @since 7.3.0
*
* @see WP_CSS_Token_Serializer::serialize()
*
* @return string|null Declaration text, or null when not on a declaration.
*/
public function serialize_declaration(): ?string {
$declaration = $this->get_current_declaration();
if ( null === $declaration ) {
return null;
}
return $this->format_declaration(
WP_CSS_Token_Serializer::escape_ident( $declaration['name'] ),
WP_CSS_Token_Serializer::serialize( $this->get_value_tokens() ),
$declaration['important']
);
}
/**
* Sets the value of the current declaration.
*
* The declaration is written as serialize_declaration() writes it: the
* name is escaped as an identifier and the value is written from its
* tokens.
*
* @since 7.3.0
*
* @param string $value CSS declaration value, without the property name.
* @param bool|null $important Optional. Whether the declaration should be important.
* Defaults to preserving the current priority.
* @return bool Whether the current declaration was updated.
*/
public function set_value( string $value, ?bool $important = null ): bool {
$declaration = $this->get_current_declaration();
if ( null === $declaration ) {
return false;
}
if ( ! $this->is_valid_declaration_value( $value ) ) {
return false;
}
$is_important = null === $important ? $declaration['important'] : $important;
$text = $this->format_declaration( WP_CSS_Token_Serializer::escape_ident( $declaration['name'] ), $this->serialize_value( $value ), $is_important );
$parsed_value = $this->get_parsed_declaration_value( $text );
if ( null === $parsed_value ) {
return false;
}
$updated = $this->verify_current_declaration_replacement(
$declaration,
$declaration['start'],
$declaration['after'] - $declaration['start'],
$text,
$is_important,
$parsed_value
);
if ( null === $updated ) {
return false;
}
$this->apply_update( $updated, $this->current_declaration );
return true;
}
/**
* Sets the !important priority of the current declaration.
*
* @since 7.3.0
*
* @param bool $important Whether the declaration should be important.
* @return bool Whether the current declaration priority is set as requested.
*/
public function set_important( bool $important ): bool {
$declaration = $this->get_current_declaration();
if ( null === $declaration ) {
return false;
}
if ( $important === $declaration['important'] ) {
return true;
}
if ( $important ) {
if ( $declaration['value_start'] >= $declaration['value_end'] ) {
return false;
}
$insert_text = ' !important';
$updated = $this->verify_current_declaration_replacement(
$declaration,
$declaration['value_end'],
0,
$insert_text,
true
);
if ( null === $updated ) {
$eof_repair = strlen( $this->style ) === $declaration['after']
? $this->get_eof_repair( $declaration['after'] )
: null;
$current_value = substr( $this->style, $declaration['value_start'], $declaration['after'] - $declaration['value_start'] );
if ( null === $eof_repair || '' === $eof_repair ) {
return false;
}
$updated = $this->verify_current_declaration_replacement(
$declaration,
$declaration['after'],
0,
$eof_repair . $insert_text,
true,
trim( $current_value . $eof_repair, self::WHITESPACE )
);
}
} else {
if ( null === $declaration['important_start'] || null === $declaration['important_end'] ) {
return false;
}
$updated = $this->verify_current_declaration_replacement(
$declaration,
$declaration['important_start'],
$declaration['important_end'] - $declaration['important_start'],
'',
false
);
}
if ( null === $updated ) {
return false;
}
$this->apply_update( $updated, $this->current_declaration );
return true;
}
/**
* Removes the current declaration from the style attribute value.
*
* Only the current declaration is removed. Other declarations with the same
* property name remain in place.
*
* Each call parses the whole style text again, so removing most of the
* declarations of a long style one at a time is slow. To keep some
* declarations, join the {@see WP_HTML_Style_Attribute_Processor::serialize_declaration()}
* output of the ones to keep instead.
*
* @since 7.3.0
*
* @return bool Whether the current declaration was removed.
*/
public function remove_declaration(): bool {
$declaration = $this->get_current_declaration();
if ( null === $declaration ) {
return false;
}
$remove_start = $this->get_offset_before_preceding_whitespace( $declaration['start'] );
$remove_end = $this->get_offset_after_following_whitespace( $declaration['after'] );
$replacement = ( $remove_start > 0 && $remove_end < strlen( $this->style ) ) ? ' ' : '';
$updated = $this->verify_declaration_removal( $this->current_declaration, $remove_start, $remove_end - $remove_start, $replacement );
if ( null === $updated ) {
return false;
}
$this->apply_update( $updated, $this->current_declaration - 1, true );
return true;
}
/**
* Appends a declaration to the style attribute value.
*
* Appending does not remove or replace existing declarations with the same
* property name. The declaration is written as serialize_declaration()
* writes it: the name is escaped as an identifier and the value is
* written from its tokens.
*
* @since 7.3.0
*
* @param string $property_name CSS property name.
* @param string $value CSS declaration value, without the property name.
* @param bool $important Optional. Whether the declaration should be important. Default false.
* @return bool Whether the declaration was appended.
*/
public function append_declaration( string $property_name, string $value, bool $important = false ): bool {
if (
'' === trim( $value, self::WHITESPACE ) ||
! $this->is_valid_property_name( $property_name ) ||
! $this->is_valid_declaration_value( $value )
) {
return false;
}
$this->ensure_parsed();
$old_declaration_count = count( $this->declarations );
$insert_at = $this->get_append_offset();
$eof_repair = $this->get_eof_repair( $insert_at );
if ( null === $eof_repair ) {
return false;
}
$separator = $this->get_append_separator( $insert_at );
$name = $this->is_custom_property_name( $property_name ) ? $property_name : strtolower( $property_name );
$declaration_text = $this->format_declaration( WP_CSS_Token_Serializer::escape_ident( $name ), $this->serialize_value( $value ), $important );
$parsed_value = $this->get_parsed_declaration_value( $declaration_text );
if ( null === $parsed_value ) {
return false;
}
$updated = $this->verify_append( $insert_at, $eof_repair, $separator, $declaration_text, $old_declaration_count, $name, $parsed_value, $important );
if ( null === $updated ) {
return false;
}
$append_after_exhausted_cursor = $this->current_declaration >= $old_declaration_count;
$current_after_append = $append_after_exhausted_cursor
? $old_declaration_count - 1
: $this->current_declaration;
$this->apply_update(
$updated,
$current_after_append,
$this->current_declaration_removed || $append_after_exhausted_cursor
);
return true;
}
/**
* Returns the updated decoded CSS text value for the style attribute.
*
* @since 7.3.0
*
* @return string Updated decoded CSS text value for the style attribute.
*/
public function get_updated_style(): string {
return $this->style;
}
/**
* Parses CSS tokens and declarations from the style attribute value.
*
* @since 7.3.0
* @ignore
*/
private function parse(): void {
$this->tokens = array();
$this->declarations = array();
$processor = new WP_CSS_Token_Processor( $this->style );
while ( $processor->next_token() ) {
$this->tokens[] = $processor->get_token();
}
$this->parse_declaration_list();
$this->parsed = true;
}
/**
* Lazily parses the style attribute value when declaration access needs it.
*
* @since 7.3.0
* @ignore
*/
private function ensure_parsed(): void {
if ( $this->parsed ) {
return;
}
$this->parse();
}
/**
* Parses the token list according to the CSS declaration-list shape.
*
* @since 7.3.0
* @ignore
*/
private function parse_declaration_list(): void {
$index = 0;
$count = count( $this->tokens );
while ( $index < $count ) {
$index = $this->skip_ignored_tokens( $index );
while ( $index < $count && WP_CSS_Token_Processor::TOKEN_SEMICOLON === $this->tokens[ $index ]->get_type() ) {
++$index;
$index = $this->skip_ignored_tokens( $index );
}
if ( $index >= $count ) {
break;
}
if ( WP_CSS_Token_Processor::TOKEN_AT_KEYWORD === $this->tokens[ $index ]->get_type() ) {
$index = $this->consume_at_rule( $index );
continue;
}
list( $declaration_end, $after_declaration, $has_semicolon ) = $this->consume_declaration_segment( $index );
$declaration = $this->parse_declaration_segment( $index, $declaration_end, $after_declaration );
if ( null !== $declaration ) {
$this->declarations[] = $declaration;
}
$index = $declaration_end + ( $has_semicolon ? 1 : 0 );
}
}
/**
* Consumes an at-rule and returns the token index after it.
*
* @since 7.3.0
* @ignore
*
* @param int $index At-keyword token index.
* @return int Token index after the at-rule.
*/
private function consume_at_rule( int $index ): int {
$count = count( $this->tokens );
++$index;
while ( $index < $count ) {
$token = $this->tokens[ $index ];
if ( WP_CSS_Token_Processor::TOKEN_SEMICOLON === $token->get_type() ) {
return $index + 1;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACE === $token->get_type() ) {
return $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE );
}
if ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET );
continue;
}
++$index;
}
return $count;
}
/**
* Consumes a simple block.
*
* @since 7.3.0
* @ignore
*
* @param int $index Token index for the opening token.
* @param string $closing_type Expected closing token type.
* @return int Token index after the block.
*/
private function consume_simple_block( int $index, string $closing_type ): int {
$count = count( $this->tokens );
++$index;
while ( $index < $count ) {
$token = $this->tokens[ $index ];
if ( $closing_type === $token->get_type() ) {
return $index + 1;
}
if ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACE === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE );
continue;
}
++$index;
}
return $count;
}
/**
* Consumes a declaration candidate up to the next top-level semicolon or EOF.
*
* @since 7.3.0
* @ignore
*
* @param int $index Starting token index.
* @return array{int,int,bool} End token index, byte offset after candidate, and whether it ended in a semicolon.
*/
private function consume_declaration_segment( int $index ): array {
$count = count( $this->tokens );
while ( $index < $count ) {
$token = $this->tokens[ $index ];
if ( WP_CSS_Token_Processor::TOKEN_SEMICOLON === $token->get_type() ) {
return array( $index, self::get_token_end( $token ), true );
}
if ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACE === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE );
continue;
}
++$index;
}
$after = empty( $this->tokens ) ? strlen( $this->style ) : self::get_token_end( $this->tokens[ $count - 1 ] );
return array( $count, $after, false );
}
/**
* Parses a declaration from a token segment.
*
* @since 7.3.0
* @ignore
*
* @param int $start_index First token index in the segment.
* @param int $end_index Token index after the segment.
* @param int $after Byte offset after the declaration.
* @return array{name:string, start:int, after:int, value_start:int, value_end:int, value_tokens:array{int,int}, important:bool, important_start:int|null, important_end:int|null}|null
*/
private function parse_declaration_segment( int $start_index, int $end_index, int $after ): ?array {
$index = $this->skip_ignored_tokens( $start_index, $end_index );
if ( $index >= $end_index || WP_CSS_Token_Processor::TOKEN_IDENT !== $this->tokens[ $index ]->get_type() ) {
return null;
}
$name_token = $this->tokens[ $index ];
$name = $name_token->get_value();
if ( null === $name ) {
return null;
}
$name = $this->is_custom_property_name( $name ) ? $name : strtolower( $name );
++$index;
$index = $this->skip_ignored_tokens( $index, $end_index );
if ( $index >= $end_index || WP_CSS_Token_Processor::TOKEN_COLON !== $this->tokens[ $index ]->get_type() ) {
return null;
}
$colon_token = $this->tokens[ $index ];
$value_start_index = $this->skip_ignored_tokens( $index + 1, $end_index );
$value_end_index = $this->trim_ignored_tokens( $value_start_index, $end_index );
$important = false;
$important_start = null;
$important_end = null;
$top_level_tokens = $this->get_top_level_non_ignored_token_indexes( $value_start_index, $value_end_index );
$top_level_count = count( $top_level_tokens );
$last_value_index = $top_level_count > 0 ? $top_level_tokens[ $top_level_count - 1 ] : null;
$before_last_index = $top_level_count > 1 ? $top_level_tokens[ $top_level_count - 2 ] : null;
if (
null !== $last_value_index &&
null !== $before_last_index &&
WP_CSS_Token_Processor::TOKEN_IDENT === $this->tokens[ $last_value_index ]->get_type() &&
0 === strcasecmp( 'important', (string) $this->tokens[ $last_value_index ]->get_value() ) &&
WP_CSS_Token_Processor::TOKEN_DELIM === $this->tokens[ $before_last_index ]->get_type() &&
'!' === $this->tokens[ $before_last_index ]->get_value()
) {
$important = true;
$important_start = $this->tokens[ $before_last_index ]->get_start();
$important_end = self::get_token_end( $this->tokens[ $last_value_index ] );
$value_end_index = $this->trim_ignored_tokens( $value_start_index, $before_last_index );
}
if ( $value_start_index >= $value_end_index ) {
$value_start = self::get_token_end( $colon_token );
$value_end = self::get_token_end( $colon_token );
} else {
$value_start = $this->tokens[ $value_start_index ]->get_start();
$value_end = self::get_token_end( $this->tokens[ $value_end_index - 1 ] );
}
if ( ! $this->is_parsed_value_valid( $value_start_index, $value_end_index, $this->is_custom_property_name( $name ) ) ) {
return null;
}
return array(
'name' => $name,
'start' => $name_token->get_start(),
'after' => $after,
'value_start' => $value_start,
'value_end' => $value_end,
'value_tokens' => array( $value_start_index, $value_end_index ),
'important' => $important,
'important_start' => $important_start,
'important_end' => $important_end,
);
}
/**
* Gets top-level non-ignored token indexes from a token range.
*
* @since 7.3.0
* @ignore
*
* @param int $start Start token index.
* @param int $end End token index.
* @return int[] Top-level non-ignored token indexes.
*/
private function get_top_level_non_ignored_token_indexes( int $start, int $end ): array {
$top_level_tokens = array();
$index = $start;
while ( $index < $end ) {
$token = $this->tokens[ $index ];
if ( $this->is_ignored_token( $token ) ) {
++$index;
continue;
}
$top_level_tokens[] = $index;
if ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET );
continue;
}
if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACE === $token->get_type() ) {
$index = $this->consume_simple_block( $index, WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE );
continue;
}
++$index;
}
return $top_level_tokens;
}
/**
* Skips ignored CSS tokens.
*
* @since 7.3.0
* @ignore
*
* @param int $index Token index.
* @param int|null $end Optional token index at which to stop.
* @return int Token index after ignored tokens.
*/
private function skip_ignored_tokens( int $index, ?int $end = null ): int {
$end = null === $end ? count( $this->tokens ) : $end;
while ( $index < $end && $this->is_ignored_token( $this->tokens[ $index ] ) ) {
++$index;
}
return $index;
}
/**
* Trims ignored CSS tokens from the end of a token range.
*
* @since 7.3.0
* @ignore
*
* @param int $start Start token index.
* @param int $end End token index.
* @return int Trimmed end token index.
*/
private function trim_ignored_tokens( int $start, int $end ): int {
while ( $end > $start && $this->is_ignored_token( $this->tokens[ $end - 1 ] ) ) {
--$end;
}
return $end;
}
/**
* Checks whether a token is ignored by declaration-list grammar.
*
* @since 7.3.0
* @ignore
*
* @param WP_CSS_Token $token Token.
* @return bool Whether the token is ignored.
*/
private function is_ignored_token( WP_CSS_Token $token ): bool {
return (
WP_CSS_Token_Processor::TOKEN_WHITESPACE === $token->get_type() ||
WP_CSS_Token_Processor::TOKEN_COMMENT === $token->get_type()
);
}
/**
* Gets the byte offset after a token.
*
* @since 7.3.0
* @ignore
*
* @param WP_CSS_Token $token Token.
* @return int Byte offset after the token.
*/
private static function get_token_end( WP_CSS_Token $token ): int {
return $token->get_start() + $token->get_length();
}
/**
* Gets the byte offset before contiguous whitespace ending at an offset.
*
* @since 7.3.0
* @ignore
*
* @param int $offset Byte offset.
* @return int Offset before preceding whitespace.
*/
private function get_offset_before_preceding_whitespace( int $offset ): int {
while ( $offset > 0 && false !== strpos( self::WHITESPACE, $this->style[ $offset - 1 ] ) ) {
--$offset;
}
return $offset;
}
/**
* Gets the byte offset after contiguous whitespace beginning at an offset.
*
* @since 7.3.0
* @ignore
*
* @param int $offset Byte offset.
* @return int Offset after following whitespace.
*/
private function get_offset_after_following_whitespace( int $offset ): int {
$length = strlen( $this->style );
while ( $offset < $length && false !== strpos( self::WHITESPACE, $this->style[ $offset ] ) ) {
++$offset;
}
return $offset;
}
/**
* Gets the current declaration metadata.
*
* @since 7.3.0
* @ignore
*
* @return array{name:string, start:int, after:int, value_start:int, value_end:int, value_tokens:array{int,int}, important:bool, important_start:int|null, important_end:int|null}|null
*/
private function get_current_declaration(): ?array {
if (
$this->current_declaration_removed ||
$this->current_declaration < 0 ||
! isset( $this->declarations[ $this->current_declaration ] )
) {
return null;
}
return $this->declarations[ $this->current_declaration ];
}
/**
* Parses the style with a replacement and checks that it preserves the current declaration's structure.
*
* @since 7.3.0
* @ignore