Skip to content

Commit

Permalink
Handle falsy header elements, fixes #514 (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuurlant authored May 2, 2022
1 parent 193515a commit 7c00709
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public function has(string $name): bool
*/
public function get(string $name)
{
if (\array_key_exists($name, $this->elements)) {
return $this->resolveXRef($name);
if (\array_key_exists($name, $this->elements) && $element = $this->resolveXRef($name)) {
return $element;
}

return new ElementMissing();
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(
?Config $config = null
) {
$this->document = $document;
$this->header = null !== $header ? $header : new Header();
$this->header = $header ?? new Header();
$this->content = $content;
$this->config = $config;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public function testGet(): void
$this->assertTrue($header->get('Font') instanceof Page);
$this->assertTrue($header->get('Image') instanceof ElementMissing);
$this->assertTrue($header->get('Resources') instanceof ElementMissing);

/**
* A double forward slash in the header's content results in a falsy element
* that should be parsed to ElementMissing instead.
*
* @see https://github.com/smalot/pdfparser/pull/525
*/
$content = '<</Type/Page/SubType//Text>>foo';
$position = 0;
$header = Header::parse($content, $document, $position);

$this->assertTrue($header->get('SubType') instanceof ElementMissing);
}

public function testResolveXRef(): void
Expand Down

0 comments on commit 7c00709

Please sign in to comment.