Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style Santiizer: force width => max-width #495

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
}
}
31 changes: 26 additions & 5 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ public function get_data() {
),

'span_two_styles_reversed' => array(
'<span style="width: 350px; color: #00ff00;">This is green.</span>',
'<span class="amp-wp-inline-c83b149f3e9c3426a6eab43e21ac2fba">This is green.</span>',
'<span style="color: #00ff00; background-color: #000; ">This is green.</span>',
'<span class="amp-wp-inline-58550689c128f3d396444313296e4c47">This is green.</span>',
array(
'.amp-wp-inline-c83b149f3e9c3426a6eab43e21ac2fba' => array(
'.amp-wp-inline-58550689c128f3d396444313296e4c47' => array(
'background-color:#000',
'color:#00ff00',
'width:350px',
),
),
),

'width_to_max-width' => array(
'<figure style="width: 300px"></figure>',
'<figure class="amp-wp-inline-2676cd1bfa7e8feb4f0e0e8086ae9ce4"></figure>',
array(
'.amp-wp-inline-2676cd1bfa7e8feb4f0e0e8086ae9ce4' => array(
'max-width:300px',
),
),
),
Expand Down Expand Up @@ -64,6 +74,17 @@ public function get_data() {
),
),
),

'existing_class_attribute' => array(
'<figure class="alignleft" style="background: #000"></figure>',
'<figure class="alignleft amp-wp-inline-3be9b2f79873ad78941ba2b3c03025a3"></figure>',
array(
'.amp-wp-inline-3be9b2f79873ad78941ba2b3c03025a3' => array(
'background:#000',
),
)

),
);
}

Expand All @@ -83,4 +104,4 @@ public function test_sanitizer( $source, $expected_content, $expected_stylesheet
$stylesheet = $sanitizer->get_styles();
$this->assertEquals( $expected_stylesheet, $stylesheet );
}
}
}