]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/css-sanitizer/src/Objects/AtRule.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / css-sanitizer / src / Objects / AtRule.php
1 <?php
2 /**
3  * @file
4  * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5  */
6
7 namespace Wikimedia\CSS\Objects;
8
9 use Wikimedia\CSS\Util;
10
11 /**
12  * Represent a CSS at-rule
13  */
14 class AtRule extends Rule implements DeclarationOrAtRule {
15
16         /** @var string */
17         protected $name;
18
19         /** @var ComponentValueList */
20         protected $prelude;
21
22         /** @var SimpleBlock|null */
23         protected $block = null;
24
25         /**
26          * @param Token $token An at-keyword token
27          */
28         public function __construct( Token $token ) {
29                 if ( $token->type() !== Token::T_AT_KEYWORD ) {
30                         throw new \InvalidArgumentException(
31                                 "At rule must begin with an at-keyword token, got {$token->type()}"
32                         );
33                 }
34
35                 parent::__construct( $token );
36                 $this->name = $token->value();
37                 $this->prelude = new ComponentValueList();
38         }
39
40         public function __clone() {
41                 $this->prelude = clone( $this->prelude );
42                 if ( $this->block ) {
43                         $this->block = clone( $this->block );
44                 }
45         }
46
47         /**
48          * Create an at-rule by name
49          * @param string $name
50          * @return AtRule
51          */
52         public static function newFromName( $name ) {
53                 return new static( new Token( Token::T_AT_KEYWORD, $name ) );
54         }
55
56         /**
57          * Return the at-rule's name, e.g. "media"
58          * @return string
59          */
60         public function getName() {
61                 return $this->name;
62         }
63
64         /**
65          * Return the at-rule's prelude
66          * @return ComponentValueList
67          */
68         public function getPrelude() {
69                 return $this->prelude;
70         }
71
72         /**
73          * Return the at-rule's block
74          * @return SimpleBlock|null
75          */
76         public function getBlock() {
77                 return $this->block;
78         }
79
80         /**
81          * Set the block
82          * @param SimpleBlock|null $block
83          */
84         public function setBlock( SimpleBlock $block = null ) {
85                 if ( $block->getStartTokenType() !== Token::T_LEFT_BRACE ) {
86                         throw new \InvalidArgumentException( 'At-rule block must be delimited by {}' );
87                 }
88                 $this->block = $block;
89         }
90
91         /**
92          * @param string $function Function to call, toTokenArray() or toComponentValueArray()
93          */
94         private function toTokenOrCVArray( $function ) {
95                 $ret = [];
96
97                 $ret[] = new Token(
98                         Token::T_AT_KEYWORD, [ 'value' => $this->name, 'position' => [ $this->line, $this->pos ] ]
99                 );
100                 // Manually looping and appending turns out to be noticably faster than array_merge.
101                 foreach ( $this->prelude->$function() as $v ) {
102                         $ret[] = $v;
103                 }
104                 if ( $this->block ) {
105                         foreach ( $this->block->$function() as $v ) {
106                                 $ret[] = $v;
107                         }
108                 } else {
109                         $ret[] = new Token( Token::T_SEMICOLON );
110                 }
111
112                 return $ret;
113         }
114
115         public function toTokenArray() {
116                 return $this->toTokenOrCVArray( __FUNCTION__ );
117         }
118
119         public function toComponentValueArray() {
120                 return $this->toTokenOrCVArray( __FUNCTION__ );
121         }
122
123         public function __toString() {
124                 return Util::stringify( $this );
125         }
126 }