Skip to content

Commit aa65e14

Browse files
committed
feat(pretty): use domxpath, add reset() and codeToHtml()
1 parent c0f09e1 commit aa65e14

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Pretty.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,26 @@ public static function configure()
5050

5151
protected function parse(string $code = null)
5252
{
53-
static::configure();
54-
55-
$html = \highlight_string($code ?? $this->code, true);
56-
$html = \str_replace('<br />', "\n", $html);
53+
$this->reset();
5754

5855
$dom = new \DOMDocument;
59-
$dom->loadHTML($html);
56+
$dom->loadHTML($this->codeToHtml($code));
6057

61-
foreach ($dom->getElementsByTagName('span') as $el) {
62-
if ('html' !== $el->getAttribute('data-type')) {
63-
$this->visit($el);
64-
}
58+
foreach ((new \DOMXPath($dom))->query('/html/body/span')[0]->childNodes as $el) {
59+
$this->visit($el);
6560
}
6661
}
6762

68-
abstract protected function visit(\DOMElement $el);
63+
protected function codeToHtml(string $code = null): string
64+
{
65+
static::configure();
66+
67+
$html = \highlight_string($code ?? $this->code, true);
68+
69+
return \str_replace(['<br />', '<code>', '</code>'], ["\n", '', ''], $html);
70+
}
71+
72+
abstract protected function reset();
73+
74+
abstract protected function visit(\DOMNode $el);
6975
}

0 commit comments

Comments
 (0)