-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[!!!][FEATURE] Allow to use custom output rules (#98)
* [!!!][TASK] Trigger deprecation error when missing Behavior instance * [!!!][FEATURE] Allow to use custom output rules As a consequence, it is required to have `Behavior` available in `Sanitizer`. As fall back and for the time being, this is not a hard requirement - but it will change in future versions of this library.
- Loading branch information
Showing
8 changed files
with
237 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under the terms | ||
* of the MIT License (MIT). For the full copyright and license information, | ||
* please read the LICENSE file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\HtmlSanitizer\Serializer; | ||
|
||
use DOMNode; | ||
use Masterminds\HTML5\Serializer\OutputRules; | ||
use Masterminds\HTML5\Serializer\Traverser; | ||
use TYPO3\HtmlSanitizer\Behavior; | ||
use TYPO3\HtmlSanitizer\InitiatorInterface; | ||
|
||
class Rules extends OutputRules implements RulesInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $options; | ||
|
||
/** | ||
* @var ?Traverser | ||
*/ | ||
protected $traverser; | ||
|
||
/** | ||
* @var ?Behavior | ||
*/ | ||
protected $behavior; | ||
|
||
/** | ||
* @var ?InitiatorInterface | ||
*/ | ||
protected $initiator; | ||
|
||
/** | ||
* @param Behavior $behavior | ||
* @param resource$output | ||
* @param array $options | ||
* @return self | ||
*/ | ||
public static function create(Behavior $behavior, $output, array $options = []): self | ||
{ | ||
$target = new self($output, $options); | ||
$target->options = $options; | ||
$target->behavior = $behavior; | ||
return $target; | ||
} | ||
|
||
/** | ||
* @param resource $output | ||
* @param array $options | ||
*/ | ||
public function __construct($output, $options = []) | ||
{ | ||
$this->options = (array)$options; | ||
parent::__construct($output, $this->options); | ||
} | ||
|
||
public function withBehavior(Behavior $behavior): self | ||
{ | ||
if ($this->behavior === $behavior) { | ||
return $this; | ||
} | ||
$target = clone $this; | ||
$target->behavior = $behavior; | ||
return $target; | ||
} | ||
|
||
public function withInitiator(?InitiatorInterface $initiator): self | ||
{ | ||
if ($this->initiator === $initiator) { | ||
return $this; | ||
} | ||
$target = clone $this; | ||
$target->initiator = $initiator; | ||
return $target; | ||
} | ||
|
||
public function traverse(DOMNode $domNode): void | ||
{ | ||
$traverser = new Traverser($domNode, $this->out, $this, $this->options); | ||
$traverser->walk(); | ||
// release the traverser to avoid cyclic references and allow PHP | ||
// to free memory without waiting for gc_collect_cycles | ||
$this->unsetTraverser(); | ||
} | ||
|
||
/** | ||
* @return resource | ||
*/ | ||
public function getStream() | ||
{ | ||
return $this->out; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under the terms | ||
* of the MIT License (MIT). For the full copyright and license information, | ||
* please read the LICENSE file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\HtmlSanitizer\Serializer; | ||
|
||
use DOMNode; | ||
use Masterminds\HTML5\Serializer\RulesInterface as MastermindsRulesInterface; | ||
use TYPO3\HtmlSanitizer\Behavior; | ||
use TYPO3\HtmlSanitizer\InitiatorInterface; | ||
|
||
interface RulesInterface extends MastermindsRulesInterface | ||
{ | ||
/** | ||
* @return self | ||
*/ | ||
public function withBehavior(Behavior $behavior); | ||
|
||
/** | ||
* @return self | ||
*/ | ||
public function withInitiator(?InitiatorInterface $initiator); | ||
|
||
public function traverse(DOMNode $domNode): void; | ||
|
||
/** | ||
* @return resource | ||
*/ | ||
public function getStream(); | ||
|
||
public function getOptions(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters