Skip to content

Commit

Permalink
allow to render empty-value attributes unsing the value in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlacot committed Aug 13, 2024
1 parent 5b0a1d9 commit cb13267
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Extension/Attributes/Util/AttributesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public static function parseAttributes(Cursor $cursor): array
/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$name, $value] = \explode('=', $attribute, 2);

if ('true' === $value) {
$attributes[$name] = true;
continue;
}

$first = $value[0];
$last = \substr($value, -1);
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'") && \strlen($value) > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ <h2 class="main shine" id="the-site">The Site</h2>
<p>c. <a class="text" href="https://example.com">Some</a>.</p>
<p>d. <a href="https://example.com">Some{text}</a>.</p>
<p>e. <a text="text" href="https://example.com">Some</a>.</p>
<p>f. <a href="https://example.com">Some{{text}}</a>.</p>
<p>f. <a text href="https://example.com">Some</a>.</p>
<p>g. <a href="https://example.com">Some{{text}}</a>.</p>
<p hello="hello">some</p>
<p class="test" hello="hello">some</p>
<p class="test" hello="hello">some</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ d. [Some{text}](https://example.com).

e. [Some](https://example.com){text="text"}.

f. [Some{{text}}](https://example.com).
f. [Some](https://example.com){text=true}.

g. [Some{{text}}](https://example.com).

{hello="hello"}
some
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/Extension/Attributes/Util/AttributesHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ public static function dataForTestParseAttributes(): iterable
yield [new Cursor('{: #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor('{: #custom-id #another-id }'), ['id' => 'another-id']];
yield [new Cursor('{: .class1 .class2 }'), ['class' => 'class1 class2']];
yield [new Cursor('{: #custom-id .class1 .class2 title="My Title" disabled }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{: #custom-id .class1 .class2 title="My Title" disabled=true }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{: #custom-id .class1 .class2 title="My Title" disabled="disabled" }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => 'disabled']];
yield [new Cursor('{:target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{: target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{: disabled}'), ['disabled' => true]];
yield [new Cursor('{: disabled=disabled}'), ['disabled' => 'disabled']];

// Examples without colons
yield [new Cursor('{title="My Title"}'), ['title' => 'My Title']];
Expand All @@ -69,12 +70,13 @@ public static function dataForTestParseAttributes(): iterable
yield [new Cursor('{ #custom-id }'), ['id' => 'custom-id']];
yield [new Cursor('{ #custom-id #another-id }'), ['id' => 'another-id']];
yield [new Cursor('{ .class1 .class2 }'), ['class' => 'class1 class2']];
yield [new Cursor('{ #custom-id .class1 .class2 title="My Title" disabled }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{ #custom-id .class1 .class2 title="My Title" disabled=true }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => true]];
yield [new Cursor('{ #custom-id .class1 .class2 title="My Title" disabled="disabled" }'), ['id' => 'custom-id', 'class' => 'class1 class2', 'title' => 'My Title', 'disabled' => 'disabled']];
yield [new Cursor('{target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{ target=_blank}'), ['target' => '_blank']];
yield [new Cursor('{target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{ target=_blank }'), ['target' => '_blank']];
yield [new Cursor('{disabled}'), ['disabled' => true]];
yield [new Cursor('{disabled=disabled}'), ['disabled' => 'disabled']];

// Stuff at the beginning
yield [new Cursor(' {: #custom-id }'), ['id' => 'custom-id']];
Expand Down

0 comments on commit cb13267

Please sign in to comment.