Skip to content

Commit dd11bbe

Browse files
committed
Update to StringEncode\Encoder 2.0
1 parent cc0c834 commit dd11bbe

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

src/PHPHtmlParser/DTO/Tag/AttributeDTO.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace PHPHtmlParser\DTO\Tag;
66

7-
use stringEncode\Encode;
8-
use stringEncode\Exception;
7+
use StringEncoder\Encoder;
8+
use StringEncoder\Exception;
99

1010
final class AttributeDTO
1111
{
@@ -28,7 +28,7 @@ private function __construct(array $values)
2828
public static function makeFromPrimitives(?string $value, bool $doubleQuote = true): AttributeDTO
2929
{
3030
return new AttributeDTO([
31-
'value' => $value,
31+
'value' => $value,
3232
'doubleQuote' => $doubleQuote,
3333
]);
3434
}
@@ -53,8 +53,13 @@ public function htmlspecialcharsDecode(): void
5353
/**
5454
* @throws Exception
5555
*/
56-
public function encodeValue(Encode $encode)
56+
public function encodeValue(Encoder $encode): void
5757
{
58-
$this->value = $encode->convert($this->value);
58+
if (\is_null($this->value)) {
59+
return;
60+
}
61+
$converter = $encode->convert();
62+
$converter->convert($this->value);
63+
$this->value = $converter->toString();
5964
}
6065
}

src/PHPHtmlParser/Dom/Node/AbstractNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
1313
use PHPHtmlParser\Finder;
1414
use PHPHtmlParser\Selector\Selector;
15-
use stringEncode\Encode;
15+
use StringEncoder\Encoder;
1616

1717
/**
1818
* Dom node object.
@@ -206,7 +206,7 @@ public function delete()
206206
*
207207
* @return void
208208
*/
209-
public function propagateEncoding(Encode $encode)
209+
public function propagateEncoding(Encoder $encode)
210210
{
211211
$this->encode = $encode;
212212
$this->tag->setEncoding($encode);

src/PHPHtmlParser/Dom/Node/InnerNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPHtmlParser\Exceptions\ChildNotFoundException;
99
use PHPHtmlParser\Exceptions\CircularException;
1010
use PHPHtmlParser\Exceptions\LogicalException;
11-
use stringEncode\Encode;
11+
use StringEncoder\Encoder;
1212

1313
/**
1414
* Inner node of the html tree, might have children.
@@ -33,7 +33,7 @@ abstract class InnerNode extends ArrayNode
3333
* Sets the encoding class to this node and propagates it
3434
* to all its children.
3535
*/
36-
public function propagateEncoding(Encode $encode): void
36+
public function propagateEncoding(Encoder $encode): void
3737
{
3838
$this->encode = $encode;
3939
$this->tag->setEncoding($encode);

src/PHPHtmlParser/Dom/Node/TextNode.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function __construct(string $text, $removeDoubleSpace = true)
5252
$replacedText = \mb_ereg_replace('\s+', ' ', $text);
5353
if ($replacedText === false) {
5454
throw new LogicalException('mb_ereg_replace returns false when attempting to clean white space from "' . $text . '".');
55+
} elseif ($replacedText === null) {
56+
throw new LogicalException('mb_ereg_replace encountered an invalid encoding for "' . $text . '".');
5557
}
5658
$text = $replacedText;
5759
}
@@ -89,7 +91,9 @@ public function text(): string
8991
// we already know the converted value
9092
return $this->convertedText;
9193
}
92-
$text = $this->encode->convert($text);
94+
$converter = $this->encode->convert();
95+
$converter->convert($text);
96+
$text = $converter->toString();
9397

9498
// remember the conversion
9599
$this->convertedText = $text;
@@ -109,10 +113,11 @@ public function setText(string $text): void
109113
{
110114
$this->text = $text;
111115
if (!\is_null($this->encode)) {
112-
$text = $this->encode->convert($text);
116+
$converter = $this->encode->convert();
117+
$converter->convert($text);
113118

114119
// remember the conversion
115-
$this->convertedText = $text;
120+
$this->convertedText = $converter->toString();
116121
}
117122
}
118123

src/PHPHtmlParser/Dom/Parser.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PHPHtmlParser\Exceptions\LogicalException;
1818
use PHPHtmlParser\Exceptions\StrictException;
1919
use PHPHtmlParser\Options;
20-
use stringEncode\Encode;
20+
use StringEncoder\Encoder;
2121

