@@ -202,18 +202,14 @@ public function remove_declaration(): bool {
202202 return false ;
203203 }
204204
205- if ( '' === trim ( substr ( $ this ->style , 0 , $ declaration ['leading_start ' ] ), self ::WHITESPACE ) ) {
206- $ remove_start = $ declaration ['leading_start ' ];
207- $ remove_end = $ declaration ['trailing_end ' ];
208- } else {
209- $ remove_start = $ declaration ['leading_start ' ];
210- $ remove_end = $ declaration ['after ' ];
211- }
205+ $ remove_start = $ this ->get_offset_before_preceding_whitespace ( $ declaration ['start ' ] );
206+ $ remove_end = $ this ->get_offset_after_following_whitespace ( $ declaration ['after ' ] );
207+ $ replacement = ( $ remove_start > 0 && $ remove_end < strlen ( $ this ->style ) ) ? ' ' : '' ;
212208
213209 $ this ->queue_lexical_update (
214210 $ remove_start ,
215211 $ remove_end - $ remove_start ,
216- '' ,
212+ $ replacement ,
217213 $ this ->current_declaration
218214 );
219215
@@ -681,6 +677,36 @@ private function is_ignored_token( array $token ): bool {
681677 );
682678 }
683679
680+ /**
681+ * Gets the byte offset before contiguous whitespace ending at an offset.
682+ *
683+ * @param int $offset Byte offset.
684+ * @return int Offset before preceding whitespace.
685+ */
686+ private function get_offset_before_preceding_whitespace ( int $ offset ): int {
687+ while ( $ offset > 0 && false !== strpos ( self ::WHITESPACE , $ this ->style [ $ offset - 1 ] ) ) {
688+ --$ offset ;
689+ }
690+
691+ return $ offset ;
692+ }
693+
694+ /**
695+ * Gets the byte offset after contiguous whitespace beginning at an offset.
696+ *
697+ * @param int $offset Byte offset.
698+ * @return int Offset after following whitespace.
699+ */
700+ private function get_offset_after_following_whitespace ( int $ offset ): int {
701+ $ length = strlen ( $ this ->style );
702+
703+ while ( $ offset < $ length && false !== strpos ( self ::WHITESPACE , $ this ->style [ $ offset ] ) ) {
704+ ++$ offset ;
705+ }
706+
707+ return $ offset ;
708+ }
709+
684710 /**
685711 * Gets the current declaration metadata.
686712 *
0 commit comments