Skip to content

Commit

Permalink
Merge pull request #10 from jolicode/fix/html-siblings-fenced-code
Browse files Browse the repository at this point in the history
Fix HTML fenced code siblings management
  • Loading branch information
xavierlacot authored Aug 1, 2024
2 parents f017ab8 + e2d7ec4 commit 4fa0006
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Fixer/HtmlBlockFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ protected function applyReplacementNodes(iterable $nodes, array $replacements):
$exploded = explode($key, $newNode->getLiteral());

if (2 === \count($exploded)) {
$className = $newNode::class;
$newNodes = [...\array_slice($newNodes, 0, $newNodeKey), new $className($exploded[0]), $replacement, new $className($exploded[1]), ...\array_slice($newNodes, $newNodeKey + 1)];
if ($newNode instanceof FencedCode) {
$newNode->setLiteral(str_replace($key, '', $newNode->getLiteral()));
} else {
$className = $newNode::class;
$newNodes = [...\array_slice($newNodes, 0, $newNodeKey), new $className($exploded[0]), $replacement, new $className($exploded[1]), ...\array_slice($newNodes, $newNodeKey + 1)];
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/data/attribute-class-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{class="some-class(value)"}
[Some text here!](/link){class="link(variant)" target="_blank"}

{.-hello}
Some text

{.smile-😎 .yolo}
Some text

{id="smile-😎"}
Some text
4 changes: 4 additions & 0 deletions tests/data/attribute-class-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p class="some-class(value)"><a class="link(variant)" target="_blank" href="/link" rel="noopener noreferrer">Some text here!</a></p>
<p class="-hello">Some text</p>
<p class="smile-😎 yolo">Some text</p>
<p id="smile-😎">Some text</p>
11 changes: 11 additions & 0 deletions tests/data/attribute-class-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{.some-class(value)}
[Some text here!](/link){.link(variant) target="_blank"}

{.-hello}
Some text

{.smile-😎 .yolo}
Some text

{#smile-😎}
Some text
20 changes: 20 additions & 0 deletions tests/data/html-siblings-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# First case

<p>Intro</p>

<pre><code>Multiline

code</code></pre>

# Second case

<p>Intro</p>
<pre><code>Multiline
code</code></pre>

# Third case

<p>Intro</p>
<pre><code>Multiline

code</code></pre>
16 changes: 16 additions & 0 deletions tests/data/html-siblings-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>First case</h1>
<p>Intro</p>
<pre><code>Multiline

code
</code></pre>
<h1>Second case</h1>
<p>Intro</p>
<pre><code>Multiline
code
</code></pre>
<h1>Third case</h1>
<p>Intro</p>
<pre><code>Multiline
</code></pre>
<p>code</p>
28 changes: 28 additions & 0 deletions tests/data/html-siblings-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# First case

Intro

```
Multiline
code
```

# Second case

Intro

```
Multiline
code
```

# Third case

Intro

```
Multiline
```

code
6 changes: 6 additions & 0 deletions tests/data/ordered_lists-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. one
1. two
1. three
1. three dot one
1. three dot two
1. four
11 changes: 11 additions & 0 deletions tests/data/ordered_lists-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ol>
<li>one</li>
<li>two</li>
<li>three
<ol>
<li>three dot one</li>
<li>three dot two</li>
</ol>
</li>
<li>four</li>
</ol>
6 changes: 6 additions & 0 deletions tests/data/ordered_lists-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. one
2. two
3. three
1. three dot one
2. three dot two
4. four
6 changes: 6 additions & 0 deletions tests/data/pre-broken-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Header</p>
<pre><code>Some code

Broken

<p>Footer</p>
6 changes: 6 additions & 0 deletions tests/data/pre-broken-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Header</p>
<pre><code>Some code

Broken

<p>Footer</p></code></pre>
7 changes: 7 additions & 0 deletions tests/data/pre-broken-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Header

<pre><code>Some code

Broken

<p>Footer</p></code></pre>

0 comments on commit 4fa0006

Please sign in to comment.