Skip to content
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
17 changes: 11 additions & 6 deletions src/Document/Dictionary/DictionaryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
$rollingCharBuffer = new RollingCharBuffer(6);
$nestingContext = (new NestingContext())->setContext(DictionaryParseContext::ROOT);
$arrayNestingLevel = 0;
$contextBeforeComment = null;
foreach ($stream->chars($startPos, $nrOfBytes) as $char) {
$rollingCharBuffer->next($char);
if ($char === DelimiterCharacter::LESS_THAN_SIGN->value && $rollingCharBuffer->getPreviousCharacter() === DelimiterCharacter::LESS_THAN_SIGN->value && $rollingCharBuffer->getPreviousCharacter(2) !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS) {
Expand Down Expand Up @@ -55,8 +56,17 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
} elseif ($nestingContext->getContext() === DictionaryParseContext::VALUE) {
self::flush($dictionaryArray, $nestingContext);
} elseif ($nestingContext->getContext() === DictionaryParseContext::COMMENT) {
$nestingContext->setContext(DictionaryParseContext::DICTIONARY);
$nestingContext->setContext($contextBeforeComment ?? DictionaryParseContext::DICTIONARY);
$contextBeforeComment = null;
}
} elseif ($char === DelimiterCharacter::PERCENT_SIGN->value && $rollingCharBuffer->getPreviousCharacter() !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_PARENTHESES) {
if ($nestingContext->getContext() === DictionaryParseContext::VALUE) {
self::flush($dictionaryArray, $nestingContext);
$contextBeforeComment = DictionaryParseContext::DICTIONARY;
} else {
$contextBeforeComment = $nestingContext->getContext();
}
$nestingContext->setContext(DictionaryParseContext::COMMENT);
} elseif (WhitespaceCharacter::tryFrom($char) !== null && $nestingContext->getContext() === DictionaryParseContext::KEY) {
$nestingContext->setContext(DictionaryParseContext::KEY_VALUE_SEPARATOR);
} elseif ($char === DelimiterCharacter::LEFT_PARENTHESIS->value && (in_array($nestingContext->getContext(), [DictionaryParseContext::KEY, DictionaryParseContext::KEY_VALUE_SEPARATOR, DictionaryParseContext::VALUE], true))) {
Expand All @@ -73,11 +83,6 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
}
} elseif (trim($char) !== '' && $nestingContext->getContext() === DictionaryParseContext::KEY_VALUE_SEPARATOR) {
$nestingContext->setContext(DictionaryParseContext::VALUE);
} elseif ($char === DelimiterCharacter::PERCENT_SIGN->value && $rollingCharBuffer->getPreviousCharacter() !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_PARENTHESES) {
if ($nestingContext->getContext() === DictionaryParseContext::VALUE) {
self::flush($dictionaryArray, $nestingContext);
}
$nestingContext->setContext(DictionaryParseContext::COMMENT);
}

match ($nestingContext->getContext()) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Document/Dictionary/DictionaryParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,23 @@ public function testHandlesKeyValuePairWithTrailingComments(): void {
DictionaryParser::parse(null, $stream, 0, $stream->getSizeInBytes()),
);
}

public function testHandlesKeyValuePairWithCommentBetweenKeyAndValue(): void {
$stream = new InMemoryStream(
<<<EOD
<< /Type %
/Catalog %
/Pages %
3 0 R %
>>
EOD,
);
static::assertEquals(
new Dictionary(
new DictionaryEntry(DictionaryKey::TYPE, TypeNameValue::CATALOG),
new DictionaryEntry(DictionaryKey::PAGES, new ReferenceValue(3, 0)),
),
DictionaryParser::parse(null, $stream, 0, $stream->getSizeInBytes()),
);
}
}
Loading