Skip to content

Commit

Permalink
[TASK] Backport library to support PHP 7.0 and 7.1 (#103)
Browse files Browse the repository at this point in the history
The PHP code of this library is backported to additionally support both
PHP 7.0 and PHP 7.1.

In order to achieve this, const visibility, nullable types and `void`
types were removed and replaced with PHPDoc comments where applicable.

The v1.5 branch is a back-port of v2 and only considered as a
transitional version between v1 and v2 of typo3/html-sanitizer.
  • Loading branch information
andreaskienast authored Dec 6, 2022
1 parent 4f46069 commit 307cbd6
Show file tree
Hide file tree
Showing 24 changed files with 205 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
php: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ This `typo3/html-sanitizer` package aims to be a standalone component that can b
project or library. Albeit it is released within the TYPO3 namespace, it is agnostic to specifics of
[TYPO3 CMS](https://github.com/typo3/typo3).

> :warning: Version 1.5 of `typo3/html-sanitizer` is a transitional version
> between v1 and v2 to support PHP 7.0 and PHP 7.1.
+ [`\TYPO3\HtmlSanitizer\Behavior`](src/Behavior.php) contains declarative settings for
a particular process for sanitizing HTML.
+ [`\TYPO3\HtmlSanitizer\Visitor\VisitorInterface`](src/Visitor/VisitorInterface.php)
Expand Down
6 changes: 3 additions & 3 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# TYPO3 HTML Sanitizer notices for upgrading

## v2.1.0
## v1.5.0

* deprecated `\TYPO3\HtmlSanitizer\Behavior\NodeException::withNode(?DOMNode $node)`,
use `\TYPO3\HtmlSanitizer\Behavior\NodeException::withDomNode(?DOMNode $domNode)` instead
* deprecated `\TYPO3\HtmlSanitizer\Behavior\NodeException::withNode($node)`,
use `\TYPO3\HtmlSanitizer\Behavior\NodeException::withDomNode($domNode)` instead
* deprecated `\TYPO3\HtmlSanitizer\Behavior\NodeException::getNode()`,
use `\TYPO3\HtmlSanitizer\Behavior\NodeException::getDomNode()` instead
* deprecated property `\TYPO3\HtmlSanitizer\Sanitizer::$root`, superfluous - don't use it anymore
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"require": {
"ext-dom": "*",
"masterminds/html5": "^2.7.6",
"php": "^7.2 || ^8.0",
"php": "^7.0 || ^8.0",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5"
"phpunit/phpunit": "^6.5 || ^8.5"
},
"scripts": {
"test": "phpunit"
Expand Down
32 changes: 20 additions & 12 deletions src/Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@ class Behavior
/**
* not having any behavioral capabilities
*/
public const BLUNT = 0;
const BLUNT = 0;

/**
* in case an unexpected tag was found, encode the whole tag as HTML
*/
public const ENCODE_INVALID_TAG = 1;
const ENCODE_INVALID_TAG = 1;

/**
* in case an unexpected attribute was found, encode the whole tag as HTML
*/
public const ENCODE_INVALID_ATTR = 2;
const ENCODE_INVALID_ATTR = 2;

/**
* remove children at nodes that did not expect children
*/
public const REMOVE_UNEXPECTED_CHILDREN = 4;
const REMOVE_UNEXPECTED_CHILDREN = 4;

/**
* https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
* custom elements must contain a hyphen (`-`), start with ASCII lower alpha
*/
public const ALLOW_CUSTOM_ELEMENTS = 8;
const ALLOW_CUSTOM_ELEMENTS = 8;

/**
* in case an unexpected comment was found, encode the whole comment as HTML
*/
public const ENCODE_INVALID_COMMENT = 16;
const ENCODE_INVALID_COMMENT = 16;

/**
* in case an unexpected CDATA section was found, encode the whole CDATA section as HTML
*/
public const ENCODE_INVALID_CDATA_SECTION = 32;
const ENCODE_INVALID_CDATA_SECTION = 32;

/**
* @var int
Expand Down Expand Up @@ -136,7 +136,7 @@ public function withoutNodes(NodeInterface ...$nodes): self
$names = array_map([$this, 'getNodeName'], $nodes);
$filteredNodes = array_filter(
$this->nodes,
static function (?NodeInterface $node, string $name) use ($nodes, $names) {
static function ($node, string $name) use ($nodes, $names) {
return $node === null && !in_array($name, $names, true)
|| $node !== null && !in_array($node, $nodes, true);
},
Expand Down Expand Up @@ -173,7 +173,10 @@ static function (NodeInterface $node) {
);
}

public function getTag(string $name): ?Tag
/**
* @return Tag|null
*/
public function getTag(string $name)
{
$name = strtolower($name);
$node = $this->nodes[$name] ?? null;
Expand All @@ -188,7 +191,10 @@ public function getNodes(): array
return $this->nodes;
}

public function getNode(string $name): ?NodeInterface
/**
* @return NodeInterface|null
*/
public function getNode(string $name)
{
$name = strtolower($name);
return $this->nodes[$name] ?? null;
Expand Down Expand Up @@ -231,9 +237,10 @@ public function shallAllowCustomElements(): bool

/**
* @param list<string> $names
* @return void
* @throws LogicException
*/
protected function assertScalarUniqueness(array $names): void
protected function assertScalarUniqueness(array $names)
{
$ambiguousNames = array_diff_assoc($names, array_unique($names));
if ($ambiguousNames !== []) {
Expand All @@ -249,8 +256,9 @@ protected function assertScalarUniqueness(array $names): void

/**
* @param array<string, NodeInterface> $nodes
* @return void
*/
protected function assertNodeUniqueness(array $nodes): void
protected function assertNodeUniqueness(array $nodes)
{
$existingNodeNames = array_intersect_key(array_filter($this->nodes), $nodes);
if ($existingNodeNames !== []) {
Expand Down
10 changes: 5 additions & 5 deletions src/Behavior/Attr.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class Attr
/**
* not having any behavioral capabilities
*/
public const BLUNT = 0;
const BLUNT = 0;

/**
* whether given name shall be considered as prefix, e.g.
* `data-` or `aria-` for multiple similar and safe attribute names
*/
public const NAME_PREFIX = 1;
const NAME_PREFIX = 1;

/**
* whether the first match in `$values` shall be considered
Expand All @@ -37,19 +37,19 @@ class Attr
*
* @deprecated since version 2.0.13 (it is the default behavior now)
*/
public const MATCH_FIRST_VALUE = 2;
const MATCH_FIRST_VALUE = 2;

/**
* whether all `$values` shall be considered as indicator an
* attribute value is valid - if this flag is not given, the
* first match in `$values` is taken
*/
public const MATCH_ALL_VALUES = 4;
const MATCH_ALL_VALUES = 4;

/**
* whether the current attribute is mandatory for the tag
*/
public const MANDATORY = 8;
const MANDATORY = 8;

/**
* either specific attribute name (`class`) or a prefix
Expand Down
9 changes: 8 additions & 1 deletion src/Behavior/Handler/AsTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@

class AsTextHandler implements HandlerInterface
{
public function handle(NodeInterface $node, ?DOMNode $domNode, Context $context, Behavior $behavior = null): ?DOMNode
/**
* @param NodeInterface $node
* @param DOMNode|null $domNode
* @param Context $context
* @param Behavior|null $behavior
* @return DOMNode|null
*/
public function handle(NodeInterface $node, $domNode, Context $context, Behavior $behavior = null)
{
if ($domNode === null) {
return null;
Expand Down
9 changes: 8 additions & 1 deletion src/Behavior/Handler/ClosureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ public function __construct(Closure $closure)
$this->closure = $closure;
}

public function handle(NodeInterface $node, ?DOMNode $domNode, Context $context, Behavior $behavior = null): ?DOMNode
/**
* @param NodeInterface $node
* @param DOMNode|null $domNode
* @param Context $context
* @param Behavior|null $behavior
* @return DOMNode|null
*/
public function handle(NodeInterface $node, $domNode, Context $context, Behavior $behavior = null)
{
$result = call_user_func($this->closure, $node, $domNode, $context, $behavior);
if ($result !== null && !$result instanceof DOMNode) {
Expand Down
9 changes: 8 additions & 1 deletion src/Behavior/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@

interface HandlerInterface
{
public function handle(NodeInterface $node, ?DOMNode $domNode, Context $context, Behavior $behavior = null): ?DOMNode;
/**
* @param NodeInterface $node
* @param DOMNode|null $domNode
* @param Context $context
* @param Behavior|null $behavior
* @return DOMNode|null
*/
public function handle(NodeInterface $node, $domNode, Context $context, Behavior $behavior = null);
}
20 changes: 14 additions & 6 deletions src/Behavior/NodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,38 @@ public static function create(): self
*/
protected $domNode;

public function withDomNode(?DOMNode $domNode): self
/**
* @param DOMNode|null $domNode
*/
public function withDomNode($domNode): self
{
$this->domNode = $domNode;
return $this;
}

/**
* @deprecated since v2.1.0, use withDomNode(?DOMNode $domNode) instead
* @param DOMNode|null $domNode
* @deprecated since v1.5.0, use withDomNode(?DOMNode $domNode) instead
*/
public function withNode(?DOMNode $domNode): self
public function withNode($domNode): self
{
$this->domNode = $domNode;
return $this;
}

public function getDomNode(): ?DOMNode
/**
* @return DOMNode|null
*/
public function getDomNode()
{
return $this->domNode;
}

/**
* @deprecated since v2.1.0, use getDomNode() instead
* @return DOMNode|null
* @deprecated since v1.5.0, use getDomNode() instead
*/
public function getNode(): ?DOMNode
public function getNode()
{
return $this->domNode;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Behavior/NodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class NodeHandler implements NodeInterface
/**
* Whether defaults shall be processed (e.g. verifying all attributes etc.)
*/
public const PROCESS_DEFAULTS = 1;
const PROCESS_DEFAULTS = 1;

/**
* Whether this handler shall be processed first (before processing defaults)
*/
public const HANDLE_FIRST = 2;
const HANDLE_FIRST = 2;

/**
* @var NodeInterface
Expand Down
19 changes: 12 additions & 7 deletions src/Behavior/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ class Tag implements NodeInterface
/**
* not having any behavioral capabilities
*/
public const BLUNT = 0;
const BLUNT = 0;

/**
* whether to purge this tag in case it does not have any attributes
*/
public const PURGE_WITHOUT_ATTRS = 1;
const PURGE_WITHOUT_ATTRS = 1;

/**
* whether to purge this tag in case it does not have children
*/
public const PURGE_WITHOUT_CHILDREN = 2;
const PURGE_WITHOUT_CHILDREN = 2;

/**
* whether this tag allows to have children
*/
public const ALLOW_CHILDREN = 8;
const ALLOW_CHILDREN = 8;

/**
* @var string
Expand Down Expand Up @@ -112,7 +112,10 @@ public function shallAllowChildren(): bool
return ($this->flags & self::ALLOW_CHILDREN) === self::ALLOW_CHILDREN;
}

public function getAttr(string $name): ?Attr
/**
* @return Attr|null
*/
public function getAttr(string $name)
{
$name = strtolower($name);
if (isset($this->attrs[$name])) {
Expand All @@ -128,9 +131,10 @@ public function getAttr(string $name): ?Attr

/**
* @param string[] $names
* @return void
* @throws LogicException
*/
protected function assertScalarUniqueness(array $names): void
protected function assertScalarUniqueness(array $names)
{
$ambiguousNames = array_diff_assoc($names, array_unique($names));
if ($ambiguousNames !== []) {
Expand All @@ -146,9 +150,10 @@ protected function assertScalarUniqueness(array $names): void

/**
* @param array<string, Attr> $attrs
* @return void
* @throws LogicException
*/
protected function assertAttrUniqueness(array $attrs): void
protected function assertAttrUniqueness(array $attrs)
{
$existingAttrNames = [];
$currentAttrNames = array_keys($this->attrs);
Expand Down
Loading

0 comments on commit 307cbd6

Please sign in to comment.