Posted on

SOLVED: CSS Inline Error DOMXPath Invalid expression Emogrifier

Emogrifier tool makes it easy to manage CSS when there are many email templates.

How to debug CSS inlining error when using Emogrifier tool

When this error occurs:
The error pops when converting styles from a CSS file to the inline style attribute of the applicable tags with Emogrifier tool.

A few style declarations especially some wild card CSS selectors are not supported in the version 0.1.1 of emogrifier.
In Magento2 the error is been reported somewhere at line number 269.

Looking in the code, it creates xpath from the CSS selectors. So the error is generated while parsing some specific CSS selectors.
The code statement is

$nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector']));

Changing the statement to
$nodesMatchingCssSelectors  =  $xpath->query($this->translateCssToXpath(preg_replace('/[\[\^\*\]]*/',  '', $value['selector'])));
solves the CSS inlining error as it ignores the wildcard CSS selectors.

Make sure you are not using any wildcard CSS selector for defining styles of your emails.

Improved product sorting extension for Magento 2 new, best sellers/popular, top rated, most reviewed products.