26 function smarty_modifier_add_styles($html, $styles, $onlyIfUnstyled=
true) {
27 return strlen($html) > 0 ? DOMUtils::processHtml($html,
function(\DOMDocument $doc) use ($styles, $onlyIfUnstyled) {
28 $xpath = new \DOMXpath($doc);
29 foreach ($styles as $name => $classes) {
30 $elements = $xpath->query(
"//".$name);
31 foreach ($elements as $element) {
32 $classAttr = trim($element->getAttribute(
"class"));
33 if (strlen($classAttr) == 0 || !$onlyIfUnstyled) {
34 $existingClasses = explode(
" ", $element->getAttribute(
"class"));
35 $allClasses = array_unique(array_merge($existingClasses, explode(
" ", $classes)));
36 $element->setAttribute(
"class", join(
" ", $allClasses));