Skip to content

Commit

Permalink
Merge branch '2.1' into 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Feb 13, 2022
2 parents f43ca03 + 34712ca commit 99be96f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 16 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/backwards-compatibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "Backwards compatibility"

on:
pull_request:

jobs:
bc-check:
name: "Backwards compatibility check"

runs-on: "ubuntu-latest"

steps:
- name: "Checkout"
uses: "actions/[email protected]"
with:
fetch-depth: 0

- name: "BC Check"
uses: docker://nyholm/roave-bc-check-ga
with:
args: --from=${{ github.event.pull_request.base.sha }}
8 changes: 0 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ jobs:

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

bc_check:
name: Backward Compatibility
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Roave BC Check
uses: docker://nyholm/roave-bc-check-ga

docs-lint:
name: Markdownlint
runs-on: ubuntu-latest
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

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

## [2.1.2] - 2022-02-13

### Fixed

- Fixed double-escaping of image alt text (#806, #810)
- Fixed Psalm typehints for event class names

## [2.1.1] - 2022-01-02

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

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

## [2.0.3] - 2022-02-13

### Fixed

- Fixed double-escaping of image alt text (#806, #810)
- Fixed Psalm typehints for event class names

## [2.0.2] - 2021-08-14

### Changed
Expand Down Expand Up @@ -398,8 +412,10 @@ No changes were introduced since the previous release.
[unreleased]: https://github.com/thephpleague/commonmark/compare/2.2.1...main
[2.2.1]: https://github.com/thephpleague/commonmark/compare/2.2.0...2.2.1
[2.2.0]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.2.0
[2.1.2]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.1.2
[2.1.1]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.1
[2.1.0]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.0
[2.0.3]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.0.3
[2.0.2]: https://github.com/thephpleague/commonmark/compare/2.0.1...2.0.2
[2.0.1]: https://github.com/thephpleague/commonmark/compare/2.0.0...2.0.1
[2.0.0]: https://github.com/thephpleague/commonmark/compare/2.0.0-rc2...2.0.0
Expand Down
6 changes: 3 additions & 3 deletions src/Environment/EnvironmentBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function addRenderer(string $nodeClass, NodeRendererInterface $renderer,
/**
* Registers the given event listener
*
* @param string $eventClass Fully-qualified class name of the event this listener should respond to
* @param callable $listener Listener to be executed
* @param int $priority Priority (a higher number will be executed earlier)
* @param class-string $eventClass Fully-qualified class name of the event this listener should respond to
* @param callable $listener Listener to be executed
* @param int $priority Priority (a higher number will be executed earlier)
*
* @return $this
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Event/ListenerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@
*/
final class ListenerData
{
/** @var class-string */
private string $event;

/** @var callable */
private $listener;

/**
* @param class-string $event
*/
public function __construct(string $event, callable $listener)
{
$this->event = $event;
$this->listener = $listener;
}

/**
* @return class-string
*/
public function getEvent(): string
{
return $this->event;
Expand Down
22 changes: 19 additions & 3 deletions src/Extension/CommonMark/Renderer/Inline/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
namespace League\CommonMark\Extension\CommonMark\Renderer\Inline;

use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
use League\CommonMark\Node\Inline\Newline;
use League\CommonMark\Node\Node;
use League\CommonMark\Node\NodeIterator;
use League\CommonMark\Node\StringContainerInterface;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
Expand Down Expand Up @@ -51,9 +54,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
$attrs['src'] = $node->getUrl();
}

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

if (($title = $node->getTitle()) !== null) {
$attrs['title'] = $title;
Expand Down Expand Up @@ -88,4 +89,19 @@ public function getXmlAttributes(Node $node): array
'title' => $node->getTitle() ?? '',
];
}

private function getAltText(Image $node): string
{
$altText = '';

foreach ((new NodeIterator($node)) as $n) {
if ($n instanceof StringContainerInterface) {
$altText .= $n->getLiteral();
} elseif ($n instanceof Newline) {
$altText .= "\n";
}
}

return $altText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Inline\HtmlInline;
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
use League\CommonMark\Extension\CommonMark\Renderer\Inline\ImageRenderer;
use League\CommonMark\Node\Inline\AbstractInline;
use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Tests\Unit\Renderer\FakeChildNodeRenderer;
use League\CommonMark\Util\HtmlElement;
use League\Config\ConfigurationInterface;
Expand Down Expand Up @@ -48,7 +50,7 @@ public function testRenderWithTitle(): void
$this->assertEquals('img', $result->getTagName());
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('::children::', $result->getAttribute('alt'));
$this->assertStringContainsString('::label::', $result->getAttribute('alt'));
$this->assertStringContainsString('::title::', $result->getAttribute('title'));
$this->assertStringContainsString('::id::', $result->getAttribute('id'));
}
Expand All @@ -64,10 +66,29 @@ public function testRenderWithoutTitle(): void
$this->assertEquals('img', $result->getTagName());
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('::children::', $result->getAttribute('alt'));
$this->assertStringContainsString('::label::', $result->getAttribute('alt'));
$this->assertNull($result->getAttribute('title'));
}

public function testRenderWithInlineChildrenForAltText(): void
{
$inline = new Image('http://example.com/foo.jpg');
$inline->appendChild(new Text('an "image" with '));
$inline->appendChild(new HtmlInline('<escapable characters>'));
$inline->appendChild(new Text(' in the & title'));

$result = $this->renderer->render($inline, new FakeChildNodeRenderer());

$this->assertTrue($result instanceof HtmlElement);
$this->assertEquals('img', $result->getTagName());
$this->assertStringContainsString('http://example.com/foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('foo.jpg', $result->getAttribute('src'));
$this->assertStringContainsString('an "image" with <escapable characters> in the & title', $result->getAttribute('alt'));
$this->assertNull($result->getAttribute('title'));

$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);
}

public function testRenderAllowUnsafeLink(): void
{
$this->renderer->setConfiguration($this->createConfiguration([
Expand Down

0 comments on commit 99be96f

Please sign in to comment.