Browse Source

only encode utf8 for php5.4

euromark 12 years ago
parent
commit
f57074be13

+ 6 - 0
Lib/InlineCssLib.php

@@ -24,6 +24,7 @@ class InlineCssLib {
 		'xhtmlOutput' => false,
 		'xhtmlOutput' => false,
 		'removeCss' => true,
 		'removeCss' => true,
 		'debug' => false,
 		'debug' => false,
+		'correctUtf8' => false
 	);
 	);
 
 
 	public $settings = array();
 	public $settings = array();
@@ -32,6 +33,8 @@ class InlineCssLib {
 	 * startup
 	 * startup
 	 */
 	 */
 	public function __construct($settings = array()) {
 	public function __construct($settings = array()) {
+		$this->_defaults['correctUtf8'] = version_compare(PHP_VERSION, '5.4.0') >= 0;
+
 		$defaults = am($this->_defaults, (array) Configure::read('InlineCss'));
 		$defaults = am($this->_defaults, (array) Configure::read('InlineCss'));
 		$this->settings = array_merge($defaults, $settings);
 		$this->settings = array_merge($defaults, $settings);
 		if (!method_exists($this, '_process' . ucfirst($this->settings['engine']))) {
 		if (!method_exists($this, '_process' . ucfirst($this->settings['engine']))) {
@@ -87,6 +90,9 @@ class InlineCssLib {
 		if ($this->settings['removeCss']) {
 		if ($this->settings['removeCss']) {
 			$CssToInlineStyles->setStripOriginalStyleTags();
 			$CssToInlineStyles->setStripOriginalStyleTags();
 		}
 		}
+		if ($this->settings['correctUtf8']) {
+			$CssToInlineStyles->setCorrectUtf8();
+		}
 		if ($this->settings['debug']) {
 		if ($this->settings['debug']) {
 			CakeLog::write('css', $html);
 			CakeLog::write('css', $html);
 		}
 		}

+ 2 - 1
Test/Case/Lib/InlineCssLibTest.php

@@ -65,6 +65,8 @@ p.small { font-size: 70%; }
 </html>';
 </html>';
 
 
 	public function testProcessUtf8() {
 	public function testProcessUtf8() {
+		$this->skipIf(version_compare(PHP_VERSION, '5.4.0') < 0, 'UTF8 only works with PHP5.4 and above');
+
 		$html = 'チェック
 		$html = 'チェック
 	<style>
 	<style>
 div#container { margin: 1em auto; }
 div#container { margin: 1em auto; }
@@ -78,7 +80,6 @@ p.small { font-size: 70%; }
 		<p class="small">チェック\'foo\'</p>
 		<p class="small">チェック\'foo\'</p>
 	</div>
 	</div>
 bla';
 bla';
-
 		$res = $this->InlineCss->process($html);
 		$res = $this->InlineCss->process($html);
 		$this->debug($html);
 		$this->debug($html);
 		$this->debug($res);
 		$this->debug($res);

+ 19 - 3
Vendor/CssToInlineStyles/CssToInlineStyles.php

@@ -67,6 +67,8 @@ class CssToInlineStyles
      */
      */
     private $excludeMediaQueries = false;
     private $excludeMediaQueries = false;
 
 
+    private $correctUtf8 = false;
+
     /**
     /**
      * Creates an instance, you could set the HTML and CSS here, or load it
      * Creates an instance, you could set the HTML and CSS here, or load it
      * later.
      * later.
@@ -458,9 +460,12 @@ class CssToInlineStyles
             $html = $document->saveHTML();
             $html = $document->saveHTML();
         }
         }
 
 
-				// Correct scrambled UTF8 chars (&atilde;&#131;...) back to their correct representation.
-        $html = html_entity_decode($html, ENT_XHTML, 'UTF-8');
-				$html = utf8_decode($html);
+        if ($this->correctUtf8) {
+            // Only for >PHP5.4
+            // Correct scrambled UTF8 chars (&atilde;&#131;...) back to their correct representation.
+            $html = html_entity_decode($html, ENT_XHTML, 'UTF-8');
+            $html = utf8_decode($html);
+        }
 
 
         // cleanup the HTML if we need to
         // cleanup the HTML if we need to
         if($this->cleanup) $html = $this->cleanupHTML($html);
         if($this->cleanup) $html = $this->cleanupHTML($html);
@@ -654,6 +659,17 @@ class CssToInlineStyles
         $this->html = (string) $html;
         $this->html = (string) $html;
     }
     }
 
 
+   /**
+     * Set utf8 correction
+     *
+     * @return void
+     * @param  bool $on.
+     */
+    public function setCorrectUtf8($on = true)
+    {
+        $this->correctUtf8 = (bool) $on;
+    }
+
     /**
     /**
      * Set use of inline styles block
      * Set use of inline styles block
      * If this is enabled the class will use the style-block in the HTML.
      * If this is enabled the class will use the style-block in the HTML.