Skip to content

Commit 99be96f

Browse files
committed
Merge branch '2.1' into 2.2
2 parents f43ca03 + 34712ca commit 99be96f

File tree

7 files changed

+89
-16
lines changed

7 files changed

+89
-16
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: "Backwards compatibility"
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
bc-check:
8+
name: "Backwards compatibility check"
9+
10+
runs-on: "ubuntu-latest"
11+
12+
steps:
13+
- name: "Checkout"
14+
uses: "actions/[email protected]"
15+
with:
16+
fetch-depth: 0
17+
18+
- name: "BC Check"
19+
uses: docker://nyholm/roave-bc-check-ga
20+
with:
21+
args: --from=${{ github.event.pull_request.base.sha }}

.github/workflows/tests.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,6 @@ jobs:
102102

103103
- run: vendor/bin/psalm --no-progress --stats --threads=$(nproc) --output-format=github --shepherd
104104

105-
bc_check:
106-
name: Backward Compatibility
107-
runs-on: ubuntu-latest
108-
steps:
109-
- uses: actions/checkout@v2
110-
- name: Roave BC Check
111-
uses: docker://nyholm/roave-bc-check-ga
112-
113105
docs-lint:
114106
name: Markdownlint
115107
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
3333

3434
- Deprecated `MarkdownConverterInterface` and its `convertToHtml()` method; use `ConverterInterface` and `convert()` instead
3535

