Skip to content

Commit

Permalink
Allow removing CSS declarations from the WP_Style_Engine_CSS_Declarat…
Browse files Browse the repository at this point in the history
…ions object (#42428)

* Add remove_declaration(s) methods

* Added tests for remove_declaration(s)

Co-authored-by: ramonjd <ramonjd@gmail.com>
  • Loading branch information
aristath and ramonjd authored Jul 14, 2022
1 parent a979537 commit c8b7177
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/style-engine/class-wp-style-engine-css-declarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function add_declaration( $property, $value ) {
$this->declarations[ $property ] = $value;
}

/**
* Remove a single declaration.
*
* @param string $property The CSS property.
*
* @return void
*/
public function remove_declaration( $property ) {
unset( $this->declarations[ $property ] );
}

/**
* Add multiple declarations.
*
Expand All @@ -80,6 +91,19 @@ public function add_declarations( $declarations ) {
}
}

/**
* Remove multiple declarations.
*
* @param array $declarations An array of properties.
*
* @return void
*/
public function remove_declarations( $declarations = array() ) {
foreach ( $declarations as $property ) {
$this->remove_declaration( $property );
}
}

/**
* Get the declarations array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,50 @@ public function test_remove_unsafe_properties_and_values() {
$css_declarations->get_declarations_string()
);
}

/**
* Should remove a declaration
*/
public function test_remove_declaration() {
$input_declarations = array(
'color' => 'tomato',
'margin' => '10em 10em 20em 1px',
'font-family' => 'Happy Font serif',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );

$this->assertSame(
'color: tomato; margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
$css_declarations->get_declarations_string()
);

$css_declarations->remove_declaration( 'color' );
$this->assertSame(
'margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
$css_declarations->get_declarations_string()
);
}

/**
* Should remove declarations
*/
public function test_remove_declarations() {
$input_declarations = array(
'color' => 'cucumber',
'margin' => '10em 10em 20em 1px',
'font-family' => 'Happy Font serif',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );

$this->assertSame(
'color: cucumber; margin: 10em 10em 20em 1px; font-family: Happy Font serif;',
$css_declarations->get_declarations_string()
);

$css_declarations->remove_declarations( array( 'color', 'margin' ) );
$this->assertSame(
'font-family: Happy Font serif;',
$css_declarations->get_declarations_string()
);
}
}

0 comments on commit c8b7177

Please sign in to comment.