Skip to content

Commit

Permalink
Fix incorrect endLine positions (#187)
Browse files Browse the repository at this point in the history
This requires manually passing in the appropriate endLine to `AbstractBlock::finalize()`
as a new parameter.
  • Loading branch information
colinodell committed Sep 27, 2015
1 parent 61b07c4 commit 36cdd2e
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## [Unreleased][unreleased]

## Changed
- `AbstractBlock::finalize()` now reqires a second parameter, `$endLineNumber`

## Fixed
- Fixed incorrect `endLine` positions (#187)

## Removed
- Removed protected function Context::addChild()
- It was a duplicate of the Context::addBlock() method
Expand Down
9 changes: 3 additions & 6 deletions src/Block/Element/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,16 @@ public function isOpen()
* Finalize the block; mark it closed for modification
*
* @param ContextInterface $context
* @param int $endLineNumber
*/
public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)

This comment has been minimized.

Copy link
@fesor

fesor Sep 27, 2015

Contributor

Why not just call setEndLine in parser?

This comment has been minimized.

Copy link
@colinodell

colinodell Sep 27, 2015

Author Member

Mostly to maintain consistency with the JavaScript reference parser: https://github.com/jgm/commonmark.js/blob/master/lib/blocks.js#L748

This comment has been minimized.

Copy link
@fesor

fesor Sep 27, 2015

Contributor

I see... Thanks for clarify.

{
if (!$this->open) {
return; // TODO: Throw AlreadyClosedException?
}

$this->open = false;
if ($context->getLineNumber() > $this->getStartLine()) {
$this->endLine = $context->getLineNumber() - 1;
} else {
$this->endLine = $context->getLineNumber();
}
$this->endLine = $endLineNumber;

$context->setTip($context->getTip()->parent());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/FencedCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public function matchesNextLine(Cursor $cursor)
return true;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

// first line becomes info string
$this->info = RegexHelper::unescape(trim($this->strings->first()));
Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function getLevel()
return $this->level;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

$this->finalStringContents = implode("\n", $this->getStrings());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Block/Element/HtmlBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function matchesNextLine(Cursor $cursor)
return true;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

$this->finalStringContents = implode("\n", $this->getStrings());
}
Expand All @@ -118,7 +118,7 @@ public function handleRemainingContents(ContextInterface $context, Cursor $curso
// Check for end condition
if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) {
if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) {
$this->finalize($context);
$this->finalize($context, $context->getLineNumber());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/IndentedCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function matchesNextLine(Cursor $cursor)
return true;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

$reversed = array_reverse($this->getStrings(), true);
foreach ($reversed as $index => $line) {
Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/ListBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function matchesNextLine(Cursor $cursor)
return true;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

$this->tight = true; // tight by default

Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function matchesNextLine(Cursor $cursor)
return true;
}

public function finalize(ContextInterface $context)
public function finalize(ContextInterface $context, $endLineNumber)
{
parent::finalize($context);
parent::finalize($context, $endLineNumber);

$this->finalStringContents = preg_replace('/^ */m', '', implode("\n", $this->getStrings()));

Expand Down
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function addBlock(AbstractBlock $block)
$this->getBlockCloser()->closeUnmatchedBlocks();
$block->setStartLine($this->lineNumber);
while (!$this->tip->canContain($block)) {
$this->tip->finalize($this);
$this->tip->finalize($this, $this->lineNumber);
}

$this->tip->appendChild($block);
Expand Down
6 changes: 3 additions & 3 deletions src/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function parse($input)
}

while ($context->getTip()) {
$context->getTip()->finalize($context);
$context->getTip()->finalize($context, count($lines));
}

$this->processInlines($context, $context->getDocument()->walker());
Expand Down Expand Up @@ -175,10 +175,10 @@ private function breakOutOfLists(ContextInterface $context, AbstractBlock $block

if ($lastList) {
while ($block !== $lastList) {
$block->finalize($context);
$block->finalize($context, $context->getLineNumber());
$block = $block->parent();
}
$lastList->finalize($context);
$lastList->finalize($context, $context->getLineNumber());
}

$context->setContainer($context->getTip());
Expand Down
2 changes: 1 addition & 1 deletion src/UnmatchedBlockCloser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function closeUnmatchedBlocks()
{
while ($this->oldTip !== $this->lastMatchedContainer) {
$oldTip = $this->oldTip->parent();
$this->oldTip->finalize($this->context);
$this->oldTip->finalize($this->context, $this->context->getLineNumber() - 1);
$this->oldTip = $oldTip;
}
}
Expand Down

0 comments on commit 36cdd2e

Please sign in to comment.