Neos\Neos\Service\HtmlAugmenter::addAttributes PHP Method

addAttributes() public method

Attributes are merged with the existing root element's attributes. If no unique root node can be determined, a wrapping tag is added with all the given attributes. The name of this tag can be specified with $fallbackTagName.
public addAttributes ( string $html, array $attributes, string $fallbackTagName = 'div', array $exclusiveAttributes = null ) : string
$html string The HTML code to augment
$attributes array Attributes to be added to the root element in the format array('' => '', ...)
$fallbackTagName string The root element tag name if one needs to be added
$exclusiveAttributes array A list of lowercase(!) attribute names that should be exclusive to the root element. If the existing root element contains one of these a new root element is wrapped
return string
    public function addAttributes($html, array $attributes, $fallbackTagName = 'div', array $exclusiveAttributes = null)
    {
        if ($attributes === array()) {
            return $html;
        }
        $rootElement = $this->getHtmlRootElement($html);
        if ($rootElement === null || $this->elementHasAttributes($rootElement, $exclusiveAttributes)) {
            return sprintf('<%s%s>%s</%s>', $fallbackTagName, $this->renderAttributes($attributes), $html, $fallbackTagName);
        }
        $this->mergeAttributes($rootElement, $attributes);
        return preg_replace('/<(' . $rootElement->nodeName . ')\\b[^>]*>/xi', '<$1' . addcslashes($this->renderAttributes($attributes), '\\\\$') . '>', $html, 1);
    }

Usage Example

 /**
  * @param string $html
  * @param array $attributes
  * @param string $fallbackTagName
  * @param string $expectedResult
  * @param array $exclusiveAttributes
  * @test
  * @dataProvider addAttributesDataProvider
  */
 public function addAttributesTests($html, array $attributes, $fallbackTagName, $exclusiveAttributes, $expectedResult)
 {
     if ($fallbackTagName === null) {
         $fallbackTagName = 'div';
     }
     $actualResult = $this->htmlAugmenter->addAttributes($html, $attributes, $fallbackTagName, $exclusiveAttributes);
     $this->assertSame($expectedResult, $actualResult);
 }
All Usage Examples Of Neos\Neos\Service\HtmlAugmenter::addAttributes