diff --git a/src/bundle/Resources/config/settings/fieldtype_services.yaml b/src/bundle/Resources/config/settings/fieldtype_services.yaml index 0ee71f70..09bdb2e0 100644 --- a/src/bundle/Resources/config/settings/fieldtype_services.yaml +++ b/src/bundle/Resources/config/settings/fieldtype_services.yaml @@ -70,3 +70,16 @@ services: Ibexa\FieldTypeRichText\RichText\TextExtractor\FullTextExtractor: ~ + Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface: + alias: Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\AggregateFilter + + Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\AggregateFilter: + arguments: + $filters: !tagged ibexa.field_type.richtext.text_extractor.node_filter + + ibexa.field_type.richtext.text_extractor.node_filter.template: + class: Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\NodePathFilter + arguments: ['eztemplate', 'ezconfig'] + tags: + - { name: ibexa.field_type.richtext.text_extractor.node_filter } + diff --git a/src/contracts/RichText/TextExtractor/NodeFilterInterface.php b/src/contracts/RichText/TextExtractor/NodeFilterInterface.php new file mode 100644 index 00000000..c6a77ad4 --- /dev/null +++ b/src/contracts/RichText/TextExtractor/NodeFilterInterface.php @@ -0,0 +1,22 @@ +filter = $filter; + } + public function extractText(DOMDocument $document): string { return null !== $document->documentElement @@ -28,8 +36,12 @@ public function extractText(DOMDocument $document): string private function extractTextFromNode(DOMNode $node): string { - $text = ''; + if ($this->filter->filter($node) === true) { + // Node is excluded + return ''; + } + $text = ''; if ($node->childNodes !== null && $node->childNodes->count() > 0) { foreach ($node->childNodes as $child) { $text .= $this->extractTextFromNode($child); diff --git a/src/lib/RichText/TextExtractor/NodeFilter/AggregateFilter.php b/src/lib/RichText/TextExtractor/NodeFilter/AggregateFilter.php new file mode 100644 index 00000000..8d1a8586 --- /dev/null +++ b/src/lib/RichText/TextExtractor/NodeFilter/AggregateFilter.php @@ -0,0 +1,37 @@ +filters = $filters; + } + + public function filter(DOMNode $node): bool + { + foreach ($this->filters as $filter) { + if ($filter->filter($node)) { + return true; + } + } + + return false; + } +} diff --git a/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php b/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php new file mode 100644 index 00000000..96e0bd58 --- /dev/null +++ b/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php @@ -0,0 +1,40 @@ +path = array_reverse($path); + } + + public function filter(DOMNode $node): bool + { + foreach ($this->path as $name) { + if ($node === null || $node->nodeName !== $name) { + return true; + } + + $node = $node->parentNode; + } + + return false; + } +}