-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyfill.module
More file actions
216 lines (202 loc) · 7.45 KB
/
polyfill.module
File metadata and controls
216 lines (202 loc) · 7.45 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
<?php
/**
* Implements hook_init().
* Adds modernizr.min.js
*/
function polyfill_init() {
drupal_add_css(drupal_get_path('module', 'polyfill') . '/modernizr.js');
}
function polyfill_flush_caches() {
file_unmanaged_delete_recursive(drupal_realpath(file_default_scheme() . '://polyfill', TRUE));
}
/**
* Implements hook_css_alter().
* Searches for files with .less ending in css files array
* and applies lessc.php and polyfills.
*/
function polyfill_css_alter(&$css) {
if (variable_get('preprocess_css') != 0) {
return;
}
$path = file_default_scheme() . '://polyfill';
$files_keys = array_keys($css);
$files_values = array_values($css);
foreach ($files_keys as $key => $input_file) {
if (drupal_substr($input_file, -5) == '.less') {
$css_path = $path . '/' . dirname($input_file);
if (!is_dir($css_path)) {
drupal_mkdir($css_path, 0777, TRUE);
}
$output_file = $css_path . '/' . basename($input_file, '.less');
if (drupal_substr($output_file, -4) != '.css') {
$output_file .= '.css';
}
if (!file_exists($output_file) || ( filemtime($input_file) > filemtime($output_file) )) {
require_once('sites/all/libraries/cssparser/CSSParser.php');
require_once('sites/all/libraries/lessphp/lessc.inc.php');
$contents = drupal_load_stylesheet($input_file, FALSE);
// Build the base URL of this CSS file: start with the full URL.
$css_base_url = file_create_url($input_file);
// Move to the parent.
$css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));
// Simplify to a relative URL if the stylesheet URL starts with the
// base URL of the website.
if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
$css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
}
_drupal_build_css_path(NULL, $css_base_url . '/');
// Prefix all paths within this CSS file, ignoring external and absolute paths.
$contents = preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
$less = new lessc();
$contents = $less->parse($contents);
$parser = new CSSParser($contents);
$css = $parser->parse();
$processors = module_invoke_all('polyfill_processors');
foreach ($css->getAllDeclarationBlocks() as $block) {
foreach ($processors as $property => $callbacks) {
foreach ($block->getRules($property) as $rule) {
foreach ($callbacks as $callback) {
foreach (call_user_func($callback, $rule) as $newrule) {
$block->addRule($newrule);
}
}
}
}
}
$contents = $css->__toString();
file_save_data($contents, $output_file, FILE_EXISTS_REPLACE);
}
if (file_exists($output_file)) {
array_splice($files_keys, $key, 1, $output_file);
$files_values[$key]['data'] = $output_file;
}
}
}
$css = array_combine($files_keys, $files_values);
}
/**
* Mapping of properties and corresponding alteration hooks.
* Polyfill hooks will be provided with the current selector
* and an array of properties their values, which is passed
* by reference to be alterable.
*/
function polyfill_polyfill_processors() {
return array(
'border-radius' => array('polyfill_vendor_prefix'),
'box-shadow' => array('polyfill_vendor_prefix'),
'transition' => array('polyfill_vendor_prefix'),
'transform' => array('polyfill_vendor_prefix'),
'border-image' => array('polyfill_vendor_prefix'),
'column-count' => array('polyfill_vendor_prefix'),
'column-gap' => array('polyfill_vendor_prefix'),
);
}
/**
* Adds vendor prefixes to all properties which require them.
*/
function polyfill_vendor_prefix($rule) {
$prefixes = array('-webkit-', '-moz-', '-o-', '-ms-');
$new = array();
foreach ($prefixes as $p) {
$r = new CSSRule($p . $rule->getRule());
$r->setValue($rule->getValue());
$new[] = $r;
}
return $new;
}
/* ====================================================================== */
/* UNUSED - PORT TO NEW SUBMODULES */
/* ====================================================================== */
function polyfill_box($sel, &$prop) {
if (in_array('box', $prop['display'])) {
$prop['display'][] = '-webkit-box';
$prop['display'][] = '-moz-box';
}
}
/**
* Search for linear gradients.
*/
function polyfill_linear_gradient($sel, &$prop) {
foreach (array_keys($prop) as $p) {
foreach ($prop[$p] as $v) {
if (preg_match('/.*(linear-gradient\((.*?)\)).*$/', $v, $matches)) {
// Basic prefixes
$prefixes = array('webkit', 'moz', 'ms', 'o');
foreach ($prefixes as $pre) {
polyfill_add_rule($p, '-' . $pre . '-' . $matches[0], $prop);
}
list($d, $colors) = _polyfill_parse_gradient($v);
// Webkit syntax
$dir = array('0', '0', '0', '0');
$rule = '-webkit-gradient(linear, ';
if(preg_match('/left/', $d)) {
$dir[2] = '100%';
}
else if(preg_match('/right/', $d)) {
$dir[0] = '100%';
}
if(preg_match('/top/', $d)) {
$dir[3] = '100%';
}
else if(preg_match('/bottom/', $d)) {
$dir[1] = '100%';
}
$rule .= $dir[0] . ' ' . $dir[1] . ', ' . $dir[2] . ' ' . $dir[3];
foreach ($colors as $k => $c) {
if ($k == 0) {
$rule .= ', from';
}
else if($k == (count($colors) - 1)) {
$rule .= ', to';
}
else {
$rule .= ', color-stop';
}
if ($c['p']) {
$rule .= '(' . $c['p']/100 . ', ' . $c['c'] . ')';
}
else {
$rule .= '(' . $c['c'] . ')';
}
}
$rule .= ')';
$rule = str_replace($matches[1], $rule, $matches[0]);
polyfill_add_rule($p, $rule, $prop);
// IE Gradient FIXXME!
$rule = 'progid:DXImageTransform.Microsoft.Gradient(StartColorstr="' . $colors[0]['c'] . '", EndColorStr="' . $colors[count($colors) - 1]['c'] . '")';
polyfill_add_rule('filter', $rule, $prop);
polyfill_add_rule('-ms-filter', $rule, $prop);
}
}
}
}
function _polyfill_parse_gradient($v) {
$edirection = '/(?:(?<direction>(?:top\s+|bottom\s+)?(?:right|left)|(?:right\s+|left\s+)?(?:top|bottom)))/';
$epercent = "(?<percent>[0-9]+)\%";
$ecolor = "(?<color>\#[a-zA-Z0-9]+)";
$ecolorstop = "/(?:$ecolor(?:\s*$epercent)?)/";
$directions = array();
$colorstops = array();
$colors = array();
preg_match_all($edirection, $v, $directions);
preg_match_all($ecolorstop, $v, $colorstops);
$direction = (count($directions['direction']) > 0) ? $directions['direction'][0] : FALSE;
for ($k = 0; $k < count($colorstops['color']); $k++) {
$colors[] = array(
'c' => $colorstops['color'][$k],
'p' => $colorstops['percent'][$k],
);
}
return array($direction, $colors);
}
/**
* Adds CSSPIE behavior for CSS3 decorations in IE6-8.
*/
function polyfill_pie($sel, &$prop) {
$pie = 'url(' . file_create_url(drupal_get_path('module', 'polyfill') . '/PIE.htc') . ')';
polyfill_add_rule('behavior', $pie, $prop);
// IE8 doesn't display statically positioned PIE elements
if (!array_key_exists('position', $prop)) {
polyfill_add_rule('position', 'relative', $prop);
}
}