A DOM manipulation library for PHP, designed for the Aegis Framework.
composer require aegis/domFirst, require Composer's autoloader:
require_once __DIR__ . '/vendor/autoload.php';Then, you can use the library's classes.
The DOM class provides utilities for creating, parsing, and manipulating DOMDocument objects.
Example: Modifying HTML
use Aegis\Dom\DOM;
$html = '<h1>Hello, World!</h1>';
// Create a DOMDocument from an HTML string
$dom = DOM::create( $html );
// Get the h1 element
$h1 = DOM::get_element( 'h1', $dom );
if ( $h1 ) {
// Add a class to the h1 element
DOM::add_classes( $h1, ['heading', 'main-title'] );
// Add inline styles
DOM::add_styles( $h1, ['color' => 'blue', 'font-size' => '24px'] );
}
// To get the modified HTML, you need to save it from the document element
echo $dom->saveHTML();The CSS class provides helpers for working with CSS rules.
Example: Converting CSS arrays to strings
use Aegis\Dom\CSS;
$styles = [
'color' => 'red',
'font-size' => '16px',
'background' => '#eee',
];
$css_string = CSS::array_to_string( $styles );
// Output: color:red;font-size:16px;background:#eee;
echo $css_string;Example: Converting CSS strings to arrays
use Aegis\Dom\CSS;
$css_string = 'color:blue; font-weight:bold;';
$styles_array = CSS::string_to_array( $css_string );
// Output:
// [
// 'color' => 'blue',
// 'font-weight' => 'bold',
// ]
print_r( $styles_array );The JS class helps format inline JavaScript.
Example: Formatting inline JS
use Aegis\Dom\JS;
$inline_js = ' alert( "Hello World" ); ';
$formatted_js = JS::format_inline_js( $inline_js );
// Output: alert( 'Hello World' )
echo $formatted_js;Contributions are welcome! Please submit a pull request or create an issue on the GitHub repository.