mimemail_compress.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * @file
  4. * Converts CSS styles into inline style attributes.
  5. *
  6. * Code based on Emogrifier by Pelago Design (http://www.pelagodesign.com).
  7. */
  8. /**
  9. * Separate CSS from HTML for processing
  10. */
  11. function mimemail_compress_clean_message($message) {
  12. $parts = array();
  13. preg_match('|(<style[^>]+)>(.*)</style>|mis', $message, $matches);
  14. if (isset($matches[0]) && isset($matches[2])) {
  15. $css = str_replace('<!--', '', $matches[2]);
  16. $css = str_replace('-->', '', $css);
  17. $css = preg_replace('|\{|', "\n{\n", $css);
  18. $css = preg_replace('|\}|', "\n}\n", $css);
  19. $html = str_replace($matches[0], '', $message);
  20. $parts = array('html' => $html, 'css' => $css);
  21. }
  22. return $parts;
  23. }
  24. /**
  25. * Compress HTML and CSS into combined message
  26. */
  27. class mimemail_compress {
  28. private $html = '';
  29. private $css = '';
  30. private $unprocessable_tags = array('wbr');
  31. public function __construct($html = '', $css = '') {
  32. $this->html = $html;
  33. $this->css = $css;
  34. }
  35. // There are some HTML tags that DOMDocument cannot process,
  36. // and will throw an error if it encounters them.
  37. // These functions allow you to add/remove them if necessary.
  38. // It only strips them from the code (does not remove actual nodes).
  39. public function add_unprocessable_tag($tag) {
  40. $this->unprocessable_tags[] = $tag;
  41. }
  42. public function remove_unprocessable_tag($tag) {
  43. if (($key = array_search($tag, $this->unprocessable_tags)) !== FALSE) {
  44. unset($this->unprocessableHTMLTags[$key]);
  45. }
  46. }
  47. public function compress() {
  48. if (!class_exists('DOMDocument', FALSE)) {
  49. return $this->html;
  50. }
  51. $body = $this->html;
  52. // Process the CSS here, turning the CSS style blocks into inline CSS.
  53. if (count($this->unprocessable_tags)) {
  54. $unprocessable_tags = implode('|', $this->unprocessable_tags);
  55. $body = preg_replace("/<($unprocessable_tags)[^>]*>/i", '', $body);
  56. }
  57. $err = error_reporting(0);
  58. $doc = new DOMDocument();
  59. // Try to set character encoding.
  60. if (function_exists('mb_convert_encoding')) {
  61. $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
  62. $doc->encoding= "UTF-8";
  63. }
  64. $doc->strictErrorChecking = FALSE;
  65. $doc->formatOutput = TRUE;
  66. $doc->loadHTML($body);
  67. $doc->normalizeDocument();
  68. $xpath = new DOMXPath($doc);
  69. // Get rid of comments.
  70. $css = preg_replace('/\/\*.*\*\//sU', '', $this->css);
  71. // Process the CSS file for selectors and definitions.
  72. preg_match_all('/^\s*([^{]+){([^}]+)}/mis', $css, $matches);
  73. $all_selectors = array();
  74. foreach ($matches[1] as $key => $selector_string) {
  75. // If there is a blank definition, skip.
  76. if (!strlen(trim($matches[2][$key]))) continue;
  77. // Else split by commas and duplicate attributes so we can sort by selector precedence.
  78. $selectors = explode(',', $selector_string);
  79. foreach ($selectors as $selector) {
  80. // Don't process pseudo-classes.
  81. if (strpos($selector, ':') !== FALSE) continue;
  82. $all_selectors[] = array(
  83. 'selector' => $selector,
  84. 'attributes' => $matches[2][$key],
  85. 'index' => $key, // Keep track of where it appears in the file, since order is important.
  86. );
  87. }
  88. }
  89. // Now sort the selectors by precedence.
  90. usort($all_selectors, array('self', 'sort_selector_precedence'));
  91. // Before we begin processing the CSS file, parse the document for inline
  92. // styles and append the normalized properties (i.e., 'display: none'
  93. // instead of 'DISPLAY: none') as selectors with full paths (highest
  94. // precedence), so they override any file-based selectors.
  95. $nodes = @$xpath->query('//*[@style]');
  96. if ($nodes->length > 0) {
  97. foreach ($nodes as $node) {
  98. $style = preg_replace_callback('/[A-z\-]+(?=\:)/S', create_function('$matches', 'return strtolower($matches[0]);'), $node->getAttribute('style'));
  99. $all_selectors[] = array(
  100. 'selector' => $this->calculateXPath($node),
  101. 'attributes' => $style,
  102. );
  103. }
  104. }
  105. foreach ($all_selectors as $value) {
  106. // Query the body for the xpath selector.
  107. $nodes = $xpath->query($this->css_to_xpath(trim($value['selector'])));
  108. foreach ($nodes as $node) {
  109. // If it has a style attribute, get it, process it, and append (overwrite) new stuff.
  110. if ($node->hasAttribute('style')) {
  111. // Break it up into an associative array.
  112. $old_style = $this->css_style_to_array($node->getAttribute('style'));
  113. $new_style = $this->css_style_to_array($value['attributes']);
  114. // New styles overwrite the old styles (not technically accurate, but close enough).
  115. $compressed = array_merge($old_style, $new_style);
  116. $style = '';
  117. foreach ($compressed as $k => $v) {
  118. $style .= (drupal_strtolower($k) . ':' . $v . ';');
  119. }
  120. }
  121. else {
  122. // Otherwise create a new style.
  123. $style = trim($value['attributes']);
  124. }
  125. $node->setAttribute('style', $style);
  126. // Convert float to align for images.
  127. $float = preg_match('/float:(left|right)/', $style, $matches);
  128. if ($node->nodeName == 'img' && $float) {
  129. $node->setAttribute('align', $matches[1]);
  130. $node->setAttribute('vspace', 5);
  131. $node->setAttribute('hspace', 5);
  132. }
  133. }
  134. }
  135. // This removes styles from your email that contain display:none. You could comment these out if you want.
  136. $nodes = $xpath->query('//*[contains(translate(@style," ",""), "display:none")]');
  137. foreach ($nodes as $node) {
  138. $node->parentNode->removeChild($node);
  139. }
  140. if (variable_get('mimemail_preserve_class', 0) == FALSE) {
  141. $nodes = $xpath->query('//*[@class]');
  142. foreach ($nodes as $node) {
  143. $node->removeAttribute('class');
  144. }
  145. }
  146. error_reporting($err);
  147. return $doc->saveHTML();
  148. }
  149. private static function sort_selector_precedence($a, $b) {
  150. $precedenceA = self::get_selector_precedence($a['selector']);
  151. $precedenceB = self::get_selector_precedence($b['selector']);
  152. // We want these sorted ascendingly so selectors with lesser precedence get processed first and selectors with greater precedence get sorted last.
  153. return ($precedenceA == $precedenceB) ? ($a['index'] < $b['index'] ? -1 : 1) : ($precedenceA < $precedenceB ? -1 : 1);
  154. }
  155. private static function get_selector_precedence($selector) {
  156. $precedence = 0;
  157. $value = 100;
  158. // Ids: worth 100, classes: worth 10, elements: worth 1.
  159. $search = array('\#', '\.', '');
  160. foreach ($search as $s) {
  161. if (trim($selector == '')) break;
  162. $num = 0;
  163. $selector = preg_replace('/' . $s . '\w+/', '', $selector, -1, $num);
  164. $precedence += ($value * $num);
  165. $value /= 10;
  166. }
  167. return $precedence;
  168. }
  169. /**
  170. * Replace callback function that matches ID attributes.
  171. */
  172. private static function replace_id_attributes($m) {
  173. return (strlen($m[1]) ? $m[1] : '*') . '[@id="' . $m[2] . '"]';
  174. }
  175. /**
  176. * Replace callback function that matches class attributes.
  177. */
  178. private static function replace_class_attributes($m) {
  179. return (strlen($m[1]) ? $m[1] : '*') .
  180. '[contains(concat(" ",normalize-space(@class)," "),concat(" ","' .
  181. implode('"," "))][contains(concat(" ",normalize-space(@class)," "),concat(" ","', explode('.', substr($m[2], 1))) .
  182. '"," "))]';
  183. }
  184. /**
  185. * Right now we only support CSS 1 selectors, but include CSS2/3 selectors are fully possible.
  186. *
  187. * @see http://plasmasturm.org/log/444
  188. */
  189. private function css_to_xpath($selector) {
  190. if (drupal_substr($selector, 0, 1) == '/') {
  191. // Already an XPath expression.
  192. return $selector;
  193. }
  194. // Returns an Xpath selector.
  195. $search = array(
  196. '/\s+>\s+/', // Matches any F element that is a child of an element E.
  197. '/(\w+)\s+\+\s+(\w+)/', // Matches any F element that is a child of an element E.
  198. '/\s+/', // Matches any F element that is a descendant of an E element.
  199. '/(\w)\[(\w+)\]/', // Matches element with attribute.
  200. '/(\w)\[(\w+)\=[\'"]?(\w+)[\'"]?\]/', // Matches element with EXACT attribute.
  201. );
  202. $replace = array(
  203. '/',
  204. '\\1/following-sibling::*[1]/self::\\2',
  205. '//',
  206. '\\1[@\\2]',
  207. '\\1[@\\2="\\3"]',
  208. );
  209. $result = preg_replace($search, $replace, trim($selector));
  210. $result = preg_replace_callback('/(\w+)?\#([\w\-]+)/', 'mimemail_compress::replace_id_attributes', $result);
  211. $result = preg_replace_callback('/(\w+|\*)?((\.[\w\-]+)+)/', 'mimemail_compress::replace_class_attributes', $result);
  212. return '//' . $result;
  213. }
  214. private function css_style_to_array($style) {
  215. $definitions = explode(';', $style);
  216. $css_styles = array();
  217. foreach ($definitions as $def) {
  218. if (empty($def) || strpos($def, ':') === FALSE) continue;
  219. list($key, $value) = explode(':', $def, 2);
  220. if (empty($key) || empty($value)) continue;
  221. $css_styles[trim($key)] = trim($value);
  222. }
  223. return $css_styles;
  224. }
  225. /**
  226. * Get the full path to a DOM node.
  227. *
  228. * @param DOMNode $node
  229. * The node to analyze.
  230. *
  231. * @return string
  232. * The full xpath to a DOM node.
  233. *
  234. * @see http://stackoverflow.com/questions/2643533/php-getting-xpath-of-a-domnode
  235. */
  236. function calculateXPath(DOMNode $node) {
  237. $xpath = '';
  238. $q = new DOMXPath($node->ownerDocument);
  239. do {
  240. $position = 1 + $q->query('preceding-sibling::*[name()="' . $node->nodeName . '"]', $node)->length;
  241. $xpath = '/' . $node->nodeName . '[' . $position . ']' . $xpath;
  242. $node = $node->parentNode;
  243. }
  244. while (!$node instanceof DOMDocument);
  245. return $xpath;
  246. }
  247. }