Skip to content

Commit

Permalink
Fixed static calls issues with escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
natepage committed Jan 16, 2017
1 parent bc79737 commit 2265476
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
42 changes: 27 additions & 15 deletions HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ public function addManyToMap(array $elements)
return $this;
}

/**
* {@inheritdoc}
*/
public function setEscaper(EscaperInterface $escaper)
{
$this->escaper = $escaper;

return $this;
}

/**
* {@inheritdoc}
*/
public function getEscaper()
{
return $this->escaper;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -224,13 +242,15 @@ public function load($name, array $parameters = array(), array $attributes = arr
*/
static public function create($type = null, $text = null, array $attributes = array(), array $children = array())
{
$attributes = self::escapeAttributes($attributes);
$htmlElement = new HtmlElement();

$attributes = $htmlElement->escapeAttributes($attributes);

foreach($children as $key => $child){
$children[$key] = self::escape($child);
$children[$key] = $htmlElement->escape($child);
}

return self::escape(new Element($type, $text, $attributes, $children));
return $htmlElement->escape(new Element($type, $text, $attributes, $children));
}

/**
Expand Down Expand Up @@ -272,13 +292,9 @@ static public function __callStatic($type, $arguments)
}

/**
* Apply escaping strategies on an element.
*
* @param ElementInterface $element The element to escape
*
* @return ElementInterface
* {@inheritdoc}
*/
private function escape(ElementInterface $element)
public function escape(ElementInterface $element)
{
if($this->escapeHtml && !in_array($element->getType(), $this->specialEscapingTypes)){
$element->setText($this->escaper->escapeHtml($element->getText()));
Expand All @@ -298,13 +314,9 @@ private function escape(ElementInterface $element)
}

/**
* Apply escaping strategies on element attributes.
*
* @param array $attributes The attributes array to escape
*
* @return array
* {@inheritdoc}
*/
private function escapeAttributes(array $attributes)
public function escapeAttributes(array $attributes)
{
if($this->escapeHtmlAttr || $this->escapeUrl){
foreach($attributes as $attr => $value){
Expand Down
34 changes: 34 additions & 0 deletions HtmlElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ public function addOneToMap($name, array $element);
*/
public function addManyToMap(array $elements);

/**
* Set the HtmlElement escaper.
*
* @param EscaperInterface $escaper The escaper to set
*
* @return HtmlElementInterface
*/
public function setEscaper(EscaperInterface $escaper);

/**
* Get the HtmlElement escaper.
*
* @return EscaperInterface
*/
public function getEscaper();

/**
* Apply escaping strategies to an element.
*
* @param ElementInterface $element The element to escape
*
* @return ElementInterface
*/
public function escape(ElementInterface $element);

/**
* Apply escaping strategies on element attributes.
*
* @param array $attributes The attributes array to escape
*
* @return array
*/
public function escapeAttributes(array $attributes);

/**
* Determine if html is escaped or not
*
Expand Down

0 comments on commit 2265476

Please sign in to comment.