2222
class Parser implements ParserInterface
2323
{
@@ -37,7 +37,7 @@ public function parse(Options $options, Content $content, int $size): AbstractNo
3737
$root->setHtmlSpecialCharsDecode($options->isHtmlSpecialCharsDecode());
3838
$activeNode = $root;
3939
while ($activeNode !== null) {
40-
if ($activeNode && $activeNode->tag->name() === 'script'
40+
if ($activeNode && is_object( $activeNode->tag ) && $activeNode->tag->name() === 'script'
4141
&& $options->isCleanupInput() !== true
4242
) {
4343
$str = $content->copyUntil('</');
@@ -104,15 +104,15 @@ public function parse(Options $options, Content $content, int $size): AbstractNo
104104
public function detectCharset(Options $options, string $defaultCharset, AbstractNode $root): bool
105105
{
106106
// set the default
107-
$encode = new Encode();
108-
$encode->from($defaultCharset);
109-
$encode->to($defaultCharset);
107+
$encode = new Encoder();
108+
$encode->setSourceEncoding($defaultCharset);
109+
$encode->setTargetEncoding($defaultCharset);
110110

111111
$enforceEncoding = $options->getEnforceEncoding();
112112
if ($enforceEncoding !== null) {
113113
// they want to enforce the given encoding
114-
$encode->from($enforceEncoding);
115-
$encode->to($enforceEncoding);
114+
$encode->setSourceEncoding($enforceEncoding);
115+
$encode->setTargetEncoding($enforceEncoding);
116116

117117
return false;
118118
}
@@ -138,7 +138,7 @@ public function detectCharset(Options $options, string $defaultCharset, Abstract
138138
}
139139
$matches = [];
140140
if (\preg_match('/charset=([^;]+)/', $content, $matches)) {
141-
$encode->from(\trim($matches[1]));
141+
$encode->setSourceEncoding(\trim($matches[1]));
142142
$root->propagateEncoding($encode);
143143

144144
return true;
@@ -169,6 +169,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
169169
try {
170170
$content->fastForward(1);
171171
} catch (ContentLengthException $exception) {
172+
unset($exception);
172173
// we are at the end of the file
173174
return TagDTO::makeFromPrimitives();
174175
}
@@ -183,7 +184,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
183184
->setOpening('<?')
184185
->setClosing(' ?>')
185186
->selfClosing();
186-
} elseif($content->string(3) == '!--') {
187+
} elseif ($content->string(3) == '!--') {
187188
// comment tag
188189
$tag = $content->fastForward(3)
189190
->copyByToken(StringToken::CLOSECOMMENT(), true);
@@ -233,15 +234,15 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
233234
/**
234235
* @throws ChildNotFoundException
235236
*/
236-
private function detectHTML5Charset(Encode $encode, AbstractNode $root): bool
237+
private function detectHTML5Charset(Encoder $encode, AbstractNode $root): bool
237238
{
238239
/** @var AbstractNode|null $meta */
239240
$meta = $root->find('meta[charset]', 0);
240241
if ($meta == null) {
241242
return false;
242243
}
243244

244-
$encode->from(\trim($meta->getAttribute('charset')));
245+
$encode->setSourceEncoding(\trim($meta->getAttribute('charset')));
245246
$root->propagateEncoding($encode);
246247

247248
return true;
@@ -286,6 +287,7 @@ private function setUpAttributes(Content $content, int $size, HtmlNode $node, Op
286287
try {
287288
$content->fastForward(1);
288289
} catch (ContentLengthException $exception) {
290+
unset($exception);
289291
// reached the end of the content
290292
break;
291293
}

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHPHtmlParser\DTO\Tag\AttributeDTO;
88
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
9-
use stringEncode\Encode;
9+
use StringEncoder\Encoder;
1010

1111
/**
1212
* Class Tag.
@@ -49,7 +49,7 @@ class Tag
4949
/**
5050
* The encoding class to... encode the tags.
5151
*
52-
* @var Encode|null
52+
* @var Encoder|null
5353
*/
5454
protected $encode;
5555

@@ -135,7 +135,7 @@ public function isSelfClosing(): bool
135135
/**
136136
* Sets the encoding type to be used.
137137
*/
138-
public function setEncoding(Encode $encode): void
138+
public function setEncoding(Encoder $encode): void
139139
{
140140
$this->encode = $encode;
141141
}
@@ -326,13 +326,15 @@ public function makeOpeningTag()
326326
foreach (\array_keys($this->attr) as $key) {
327327
try {
328328
$attributeDTO = $this->getAttribute($key);
329+
$val = $attributeDTO->getValue();
329330
} catch (AttributeNotFoundException $e) {
331+
unset($e);
330332
// attribute that was in the array not found in the array... let's continue.
331333
continue;
332334
} catch (\TypeError $e) {
333-
$val = null;
335+
unset($e);
336+
$val = null;
334337
}
335-
$val = $attributeDTO->getValue();
336338
if (\is_null($val)) {
337339
$return .= ' ' . $key;
338340
} elseif ($attributeDTO->isDoubleQuote()) {

0 commit comments

Comments
 (0)