Skip to content

Commit

Permalink
Merge pull request #7 from snebes/fixes
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
snebes authored Feb 28, 2019
2 parents f20adf4 + 10ebe23 commit b804026
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/Node/TagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ class TagNode extends AbstractNode implements TagNodeInterface
*/
private $attributes = [];

private $isChildless = false;

/**
* Default values.
*
* @param NodeInterface $parent
* @param string $qName
* @param array $attributes
* @param bool $isChildless
*/
public function __construct(NodeInterface $parent, string $qName, array $attributes = [])
public function __construct(NodeInterface $parent, string $qName, array $attributes = [], bool $isChildless = false)
{
parent::__construct($parent);

$this->qName = $qName;
$this->attributes = $attributes;
$this->isChildless = $isChildless;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,17 @@

use SN\HtmlSanitizer\Model\Cursor;

class IgnoredNodeVisitor implements NodeVisitorInterface
class RemoveNodeVisitor extends TagNodeVisitor
{
/**
* @var string
*/
protected $qName;

/**
* Default values.
*
* @param string $qName
*/
public function __construct(string $qName)
{
$this->qName = $qName;
}

/**
* @param \DOMNode $domNode
* @param Cursor $cursor
* @return bool
*/
public function supports(\DOMNode $domNode, Cursor $cursor): bool
{
return $domNode->nodeName === $this->qName;
}

/**
* @param \DOMNode $domNode
* @param Cursor $cursor
*/
public function enterNode(\DOMNode $domNode, Cursor $cursor)
{
while ($domNode->hasChildNodes()) {
$domNode->removeChild($domNode->childNodes[0]);
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/NodeVisitor/TagNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function supports(\DOMNode $domNode, Cursor $cursor): bool
*/
public function createNode(\DOMNode $domNode, Cursor $cursor): TagNodeInterface
{
$node = new TagNode($cursor->node, $this->qName);
$childless = $this->config['childless'] ?? false;

$node = new TagNode($cursor->node, $this->qName, [], $childless);
$this->setAttributes($domNode, $node);

return $node;
Expand Down
9 changes: 7 additions & 2 deletions src/Sanitizer/StringSanitizerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
trait StringSanitizerTrait
{
/** @var array<string, string> */
private static $replacements = [
// "&#34;" is shorter than "&quot;"
'&quot;' => '&#34;',
Expand All @@ -33,10 +34,14 @@ trait StringSanitizerTrait
'' => '&#xFF40;',
];

/**
* @param string $string
* @return string
*/
public function encodeHtmlEntities(string $string): string
{
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$string = str_replace(array_keys(self::$replacements), array_values(self::$replacements), $string);
$string = \htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$string = \str_replace(\array_keys(self::$replacements), \array_values(self::$replacements), $string);

return $string;
}
Expand Down

0 comments on commit b804026

Please sign in to comment.