Skip to content

Commit

Permalink
Implement css vars
Browse files Browse the repository at this point in the history
May be a dirty hack, but it works well.
  • Loading branch information
tmolitor-stud-tu committed Sep 7, 2023
1 parent 87bea32 commit e7044fb
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Css/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,8 @@ public function set_prop(string $prop, $val, bool $important = false, bool $clea
$prop = self::$_props_alias[$prop];
}

if (!isset(self::$_defaults[$prop])) {
// props beginning with "--" (changed above to "__") are css variables --> always allow them
if (!isset(self::$_defaults[$prop]) && !substr($prop, 0, 2) == '__') {
global $_dompdf_warnings;
$_dompdf_warnings[] = "'$prop' is not a recognized CSS property.";
return;
Expand Down Expand Up @@ -1367,7 +1368,8 @@ public function get_specified(string $prop)
$prop = self::$_props_alias[$prop];
}

if (!isset(self::$_defaults[$prop])) {
// props beginning with "--" (changed in set_prop() to "__") are css variables --> always allow them
if (!isset(self::$_defaults[$prop]) && !substr($prop, 0, 2) == '__') {
throw new Exception("'$prop' is not a recognized CSS property.");
}

Expand Down Expand Up @@ -1508,6 +1510,12 @@ public function __get(string $prop)
*/
protected function compute_prop(string $prop, $val)
{
// handle css var() functions as already computed (late binding)
// TODO: extend to calc() and other css functons?
if(is_string($val) && substr($val, 0, 4) == 'var(') {
return $val;
}

// During style merge, the parent style is not available yet, so
// temporarily use the initial value for `inherit` properties. The
// keyword is properly resolved during inheritance
Expand Down Expand Up @@ -1551,6 +1559,24 @@ protected function computed(string $prop)
$this->_props_computed[$prop] = $computed;
}

// now compute value of css var access (late binding)
if(isset($this->_props_computed[$prop]))
{
$value = $this->_props_computed[$prop];
if(!is_array($value)) {
$value = [$value];
}
foreach($value as &$part) {
if(is_string($part) && substr($part, 0, 4) == 'var(') {
$css_var = str_replace("-", "_", substr(trim($part), 4, -1));
$part = $this->get_specified($css_var) ?? (isset($this->parent_style) ? $this->parent_style->get_specified($css_var) : null);
}
}
if(!is_array($this->_props_computed[$prop])) {
$this->_props_computed[$prop] = $value[0];
}
}

return $this->_props_computed[$prop];
}

Expand Down

0 comments on commit e7044fb

Please sign in to comment.