diff --git a/src/Value/URL.php b/src/Value/URL.php index f6e7b974..55ee04f1 100644 --- a/src/Value/URL.php +++ b/src/Value/URL.php @@ -9,12 +9,15 @@ use Sabberworm\CSS\Parsing\SourceException; use Sabberworm\CSS\Parsing\UnexpectedEOFException; use Sabberworm\CSS\Parsing\UnexpectedTokenException; +use Sabberworm\CSS\ShortClassNameProvider; /** * This class represents URLs in CSS. `URL`s always output in `URL("")` notation. */ class URL extends PrimitiveValue { + use ShortClassNameProvider; + /** * @var CSSString */ @@ -80,4 +83,19 @@ public function render(OutputFormat $outputFormat): string { return "url({$this->url->render($outputFormat)})"; } + + /** + * @return array|null> + * + * @internal + */ + public function getArrayRepresentation(): array + { + return [ + 'class' => $this->getShortClassName(), + // We're using the term "uri" here to match the wording used in the specs: + // https://www.w3.org/TR/CSS22/syndata.html#uri + 'uri' => $this->url->getArrayRepresentation(), + ]; + } } diff --git a/tests/Unit/Value/URLTest.php b/tests/Unit/Value/URLTest.php index 8b4c0880..9cdda5dc 100644 --- a/tests/Unit/Value/URLTest.php +++ b/tests/Unit/Value/URLTest.php @@ -84,12 +84,27 @@ public function getLineNumberReturnsLineNumberProvidedToConstructor(): void /** * @test */ - public function getArrayRepresentationThrowsException(): void + public function getArrayRepresentationIncludesClassName(): void { - $this->expectException(\BadMethodCallException::class); + $subject = new URL(new CSSString('https://example.com')); - $subject = new URL(new CSSString('http://example.com')); + $result = $subject->getArrayRepresentation(); + + self::assertArrayHasKey('class', $result); + self::assertSame('URL', $result['class']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesUri(): void + { + $uri = 'https://example.com'; + $subject = new URL(new CSSString($uri)); + + $result = $subject->getArrayRepresentation(); - $subject->getArrayRepresentation(); + self::assertArrayHasKey('uri', $result); + self::assertSame(['class' => 'CSSString', 'contents' => $uri], $result['uri']); } }