I've been looking at writing a simple CSS inliner as the others available online either use a front end, jquery library or have masses of includes that need composer to manage.
I've been creating XPath queries by applying rules to the CSS selectors and the rules are working when manually checking them on chrome.
Code:
$new_document = new DOMDocument();
$new_document->loadHTML( $page );
$xpa_document = new DOMXpath($new_document);
foreach( $css['selector'] as $key => $path){
$node = $xpa_document->query( $xpath );
$item = $node->item(0);
$styles = $item->getAttribute( 'style' );
$styles .= $stylemap['styles'];
$item->setAttribute('style', $styles );
$new_document->saveHTML();
}
I'm getting problems returning the node to manipulate it with DOMDocument. The line $item = $node->item(0);
causes a crash during the script and checking for empty() seems to have no effect.
Is there a way to load the node found in XPATH into a DOMDocument
nodelist?
Try to wrap the XPath query result in a foreach loop, this should avoid the problem
foreach( $css['selector'] as $key => $path){
$entries = $xpa_document->query( $xpath );
foreach ($entries as $entry) {
$styles = $entry->getAttribute( 'style' );
$styles .= $stylemap['styles'];
$entry->setAttribute('style', $styles );
}
$new_document->saveHTML();
}