-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcssimportnew.php
More file actions
1444 lines (1330 loc) · 51.1 KB
/
Copy pathcssimportnew.php
File metadata and controls
1444 lines (1330 loc) · 51.1 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class for importing and using CSS (new version).
* Partly uses code from the old version, e.g. css_declaration.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author LarsDW223
*/
/**
* Class css_attribute_selector.
* Simple storage class to save exactly one CSS attribute selector.
*
* @package CSS\CSSAttributeSelector
*/
class css_attribute_selector {
/** var The namespace to which this attribute selector belongs */
protected $namespaze = NULL;
/** var The attribute name */
protected $attribute = NULL;
/** var The attribute selector operator */
protected $operator = NULL;
/** var The attribute selector value */
protected $value = NULL;
/**
* Construct the selector from $attribute_string.
*
* @param string $attribute_string String containing the selector
*/
public function __construct($attribute_string) {
$attribute_string = trim ($attribute_string, '[] ');
$found = strpos ($attribute_string, '|');
if ($found !== false &&
$attribute_string [$found+1] == '=') {
$found = strpos ($attribute_string, '|', $found+1);
}
if ($found !== false) {
if ($found > 0) {
$this->namespaze = substr ($attribute_string, 0, $found);
}
$attribute_string = substr ($attribute_string, $found + 1);
}
$found = strpos ($attribute_string, '=');
if ($found === false) {
$this->attribute = $attribute_string;
} else {
if (ctype_alpha($attribute_string [$found-1])) {
$this->attribute = substr($attribute_string, 0, $found);
$this->operator = '=';
$this->value = substr($attribute_string, $found + 1);
} else {
$this->attribute = substr($attribute_string, 0, $found - 1);
$this->operator = $attribute_string [$found-1].$attribute_string [$found];
$this->value = substr($attribute_string, $found + 1);
}
$this->value = trim ($this->value, '"');
}
}
/**
* The function checks if this atrribute selector matches the
* attributes given in $attributes as key - value pairs.
*
* @param string $attributes String containing the selector
* @return boolean
*/
public function matches (array $attributes=NULL) {
if (!isset($this->operator)) {
// Attribute should be present
return isset($attributes) && array_key_exists($this->attribute, $attributes);
} else {
if(isset($attributes[$this->attribute])) {
switch ($this->operator) {
case '=':
// Attribute should have exactly the value $this->value
if ($attributes[$this->attribute] == $this->value) {
return true;
} else {
return false;
}
break;
case '~=':
// Attribute value should contain the word $this->value
$words = preg_split ('/\s/', $attributes [$this->attribute]);
if (array_search($this->value, $words) !== false) {
return true;
} else {
return false;
}
break;
case '|=':
// Attribute value should contain the word $this->value
// or a word starting with $this->value.'-'
$with_hypen = $this->value.'-';
$length = strlen ($with_hypen);
if ($attributes [$this->attribute] == $this->value ||
strncmp($attributes [$this->attribute], $with_hypen, $length) == 0) {
return true;
}
break;
case '^=':
// Attribute value should contain
// a word starting with $this->value
$length = strlen ($this->value);
if (strncmp($attributes [$this->attribute], $this->value, $length) == 0) {
return true;
}
break;
case '$=':
// Attribute value should contain
// a word ending with $this->value
$length = -1 * strlen ($this->value);
if (substr($attributes [$this->attribute], $length) == $this->value) {
return true;
}
break;
case '*=':
// Attribute value should include $this->value
if (strpos($attributes [$this->attribute], $this->value) !== false) {
return true;
}
break;
}
}
}
return false;
}
/**
* The function returns a string representation of this attribute
* selector (only for debugging purpose).
*
* @return string
*/
public function toString () {
$returnstring = '[';
if (!empty($this->namespaze)) {
$returnstring .= $this->namespaze.'|';
}
$returnstring .= $this->attribute.$this->operator.$this->value;
$returnstring .= ']';
return $returnstring;
}
}
/**
* Class css_simple_selector
* Simple storage class to save a simple CSS selector.
*
* @package CSS\CSSSimpleSelector
*/
class css_simple_selector {
/** var Element name/Type of this simple selector */
protected $type = NULL;
/** var Pseudo element which this selector matches */
protected $pseudo_element = NULL;
/** var Id which this selector matches */
protected $id = NULL;
/** var Classes which this selector matches */
protected $classes = array();
/** var Pseudo classes which this selector matches */
protected $pseudo_classes = array();
/** var Attributes which this selector matches */
protected $attributes = array();
/** var Specificity of this selector */
protected $specificity = 0;
/**
* Internal function that checks if $sign is a sign that
* separates/identifies the different parts of an simple selector.
*
* @param character $sign
*/
protected function isSpecialSign ($sign) {
switch ($sign) {
case '.':
case '[':
case '#':
case ':':
return true;
}
return false;
}
/**
* Construct the simple selector from $simple_selector_string.
*
* @param string $simple_selector_string String containing the selector
*/
public function __construct($simple_selector_string) {
$pos = 0;
$simple_selector_string = trim ($simple_selector_string);
$max = strlen ($simple_selector_string);
if ($max == 0) {
$this->type = '*';
return;
}
$a = 0;
$b = 0;
$c = 0;
$content = '';
$first_sign = '';
$next_sign = '';
$first = true;
$pseudo_element = false;
while ($pos < $max) {
$sign = $simple_selector_string [$pos];
if ($this->isSpecialSign ($sign)) {
if ($pos == 0) {
$first_sign = $sign;
} else {
// Found the end.
if (empty($first_sign)) {
// Element name/type
$this->type = $content;
if ($content != '*') {
$c++;
}
} else if ($first_sign == '.') {
// Class
$this->classes[] = $content;
$b++;
} else if ($first_sign == '#') {
// ID
$this->id = $content;
$a++;
} else if ($first_sign == ':') {
//if ($next_sign != ':') {
if (!$pseudo_element) {
// Pseudo class
$this->pseudo_classes[] = $content;
$b++;
} else {
// Pseudo element
$this->pseudo_element = $content;
$c++;
}
} else if ($first_sign == '[') {
$this->attributes [] = new css_attribute_selector($content);
$b++;
}
$first_sign = $sign;
$next_sign = $simple_selector_string [$pos+1];
if ($first_sign == ':' && $next_sign == ':') {
$pseudo_element = true;
$pos++;
} else {
$pseudo_element = false;
}
$content = '';
}
} else {
$content .= $sign;
}
$pos++;
}
// If $content is not empty then parse it
if (!empty($content)) {
if (empty($first_sign)) {
// Element name/type
$this->type = $content;
if ($content != '*') {
$c++;
}
} else if ($first_sign == '.') {
// Class
$this->classes[] = $content;
$b++;
} else if ($first_sign == '#') {
// ID
$this->id = $content;
$a++;
} else if ($first_sign == ':') {
if ($next_sign != ':') {
// Pseudo class
$this->pseudo_classes[] = $content;
$b++;
} else {
// Pseudo element
$this->pseudo_element = $content;
$c++;
}
} else if ($first_sign == '[') {
$this->attributes [] = new css_attribute_selector($content);
$b++;
}
}
// Calculate specificity
$this->specificity = $a * 100 + $b *10 + $c;
}
/**
* The functions checks wheter this simple selector matches the given
* $element or not. $element must support the interface iElementCSSMatchable
* to enable this class to do the CSS selector matching.
*
* @param iElementCSSMatchable $element Element to check
* @return boolean
*/
public function matches_entry (iElementCSSMatchable $element) {
$element_attrs = $element->iECSSM_getAttributes();
// Match type/element
if (!empty($this->type) &&
$this->type != '*' &&
$this->type != $element->iECSSM_getName()) {
return false;
}
// Match class(es)
if (count($this->classes) > 0) {
if (empty($element_attrs ['class'])) {
return false;
}
$comp = explode (' ', $element_attrs ['class']);
foreach ($this->classes as $search) {
if (array_search($search, $comp) === false) {
return false;
}
}
}
// Match id
if (!empty($this->id) &&
$this->id != ($element_attrs ['id'] ?? null)) {
return false;
}
// Match attributes
foreach ($this->attributes as $attr_sel) {
if ($attr_sel->matches ($element_attrs) === false) {
return false;
}
}
// Match pseudo class(es)
if (count($this->pseudo_classes) > 0) {
foreach ($this->pseudo_classes as $search) {
if ($element->iECSSM_has_pseudo_class($search) == false) {
return false;
}
}
}
// Match pseudo element
if (!empty($this->pseudo_element)) {
if ($element->iECSSM_has_pseudo_element($this->pseudo_element) == false) {
return false;
}
}
return true;
}
/**
* The function returns a string representation of this simple
* selector (only for debugging purpose).
*
* @return string
*/
public function toString () {
$returnstring = '';
if (!empty($this->type)) {
$returnstring .= $this->type;
}
if (!empty($this->id)) {
$returnstring .= '#'.$this->id;
}
foreach ($this->classes as $class) {
$returnstring .= '.'.$class;
}
foreach ($this->attributes as $attr_sel) {
$returnstring .= $attr_sel->toString();
}
return $returnstring;
}
/**
* Return the specificity of this simple selector.
*
* @return integer
*/
public function getSpecificity () {
return $this->specificity;
}
}
/**
* Class css_selector.
* Storage class to save a complete CSS selector.
* The class can also store multiple selectors, e.g. like 'h1 , h2, h3 {...}'
*
* @package CSS\CSSSelector
*/
class css_selector {
/** var Known combinators */
static protected $combinators = ' ,>+~';
/** var Brackets */
static protected $brackets = '[]';
/** var String from which this selector was created */
protected $selector_string = NULL;
/** var Array with parsed selector(s) */
protected $selectors_parsed = array();
/** var Specificity of this selector */
protected $specificity = array();
/**
* Construct the selector from $selector_string.
*
* @param string $selector_string String containing the selector
*/
public function __construct($selector_string) {
$selector_string = str_replace("\n", '', $selector_string);
$this->selector_string = trim($selector_string);
$pos = 0;
$max = strlen($this->selector_string);
$current = '';
$selector = array();
$specificity = 0;
$size = 0;
$in_brackets = false;
$separators = self::$combinators.self::$brackets;
while ($pos < $max) {
$sign = $this->selector_string [$pos];
$result = strpos ($separators, $sign);
if ($sign == '[') {
$in_brackets = true;
}
if ($result === false || $in_brackets == true) {
// No combinator
$current .= $sign;
$pos++;
if ($sign == ']') {
$in_brackets = false;
}
} else {
// Parse current
$selector [$size]['selector'] = new css_simple_selector($current);
$specificity += $selector [$size]['selector']->getSpecificity();
$size++;
$current = '';
$combinator = $sign;
$pos++;
while ($pos < $max) {
$sign = $this->selector_string[$pos];
if (strpos (self::$combinators, $sign) === false) {
break;
}
$combinator .= $sign;
$pos++;
}
if (ctype_space($combinator)) {
$selector [$size]['combinator'] = ' ';
$size++;
} else {
$combinator = trim ($combinator, ' ');
if ($combinator != ',') {
$selector [$size]['combinator'] = $combinator[0];
$size++;
} else {
$this->selectors_parsed [] = $selector;
$this->specificity [] = $specificity;
$selector = array();
$size = 0;
$specificity = 0;
}
}
}
}
if (!empty($current)) {
$selector [$size]['selector'] = new css_simple_selector($current);
$specificity += $selector [$size]['selector']->getSpecificity();
$this->selectors_parsed [] = $selector;
$this->specificity [] = $specificity;
}
}
/**
* The function checks if the combined simple selectors in $selector
* match $element or not. $element must support the interface iElementCSSMatchable
* to enable this class to do the CSS selector matching.
*
* @param array $selector Internal selector array
* @param iElementCSSMatchable $element Element to check
* @return boolean
*/
protected function selector_matches (array $selector, iElementCSSMatchable $element) {
$combinator = '';
$found = 0;
$size = count($selector);
if ($size == 0 ) {
return false;
}
// First entry should be a selector
if (!isset($selector [$size-1]['selector'])) {
// No! (Error)
return false;
}
// Start comparison with the current element
$simple = $selector [$size-1]['selector'];
if ($simple->matches_entry ($element) == false) {
// If the current open element does not match then there is no match
return false;
}
if ($size == 1) {
// We are finished already
return true;
}
// Next entry should be a combinator
if (!isset($selector [$size-2]['combinator'])) {
// No! (Error)
return false;
}
$combinator = $selector [$size-2]['combinator'];
$start_search = $element;
for ($index = $size-3 ; $index >= 0 ; $index--) {
// If we get here but start_search is already negative then there are
// selectors left but no more subjects/element to match.
if ($start_search < 0) {
return false;
}
if (empty($selector [$index]['combinator'])) {
$simple = $selector [$index]['selector'];
switch ($combinator) {
case ' ':
// Find any parent, parent's parent... that matches our simple selector
do {
$parent = $start_search->iECSSM_getParent();
if (!isset($parent)) {
return false;
}
$start_search = $parent;
$is_match = $simple->matches_entry ($parent);
if ($is_match == true) {
// Found match. Stop this search.
break;
}
}while (isset($parent));
// Did we find anything?
if (!$is_match) {
// No.
return false;
}
$start_search = $parent;
break;
case '>':
// Check if we have a parent and if it matches our simple selector
$parent = $start_search->iECSSM_getParent();
if (!isset($parent)) {
return false;
}
if ($simple->matches_entry ($parent) == false) {
// No match.
return false;
}
$start_search = $parent;
break;
case '+':
// Immediate preceding sibling must match our simple selector
$sibling = $start_search->iECSSM_getPrecedingSibling();
if (!isset($sibling)) {
return false;
}
if ($simple->matches_entry ($sibling) == false) {
// No match.
return false;
}
$start_search = $sibling;
break;
case '~':
// One of the preceding siblings must match our simple selector
do {
$sibling = $start_search->iECSSM_getPrecedingSibling();
if (!isset($sibling)) {
return false;
}
$start_search = $sibling;
if ($simple->matches_entry ($sibling) == true) {
// Found match. Stop this search.
break;
}
}while (isset($sibling));
// Did we find anything?
if (!isset($sibling)) {
// No.
return false;
}
$start_search = $sibling;
break;
// We won't get the combinator ',' here cause that is
// handled at construction time by creating an array of selectors
//case ',':
// break;
}
} else {
$combinator = $selector [$index]['combinator'];
}
}
// If we get here then everything matches!
return true;
}
/**
* The functions checks wheter any selector stored in this object
* match the given $element or not. $element must support the interface
* iElementCSSMatchable to enable this class to do the CSS selector matching.
*
* @param iElementCSSMatchable $element Element to check
* @param integer $specificity Specificity of matching selector
* @return boolean
*/
public function matches (iElementCSSMatchable $element, &$specificity) {
$size = count ($this->selectors_parsed);
$match = false;
$specificity = 0;
for ($index = 0 ; $index < $size ; $index++) {
if ($this->selector_matches ($this->selectors_parsed [$index], $element) == true) {
if ($this->specificity [$index] > $specificity) {
$specificity = $this->specificity [$index];
}
$match = true;
}
}
return $match;
}
/**
* The function returns a string representation of this
* selector (only for debugging purpose).
*
* @return string
*/
public function toString () {
$returnstring = '';
$max = count($this->selectors_parsed);
$index_parsed = 0;
foreach ($this->selectors_parsed as $selector) {
$size = count($selector);
for ($index = 0 ; $index < $size ; $index++) {
if ( isset($selector [$index]['combinator']) ) {
if ($selector [$index]['combinator'] == ' ') {
$returnstring .= ' ';
} else {
$returnstring .= ' '.$selector [$index]['combinator'].' ';
}
} else {
$simple = $selector [$index]['selector'];
$returnstring .= $simple->toString();
if ($index < $size-1) {
$returnstring .= ' ';
}
}
}
$index_parsed++;
if ($index_parsed < $max) {
$returnstring .= ',';
}
}
return $returnstring;
}
}
/**
* Class css_rule_new.
*
* @package CSS\CSSRuleNew
*/
class css_rule_new {
/** @var Media selector to which this rule belongs */
protected $media = NULL;
/** @var Selector string from which this rule was created */
protected $selector = NULL;
/** @var Array of css_declaration objects */
protected $declarations = array ();
/**
* Construct rule from strings $selector and $decls.
*
* @param string $selector String containing the selector
* @param string $decls String containing the declarations
* @param string|null $media String containing the media selector
*/
public function __construct($selector, $decls, $media = NULL) {
$this->media = trim ($media);
//print ("\nNew rule: ".$media."\n"); //Debuging
// Create/parse selector
$this->selector = new css_selector ($selector);
$decls = trim ($decls, '{}');
// Parse declarations
$pos = 0;
$end = strlen ($decls);
while ( $pos < $end ) {
$colon = strpos ($decls, ':', $pos);
if ( $colon === false ) {
break;
}
$semi = strpos ($decls, ';', $colon + 1);
if ( $semi === false ) {
break;
}
$property = substr ($decls, $pos, $colon - $pos);
$property = trim($property);
$value = substr ($decls, $colon + 1, $semi - ($colon + 1));
$value = trim ($value);
$values = preg_split ('/\s+/', $value);
$value = '';
foreach ($values as $part) {
if ( $part != '!important' ) {
$value .= ' '.$part;
}
}
$value = trim($value);
// Create new declaration
$declaration = new css_declaration ($property, $value);
$this->declarations [] = $declaration;
// Handle CSS shorthands, e.g. 'border'
if ( $declaration->isShorthand () === true ) {
$declaration->explode ($this->declarations);
}
$pos = $semi + 1;
}
}
/**
* The function returns a string representation of this
* rule (only for debugging purpose).
*
* @return string
*/
public function toString () {
$returnString = '';
$returnString .= "Media= \"".$this->media."\"\n";
$returnString .= $this->selector->toString().' ';
$returnString .= "{\n";
foreach ($this->declarations as $declaration) {
$returnString .= $declaration->getProperty ().':'.$declaration->getValue ().";\n";
}
$returnString .= "}\n";
return $returnString;
}
/**
* The functions checks wheter this rule matches the given $element
* or not. $element must support the interface iElementCSSMatchable
* to enable this class to do the CSS selector matching.
*
* @param iElementCSSMatchable $element Element to check
* @param integer $specificity Specificity of matching selector
* @param string $media Media selector to match
* @return boolean
*/
public function matches(iElementCSSMatchable $element, &$specificity, $media = NULL) {
$media = trim($media);
if ( !empty($this->media) && $media !== $this->media ) {
// Wrong media
//print ("\nNo-Match ".$this->media."==".$media); //Debuging
return false;
}
// The rules does match if the selector does match
$result = $this->selector->matches($element, $specificity);
return $result;
}
/**
* The function returns the value of property $name or null if a
* property with that name does not exist in this rule.
*
* @param string $name The property name
* @return string|null
*/
public function getProperty ($name) {
foreach ($this->declarations as $declaration) {
if ( $name == $declaration->getProperty () ) {
return $declaration->getValue ();
}
}
return NULL;
}
/**
* The function stores all properties of this rule in the array
* $values as key - value pairs, e.g. $values ['color'] = 'red';
*
* @param array $values Array for property storage
* @return null
*/
public function getProperties (&$values) {
foreach ($this->declarations as $declaration) {
$property = $declaration->getProperty ();
$value = $declaration->getValue ();
$values [$property] = $value;
}
return NULL;
}
/**
* The function calls $callback for each property stored in this
* rule containing a length value. The return value of $callback
* is saved as the new property value.
*
* @param callable $callback
*/
public function adjustLengthValues ($callback) {
foreach ($this->declarations as $declaration) {
$declaration->adjustLengthValues ($callback, $this);
}
}
/**
* The function calls $callback for each property stored in this
* rule containing a URL reference. The return value of $callback
* is saved as the new property value.
*
* @param callable $callback
*/
public function replaceURLPrefixes ($callback) {
foreach ($this->declarations as $declaration) {
$declaration->replaceURLPrefixes ($callback);
}
}
}
/**
* Class cssimportnew
*
* @package CSS\CSSImportNew
*/
class cssimportnew {
/** var Imported raw CSS code */
protected $raw;
/** @var Array of css_rule_new */
protected $rules = array ();
/** @var Actually set media selector */
protected $media = NULL;
/**
* Import CSS code from string $contents.
* Returns true on success or false if any error occured during CSS parsing.
*
* @param string $contents
* @return boolean
*/
function importFromString($contents) {
$this->deleteComments ($contents);
return $this->importFromStringInternal ($contents);
}
/**
* Delete comments in $contents. All comments are overwritten with spaces.
* The '&' is required. DO NOT DELETE!!!
*
* @param $contents
*/
protected function deleteComments (&$contents) {
// Delete all comments first
$pos = 0;
$max = strlen ($contents);
$in_comment = false;
while ( $pos < $max ) {
if ( ($pos+1) < $max &&
$contents [$pos] == '/' &&
$contents [$pos+1] == '*' ) {
$in_comment = true;
$contents [$pos] = ' ';
$contents [$pos+1] = ' ';
$pos += 2;
continue;
}
if ( ($pos+1) < $max &&
$contents [$pos] == '*' &&
$contents [$pos+1] == '/' &&
$in_comment === true ) {
$in_comment = false;
$contents [$pos] = ' ';
$contents [$pos+1] = ' ';
$pos += 2;
continue;
}
if ( $in_comment === true ) {
$contents [$pos] = ' ';
}
$pos++;
}
}
/**
* Set the media selector to use for CSS matching to $media.
*
* @param string $media
*/
public function setMedia($media) {
$this->media = $media;
}
/**
* Return the actually set media selector.
*
* @return string
*/
public function getMedia() {
return $this->media;
}
/**
* Internal function that imports CSS code from string $contents.
* (The function is calling itself recursively)
*
* @param string $contents
* @param string|null $media Actually valid media selector
* @param integer $processed Position to which $contents were parsed
* @return bool
*/
protected function importFromStringInternal($contents, $media = NULL, &$processed = NULL) {
// Find all CSS rules
$pos = 0;
$max = strlen ($contents);
while ( $pos < $max ) {
$bracket_open = strpos ($contents, '{', $pos);
if ( $bracket_open === false ) {
return false;
}
$bracket_close = strpos ($contents, '}', $pos);
if ( $bracket_close === false ) {
return false;
}
// If this is a nested call we might hit a closing } for the media section
// which was the reason for this function call. In this case break and return.
if ( $bracket_close < $bracket_open ) {
$pos = $bracket_close + 1;
break;
}
// Get the part before the open bracket and the last closing bracket
// (or the start of the string).
$before_open_bracket = substr ($contents, $pos, $bracket_open - $pos);
// Is it a @something rule?
$before_open_bracket = trim ($before_open_bracket);
$at_rule_pos = stripos($before_open_bracket, '@');
if ( $at_rule_pos !== false ) {
$at_rule_end = stripos($before_open_bracket, ' ');
// Yes, decode content as normal rules with @something ... { ... }
$at_rule_name = substr ($before_open_bracket, $at_rule_pos, $at_rule_end - $at_rule_pos);
if ($at_rule_name == '@media') {
$at_rule_name = substr ($before_open_bracket, $at_rule_end);
}
$contents_in_media = substr ($contents, $bracket_open + 1);
$nested_processed = 0;
$result = $this->importFromStringInternal ($contents_in_media, $at_rule_name, $nested_processed);
if ( $result !== true ) {
// Stop parsing on error.
return false;
}
unset ($at_rule_name);
$pos = $bracket_open + 1 + $nested_processed;
} else {
// No, decode rule the normal way selector { ... }
// The selector is stored in $before_open_bracket
$decls = substr ($contents, $bracket_open + 1, $bracket_close - $bracket_open);