Skip to content

Commit

Permalink
Preserve inline styles: add routine to process styles before output
Browse files Browse the repository at this point in the history
Filter the styles using `safecss_filter_attr()` to make sure there
aren't any funky properties. Also alphabetize the valid properties so
that variable order doesn't cause unnecessary duplication.
  • Loading branch information
coreymckrill committed Sep 9, 2016
1 parent 36670c4 commit de2a303
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ private function collect_styles_recursive( $node ) {
$class = $node->getAttribute( 'class' );

if ( $style ) {
$style = $this->process_style( $style );

$class_name = $this->generate_class_name( $style );
$new_class = trim( $class . ' ' . $class_name );

Expand All @@ -43,16 +45,30 @@ private function collect_styles_recursive( $node ) {
}
}

private function process_style( $string ) {
// Filter properties
$string = safecss_filter_attr( $string );

// Normalize order
$arr = array_map( 'trim', explode( ';', $string ) );
sort( $arr );

return implode( ";\n", $arr ) . ';';
}

private function generate_class_name( $string ) {
return 'amp-style-' . md5( $string );
return 'amp-inline-style-' . md5( $string );
}

public function append_styles() {
?>

/* Inline Styles */
<?php foreach ( $this->styles as $class_name => $style ) : ?>
.<?php echo $class_name; ?> { <?php echo $style; ?> }
.<?php echo $class_name; ?> {
<?php echo $style; ?>

}
<?php endforeach; ?>

<?php
Expand Down

0 comments on commit de2a303

Please sign in to comment.