Skip to content

Commit 6911ebe

Browse files
fix (dynamic breakpoints): added better support for dynamically inserted content
If there are multiple passes of the dynamic breakpoint, replacing breakpoints should still now work
1 parent ce727ce commit 6911ebe

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/dynamic-breakpoints.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -184,43 +184,35 @@ public function adjust_breakpoints( $css ) {
184184
// Handle CSS optimized values where there are no spaces between the
185185
// colon and the value and if there is a space between the colon and
186186
// the value.
187-
if ( ! empty( $new_tablet ) &&
188-
( strpos( $css, 'width:1024px)' ) !== false || strpos( $css, 'width:1023px)' ) !== false ||
189-
strpos( $css, 'width: 1024px)' ) !== false || strpos( $css, 'width: 1023px)' ) !== false )
190-
) {
191-
192-
// Check if the style generated contains new breakpoints
193-
$has_new_tablet = strpos( $css, 'width: ' . $new_tablet . 'px)' ) !== false ||
194-
strpos( $css, 'width:' . $new_tablet . 'px)' ) !== false;
195-
$has_new_tablet_minus_1 = strpos( $css, 'width: ' . ( $new_tablet - 1 ) . 'px)' ) !== false ||
196-
strpos( $css, 'width:' . ( $new_tablet - 1 ) . 'px)' ) !== false;
197-
198-
if ( ! $has_new_tablet ) {
199-
$css = preg_replace( "/(@media[^{]+)width:\s*1024px/", "$1width:" . $new_tablet . "px", $css );
187+
if ( ! empty( $new_tablet ) ) {
188+
// Check if the style generated contains old breakpoints that we need to replace.
189+
$has_old_tablet = strpos( $css, 'width: 1024px)' ) !== false ||
190+
strpos( $css, 'width:1024px)' ) !== false;
191+
$has_old_tablet_minus_1 = strpos( $css, 'width: 1023px)' ) !== false ||
192+
strpos( $css, 'width:1023px)' ) !== false;
193+
194+
if ( $has_old_tablet ) {
195+
$css = preg_replace( "/(@media[^{]+)width:\s*1024px/m", "$1width:" . $new_tablet . "px", $css );
200196
}
201-
202-
if ( ! $has_new_tablet_minus_1 ) {
203-
$css = preg_replace( "/(@media[^{]+)width:\s*1023px/", "$1width:" . ( $new_tablet - 1 ) . "px", $css );
197+
if ( $has_old_tablet_minus_1 ) {
198+
$css = preg_replace( "/(@media[^{]+)width:\s*1023px/m", "$1width:" . ( $new_tablet - 1 ) . "px", $css );
204199
}
205200
}
206201

207202
// Mobile version
208-
if ( ! empty( $new_mobile ) &&
209-
( strpos( $css, 'width:768px)' ) !== false || strpos( $css, 'width:767px)' ) !== false ||
210-
strpos( $css, 'width: 768px)' ) !== false || strpos( $css, 'width: 767px)' ) !== false )
211-
) {
212-
213-
$has_new_mobile = strpos( $css, 'width:' . $new_mobile . 'px)' ) !== false ||
214-
strpos( $css, 'width: ' . $new_mobile . 'px)' ) !== false;
215-
$has_new_mobile_minus_1 = strpos( $css, 'width:' . ( $new_mobile - 1 ) . 'px)' ) !== false ||
216-
strpos( $css, 'width: ' . ( $new_mobile - 1 ) . 'px)' ) !== false;
217-
218-
if ( ! $has_new_mobile ) {
219-
$css = preg_replace( "/(@media[^{]+)width:\s*768px/", "$1width:" . $new_mobile . "px", $css );
220-
}
203+
if ( ! empty( $new_mobile ) ) {
221204

222-
if ( ! $has_new_mobile_minus_1 ) {
223-
$css = preg_replace( "/(@media[^{]+)width:\s*767px/", "$1width:" . ( $new_mobile - 1 ) . "px", $css );
205+
// Check if the style generated contains old breakpoints that we need to replace.
206+
$has_old_mobile = strpos( $css, 'width: 768px)' ) !== false ||
207+
strpos( $css, 'width:768px)' ) !== false;
208+
$has_old_mobile_minus_1 = strpos( $css, 'width: 767px)' ) !== false ||
209+
strpos( $css, 'width:767px)' ) !== false;
210+
211+
if ( $has_old_mobile ) {
212+
$css = preg_replace( "/(@media[^{]+)width:\s*768px/m", "$1width:" . $new_mobile . "px", $css );
213+
}
214+
if ( $has_old_mobile_minus_1 ) {
215+
$css = preg_replace( "/(@media[^{]+)width:\s*767px/m", "$1width:" . ( $new_mobile - 1 ) . "px", $css );
224216
}
225217
}
226218

0 commit comments

Comments
 (0)