Skip to content

Commit

Permalink
Add support for <pre>
Browse files Browse the repository at this point in the history
  • Loading branch information
bytestream committed Sep 27, 2023
1 parent 4e885e3 commit 7a2a66b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Text/Elements/ContentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ContentModel
'th' => TableCell::class,
'img' => Image::class,
'a' => Anchor::class,
'pre' => Pre::class,
];

private DOMNode $node;
Expand Down
7 changes: 6 additions & 1 deletion src/Text/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ protected function isWhitespace(string $text): bool
return strlen(trim($this->processWhitespace($text), "\n\r\t ")) === 0;
}

protected function removeZwnjCodes(string $text): string
{
return str_replace($this->zwnjCodes(), '', $text);
}

protected function processWhitespace(string $text): string
{
$text = rtrim($text);
$text = str_replace($this->zwnjCodes(), '', $text);
$text = $this->removeZwnjCodes($text);
$text = (string) preg_replace("/[\\t\\n\\f\\r ]+/im", ' ', $text);

return trim($text);
Expand Down
28 changes: 28 additions & 0 deletions src/Text/Elements/Pre.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace SupportPal\DomUtils\Text\Elements;

use SupportPal\DomUtils\Text\Css\Breakline;
use SupportPal\DomUtils\Text\Css\Content;
use SupportPal\DomUtils\Text\Css\Margin;

class Pre extends Element
{
public function startNode(): ?Content
{
if ($this->previous instanceof Margin || $this->previous instanceof Breakline) {
return null;
}

return new Margin(1);
}

public function endNode(): ?Content
{
if ($this->previous instanceof Margin || $this->previous instanceof Breakline) {
return null;
}

return new Margin(1);
}
}
7 changes: 5 additions & 2 deletions src/Text/Elements/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public function endNode(): ?Content
return null;
}

$text = $this->processWhitespace($text);
$text = str_replace($this->nbspCodes(), ' ', $text);
if (! in_array($this->node->parentNode?->nodeName, ['pre'])) {
$text = $this->processWhitespace($text);
}

$text = trim(str_replace([$this->nbspCodes(), $this->zwnjCodes()], ' ', $text));

if (empty($text)) {
return null;
Expand Down
1 change: 1 addition & 0 deletions test/Text/TextParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function toTextProvider(): iterable
yield [10];
yield [11];
yield [12];
yield [13];
}

#[DataProvider('marginProvider')]
Expand Down
10 changes: 10 additions & 0 deletions test/Text/fixtures/fixture13.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p>Here is the code</p>
<pre>
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

int main(){
return 0;
};

</pre>
8 changes: 8 additions & 0 deletions test/Text/fixtures/fixture13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Here is the code

#include <stdlib.h>
#include <stdio.h>

int main(){
return 0;
};

0 comments on commit 7a2a66b

Please sign in to comment.