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

Allow removing CSS declarations from the WP_Style_Engine_CSS_Declarations object #42428

Merged
merged 2 commits into from
Jul 14, 2022
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
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()
);
}
}