36+
## [2.1.2] - 2022-02-13
37+
38+
### Fixed
39+
40+
- Fixed double-escaping of image alt text (#806, #810)
41+
- Fixed Psalm typehints for event class names
42+
3643
## [2.1.1] - 2022-01-02
3744

3845
### Added
@@ -51,6 +58,13 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
5158

5259
- Fixed PHP 8.1 deprecation warning (#759, #762)
5360

61+
## [2.0.3] - 2022-02-13
62+
63+
### Fixed
64+
65+
- Fixed double-escaping of image alt text (#806, #810)
66+
- Fixed Psalm typehints for event class names
67+
5468
## [2.0.2] - 2021-08-14
5569

5670
### Changed
@@ -398,8 +412,10 @@ No changes were introduced since the previous release.
398412
[unreleased]: https://github.com/thephpleague/commonmark/compare/2.2.1...main
399413
[2.2.1]: https://github.com/thephpleague/commonmark/compare/2.2.0...2.2.1
400414
[2.2.0]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.2.0
415+
[2.1.2]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.1.2
401416
[2.1.1]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.1
402417
[2.1.0]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.0
418+
[2.0.3]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.0.3
403419
[2.0.2]: https://github.com/thephpleague/commonmark/compare/2.0.1...2.0.2
404420
[2.0.1]: https://github.com/thephpleague/commonmark/compare/2.0.0...2.0.1
405421
[2.0.0]: https://github.com/thephpleague/commonmark/compare/2.0.0-rc2...2.0.0

src/Environment/EnvironmentBuilderInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public function addRenderer(string $nodeClass, NodeRendererInterface $renderer,
7474
/**
7575
* Registers the given event listener
7676
*
77-
* @param string $eventClass Fully-qualified class name of the event this listener should respond to
78-
* @param callable $listener Listener to be executed
79-
* @param int $priority Priority (a higher number will be executed earlier)
77+
* @param class-string $eventClass Fully-qualified class name of the event this listener should respond to
78+
* @param callable $listener Listener to be executed
79+
* @param int $priority Priority (a higher number will be executed earlier)
8080
*
8181
* @return $this
8282
*/

src/Event/ListenerData.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@
2020
*/
2121
final class ListenerData
2222
{
23+
/** @var class-string */
2324
private string $event;
2425

2526
/** @var callable */
2627
private $listener;
2728

29+
/**
30+
* @param class-string $event
31+
*/
2832
public function __construct(string $event, callable $listener)
2933
{
3034
$this->event = $event;
3135
$this->listener = $listener;
3236
}
3337

38+
/**
39+
* @return class-string
40+
*/
3441
public function getEvent(): string
3542
{
3643
return $this->event;

src/Extension/CommonMark/Renderer/Inline/ImageRenderer.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
namespace League\CommonMark\Extension\CommonMark\Renderer\Inline;
1818

1919
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
20+
use League\CommonMark\Node\Inline\Newline;
2021
use League\CommonMark\Node\Node;
22+
use League\CommonMark\Node\NodeIterator;
23+
use League\CommonMark\Node\StringContainerInterface;
2124
use League\CommonMark\Renderer\ChildNodeRendererInterface;
2225
use League\CommonMark\Renderer\NodeRendererInterface;
2326
use League\CommonMark\Util\HtmlElement;
@@ -51,9 +54,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
5154
$attrs['src'] = $node->getUrl();
5255
}
5356

54-
$alt = $childRenderer->renderNodes($node->children());
55-
$alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt);
56-
$attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt ?? '');
57+
$attrs['alt'] = $this->getAltText($node);
5758

5859
if (($title = $node->getTitle()) !== null) {
5960
$attrs['title'] = $title;
@@ -88,4 +89,19 @@ public function getXmlAttributes(Node $node): array
8889
'title' => $node->getTitle() ?? '',
8990
];
9091
}
92+
93+
private function getAltText(Image $node): string
94+
{
95+
$altText = '';
96+
97+
foreach ((new NodeIterator($node)) as $n) {
98+
if ($n instanceof StringContainerInterface) {
99+
$altText .= $n->getLiteral();
100+
} elseif ($n instanceof Newline) {
101+
$altText .= "\n";
102+
}
103+
}
104+
105+
return $altText;
106+
}
91107
}

tests/unit/Extension/CommonMark/Renderer/Inline/ImageRendererTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
use League\CommonMark\Environment\Environment;
2020
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
21+
use League\CommonMark\Extension\CommonMark\Node\Inline\HtmlInline;
2122
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
2223
use League\CommonMark\Extension\CommonMark\Renderer\Inline\ImageRenderer;
2324
use League\CommonMark\Node\Inline\AbstractInline;
25+
use League\CommonMark\Node\Inline\Text;
2426
use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer;
2527
use League\CommonMark\Util\HtmlElement;
2628
use League\Config\ConfigurationInterface;
@@ -48,7 +50,7 @@ public function testRenderWithTitle(): void
4850
$this->assertEquals('img', $result->getTagName());
4951
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
5052
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
51-
$this->assertStringContainsString('::children::', $result->getAttribute('alt'));
53+
$this->assertStringContainsString('::label::', $result->getAttribute('alt'));
5254
$this->assertStringContainsString('::title::', $result->getAttribute('title'));
5355
$this->assertStringContainsString('::id::', $result->getAttribute('id'));
5456
}
@@ -64,10 +66,29 @@ public function testRenderWithoutTitle(): void
6466
$this->assertEquals('img', $result->getTagName());
6567
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
6668
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
67-
$this->assertStringContainsString('::children::', $result->getAttribute('alt'));
69+
$this->assertStringContainsString('::label::', $result->getAttribute('alt'));
6870
$this->assertNull($result->getAttribute('title'));
6971
}
7072

73+
public function testRenderWithInlineChildrenForAltText(): void
74+
{
75+
$inline = new Image('http://example.com/foo.jpg');
76+
$inline->appendChild(new Text('an "image" with '));
77+
$inline->appendChild(new HtmlInline('<escapable characters>'));
78+
$inline->appendChild(new Text(' in the & title'));
79+
80+
$result = $this->renderer->render($inline, new FakeChildNodeRenderer());
81+
82+
$this->assertTrue($result instanceof HtmlElement);
83+
$this->assertEquals('img', $result->getTagName());
84+
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
85+
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
86+
$this->assertStringContainsString('an "image" with <escapable characters> in the & title', $result->getAttribute('alt'));
87+
$this->assertNull($result->getAttribute('title'));
88+
89+
$this->assertSame('<img src="http://example.com/foo.jpg" alt="an &quot;image&quot; with &lt;escapable characters&gt; in the &amp; title" />', (string) $result);
90+
}
91+
7192
public function testRenderAllowUnsafeLink(): void
7293
{
7394
$this->renderer->setConfiguration($this->createConfiguration([

0 commit comments

Comments
 (0)