Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Table::add_class_to_table_element
Add a CSS class to the table element.
Method of the class: Table{}
No Hooks.
Returns
String. Table content with class added.
Usage
// private - for code of main (parent) class only $result = $this->add_class_to_table_element( $table_content, $class_name ): string;
- $table_content(string) (required)
- Table HTML content.
- $class_name(string) (required)
- CSS class to add.
Table::add_class_to_table_element() Table::add class to table element code WC 10.4.3
private function add_class_to_table_element( string $table_content, string $class_name ): string {
// Validate class name to prevent XSS.
if ( ! preg_match( '/^[a-zA-Z0-9\-_]+$/', $class_name ) ) {
return $table_content;
}
$html = new \WP_HTML_Tag_Processor( $table_content );
if ( $html->next_tag( array( 'tag_name' => 'TABLE' ) ) ) {
$existing_class = (string) ( $html->get_attribute( 'class' ) ?? '' );
$existing_class = trim( $existing_class );
// Only add if not already present.
if ( false === strpos( $existing_class, $class_name ) ) {
$new_class = $existing_class ? $existing_class . ' ' . $class_name : $class_name;
$html->set_attribute( 'class', $new_class );
}
return $html->get_updated_html();
}
return $table_content;
}