Skip to content

Commit

Permalink
Merge pull request #27 from dbohn/fix-invalid-codeblock-parsing
Browse files Browse the repository at this point in the history
Don't fail on missing code tag in code block parsing
  • Loading branch information
nperez0111 committed Aug 12, 2024
2 parents 303877d + d7d6303 commit c9e4d37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Nodes/CodeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function addAttributes()
return [
'language' => [
'parseHTML' => function ($DOMNode) {
if (!($DOMNode->childNodes[0] instanceof \DOMElement)) {
return null;
}

return preg_replace(
"/^" . $this->options['languageClassPrefix']. "/",
"",
Expand Down
21 changes: 21 additions & 0 deletions tests/DOMParser/Nodes/CodeBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,24 @@
],
]);
});

test('it handles code blocks without a code tag', function () {
$html = '<pre>body { display: none }</pre>';

$result = (new Editor)->setContent($html)->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'codeBlock',
'content' => [
[
'type' => 'text',
'text' => 'body { display: none }',
],
],
],
],
]);
});

0 comments on commit c9e4d37

Please sign in to comment.