Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1260,30 +1260,6 @@ parameters:
count: 1
path: src/lib/RichText/TextExtractor/ShortTextExtractor.php

-
message: '#^Argument of an invalid type DOMNodeList\<DOMNode\>\|false supplied for foreach, only iterables are supported\.$#'
identifier: foreach.nonIterable
count: 2
path: src/lib/RichText/Validator/CustomTagsValidator.php

-
message: '#^Call to an undefined method DOMNode\:\:getAttribute\(\)\.$#'
identifier: method.notFound
count: 2
path: src/lib/RichText/Validator/CustomTagsValidator.php

-
message: '#^Method Ibexa\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidator\:\:__construct\(\) has parameter \$customTagsConfiguration with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: src/lib/RichText/Validator/CustomTagsValidator.php

-
message: '#^Property Ibexa\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidator\:\:\$customTagsConfiguration type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: src/lib/RichText/Validator/CustomTagsValidator.php

-
message: '#^Argument of an invalid type DOMNodeList\<DOMNode\>\|false supplied for foreach, only iterables are supported\.$#'
identifier: foreach.nonIterable
Expand Down Expand Up @@ -3480,30 +3456,6 @@ parameters:
count: 6
path: tests/lib/RichText/RendererTest.php

-
message: '#^Method Ibexa\\Tests\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidatorTest\:\:providerForTestValidateDocument\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: '#^Method Ibexa\\Tests\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidatorTest\:\:testValidateDocument\(\) has no return type specified\.$#'
identifier: missingType.return
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: '#^Method Ibexa\\Tests\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidatorTest\:\:testValidateDocument\(\) has parameter \$expectedErrors with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: '#^Method Ibexa\\Tests\\FieldTypeRichText\\RichText\\Validator\\CustomTagsValidatorTest\:\:testValidateDocumentAcceptsLegacyTags\(\) has no return type specified\.$#'
identifier: missingType.return
count: 1
path: tests/lib/RichText/Validator/CustomTagsValidatorTest.php

-
message: '#^Call to method validateDocument\(\) on an unknown class Ibexa\\FieldTypeRichText\\RichText\\ValidatorInterface\.$#'
identifier: class.notFound
Expand Down
6 changes: 4 additions & 2 deletions src/bundle/Resources/config/settings/fieldtype_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ services:
class: Ibexa\FieldTypeRichText\RichText\Normalizer\Aggregate

# Symfony 3.4+ service definitions:
Ibexa\FieldTypeRichText\RichText\Validator\CustomTagsValidator:
Ibexa\FieldTypeRichText\RichText\Validator\CustomTemplateValidator:
public: false
arguments: ['%ibexa.field_type.richtext.custom_tags%']
arguments:
$customTagsConfiguration: '%ibexa.field_type.richtext.custom_tags%'
$customStylesConfiguration: '%ibexa.field_type.richtext.custom_styles%'
tags:
- { name: ibexa.field_type.richtext.validator.input.xhtml5 }

Expand Down
105 changes: 0 additions & 105 deletions src/lib/RichText/Validator/CustomTagsValidator.php

This file was deleted.

133 changes: 133 additions & 0 deletions src/lib/RichText/Validator/CustomTemplateValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\FieldTypeRichText\RichText\Validator;

use DOMDocument;
use DOMElement;
use DOMNodeList;
use DOMXPath;
use Ibexa\Contracts\FieldTypeRichText\RichText\ValidatorInterface;

/**
* Validator for Custom Tags and Styles input.
*
* The Validator checks if the given XML reflects proper Custom Tags or Styles configuration,
* mostly the existence of a specific Custom Tag or Style and their required attributes.
*/
final class CustomTemplateValidator implements ValidatorInterface
{
/**
* Custom Tags global configuration (ibexa.richtext.custom_tags Semantic Config).
*
* @var array<string,array{template:string, is_inline:bool, icon:string, attributes:array<string, array{type: string, required: bool, default_value: mixed}>}>
*/
private array $customTagsConfiguration;

/**
* Custom Styles global configuration (ibexa.richtext.custom_styles Semantic Config).
*
* @var array<string,array{template:string,inline:bool}>
*/
private array $customStylesConfiguration;

/**
* CustomTemplateValidator constructor.
*
* @param array<string,array{template:string, is_inline:bool, icon:string, attributes:array<string, array{type: string, required: bool, default_value: mixed}>}> $customTagsConfiguration
* @param array<string,array{template:string, inline:bool}> $customStylesConfiguration
*/
public function __construct(
array $customTagsConfiguration,
array $customStylesConfiguration
) {
$this->customTagsConfiguration = $customTagsConfiguration;
$this->customStylesConfiguration = $customStylesConfiguration;
}

/**
* Validate Custom Tags found in the document.
*
* @return string[] an array of error messages
*/
public function validateDocument(DOMDocument $xmlDocument): array
{
$configuredTemplateNames = array_merge(array_keys($this->customTagsConfiguration), array_keys($this->customStylesConfiguration));
$errors = [];

$xpath = new DOMXPath($xmlDocument);
$xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');

$eztemplateElements = $xpath->query('//docbook:eztemplate');
if ($eztemplateElements instanceof DOMNodeList) {
foreach ($eztemplateElements as $tagElement) {
if (!$tagElement instanceof DOMElement) {
continue;
}
$tagName = $tagElement->getAttribute('name');
if (empty($tagName)) {
$errors[] = 'Missing RichText Custom Tag name';
continue;
}

if (!in_array($tagName, $configuredTemplateNames, true)) {
@trigger_error(
"Configuration for RichText Custom Tag or Custom Style '{$tagName}' not found. " .
'Custom Tags and Custom Style configuration is required since 7.1, its lack will result in validation error in 8.x',
E_USER_DEPRECATED
);
continue;
}

// Custom Styles does not have any attributes, so we can skip validation for them
if (isset($this->customStylesConfiguration[$tagName])) {
continue;
}

$nonEmptyAttributes = [];
$tagAttributes = $this->customTagsConfiguration[$tagName]['attributes'];

// iterate over all attributes defined in XML document to check if their names match configuration
$configElements = $xpath->query('./docbook:ezconfig/docbook:ezvalue', $tagElement);
if ($configElements instanceof DOMNodeList) {
foreach ($configElements as $configElement) {
if (!$configElement instanceof DOMElement) {
continue;
}
$attributeName = $configElement->getAttribute('key');
if (empty($attributeName)) {
$errors[] = "Missing attribute name for RichText Custom Tag '{$tagName}'";
continue;
}
if (!isset($tagAttributes[$attributeName])) {
$errors[] = "Unknown attribute '{$attributeName}' of RichText Custom Tag '{$tagName}'";
}

// collect information about non-empty attributes
if (!empty($configElement->textContent)) {
$nonEmptyAttributes[] = $attributeName;
}
}
}

// check if all required attributes are present
foreach ($tagAttributes as $attributeName => $attributeSettings) {
if (empty($attributeSettings['required'])) {
continue;
}

if (!in_array($attributeName, $nonEmptyAttributes)) {
$errors[] = "The attribute '{$attributeName}' of RichText Custom Tag '{$tagName}' cannot be empty";
}
}
}
}

return $errors;
}
}
Loading
Loading