C:\xampp\htdocs\landing\wp-content\plugins\amp\vendor\ampproject\optimizer\src\CssRule.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php

namespace AmpProject\Optimizer;

/**
 * CSS rule that provides semantic handling of media queries, selectors and properties.
 *
 * This is used in conjunction with CssRules for deduplication of CSS when adding styles during transformations.
 *
 * Note: This is a simplistic representation of CSS rules built for a specific purpose.
 * Make sure it supports a given use case before including in new code!
 *
 * @package ampproject/optimizer
 */
final class CssRule
{

    
/**
     * Placeholder to use in the rule to denote an ID that has yet to be defined.
     *
     * Use applyId() to finalize the CSS rule.
     *
     * @var string.
     */
    
const ID_PLACEHOLDER '__ID__';

    
/**
     * Selector(s) to use for the CSS rule.
     *
     * @var string[]
     */
    
private $selectors;

    
/**
     * Properties to apply to the selector(s).
     *
     * @var string[]
     */
    
private $properties;

    
/**
     * Media query that wraps the CSS rule.
     *
     * @var string
     */
    
private $mediaQuery '';

    
/**
     * Rendered CSS cache.
     *
     * @var string|null
     */
    
private $renderedCss;

    
/**
     * Byte count cache.
     *
     * @var int|null
     */
    
private $byteCount;

    
/**
     * Instantiate a CssRule object.
     *
     * @param string|string[] $selectors  One or more selectors to use.
     * @param string|string[] $properties One or more properties to apply to the selector(s).
     */
    
public function __construct($selectors$properties)
    {
        
$this->selectors array_values(
            
array_unique(
                
array_filter(
                    
array_map(
                        [
$this'normalizeSelector'],
                        
$this->separateSelectors($selectors)
                    )
                )
            )
        );

        
$this->properties array_values(
            
array_unique(
                
array_filter(
                    
array_map(
                        [
$this'normalizeProperty'],
                        
$this->separateProperties($properties)
                    )
                )
            )
        );

        
sort($this->properties);
    }

    
/**
     * Create a new CSS rule that is wrapped in a media query.
     *
     * @param string $mediaQuery Media query to wrap the CSS rule in.
     * @param string|string[] $selectors  One or more selectors to use.
     * @param string|string[] $properties One or more properties to apply to the selector(s).
     * @return self CSS rule wrapped in a media query.
     */
    
public static function withMediaQuery($mediaQuery$selectors$properties)
    {
        
$cssRule = new self($selectors$properties);
        
$cssRule->mediaQuery $mediaQuery;
        return 
$cssRule;
    }

    
/**
     * Get the selector(s) for this CSS rule.
     *
     * @return string[] Selector(s) of the CSS rule.
     */
    
public function getSelectors()
    {
        return 
$this->selectors;
    }

    
/**
     * Get the properties for this CSS rule.
     *
     * @return string[] Properties of the CSS rule.
     */
    
public function getProperties()
    {
        return 
$this->properties;
    }

    
/**
     * Get the media query for this CSS rule.
     *
     * @return string Media query for the CSS rule or an empty string if none is set.
     */
    
public function getMediaQuery()
    {
        return 
$this->mediaQuery;
    }

    
/**
     * Get the CSS for this CSS rule.
     *
     * @return string CSS for this CSS rule.
     */
    
public function getCss()
    {
        if (
$this->renderedCss === null) {
            
$selectors implode(','$this->selectors);

            
$properties implode(
                
';',
                
array_map(
                    static function (
$property) {
                        return 
trim($property" \t\n\r\0\x0B;");
                    },
                    
$this->properties
                
)
            );

            if (empty(
$selectors) || empty($properties)) {
                
$this->renderedCss '';
            } else {
                
$this->renderedCss "{$selectors}{{$properties}}";

                if (! empty(
$this->mediaQuery)) {
                    
$this->renderedCss "{$this->mediaQuery}{{$this->renderedCss}}";
                }
            }
        }

        return 
$this->renderedCss;
    }

    
/**
     * Apply the provided ID across all ID placeholders.
     *
     * @param string $id ID to apply.
     * @return self
     */
    
public function applyID($id)
    {
        
$replacement_callback = static function ($css) use ($id) {
            return 
str_replace(self::ID_PLACEHOLDER$id$css);
        };

        
$this->selectors  array_map($replacement_callback$this->selectors);
        
$this->properties array_map($replacement_callback$this->properties);

        
// Reset caches so they will need to be rebuilt.
        
$this->renderedCss null;
        
$this->byteCount   null;

        return 
$this;
    }

    
/**
     * Check if the CSS rule can be merged with another provided CSS rule.
     *
     * @param CssRule $that CSS rule to check against.
     * @return bool Whether the two CSS rules can be merged.
     */
    
public function canBeMerged(CssRule $that)
    {
        if (
$this->mediaQuery !== $that->mediaQuery) {
            return 
false;
        }

        if (
            
count($this->properties) !== count($that->properties)
            || 
array_diff($this->properties$that->properties)
        ) {
            return 
false;
        }

        return 
true;
    }

    
/**
     * Merge this CSS rule with another CSS rule.
     *
     * This should only be done to same-properties rules within the same media query,
     * as the result will be wild otherwise.
     *
     * @param CssRule $that CSS rule to merge the current one with.
     * @return CssRule Merged Css rule.
     */
    
public function mergeWith(CssRule $that)
    {
        
$cssRule = new self(
            
array_merge($this->selectors$that->selectors),
            
array_merge($this->properties$that->properties)
        );

        
$cssRule->mediaQuery $this->mediaQuery;

        return 
$cssRule;
    }

    
/**
     * Get the byte count for the CSS rule.
     *
     * @return int Byte count of the CSS rule.
     */
    
public function getByteCount()
    {
        if (
$this->byteCount === null) {
            
$this->byteCount strlen($this->getCss());
        }

        return 
$this->byteCount;
    }

    
/**
     * Normalize a single selector.
     *
     * @param string $selector Selector to normalize.
     * @return string Normalized selector.
     */
    
private function normalizeSelector($selector)
    {
        
// Turn all series of whitespace into single spaces.
        
$selector preg_replace('/\s+/'' '$selector);

        
// Remove spaces around selector qualifiers to keep properties compact.
        
$selector preg_replace('/ ?([>+~]) ?/''$1'$selector);

        
// Remove leading and trailing whitespace and commas.
        
$selector trim($selector" \t\n\r\0\x0B;");

        return 
$selector;
    }

    
/**
     * Normalize single property.
     *
     * @param string $property Property to normalize.
     * @return string Normalized property.
     */
    
private function normalizeProperty($property)
    {
        
// Turn all series of whitespace into single spaces.
        
$property preg_replace('/\s+/'' '$property);

        
// Remove spaces around colons and semicolons to keep properties compact.
        
$property preg_replace('/ ?([:;]) ?/''$1'$property);

        
// Deduplicate semicolons.
        
$property preg_replace('/([;]+)/'';'$property);

        
// Remove leading and trailing whitespace and semicolons.
        
$property trim($property" \t\n\r\0\x0B;");

        return 
$property;
    }

    
/**
     * Separate selectors into individual values.
     *
     * @param string|string[]|array[] $selectors Selectors to separate.
     * @return string[] Separated selectors.
     */
    
private function separateSelectors($selectors)
    {
        
$separatedSelectors = [];

        foreach ((array)
$selectors as $selectorString) {
            if (
is_array($selectorString)) {
                
$separatedSelectors array_merge($separatedSelectors$this->separateSelectors($selectorString));
            } else {
                
$separatedSelectors array_merge($separatedSelectorsexplode(','$selectorString));
            }
        }

        return 
$separatedSelectors;
    }

    
/**
     * Separate properties into individual values.
     *
     * @param string|string[]|array[] $properties Properties to separate.
     * @return string[] Separated properties.
     */
    
private function separateProperties($properties)
    {
        
$separatedProperties = [];

        foreach ((array)
$properties as $propertyString) {
            if (
is_array($propertyString)) {
                
$separatedProperties array_merge($separatedProperties$this->separateProperties($propertyString));
            } else {
                
$separatedProperties array_merge($separatedPropertiesexplode(';'$propertyString));
            }
        }

        return 
$separatedProperties;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586