Exemplo n.º 1
0
 /**
  * Prepares everything and inserts inline styles into html
  * @param string $html represents html document
  * @return string
  */
 public function render($html)
 {
     $this->dom = new \DOMDocument();
     $this->dom->loadHTML($html);
     $this->finder = new \DOMXPath($this->dom);
     $this->css = $this->getCSS();
     foreach ($this->css->getAllRuleSets() as $ruleSet) {
         $selector = $ruleSet->getSelector();
         foreach ($this->finder->evaluate(CssSelector::toXPath($selector[0])) as $node) {
             if ($node->getAttribute('style')) {
                 $node->setAttribute('style', $node->getAttribute('style') . implode(' ', $ruleSet->getRules()));
             } else {
                 $node->setAttribute('style', implode(' ', $ruleSet->getRules()));
             }
         }
     }
     return $this->dom->saveHTML();
 }
Exemplo n.º 2
0
 function rtl()
 {
     $this->remove_direction_neutral_rules();
     foreach ($this->document->getAllRuleSets() as $rule_sets) {
         /* @var $rule_sets CSS\RuleSet\RuleSet */
         foreach ($rule_sets->getRules() as $rule) {
             /* @var $rule CSS\Rule\Rule */
             $neutral = TRUE;
             /* @var $value CSS\Value\RuleValueList */
             $value = $rule->getValue();
             if ($value instanceof CSS\Value\RuleValueList) {
                 if ($rule->getRule() == "background" || $rule->getRule() == "background-position") {
                     $neutral = $this->rtl_background($value);
                 } elseif ($rule->getRule() == "box-shadow") {
                     $neutral = $this->rtl_box_shadow($value);
                 } elseif ($rule->getRule() == "border-radius") {
                     $neutral = $this->rtl_border_radius_components($value);
                 } else {
                     $components = $value->getListComponents();
                     if (count($components) == 4) {
                         $neutral = $this->rtl_four_components($value);
                     }
                 }
             }
             /**
              * Replace ltr, left to rtl, right both in rule and value
              * it must not be replaced in the selectors themeselves
              */
             if (is_int(strpos($rule->getRule(), "left")) || is_int(strpos($rule->getRule(), "right"))) {
                 $neutral = FALSE;
                 $swaped_rule = str_replace(array("left", "right", "swap"), array("swap", "left", "right"), $rule->getRule());
                 $current_rules = $rule_sets->getRules($swaped_rule);
                 $reset_rule = empty($current_rules) ? clone $rule : FALSE;
                 $rule->setRule($swaped_rule);
                 // reset the defualt rule to auto if not exists already
                 if ($reset_rule) {
                     $reset_value = "initial";
                     if (is_int(strpos($rule->getRule(), "border-"))) {
                         $reset_value = "transparent";
                     }
                     $reset_rule->setValue($reset_value);
                     $rule_sets->addRule($reset_rule);
                 }
             }
             if (is_int(strpos($rule->getValue(), "left")) || is_int(strpos($rule->getValue(), "right"))) {
                 $neutral = FALSE;
                 $rule->setValue(str_replace(array("left", "right", "swap"), array("swap", "left", "right"), $rule->getValue()));
             }
             if (is_int(strpos($rule->getValue(), "left")) || is_int(strpos($rule->getValue(), "right"))) {
                 $neutral = FALSE;
                 $rule->setValue(str_replace(array("ltr", "rtl", "swap"), array("swap", "ltr", "rtl"), $rule->getValue()));
             }
             if ($neutral) {
                 $rule_sets->removeRule($rule);
             }
         }
         if (empty($rule_sets->getRules())) {
             $this->document->remove($rule_sets);
         }
     }
     $this->remove_empty_rulelist();
 }