1 | <?php |
||
12 | final class FixedQuotation implements QuotationInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $headerText; |
||
18 | |||
19 | /** |
||
20 | * @param string $headerText |
||
21 | */ |
||
22 | 11 | public function __construct(string $headerText = '%s (%s):') |
|
26 | |||
27 | /** |
||
28 | * @param MessageBodyCollection $body |
||
29 | * @param MessageInterface $originalMessage |
||
30 | * @return MessageBodyCollection |
||
31 | * @throws \DOMException |
||
32 | */ |
||
33 | 11 | public function quote(MessageBodyCollection $body, MessageInterface $originalMessage): MessageBodyCollection |
|
73 | |||
74 | /** |
||
75 | * @param string $newHtml |
||
76 | * @param string $originalHtml |
||
77 | * @param string $headerText |
||
78 | * @return string |
||
79 | * @throws \DOMException |
||
80 | */ |
||
81 | 11 | private function quoteHtml(string $newHtml, string $originalHtml, string $headerText): string |
|
82 | { |
||
83 | 11 | $originalHtml = \trim($originalHtml); |
|
84 | 11 | if ($originalHtml === '') { |
|
85 | 2 | return ''; |
|
86 | } |
||
87 | |||
88 | 9 | $document = new \DOMDocument(); |
|
89 | 9 | $document->substituteEntities = false; |
|
90 | 9 | $document->resolveExternals = false; |
|
91 | |||
92 | 9 | $result = @$document->loadHTML($originalHtml); |
|
93 | 9 | if ($result === false) { |
|
94 | throw new \DOMException('Incorrect HTML'); |
||
95 | } |
||
96 | |||
97 | 9 | $query = new \DOMXPath($document); |
|
98 | 9 | $removeItems = $query->query('//head|//script|//body/@style|//html/@style', $document->documentElement); |
|
99 | /** @var \DOMElement $removeItem */ |
||
100 | 9 | foreach ($removeItems as $removeItem) { |
|
101 | 1 | $parent = $removeItem->parentNode; |
|
102 | 1 | $parent->removeChild($removeItem); |
|
103 | } |
||
104 | |||
105 | 9 | $body = $document->getElementsByTagName('body'); |
|
106 | 9 | $quote = $document->createElement('blockquote'); |
|
107 | 9 | $quote->setAttribute('type', 'cite'); |
|
108 | |||
109 | 9 | if ($body->length === 0) { |
|
110 | $quote->appendChild($document->removeChild($document->documentElement)); |
||
111 | } else { |
||
112 | 9 | $root = $body->item(0); |
|
113 | 9 | while ($root->childNodes->length !== 0) { |
|
114 | 9 | $quote->appendChild($root->childNodes->item(0)); |
|
115 | } |
||
116 | } |
||
117 | |||
118 | 9 | $newDocument = new \DOMDocument(); |
|
119 | 9 | $newDocument->substituteEntities = false; |
|
120 | 9 | $newDocument->resolveExternals = false; |
|
121 | 9 | $result = @$newDocument->loadHTML($newHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
|
122 | 9 | if ($result === false) { |
|
123 | throw new \DOMException('Incorrect HTML'); |
||
124 | } |
||
125 | |||
126 | 9 | $quotedNode = $newDocument->importNode($quote, true); |
|
127 | 9 | $newBody = $this->prepareBody($newDocument); |
|
128 | 9 | $newBody->appendChild($quotedNode); |
|
129 | |||
130 | 9 | $header = $newDocument->createElement('p'); |
|
131 | 9 | $header->textContent = $headerText; |
|
132 | |||
133 | 9 | $quotedNode->parentNode->insertBefore($header, $quotedNode); |
|
134 | 9 | return \trim($newDocument->saveHTML()); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param \DOMDocument $document |
||
139 | * @return \DOMElement |
||
140 | */ |
||
141 | 9 | private function prepareBody(\DOMDocument $document): \DOMElement |
|
172 | |||
173 | /** |
||
174 | * @param AlternativeText $newText |
||
175 | * @param AlternativeText $originalText |
||
176 | * @param string $headerText |
||
177 | * @return AlternativeText |
||
178 | */ |
||
179 | 11 | private function quoteText( |
|
193 | } |
||
194 |