Source for file Style.php
Documentation is available at Style.php
* FluentDOMStyle extends the FluentDOM class with a function to edit
* the style attribute of html tags
* @version $Id: Style.php 431 2010-03-29 20:42:04Z subjective $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
* include the parent class (FluentDOM)
require_once(dirname(__FILE__ ). '/../FluentDOM.php');
* Function to create a new FluentDOMStyleinstance and loads data into it if
* a valid $source is provided.
* @param string $contentType optional, default value 'text/xml'
* @return object FluentDOMStyle
return $result->load($source, $contentType);
* FluentDOMStyle extends the FluentDOM class with a function to edit
* the style attribute of html tags
* Pattern to decode the stlye property string
const STYLE_PATTERN = '((?:^|;)\s*(?P<name>[-\w]+)\s*:\s*(?P<value>[^;]+))';
* get or set CSS values in style attributes
* @param string|array$property
* @param NULL|string|object Closure $value
* @return string|object FluentDOMStyle
public function css($property, $value = NULL) {
//set list of properties to all elements
foreach ($this->_array as $node) {
if ($node instanceof DOMElement) {
foreach ($property as $name => $value) {
if (isset ($options[$name]) && empty($value)) {
} elseif (!empty($value)) {
$options[$name] = $value;
throw new InvalidArgumentException('Invalid css property name: '. $property);
if (empty($styleString) && $node->hasAttribute('style')) {
$node->removeAttribute('style');
} elseif (!empty($styleString)) {
$node->setAttribute('style', $styleString);
//get value from first DOMElement
foreach ($this->_array as $node) {
if ($node instanceof DOMElement) {
if (isset ($options[$property])) {
return $options[$property];
foreach ($this->_array as $index => $node) {
if ($node instanceof DOMElement) {
if (isset ($options[$property])) {
unset ($options[$property]);
$options[$property] = $value;
empty($options[$property]) ? '' : $options[$property]
if (empty($styleString) && $node->hasAttribute('style')) {
$node->removeAttribute('style');
} elseif (!empty($styleString)) {
$node->setAttribute('style', $styleString);
throw new InvalidArgumentException('Invalid css property name: '. $property);
* check if string is an valid css property name
* @param string $propertyName
$pattern = '(^-?(?:[a-z]+-)*(?:[a-z]+)$)D';
* decode style attribute to css properties array
* @param string $styleString
if (!empty($styleString)) {
if (preg_match_all(self::STYLE_PATTERN, $styleString, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (isset ($match['name']) &&
!empty($match['value'])) {
$result[$match['name']] = $match['value'];
* encode css options array for the style string
* @param array $properties
uksort($properties, array($this, '_compareCSSProperties'));
foreach ($properties as $name => $value) {
$result .= ' '. $name. ': '. $value. ';';
* compare to css property names
* by name, browser-prefix, level
* @param string $propertyNameOne
* @param string $propertyNameTwo
$propertyOneLevels = count($propertyOne);
$propertyTwoLevels = count($propertyTwo);
$maxLevels = ($propertyOneLevels > $propertyTwoLevels)
? $propertyOneLevels : $propertyTwoLevels;
for ($i = 0; $i < $maxLevels; ++ $i) {
if (isset ($propertyOne[$i]) &&
isset ($propertyTwo[$i])) {
if ($propertyOneLevels > $propertyTwoLevels) {
* decodes the css property name into an compareable array
if (substr($propertyName, 0, 1) == '-') {
$pos = strpos($propertyName, '-', 1);
$items[] = substr($propertyName, 1, $pos);
$items = explode('-', $propertyName);
|