|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* @author ostico [email protected] / [email protected] |
|
5
|
|
|
* Date: 14/01/20 |
|
6
|
|
|
* Time: 18:28 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Matecat\SubFiltering\Filters\Html; |
|
11
|
|
|
|
|
12
|
|
|
use Matecat\SubFiltering\Commons\Pipeline; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait CallbacksHandler |
|
16
|
|
|
* |
|
17
|
|
|
* Defines the abstract contract for a handler that processes the output of the HtmlParser. |
|
18
|
|
|
* A class using this trait must implement the defined abstract methods. These methods |
|
19
|
|
|
* are invoked by `HtmlParser` as it identifies different components of an input string, |
|
20
|
|
|
* such as valid tags, plain text, invalid markup, and special blocks like scripts or comments. |
|
21
|
|
|
* |
|
22
|
|
|
* This decouples the parsing logic of `HtmlParser` from the specific actions performed |
|
23
|
|
|
* on the parsed segments, allowing for flexible and interchangeable handling logic. |
|
24
|
|
|
* |
|
25
|
|
|
* @package Matecat\SubFiltering\Filters\Html |
|
26
|
|
|
*/ |
|
27
|
|
|
trait CallbacksHandler { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Pipeline |
|
31
|
|
|
*/ |
|
32
|
|
|
protected Pipeline $pipeline; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Processes a buffer that has been identified as a valid and complete HTML tag. |
|
36
|
|
|
* This method is called by `HtmlParser` when it successfully parses a tag. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $buffer The complete HTML tag string (e.g., "<p>", "</div>"). |
|
39
|
|
|
* @return string The processed result that should replace the tag in the output. |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract protected function _finalizeMarkupTag( string $buffer ): string; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Handles buffers that are determined to be invalid or malformed markup. |
|
45
|
|
|
* This is the callback for error handling, for instance, when an unclosed tag is found |
|
46
|
|
|
* at the end of the string or an unexpected character appears within a tag. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $buffer The invalid or incomplete tag-like string. |
|
49
|
|
|
* @return string The processed, safe representation of the buffer. |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract protected function _fixWrongBuffer( string $buffer ): string; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Validates if a given buffer constitutes a well-formed HTML tag. |
|
55
|
|
|
* `HtmlParser` uses this to decide whether to call `_finalizeHTMLTag` or `_fixWrongBuffer`. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $buffer The buffer to be validated. |
|
58
|
|
|
* @return bool A truthy value if the tag is valid, falsy otherwise. |
|
59
|
|
|
* (Note: The return type is string for legacy reasons but is treated as a boolean). |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract protected function _isTagValid( string $buffer ): bool; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Processes a buffer containing plain text found between or outside of HTML tags. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $buffer The segment of plain text. |
|
67
|
|
|
* @return string The processed representation of the text. |
|
68
|
|
|
*/ |
|
69
|
|
|
abstract protected function _finalizePlainText( string $buffer ): string; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Processes the entire content of special blocks, such as HTML comments (`<!-- ... -->`), |
|
73
|
|
|
* scripts (`<script>...</script>`), or styles (`<style>...</style>`). |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $buffer The full content of the script, style, or comment block. |
|
76
|
|
|
* @return string The processed representation of the block. |
|
77
|
|
|
*/ |
|
78
|
|
|
abstract protected function _finalizeScriptTag( string $buffer ): string; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Signals to the processing pipeline that the current segment contains HTML markup. |
|
82
|
|
|
* This method assumes the class using this trait has a `pipeline` property |
|
83
|
|
|
* which is an instance of `Matecat\SubFiltering\Commons\Pipeline`. |
|
84
|
|
|
*/ |
|
85
|
26 |
|
protected function _setSegmentContainsMarkup() { |
|
86
|
26 |
|
$this->pipeline->_setSegmentContainsMarkup(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |