Skip to content

[FIX]: be more strict on anonymous links #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use RuntimeException;

use function array_merge;
use function array_shift;
use function strtolower;
use function trim;

/**
* Our document parser contains
Expand All @@ -42,6 +45,12 @@ class DocumentParserContext
/** @var string[] */
private array $titleLetters = [];

/** @var array<string, string> */
private array $links = [];

/** @var string[] */
private array $anonymous = [];

public function __construct(
private readonly ParserContext $context,
TextRoleFactory $textRoleFactory,
Expand Down Expand Up @@ -110,6 +119,33 @@ public function setCodeBlockDefaultLanguage(string $codeBlockDefaultLanguage): v
$this->codeBlockDefaultLanguage = $codeBlockDefaultLanguage;
}

public function setLink(string $name, string $url): void
{
$name = strtolower(trim($name));

if ($name === '_') {
$name = array_shift($this->anonymous);
}

$this->links[$name] = trim($url);
}

public function resetAnonymousStack(): void
{
$this->anonymous = [];
}

public function pushAnonymous(string $name): void
{
$this->anonymous[] = strtolower(trim($name));
}

/** @return array<string, string> */
public function getLinks(): array
{
return $this->links;
}

/** @return array<string, string> */
public function getLoggerInformation(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected function getCatchablePatterns(): array
'\\\\``', // must be a separate case, as the next pattern would split in "\`" + "`", causing it to become a intepreted text
'\\\\[\s\S]', // Escaping hell... needs escaped slash in regex, but also in php.
'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}',
'[a-z0-9-]+_{2}', //Inline href.
'[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href.
'(?<=^|\s)[a-z0-9-]+_{2}', //Inline href.
'(?<=^|\s)[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href.
'``.+?``(?!`)',
'_{2}',
'_',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
$this->structuralElements->apply($blockContext, $on);
}

$on->setLinks($blockContext->getDocumentParserContext()->getLinks());

return $on;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink

private function createAnonymousReference(BlockContext $blockContext, string $link, string|null $embeddedUrl): HyperLinkNode
{
$blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack();
$node = $this->createReference($blockContext, $link, $embeddedUrl, false);
$blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link);
$blockContext->getDocumentParserContext()->pushAnonymous($link);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink

private function createAnonymousReference(BlockContext $blockContext, string $link): HyperLinkNode
{
$blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack();
$node = $this->createReference($blockContext, $link, null, false);
$blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link);
$blockContext->getDocumentParserContext()->pushAnonymous($link);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function createReference(BlockContext $blockContext, string $link, str
$link = trim(preg_replace('/\s+/', ' ', $link) ?? '');

if ($registerLink && $embeddedUrl !== null) {
$blockContext->getDocumentParserContext()->getContext()->setLink($link, $embeddedUrl);
$blockContext->getDocumentParserContext()->setLink($link, $embeddedUrl);
}

return new HyperLinkNode($link, $embeddedUrl ?? $link);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
}

//TODO: pass link object to setLink
$blockContext->getDocumentParserContext()->getContext()->setLink($link->getName(), $link->getUrl());
$blockContext->getDocumentParserContext()->setLink($link->getName(), $link->getUrl());

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testParseLink(
self::assertTrue($this->rule->applies($blockContext));
self::assertEquals($expectedNode, $this->rule->apply($blockContext));

self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getContext()->getLinks());
self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getLinks());
}

/** @return Generator<string, array{string, string, string, AnchorNode|null}> */
Expand Down
1 change: 0 additions & 1 deletion packages/guides/src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function parse(
$parser = $this->determineParser($inputFormat);

$document = $parser->parse($this->parserContext, $text);
$document->setLinks($this->parserContext->getLinks());

$this->parserContext = null;

Expand Down
36 changes: 0 additions & 36 deletions packages/guides/src/ParserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@
use League\Uri\UriInfo;
use phpDocumentor\Guides\Nodes\ProjectNode;

use function array_shift;
use function dirname;
use function ltrim;
use function strtolower;
use function trim;

class ParserContext
{
/** @var array<string, string> */
private array $links = [];

/** @var string[] */
private array $anonymous = [];

public function __construct(
private readonly ProjectNode $projectNode,
private readonly string $currentFileName,
Expand All @@ -43,33 +34,6 @@ public function getInitialHeaderLevel(): int
return $this->initialHeaderLevel;
}

public function setLink(string $name, string $url): void
{
$name = strtolower(trim($name));

if ($name === '_') {
$name = array_shift($this->anonymous);
}

$this->links[$name] = trim($url);
}

public function resetAnonymousStack(): void
{
$this->anonymous = [];
}

public function pushAnonymous(string $name): void
{
$this->anonymous[] = strtolower(trim($name));
}

/** @return array<string, string> */
public function getLinks(): array
{
return $this->links;
}

public function absoluteRelativePath(string $url): string
{
$uri = Uri::createFromString($url);
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/tests/anonymous/anonymous.html
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
SKIP anonymous references don't work
<p>I love <a href="https://github.com/">GitHub</a>, <a href="https://gitlab.com/">GitLab</a> and <a href="https://facebook.com/">Facebook</a>. But I don't affect references to __CLASS__.</p>
<p>I love <a href="https://github.com/">GitHub</a>, <a href="https://gitlab.com/">GitLab</a> and <a href="https://facebook.com/">Facebook</a>. But I don&#039;t affect references to __CLASS__.</p>
2 changes: 1 addition & 1 deletion tests/Functional/tests/anonymous/anonymous.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
I love GitHub__, GitLab__ and `Facebook`__. But I don't affect references to __CLASS__.

.. __: https://github.com/
__ https://github.com/
.. __: https://gitlab.com/
.. __: https://facebook.com/

This file was deleted.

This file was deleted.