Skip to content

Commit

Permalink
Merge pull request #65 from marconn/fix-css-inline-err
Browse files Browse the repository at this point in the history
Fix content removal on css inline cleanup
  • Loading branch information
kenntchan authored Feb 5, 2024
2 parents 3a16643 + ef2c629 commit 64433a7
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions library/Kima/Html/CssToInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,79 @@
*/
namespace Kima\Html;

use Kima\Error;
use DOMDocument;
use DOMXPath;
use Kima\Error;

/**
* HTML CSS to Inline library
* Based on https://github.com/tijsverkoyen/CssToInlineStyles
*/
class CssToInline
{

/**
* Error messages
*/
const ERROR_NO_HTML = 'No HTML was provided';
public const ERROR_NO_HTML = 'No HTML was provided';

/**
* Original styles
*/
const ORIGINAL_STYLES = 'data-original-styles';
public const ORIGINAL_STYLES = 'data-original-styles';

/**
* The HTML to process
*
* @var string
*/
private $html;

/**
* The css
*
* @var string
*/
private $css;

/**
* The css rules
*
* @var array
*/
private $css_rules;

/**
* Should the generated HTML be cleaned
*
* @var bool
*/
private $cleanup = true;

/**
* The encoding to use
*
* @var string
*/
private $encoding = 'UTF-8';

/**
* Include the media queries to the inlined styles
*
* @var bool
*/
private $include_media_queries = false;

/**
* Should the tag of styles by skip when cleaned
*
* @var bool
*/
private $skip_style_tag = false;

/**
* Creates a CssToInline instance
* Optionally sets the html and css to use
*
* @param string $html The HTML to process.
* @param string $css The CSS to use.
*/
Expand All @@ -94,6 +101,7 @@ public function skip_style_tag()

/**
* Set HTML to process
*
* @param string $html The HTML to process.
*/
public function set_html($html)
Expand All @@ -103,6 +111,7 @@ public function set_html($html)

/**
* Set CSS to use
*
* @param string $css The CSS to use.
*/
public function set_css($css)
Expand All @@ -121,6 +130,7 @@ public function cleanup()

/**
* Set the encoding to use with the DOMDocument
*
* @param string $encoding
*/
public function set_encoding($encoding)
Expand All @@ -138,6 +148,7 @@ public function include_media_queries()

/**
* Convert HTML/CSS to HTML inline style
*
* @return string
*/
public function convert()
Expand Down Expand Up @@ -230,7 +241,10 @@ public function convert()

/**
* Gets the style properties for an atribute
*
* @param DOMAtrr
* @param mixed $attribute
*
* @return array
*/
private function get_properties($attribute)
Expand Down Expand Up @@ -266,6 +280,7 @@ private function get_properties($attribute)

/**
* Sets the properties to the defined element
*
* @param array $original_properties
* @param array $properties
* @param DOMELement $element
Expand Down Expand Up @@ -298,8 +313,10 @@ private function set_properties($original_properties, $properties, &$element)

/**
* Convert a CSS-selector into an xpath-query
*
* @param string $selector The CSS-selector.
*
* @return string
* @param string $selector The CSS-selector.
*/
private function build_xpath_query($selector)
{
Expand Down Expand Up @@ -362,6 +379,7 @@ private function build_xpath_query($selector)

/**
* Sets the original styles in the html to the DOMElement
*
* @param DOMElement $element
*/
private function set_original_styles(&$element)
Expand All @@ -385,20 +403,20 @@ private function set_original_styles(&$element)
/**
* Cleanup the generated HTML
*
* @param string $html The HTML to cleanup.
* @param string $html The HTML to cleanup.
*
* @return string
*/
private function cleanup_html($html)
{
// remove classes
$html = preg_replace('/(\s)+class="(.*)"(\s)+/U', ' ', $html);
$html = preg_replace('/(\s)+class="(.*)"(\s)*/U', ' ', $html);

// remove IDs
$html = preg_replace('/(\s)+id="(.*)"(\s)+/U', ' ', $html);
$html = preg_replace('/(\s)+id="(.*)"(\s)*/U', ' ', $html);

// remove style tags
if(!$this->skip_style_tag) {
if (!$this->skip_style_tag) {
$html = preg_replace('|<style(.*)>(.*)</style>|isU', '', $html);
}

Expand All @@ -409,6 +427,7 @@ private function cleanup_html($html)

/**
* Get the encoding to use
*
* @return string
*/
private function get_encoding()
Expand All @@ -418,8 +437,6 @@ private function get_encoding()

/**
* Process the loaded CSS
*
* @return void
*/
private function process_css()
{
Expand Down Expand Up @@ -489,7 +506,9 @@ private function process_css()

/**
* Cleans the css string
* @param string $css
*
* @param string $css
*
* @return string
*/
private function clean_css($css)
Expand All @@ -511,7 +530,9 @@ private function clean_css($css)

/**
* Process the CSS-properties
* @param string $property_string The CSS-properties.
*
* @param string $property_string The CSS-properties.
*
* @return array
*/
private function process_properties($property_string)
Expand All @@ -527,16 +548,17 @@ private function process_properties($property_string)
$chunks = explode(':', $property, 2);

// validate
if(!isset($chunks[1])) continue;
if (!isset($chunks[1])) {
continue;
}

// cleanup
$chunks[0] = trim($chunks[0]);
$chunks[1] = trim($chunks[1]);

// add to pairs array
if (!isset($pairs[$chunks[0]])
|| !in_array($chunks[1], $pairs[$chunks[0]]))
{
|| !in_array($chunks[1], $pairs[$chunks[0]])) {
$pairs[$chunks[0]][] = $chunks[1];
}
}
Expand All @@ -551,8 +573,9 @@ private function process_properties($property_string)
/**
* Calculate the specifity for the CSS-selector
*
* @param string $selector The selector to calculate the specifity for.
*
* @return int
* @param string $selector The selector to calculate the specifity for.
*/
private function get_specifity($selector)
{
Expand All @@ -572,7 +595,7 @@ private function get_specifity($selector)
$specifity += 100;
}
// classes are more important than a tag, but less important then an ID
else if ((strstr($chunk, '.'))) {
elseif ((strstr($chunk, '.'))) {
$specifity += 10;
}
// anything else isn't that important
Expand All @@ -587,8 +610,10 @@ private function get_specifity($selector)

/**
* Sort an array on the specifity element
* @param array $e1 The first element.
* @param array $e2 The second element.
*
* @param array $e1 The first element.
* @param array $e2 The second element.
*
* @return int
*/
private function sort_on_specifity($e1, $e2)
Expand All @@ -606,5 +631,4 @@ private function sort_on_specifity($e1, $e2)
// fallback
return 0;
}

}

0 comments on commit 64433a7

Please sign in to comment.