Skip to content

Commit

Permalink
Fix elements with content of "0" not being rendered (fixes #376)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Jun 18, 2019
1 parent c223969 commit d888434
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

### Fixed

- Fixed bug where elements with content of `"0"` wouldn't be rendered (#376)

## [0.19.2] - 2019-05-19

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlElement.php
Expand Up @@ -38,7 +38,7 @@ public function __construct(string $tagName, array $attributes = [], $contents =
$this->attributes = $attributes;
$this->selfClosing = $selfClosing;

$this->setContents($contents ?: '');
$this->setContents($contents ?? '');
}

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ public function getContents(bool $asString = true)
*/
public function setContents($contents): self
{
$this->contents = $contents ?: '';
$this->contents = $contents ?? '';

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Inline/Parser/CloseBracketParser.php
Expand Up @@ -162,7 +162,7 @@ protected function tryParseInlineLinkAndTitle(Cursor $cursor)
$title = '';
// make sure there's a space before the title:
if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $cursor->peek(-1))) {
$title = LinkParserHelper::parseLinkTitle($cursor) ?: '';
$title = LinkParserHelper::parseLinkTitle($cursor) ?? '';
}

$cursor->advanceToNextNonSpaceOrNewline();
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/HtmlElementTest.php
Expand Up @@ -114,4 +114,17 @@ public function testNullContentSetter()
$img->setContents(null);
$this->assertTrue($img->getContents(false) === '');
}

/**
* See https://github.com/thephpleague/commonmark/issues/376
*/
public function testRegressionWith0NotBeingRendered()
{
$element = new HtmlElement('em');
$element->setContents('0');
$this->assertSame('0', $element->getContents());

$element = new HtmlElement('em', [], '0');
$this->assertSame('0', $element->getContents());
}
}

0 comments on commit d888434

Please sign in to comment.