From 0cfbfbe48ef6a273bc8aa9331402dc21e9eab005 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Tue, 13 Aug 2024 14:50:07 +0200 Subject: [PATCH 1/5] added tests for texts containing curly-braces --- .../Attributes/data/special_attributes.html | 13 ++++++++- .../Attributes/data/special_attributes.md | 27 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/functional/Extension/Attributes/data/special_attributes.html b/tests/functional/Extension/Attributes/data/special_attributes.html index c37856c51f..4a2f60ebcd 100644 --- a/tests/functional/Extension/Attributes/data/special_attributes.html +++ b/tests/functional/Extension/Attributes/data/special_attributes.html @@ -12,7 +12,18 @@

The Site

Attributes without quote and non-whitespace char link

Attributes without quote and non-whitespace char and a dot link.

Multiple attributes without quote and non-whitespace char and a dot link.

-

image

+

image{some-text}

+

image

A paragraph containing {{ mustache }} templating

A paragraph ending with {{ mustache }} templating

{{ mustache }} A paragraph starting with mustache templating

+

a. Some{text}.

+

b. Some.

+

c. Some.

+

d. Some{text}.

+

e. Some.

+

f. Some{{text}}.

+

some

+

some

+

some

+

some

diff --git a/tests/functional/Extension/Attributes/data/special_attributes.md b/tests/functional/Extension/Attributes/data/special_attributes.md index c94db9739e..17d7b43897 100644 --- a/tests/functional/Extension/Attributes/data/special_attributes.md +++ b/tests/functional/Extension/Attributes/data/special_attributes.md @@ -29,7 +29,9 @@ Attributes without quote and non-whitespace char and a dot [link](http://url.com Multiple attributes without quote and non-whitespace char and a dot [link](http://url.com){#id .class target=_blank}. -![image](/assets/image.jpg){valueless-attribute} +![image](/assets/image.jpg){some-text} + +![image](/assets/image.jpg){boolean-attribute="boolean-attribute"} A paragraph containing {{ mustache }} templating @@ -37,3 +39,26 @@ A paragraph ending with {{ mustache }} templating {{ mustache }} A paragraph starting with mustache templating +a. [Some{text}](https://example.com). + +b. [Some{.text}](https://example.com). + +c. [Some](https://example.com){.text}. + +d. [Some{text}](https://example.com). + +e. [Some](https://example.com){text="text"}. + +f. [Some{{text}}](https://example.com). + +{hello="hello"} +some + +{.test hello="hello"} +some + +{hello="hello" .test} +some + +{hello="hello" goodbye="goodbye"} +some From 5b0a1d9b5a9c3fc288d8cfa389579152a8857069 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Tue, 13 Aug 2024 14:52:17 +0200 Subject: [PATCH 2/5] fixed the parser behavior for texts that contain curly braces --- src/Extension/Attributes/Util/AttributesHelper.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Extension/Attributes/Util/AttributesHelper.php b/src/Extension/Attributes/Util/AttributesHelper.php index 5fbbdea1c3..db499a50bd 100644 --- a/src/Extension/Attributes/Util/AttributesHelper.php +++ b/src/Extension/Attributes/Util/AttributesHelper.php @@ -23,8 +23,8 @@ */ final class AttributesHelper { - private const SINGLE_ATTRIBUTE = '\s*([.]-?[_a-z][^\s}]*|[#][^\s}]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . '?)\s*'; - private const ATTRIBUTE_LIST = '/^{:?(' . self::SINGLE_ATTRIBUTE . ')+}(?!})/i'; + private const SINGLE_ATTRIBUTE = '\s*([.]-?[_a-z][^\s}]*|[#][^\s}]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')\s*'; + private const ATTRIBUTE_LIST = '/^{:?(' . self::SINGLE_ATTRIBUTE . ')+}/i'; /** * @return array @@ -72,14 +72,8 @@ public static function parseAttributes(Cursor $cursor): array continue; } - $parts = \explode('=', $attribute, 2); - if (\count($parts) === 1) { - $attributes[$attribute] = true; - continue; - } - /** @psalm-suppress PossiblyUndefinedArrayOffset */ - [$name, $value] = $parts; + [$name, $value] = \explode('=', $attribute, 2); $first = $value[0]; $last = \substr($value, -1); From cb13267233307883092b585e3cba91fe3273e555 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Tue, 13 Aug 2024 15:31:00 +0200 Subject: [PATCH 3/5] allow to render empty-value attributes unsing the value in markdown --- src/Extension/Attributes/Util/AttributesHelper.php | 5 +++++ .../Extension/Attributes/data/special_attributes.html | 3 ++- .../Extension/Attributes/data/special_attributes.md | 4 +++- .../Extension/Attributes/Util/AttributesHelperTest.php | 10 ++++++---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Extension/Attributes/Util/AttributesHelper.php b/src/Extension/Attributes/Util/AttributesHelper.php index db499a50bd..2a87d60391 100644 --- a/src/Extension/Attributes/Util/AttributesHelper.php +++ b/src/Extension/Attributes/Util/AttributesHelper.php @@ -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) { diff --git a/tests/functional/Extension/Attributes/data/special_attributes.html b/tests/functional/Extension/Attributes/data/special_attributes.html index 4a2f60ebcd..be4efd48fb 100644 --- a/tests/functional/Extension/Attributes/data/special_attributes.html +++ b/tests/functional/Extension/Attributes/data/special_attributes.html @@ -22,7 +22,8 @@

The Site

c. Some.

d. Some{text}.

e. Some.

-

f. Some{{text}}.

+

f. Some.

+

g. Some{{text}}.

some

some

some

diff --git a/tests/functional/Extension/Attributes/data/special_attributes.md b/tests/functional/Extension/Attributes/data/special_attributes.md index 17d7b43897..83688fcddc 100644 --- a/tests/functional/Extension/Attributes/data/special_attributes.md +++ b/tests/functional/Extension/Attributes/data/special_attributes.md @@ -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 diff --git a/tests/unit/Extension/Attributes/Util/AttributesHelperTest.php b/tests/unit/Extension/Attributes/Util/AttributesHelperTest.php index 500b270a4b..257621f910 100644 --- a/tests/unit/Extension/Attributes/Util/AttributesHelperTest.php +++ b/tests/unit/Extension/Attributes/Util/AttributesHelperTest.php @@ -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']]; @@ -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']]; From e4c17fb3ac27c51104ddfd93e94ee223ff869ab1 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Tue, 13 Aug 2024 15:31:31 +0200 Subject: [PATCH 4/5] added documentation about empty-value attributes --- docs/2.5/extensions/attributes.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/2.5/extensions/attributes.md b/docs/2.5/extensions/attributes.md index 7ee11b9a4a..9c8a7b01d5 100644 --- a/docs/2.5/extensions/attributes.md +++ b/docs/2.5/extensions/attributes.md @@ -55,6 +55,21 @@ Output:

This is red.

``` +### Empty-Value Attributes + +Attributes can be rendered in HTML without a value by using `true` value in the markdown document: + +```markdown +{itemscope=true} +## Header +``` + +Output: + +```html +

Header

+``` + ## Installation This extension is bundled with `league/commonmark`. This library can be installed via Composer: From f8e47fc52a04298b6709e1a926994d2390e52c48 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Tue, 13 Aug 2024 15:43:49 +0200 Subject: [PATCH 5/5] phpcs fix --- src/Extension/Attributes/Util/AttributesHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extension/Attributes/Util/AttributesHelper.php b/src/Extension/Attributes/Util/AttributesHelper.php index 2a87d60391..d13a565ef6 100644 --- a/src/Extension/Attributes/Util/AttributesHelper.php +++ b/src/Extension/Attributes/Util/AttributesHelper.php @@ -75,7 +75,7 @@ public static function parseAttributes(Cursor $cursor): array /** @psalm-suppress PossiblyUndefinedArrayOffset */ [$name, $value] = \explode('=', $attribute, 2); - if ('true' === $value) { + if ($value === 'true') { $attributes[$name] = true; continue; }