', $located['/@context']['value']);
+ $this->assertSame('name', $located['/name']['key']);
+ $this->assertSame('Jane Doe', $located['/name']['value']);
+ $this->assertSame('jobTitle', $located['/jobTitle']['key']);
+ $this->assertSame('Web developer', $located['/jobTitle']['value']);
+ $this->assertSame('url', $located['/url']['key']);
+ $this->assertSame('https://example.com/team/jane-doe', $located['/url']['value']);
+ $this->assertSame('worksFor', $located['/worksFor']['key']);
+ $this->assertSame('
', $located['/worksFor']['value']);
+ $this->assertSame('foundingYear', $located['/worksFor/foundingYear']['key']);
+ // the text alone, without the whitespace around it
+ $this->assertSame('2010', $located['/worksFor/foundingYear']['value']);
+ $this->assertSame('10:38-10:42', $located['/worksFor/foundingYear']['valueRange']);
+ }
+
+ public function testEachValueOfARepeatedPropertyPointsAtItsOwnMarkup(): void
+ {
+ $document = <<<'HTML'
+
+



+
+ - Step 1k1
+ - Step 2
+
+
k2
+
+HTML;
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ $this->assertSame('1.jpg', $located['/image/0']['value']);
+ $this->assertSame('2:64-2:69', $located['/image/1']['valueRange']);
+ $this->assertSame('2.jpg', $located['/image/1']['value']);
+ $this->assertSame('k2', $located['/keywords/1']['value']);
+ }
+
+ public function testRdfaKeysAndValuesPointAtTheMarkup(): void
+ {
+ $document = <<<'HTML'
+
+
Jane Doe
+
site
+
fren
+
+HTML;
+
+ $located = $this->locate(new RdfaExtractor(), $document);
+
+ $this->assertSame('typeof', $located['/@type']['key']);
+ $this->assertSame('Person', $located['/@type']['value']);
+ $this->assertSame('resource', $located['/@id']['key']);
+ $this->assertSame('#jane', $located['/@id']['value']);
+ $this->assertSame('name', $located['/name']['key']);
+ $this->assertSame('Jane Doe', $located['/name']['value']);
+ $this->assertSame('https://example.com', $located['/url']['value']);
+ $this->assertSame('fr', $located['/knowsLanguage/0']['value']);
+ $this->assertSame('en', $located['/knowsLanguage/1']['value']);
+ }
+
+ public function testColumnsCountUtf16CodeUnits(): void
+ {
+ $document = <<<'HTML'
+
🙂Jane
+HTML;
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ // the emoji takes two columns, as in a browser editor: "Jane" starts on
+ // the 90th code unit, the 89th character
+ $this->assertSame('1:90-1:94', $located['/name']['valueRange']);
+ $this->assertSame('Jane', $located['/name']['value']);
+
+ $jsonLd = '
🙂
';
+ $located = $this->locate(new JsonLdNodeExtractor(), $jsonLd);
+
+ $this->assertSame('🙂 Jane', $located['/name']['value']);
+ }
+
+ public function testIdenticalItemsOnOneLineKeepTheirOwnPositions(): void
+ {
+ $item = '
Same
';
+ $extractor = new MicrodataExtractor();
+ $parser = new JsonLdParser();
+ $elements = $extractor->extractStructuredDataContent($item . $item);
+
+ $this->assertCount(2, $elements);
+ $this->assertSame($elements[0]->content, $elements[1]->content);
+
+ $first = $parser->parse($elements[0]);
+ $second = $parser->parse($elements[1]);
+
+ $this->assertInstanceOf(ObjectStructure::class, $first);
+ $this->assertInstanceOf(ObjectStructure::class, $second);
+ $this->assertSame('1:74', (string) $first->getProperty('name')->value?->range->start);
+ $this->assertSame('1:' . (74 + \strlen($item)), (string) $second->getProperty('name')->value?->range->start);
+ }
+
+ public function testMarkupOutsideTheDocumentTreeDoesNotShiftPositions(): void
+ {
+ $document = <<<'HTML'
+
template
+
Real
+HTML;
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ $this->assertSame('Real', $located['/name']['value']);
+ $this->assertSame('name', $located['/name']['key']);
+ }
+
+ public function testAnElementWhoseTextCannotBeLocatedFallsBackOnItsStartTag(): void
+ {
+ $document = <<<'HTML'
+
+HTML;
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ $this->assertSame('
', $located['/description']['value']);
+ }
+
+ public function testMalformedMarkupIsLocatedTolerantly(): void
+ {
+ $document = <<<'HTML'
+
Jane
+
Unclosed
+HTML;
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ $this->assertSame('https://schema.org/Thing', $located['/@type']['value']);
+ $this->assertSame('name', $located['/name']['key']);
+ $this->assertSame('Jane', $located['/name']['value']);
+ $this->assertSame('', $located['/description']['value']);
+ }
+
+ public function testWindowsLineBreaksCountAsOneLine(): void
+ {
+ $document = "\r\n Jane\r\n
";
+
+ $located = $this->locate(new MicrodataExtractor(), $document);
+
+ $this->assertSame('2:25-2:29', $located['/name']['valueRange']);
+ }
+
+ /**
+ * Parses the first document extracted from $document, and reads back, for
+ * each JSON pointer, the source text its key and value ranges cover.
+ *
+ * @return array>
+ */
+ private function locate(FormatExtractorInterface $extractor, string $document): array
+ {
+ $elements = $extractor->extractStructuredDataContent($document);
+ $this->assertNotEmpty($elements);
+
+ $structure = (new JsonLdParser())->parse($elements[0]);
+ $this->assertNotNull($structure);
+
+ $lines = preg_split('/\r\n|\r|\n/', $document) ?: [];
+ $located = [];
+ $this->collect($structure, '', $lines, $located);
+
+ return $located;
+ }
+
+ /**
+ * @param list $lines
+ * @param array> $located
+ */
+ private function collect(AbstractStructure $structure, string $pointer, array $lines, array &$located): void
+ {
+ if ($structure instanceof ObjectStructure) {
+ foreach ($structure->getProperties() as $name => $property) {
+ $propertyPointer = $pointer . '/' . $name;
+ $located[$propertyPointer]['key'] = $this->read($lines, $property->key->range);
+
+ if (null === $property->value) {
+ continue;
+ }
+
+ $located[$propertyPointer]['value'] = $this->read($lines, $property->value->range);
+ $located[$propertyPointer]['valueRange'] = $property->value->range->start . '-' . $property->value->range->end;
+
+ if ($property->value->content instanceof AbstractStructure) {
+ $this->collect($property->value->content, $propertyPointer, $lines, $located);
+ }
+ }
+ } elseif ($structure instanceof ArrayStructure) {
+ foreach ($structure->getValues() as $index => $value) {
+ $valuePointer = $pointer . '/' . $index;
+ $located[$valuePointer]['value'] = $this->read($lines, $value->range);
+ $located[$valuePointer]['valueRange'] = $value->range->start . '-' . $value->range->end;
+
+ if ($value->content instanceof AbstractStructure) {
+ $this->collect($value->content, $valuePointer, $lines, $located);
+ }
+ }
+ }
+ }
+
+ /**
+ * Reads the text a single-line range covers, counting columns in UTF-16
+ * code units.
+ *
+ * @param list $lines
+ */
+ private function read(array $lines, Range $range): string
+ {
+ $this->assertNotNull($range->start);
+ $this->assertNotNull($range->end);
+ $this->assertSame($range->start->line, $range->end->line, 'The range spans one line.');
+
+ $line = mb_convert_encoding($lines[$range->start->line - 1] ?? '', 'UTF-16LE', 'UTF-8');
+ $slice = substr($line, ($range->start->column - 1) * 2, ($range->end->column - $range->start->column) * 2);
+
+ return mb_convert_encoding($slice, 'UTF-8', 'UTF-16LE');
+ }
+}
diff --git a/tests/Validation/JsonLdParserTest.php b/tests/Validation/JsonLdParserTest.php
index eb699668a..5d0d0b44a 100644
--- a/tests/Validation/JsonLdParserTest.php
+++ b/tests/Validation/JsonLdParserTest.php
@@ -65,6 +65,19 @@ public function testADocumentWithATrailingCommaIsStillRead(): void
$this->assertSame($this->range($wellFormed->getProperty('c')->value?->content), $this->range($list));
}
+ public function testColumnsCountUtf16CodeUnits(): void
+ {
+ // The emoji takes two columns, as in a browser editor; both parsers agree.
+ foreach (['{"a":"🙂","b":"x"}', '{"a":"🙂","b":"x",}'] as $content) {
+ $structure = $this->parse($content);
+ $this->assertInstanceOf(ObjectStructure::class, $structure);
+
+ $property = $structure->getProperty('b');
+ $this->assertSame('11:16 to 11:17', $property->key->range->start . ' to ' . $property->key->range->end, $content);
+ $this->assertSame('11:20 to 11:21', $property->value?->range->start . ' to ' . $property->value?->range->end, $content);
+ }
+ }
+
private function range(mixed $structure): string
{
$this->assertInstanceOf(AbstractStructure::class, $structure);