public function css($name = null, $value = '')
{
if (empty($name)) {
return $this->attr('style');
}
// Get any existing CSS.
$css = array();
foreach ($this->matches as $match) {
$style = $match->getAttribute('style');
if (!empty($style)) {
// XXX: Is this sufficient?
$style_array = explode(';', $style);
foreach ($style_array as $item) {
$item = trim($item);
// Skip empty attributes.
if (strlen($item) == 0) {
continue;
}
list($css_att, $css_val) = explode(':', $item, 2);
$css[$css_att] = trim($css_val);
}
}
}
if (is_array($name)) {
// Use array_merge instead of + to preserve order.
$css = array_merge($css, $name);
} else {
$css[$name] = $value;
}
// Collapse CSS into a string.
$format = '%s: %s;';
$css_string = '';
foreach ($css as $n => $v) {
$css_string .= sprintf($format, $n, trim($v));
}
$this->attr('style', $css_string);
return $this;
}