diff --git a/includes/sanitizers/class-amp-style-sanitizer.php b/includes/sanitizers/class-amp-style-sanitizer.php
index 7628aab3ce8..8fb66ed460a 100644
--- a/includes/sanitizers/class-amp-style-sanitizer.php
+++ b/includes/sanitizers/class-amp-style-sanitizer.php
@@ -28,7 +28,6 @@ private function collect_styles_recursive( $node ) {
if ( $style ) {
$style = $this->process_style( $style );
-
if ( ! empty( $style ) ) {
$class_name = $this->generate_class_name( $style );
$new_class = trim( $class . ' ' . $class_name );
@@ -58,30 +57,47 @@ private function process_style( $string ) {
}
// Normalize order
- $arr = array_map( 'trim', explode( ';', $string ) );
- sort( $arr );
+ $styles = array_map( 'trim', explode( ';', $string ) );
+ sort( $styles );
+
+ $processed_styles = array();
- // Normalize whitespace
- foreach ( $arr as $index => $rule ) {
+ // Normalize whitespace and filter rules
+ foreach ( $styles as $index => $rule ) {
$arr2 = array_map( 'trim', explode( ':', $rule, 2 ) );
- if ( 2 === count( $arr2 ) ) {
- $arr[ $index ] = $arr2[0] . ':' . $arr2[1];
+ if ( 2 !== count( $arr2 ) ) {
+ continue;
}
+
+ list( $property, $value ) = $this->filter_style( $arr2[0], $arr2[1] );
+ if ( empty( $property ) || empty( $value ) ) {
+ continue;
+ }
+
+ $processed_styles[ $index ] = $property . ':' . $value;
}
+ return $processed_styles;
+ }
+
+ private function filter_style( $property, $value ) {
// Handle overflow rule
// https://www.ampproject.org/docs/reference/spec.html#properties
- if ( $overflow = preg_grep( '/^overflow.*(auto|scroll)$/', $arr ) ) {
- foreach( $overflow as $index => $rule ) {
- unset( $arr[ $index ] );
- }
+ if ( 0 === strpos( $property, 'overflow' )
+ && ( false !== strpos( $value, 'auto' ) || false !== strpos( $value, 'scroll' ) )
+ ) {
+ return false;
+ }
+
+ if ( 'width' === $property ) {
+ $property = 'max-width';
}
- return $arr;
+ return array( $property, $value );
}
private function generate_class_name( $data ) {
$string = maybe_serialize( $data );
return 'amp-wp-inline-' . md5( $string );
}
-}
\ No newline at end of file
+}
diff --git a/tests/test-amp-style-sanitizer.php b/tests/test-amp-style-sanitizer.php
index aa6763516b9..c00c80b45af 100644
--- a/tests/test-amp-style-sanitizer.php
+++ b/tests/test-amp-style-sanitizer.php
@@ -30,12 +30,22 @@ public function get_data() {
),
'span_two_styles_reversed' => array(
- 'This is green.',
- 'This is green.',
+ 'This is green.',
+ 'This is green.',
array(
- '.amp-wp-inline-c83b149f3e9c3426a6eab43e21ac2fba' => array(
+ '.amp-wp-inline-58550689c128f3d396444313296e4c47' => array(
+ 'background-color:#000',
'color:#00ff00',
- 'width:350px',
+ ),
+ ),
+ ),
+
+ 'width_to_max-width' => array(
+ '',
+ '',
+ array(
+ '.amp-wp-inline-2676cd1bfa7e8feb4f0e0e8086ae9ce4' => array(
+ 'max-width:300px',
),
),
),
@@ -64,6 +74,17 @@ public function get_data() {
),
),
),
+
+ 'existing_class_attribute' => array(
+ '',
+ '',
+ array(
+ '.amp-wp-inline-3be9b2f79873ad78941ba2b3c03025a3' => array(
+ 'background:#000',
+ ),
+ )
+
+ ),
);
}
@@ -83,4 +104,4 @@ public function test_sanitizer( $source, $expected_content, $expected_stylesheet
$stylesheet = $sanitizer->get_styles();
$this->assertEquals( $expected_stylesheet, $stylesheet );
}
-}
\ No newline at end of file
+}