2026-03-30 14:54:57 +07:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace Sabberworm\CSS\Property;
|
|
|
|
|
|
|
|
|
|
|
|
use Sabberworm\CSS\Comment\CommentContainer;
|
|
|
|
|
|
use Sabberworm\CSS\OutputFormat;
|
|
|
|
|
|
use Sabberworm\CSS\Position\Position;
|
|
|
|
|
|
use Sabberworm\CSS\Position\Positionable;
|
2026-04-18 20:32:18 +07:00
|
|
|
|
use Sabberworm\CSS\ShortClassNameProvider;
|
2026-03-30 14:54:57 +07:00
|
|
|
|
use Sabberworm\CSS\Value\CSSString;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Class representing an `@charset` rule.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The following restrictions apply:
|
|
|
|
|
|
* - May not be found in any CSSList other than the Document.
|
|
|
|
|
|
* - May only appear at the very top of a Document’s contents.
|
|
|
|
|
|
* - Must not appear more than once.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class Charset implements AtRule, Positionable
|
|
|
|
|
|
{
|
|
|
|
|
|
use CommentContainer;
|
|
|
|
|
|
use Position;
|
2026-04-18 20:32:18 +07:00
|
|
|
|
use ShortClassNameProvider;
|
2026-03-30 14:54:57 +07:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @var CSSString
|
|
|
|
|
|
*/
|
|
|
|
|
|
private $charset;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param int<1, max>|null $lineNumber
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function __construct(CSSString $charset, ?int $lineNumber = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
|
$this->setPosition($lineNumber);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param string|CSSString $charset
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function setCharset($charset): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$charset = $charset instanceof CSSString ? $charset : new CSSString($charset);
|
|
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getCharset(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->charset->getString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return non-empty-string
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function render(OutputFormat $outputFormat): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return "{$outputFormat->getFormatter()->comments($this)}@charset {$this->charset->render($outputFormat)};";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return non-empty-string
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function atRuleName(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'charset';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function atRuleArgs(): CSSString
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->charset;
|
|
|
|
|
|
}
|
2026-04-18 20:32:18 +07:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return array<string, bool|int|float|string|array<mixed>|null>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @internal
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getArrayRepresentation(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'class' => $this->getShortClassName(),
|
|
|
|
|
|
'charset' => $this->charset->getArrayRepresentation(),
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
2026-03-30 14:54:57 +07:00
|
|
|
|
}
|