Skip to content
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

Fix duplicate implicit references with the same id #175

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Upgrade to 0.6

## TitleNode requires an id

The TitleNode constructor is changed to require an ID as 4th argument.
Similarly, `Doctrine\RST\NodeFactory::createTitleNode()` has been
updated with an ID as 4th argument.

# Upgrade to 0.5

## Property visibility changed from protected to private
Expand Down
4 changes: 2 additions & 2 deletions lib/NodeFactory/DefaultNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public function createTocNode(Environment $environment, array $files, array $opt
return $tocNode;
}

public function createTitleNode(Node $value, int $level, string $token): TitleNode
public function createTitleNode(Node $value, int $level, string $token, string $id): TitleNode
{
$titleNode = $this->create(NodeTypes::TITLE, [$value, $level, $token]);
$titleNode = $this->create(NodeTypes::TITLE, [$value, $level, $token, $id]);
assert($titleNode instanceof TitleNode);

return $titleNode;
Expand Down
2 changes: 1 addition & 1 deletion lib/NodeFactory/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function createDocumentNode(Environment $environment): DocumentNode;
*/
public function createTocNode(Environment $environment, array $files, array $options): TocNode;

public function createTitleNode(Node $value, int $level, string $token): TitleNode;
public function createTitleNode(Node $value, int $level, string $token, string $id): TitleNode;

public function createSeparatorNode(int $level): SeparatorNode;

Expand Down
22 changes: 22 additions & 0 deletions lib/Nodes/DocumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function array_unshift;
use function assert;
use function count;
use function in_array;
use function is_string;
use function sprintf;

Expand All @@ -33,6 +34,9 @@ class DocumentNode extends Node
/** @var Node[] */
private $nodes = [];

/** @var string[] */
private $implicitLinkTargets = [];

public function __construct(Environment $environment)
{
parent::__construct();
Expand Down Expand Up @@ -91,6 +95,24 @@ public function getNodes(?callable $function = null): array
return $nodes;
}

/**
* Creates an implicit hyperlink target for this document.
*
* @param string $preferredId The preferred ID of the hyperlink target, the actual ID is returned by the method (this avoids duplicates)
wouterj marked this conversation as resolved.
Show resolved Hide resolved
*/
public function createImplicitLinkTarget(string $preferredId): string
{
$i = 1;
$actualId = $preferredId;
while (in_array($actualId, $this->implicitLinkTargets, true)) {
$actualId = $preferredId . '-' . ($i++);
}

$this->implicitLinkTargets[] = $actualId;

return $actualId;
}

public function getTitle(): ?string
{
foreach ($this->nodes as $node) {
Expand Down
6 changes: 2 additions & 4 deletions lib/Nodes/TitleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Doctrine\RST\Nodes;

use Doctrine\RST\Environment;

class TitleNode extends Node
{
/** @var SpanNode */
Expand All @@ -23,13 +21,13 @@ class TitleNode extends Node
/** @var string */
private $target = '';

public function __construct(Node $value, int $level, string $token)
public function __construct(Node $value, int $level, string $token, string $id)
{
parent::__construct($value);

$this->level = $level;
$this->token = $token;
$this->id = Environment::slugify($this->value->getText());
$this->id = $id;
}

public function getValue(): SpanNode
Expand Down
4 changes: 3 additions & 1 deletion lib/Parser/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,13 @@ private function flush(): void
$level = $this->environment->getConfiguration()->getInitialHeaderLevel() + $level - 1;

$token = $this->environment->createTitle($level);
$id = $this->document->createImplicitLinkTarget(Environment::slugify($data));

$node = $this->nodeFactory->createTitleNode(
$this->parser->createSpanNode($data),
$level,
$token
$token,
$id
);

if ($this->lastTitleNode !== null) {
Expand Down
4 changes: 2 additions & 2 deletions tests/DefaultNodeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public function testCreateTitle(): void

$nodeInstantiator->expects(self::once())
->method('create')
->with([$node, 1, 'test'])
->with([$node, 1, 'test', 'test'])
->willReturn($expectedReturn);

$defaultNodeFactory = $this->createDefaultNodeFactory($nodeInstantiator);

self::assertSame($expectedReturn, $defaultNodeFactory->createTitleNode($node, 1, 'test'));
self::assertSame($expectedReturn, $defaultNodeFactory->createTitleNode($node, 1, 'test', 'test'));
}

public function testCreateSeparator(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1>
<h1>
Level 1 Test 4
</h1>
<div class="section" id="level-2-test-3">
<div class="section" id="level-2-test-3-1">
<h2>
Level 2 Test 3
</h2>
Expand Down
5 changes: 5 additions & 0 deletions tests/Functional/tests/titles/titles.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ <h3>
<h2>
em
</h2>
<div class="section" id="first-subtitle-1">
<h3>
First subtitle
</h3>
</div>
</div>
</div>
<div class="section" id="second-main-title">
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/tests/titles/titles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Fourth subsubtitle
em
--

First subtitle
~~~~~~~~~~~~~~

Second Main Title
=================

Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/tests/titles/titles.tex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ \subsection{Fourth subsubtitle}

\section{em}

\subsection{First subtitle}




\chapter{Second Main Title}
Expand Down