Skip to content

Commit

Permalink
Convert width attribute of col tag to equivalent CSS rule
Browse files Browse the repository at this point in the history
  • Loading branch information
amedina committed Apr 9, 2018
1 parent 3a6df82 commit 11696b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ public function sanitize() {
$elements[] = $element;
}

// If 'width' attribute is present for 'col' tag, convert to proper CSS rule.
foreach ( $this->dom->getElementsByTagName( 'col' ) as $col ) {
$width_attr = $col->getAttribute( 'width' );
if ( ! empty( $width_attr ) && ! strpos( $width_attr, '*' ) ) {
strpos( $width_attr, '%' )
? $col->setAttribute( 'style', 'width: ' . $width_attr )
: $col->setAttribute( 'style', 'width: ' . $width_attr . 'px' );
$col->removeAttribute( 'width' );
}
}

/**
* Element.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/test-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ public function get_body_style_attribute_data() {
':root:not(#_):not(#_):not(#_):not(#_):not(#_):not(#_):not(#_):not(#_):not(#_):not(#_):not(#_) .amp-wp-ab79d9e{color:purple;}',
),
),

'col_with_width_attribute' => array(
'<table><colgroup><col width="253"/></colgroup></table>',
'<table><colgroup><col class="amp-wp-cbcb5c2"></colgroup></table>',
array(
':root:not(#_):not(#_):not(#_):not(#_):not(#_) .amp-wp-cbcb5c2{max-width:253px;}',
),
),

'col_with_percent_width_attribute' => array(
'<table><colgroup><col width="50%"/></colgroup></table>',
'<table><colgroup><col class="amp-wp-cd7753e"></colgroup></table>',
array(
':root:not(#_):not(#_):not(#_):not(#_):not(#_) .amp-wp-cd7753e{max-width:50%;}',
),
),

'col_with_star_width_attribute' => array(
'<table><colgroup><col width="0*"/></colgroup></table>',
'<table><colgroup><col width="0*"></colgroup></table>',
array(),
),
);
}

Expand Down

0 comments on commit 11696b1

Please sign in to comment.