Skip to content
Newer
Older
100644 165 lines (135 sloc) 3.75 KB
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
1 <?php
2 /**
3 * Property sorter.
4 * Order CSS properties according to a sorting table.
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
5 *
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
6 * Examples use the predefined sorting table.
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
7 *
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
8 * To define a custom sorting table globally define $CSSCRUSH_PROPERTY_SORT_ORDER.
9 * Assign an empty array to create an alphabetical sort:
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
10 *
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
11 * $CSSCRUSH_PROPERTY_SORT_ORDER = array( 'color', ... );
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
12 *
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
13 *
14 * @before
15 * color: red;
16 * background: #000;
17 * opacity: .5;
18 * display: block;
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
19 *
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
20 * @after
21 * display: block;
22 * opacity: .5;
23 * color: red;
24 * background: #000;
25 *
26 */
27
28 csscrush_hook::add( 'rule_prealias', 'csscrush__property_sorter' );
29
30 function csscrush__property_sorter ( csscrush_rule $rule ) {
31
32 $new_set = array();
33
34 // Create plain array of rule declarations.
35 foreach ( $rule as $declaration ) {
36 $new_set[] = $declaration;
37 }
38
39 usort( $new_set, '_csscrush__property_sorter_callback' );
40
41 $rule->declarations = $new_set;
42 }
43
44
45 /*
46 Callback for sorting
47 */
48 function _csscrush__property_sorter_callback ( $a, $b ) {
49
50 $map =& _csscrush__property_sorter_get_table();
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
51 $a_prop =& $a->canonicalProperty;
52 $b_prop =& $b->canonicalProperty;
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
53 $a_listed = isset( $map[ $a_prop ] );
54 $b_listed = isset( $map[ $b_prop ] );
55
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
56 // If the properties are identical we need to flag for an index comparison.
57 $compare_indexes = false;
58
59 // If the 'canonical' properties are identical we need to flag for a vendor comparison.
60 $compare_vendor = false;
61
62 // If both properties are listed.
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
63 if ( $a_listed && $b_listed ) {
64
65 if ( $a_prop === $b_prop ) {
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
66 if ( $a->vendor || $b->vendor ) {
67 $compare_vendor = true;
68 }
69 else {
70 $compare_indexes = true;
71 }
72 }
73 else {
74 // Table comparison.
75 return $map[ $a_prop ] > $map[ $b_prop ] ? 1 : -1;
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
76 }
77 }
78
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
79 // If one property is listed it always takes higher priority.
80 elseif ( $a_listed && ! $b_listed ) {
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
81 return -1;
82 }
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
83 elseif ( $b_listed && ! $a_listed ) {
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
84 return 1;
85 }
86
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
87 // If neither property is listed.
88 else {
89
90 if ( $a_prop === $b_prop ) {
91 if ( $a->vendor || $b->vendor ) {
92 $compare_vendor = true;
93 }
94 else {
95 $compare_indexes = true;
96 }
97 }
98 else {
99 // Regular sort.
100 return $a_prop > $b_prop ? 1 : -1;
101 }
102 }
103
104 // Comparing by index.
105 if ( $compare_indexes ) {
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
106 return $a->index > $b->index ? 1 : -1;
107 }
108
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
109 // Comparing by vendor mark.
110 if ( $compare_vendor ) {
111 if ( ! $a->vendor && $b->vendor ) {
112 return 1;
113 }
114 elseif ( $a->vendor && ! $b->vendor ) {
115 return -1;
116 }
117 else {
118 // If both have a vendor mark compare vendor name length.
119 return strlen( $b->vendor ) > strlen( $a->vendor ) ? 1 : -1;
120 }
121 }
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
122 }
123
124
125 /*
126 Cache for the table of values to compare against.
127
128 If you need to re-define the sort table during runtime unset the cache first:
129 unset( $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ] );
130 */
131 function &_csscrush__property_sorter_get_table () {
132
133 // Check for cached table.
134 if ( isset( $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ] ) ) {
135 return $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ];
136 }
137
138 $table = array();
139
140 // Nothing cached, check for a user-defined table.
141 if ( isset( $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER' ] ) ) {
142 $table = (array) $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER' ];
143 }
144
145 // No user-defined table, use pre-defined.
146 else {
147
148 // Load from property-sorting.ini
149 if ( $sorting_file_contents
150 = file_get_contents( csscrush::$config->location . '/misc/property-sorting.ini' ) ) {
151
152 $sorting_file_contents = preg_replace( '!;[^\r\n]*!', '', $sorting_file_contents );
153 $table = preg_split( '!\s+!', trim( $sorting_file_contents ) );
154 }
155 else {
156 trigger_error( __METHOD__ . ": Property sorting file not found.\n", E_USER_NOTICE );
157 }
158 }
159
7b1c3fc @peteboere Property sorter plugin now supports legacy vendor prefixes (for linti…
authored Sep 12, 2012
160 // Cache the table (and flip it).
161 $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ] = array_flip( $table );
c324685 @peteboere Plugin API updates
authored Sep 1, 2012
162
163 return $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ];
164 }
Something went wrong with that request. Please try again.