Skip to content

Commit

Permalink
Merge pull request #8 from jolicode/fix/html-code-with-children
Browse files Browse the repository at this point in the history
Do not transform HTML code blocks with multiple children
  • Loading branch information
xavierlacot committed Sep 26, 2023
2 parents 8ff6447 + a975f78 commit 9222e45
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/Fixer/HtmlBlockFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ protected function buildNodeFromDomElement(\DOMNode $element, bool $allowLtrim =
}
} elseif ('pre' === $element->tagName) {
foreach ($element->childNodes as $key => $child) {
if (1 !== $child->childNodes->length) {
$node = null;

break;
}
if ($child instanceof \DOMElement && 'code' === $child->tagName) {
$node = new FencedCode(0, '', 0);
$childAttributes = $this->getAttributes($child);
Expand Down
9 changes: 4 additions & 5 deletions tests/data/code_block_html-out.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<pre><code class="language-javascript">{
&quot;example&quot;: &quot;example&quot;,
&quot;authors&quot;: [
&quot;John Appleseed &quot;
"example": "example",
"authors": [
"John Appleseed <john at="" example.com="">"
]
}
</code></pre>
}</john></code></pre>
<pre><code class="language-javascript">{
&quot;example&quot;: &quot;example&quot;,
&quot;authors&quot;: [
Expand Down
8 changes: 3 additions & 5 deletions tests/data/code_block_html-out.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
```javascript
{
<pre><code class="language-javascript">{
"example": "example",
"authors": [
"John Appleseed "
"John Appleseed <john at="" example.com="">"
]
}
```
}</john></code></pre>

```javascript
{
Expand Down
2 changes: 2 additions & 0 deletions tests/data/html_code-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre><code class="language-bash">Some <strong>HTML</strong> goes here
Some <em>HTML</em> goes here</code></pre>
2 changes: 2 additions & 0 deletions tests/data/html_code-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre><code class="language-bash">Some <strong>HTML</strong> goes here
Some <em>HTML</em> goes here</code></pre>
2 changes: 2 additions & 0 deletions tests/data/html_code-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre><code class="language-bash">Some <strong>HTML</strong> goes here
Some <em>HTML</em> goes here</code></pre>

0 comments on commit 9222e45

Please sign in to comment.