public function mimemail_compress::compress in Mime Mail 6
Same name and namespace in other branches
- 7 modules/mimemail_compress/mimemail_compress.inc \mimemail_compress::compress()
File
- modules/mimemail_compress/mimemail_compress.inc, line 52
- Converts CSS styles into inline style attributes.
Class
- mimemail_compress
- Compress HTML and CSS into combined message.
Code
public function compress() {
if (!class_exists('DOMDocument', FALSE)) {
return $this->html;
}
$body = $this->html;
if (count($this->unprocessable_tags)) {
$unprocessable_tags = implode('|', $this->unprocessable_tags);
$body = preg_replace("/<({$unprocessable_tags})[^>]*>/i", '', $body);
}
$err = error_reporting(0);
$doc = new DOMDocument();
if (function_exists('mb_convert_encoding')) {
$body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
$doc->encoding = "UTF-8";
}
$doc->strictErrorChecking = FALSE;
$doc->formatOutput = TRUE;
$doc
->loadHTML($body);
$doc
->normalizeDocument();
$xpath = new DOMXPath($doc);
$css = preg_replace('/\\/\\*.*\\*\\//sU', '', $this->css);
preg_match_all('/^\\s*([^{]+){([^}]+)}/mis', $css, $matches);
$all_selectors = array();
foreach ($matches[1] as $key => $selector_string) {
if (!strlen(trim($matches[2][$key]))) {
continue;
}
$selectors = explode(',', $selector_string);
foreach ($selectors as $selector) {
if (strpos($selector, ':') !== FALSE) {
continue;
}
$all_selectors[] = array(
'selector' => $selector,
'attributes' => $matches[2][$key],
'index' => $key,
);
}
}
usort($all_selectors, array(
'self',
'sort_selector_precedence',
));
$nodes = @$xpath
->query('//*[@style]');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$style = preg_replace('/[A-z\\-]+(?=\\:)/Se', "drupal_strtolower('\\0')", $node
->getAttribute('style'));
$all_selectors[] = array(
'selector' => $this
->calculateXPath($node),
'attributes' => $style,
);
}
}
foreach ($all_selectors as $value) {
$nodes = $xpath
->query($this
->css_to_xpath(trim($value['selector'])));
foreach ($nodes as $node) {
if ($node
->hasAttribute('style')) {
$old_style = $this
->css_style_to_array($node
->getAttribute('style'));
$new_style = $this
->css_style_to_array($value['attributes']);
$compressed = array_merge($old_style, $new_style);
$style = '';
foreach ($compressed as $k => $v) {
$style .= drupal_strtolower($k) . ':' . $v . ';';
}
}
else {
$style = trim($value['attributes']);
}
$node
->setAttribute('style', $style);
$float = preg_match('/float:(left|right)/', $style, $matches);
if ($float) {
$node
->setAttribute('align', $matches[1]);
$node
->setAttribute('vspace', 5);
$node
->setAttribute('hspace', 5);
}
}
}
$nodes = $xpath
->query('//*[contains(translate(@style," ",""), "display:none")]');
foreach ($nodes as $node) {
$node->parentNode
->removeChild($node);
}
if (variable_get('mimemail_preserve_class', 0) == FALSE) {
$nodes = $xpath
->query('//*[@class]');
foreach ($nodes as $node) {
$node
->removeAttribute('class');
}
}
error_reporting($err);
return $doc
->saveHTML();
}