/[drupal]/contributions/modules/mimemail/modules/mimemail_compress/mimemail_compress.inc
ViewVC logotype

Diff of /contributions/modules/mimemail/modules/mimemail_compress/mimemail_compress.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4.2.1, Tue Apr 14 15:17:49 2009 UTC revision 1.4.2.2, Tue Apr 14 23:09:00 2009 UTC
# Line 0  Line 1 
1    <?php // $Id: mimemail_compress.inc,v 1.4 2009/04/14 15:17:49 jerdavis Exp $
2    
3    /**
4     * Code based on emogrifier by Pelago Design and licensed under the MIT license
5     * http://www.pelagodesign.com/sidecar/emogrifier/
6     *
7     * http://www.opensource.org/licenses/mit-license.php
8     *
9     * Copyright (c) 2009 Pelago Design
10     *
11     * Permission is hereby granted, free of charge, to any person obtaining a copy
12     * of this software and associated documentation files (the "Software"), to deal
13     * in the Software without restriction, including without limitation the rights
14     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15     * copies of the Software, and to permit persons to whom the Software is
16     * furnished to do so.
17     */
18    
19    function mimemail_compress_clean_message($message) {
20      $parts = array();
21      preg_match('|(<style[^>]+)>(.*)</style>|mis', $message, $matches);
22      $css  = str_replace('<!--', '', $matches[2]);
23      $css  = str_replace('-->', '', $css);
24      $css = preg_replace('|\{|',"\n{\n", $css);
25      $css = preg_replace('|\}|',"\n}\n", $css);
26      $html = str_replace($matches[0], '', $message);
27      $parts = array('html' => $html, 'css' => $css);
28      return $parts;
29    }
30    
31    class mimemail_compress {
32      private $html = '';
33      private $css  = '';
34    
35      public function mimemail_compress($html = '', $css = '') {
36        $this->html = $html;
37        $this->css  = $css;
38      }
39    
40      public function compress() {
41        error_reporting(0);
42        $doc = new DOMDocument('1.0', 'utf8');
43        $doc->strictErrorChecking = false;
44        $doc->formatOutput = true;
45        $doc->loadHTML($this->html);
46        $doc->normalizeDocument();
47        $xpath = new DOMXPath($doc);
48        $css = preg_replace('/\/\*.*\*\//sU', '', $this->css);
49        preg_match_all('/^\s*([^{]+){([^}]+)}/mis', $css, $matches);
50    
51        foreach ($matches[1] as $key => $selector_string) {
52          if (!strlen(trim($matches[2][$key]))) continue;
53          $selectors = explode(',',$selector_string);
54          foreach ($selectors as $selector) {
55            if (strpos($selector,':') !== false) continue;
56            $nodes = $xpath->query($this->css_to_xpath(trim($selector)));
57            foreach($nodes as $node) {
58              if ($node->hasAttribute('style')) {
59                $style = $node->getAttribute('style');
60                $old_style = $this->css_style_to_array($node->getAttribute('style'));
61                $new_style = $this->css_style_to_array($matches[2][$key]);
62                $compressed = array_merge($old_style,$new_style);
63                $style = '';
64                foreach ($compressed as $k => $v) $style .= ($k . ':' . $v . ';');
65              }
66              else {
67                $style = trim($matches[2][$key]);
68              }
69              $node->setAttribute('style',$style);
70            }
71          }
72        }
73        $nodes = $xpath->query('//*[contains(translate(@style," ",""),"display:none;")]');
74        foreach ($nodes as $node) $node->parentNode->removeChild($node);
75        return $doc->saveHTML();
76      }
77    
78      private function css_to_xpath($selector) {
79        $search = array(
80          '/\s+>\s+/',
81          '/(\w+)\s+\+\s+(\w+)/',
82          '/\s+/',
83          '/(\w+)?\#([\w\-]+)/e',
84          '/(\w+)?\.([\w\-]+)/e',
85        );
86        $replace = array(
87          '/',
88          '\\1/following-sibling::*[1]/self::\\2',
89          '//',
90          "(strlen('\\1') ? '\\1' : '*').'[@id=\"\\2\"]'",
91          "(strlen('\\1') ? '\\1' : '*').'[contains(concat(\" \",@class,\" \"),concat(\" \",\"\\2\",\" \"))]'",
92        );
93        return '//'.preg_replace($search, $replace, trim($selector));
94      }
95    
96      private function css_style_to_array($style) {
97        $definitions = explode(';',$style);
98        $css_styles = array();
99        foreach ($definitions as $def) {
100          list($key,$value) = explode(':',$def);
101          if (empty($key) || empty($value)) continue;
102          $css_styles[trim($key)] = trim($value);
103        }
104        return $css_styles;
105      }
106    }

Legend:
Removed from v.1.4.2.1  
changed lines
  Added in v.1.4.2.2

  ViewVC Help
Powered by ViewVC 1.1.2