Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Autoprefixer.
19
 *
20
 * This autoprefixer has been developed to satisfy the basic needs of the
21
 * theme universe when working with Bootstrap 4 universe. We do not recommend
22
 * that this tool is shared, nor used outside of this theme.
23
 *
24
 * @package    theme_universe
25
 * @copyright  2016 Frédéric Massart - FMCorz.net
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
 
29
namespace theme_universe;
30
 
31
use Sabberworm\CSS\CSSList\CSSList;
32
use Sabberworm\CSS\CSSList\Document;
33
use Sabberworm\CSS\CSSList\KeyFrame;
34
use Sabberworm\CSS\OutputFormat;
35
use Sabberworm\CSS\Parser;
36
use Sabberworm\CSS\Property\AtRule;
37
use Sabberworm\CSS\Property\Selector;
38
use Sabberworm\CSS\Rule\Rule;
39
use Sabberworm\CSS\RuleSet\AtRuleSet;
40
use Sabberworm\CSS\RuleSet\DeclarationBlock;
41
use Sabberworm\CSS\RuleSet\RuleSet;
42
use Sabberworm\CSS\Settings;
43
use Sabberworm\CSS\Value\CSSFunction;
44
use Sabberworm\CSS\Value\CSSString;
45
use Sabberworm\CSS\Value\PrimitiveValue;
46
use Sabberworm\CSS\Value\RuleValueList;
47
use Sabberworm\CSS\Value\Size;
48
use Sabberworm\CSS\Value\ValueList;
49
 
50
 
51
/**
52
 * Autoprefixer class.
53
 *
54
 * Very basic implementation covering simple needs for Bootstrap 4.
55
 *
56
 * @package    theme_universe
57
 * @copyright  2016 Frédéric Massart - FMCorz.net
58
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59
 */
60
class autoprefixer {
61
 
62
    /** @var object The CSS tree. */
63
    protected $tree;
64
 
65
    /** @var string Pseudo classes regex. */
66
    protected $pseudosregex;
67
 
68
    /** @var array At rules prefixes. */
69
    protected static $atrules = [
70
        'keyframes' => ['-webkit-', '-o-']
71
    ];
72
 
73
    /** @var array Pseudo classes prefixes. */
74
    protected static $pseudos = [
75
        '::placeholder' => ['::-webkit-input-placeholder', '::-moz-placeholder', ':-ms-input-placeholder']
76
    ];
77
 
78
    /** @var array Rule properties prefixes. */
79
    protected static $rules = [
80
        'animation' => ['-webkit-'],
81
        'appearance' => ['-webkit-', '-moz-'],
82
        'backface-visibility' => ['-webkit-'],
83
        'box-sizing' => ['-webkit-'],
84
        'box-shadow' => ['-webkit-'],
85
        'background-clip' => ['-webkit-'],
86
        'backdrop-filter' => ['-webkit-'],
87
        'background-size' => ['-webkit-'],
88
        'box-shadow' => ['-webkit-'],
89
        'column-count' => ['-webkit-', '-moz-'],
90
        'column-gap' => ['-webkit-', '-moz-'],
91
        'perspective' => ['-webkit-'],
92
        'touch-action' => ['-ms-'],
93
        'transform' => ['-webkit-', '-moz-', '-ms-', '-o-'],
94
        'transition' => ['-webkit-', '-o-'],
95
        'transition-timing-function' => ['-webkit-', '-o-'],
96
        'transition-duration' => ['-webkit-', '-o-'],
97
        'transition-property' => ['-webkit-', '-o-'],
98
        'user-select' => ['-webkit-', '-moz-', '-ms-'],
99
    ];
100
 
101
    /**
102
     * Constructor.
103
     *
104
     * @param Document $tree The CSS tree.
105
     */
106
    public function __construct(Document $tree) {
107
        debugging('theme_universe\autoprefixer() is deprecated. Required prefixes for Bootstrap ' .
108
            'are now in theme/universe/scss/moodle/prefixes.scss', DEBUG_DEVELOPER);
109
        $this->tree = $tree;
110
 
111
        $pseudos = array_map(function ($pseudo) {
112
            return '(' . preg_quote($pseudo) . ')';
113
        }, array_keys(self::$pseudos));
114
        $this->pseudosregex = '(' . implode('|', $pseudos) . ')';
115
    }
116
 
117
    /**
118
     * Manipulate an array of rules to adapt their values.
119
     *
120
     * @param array $rules The rules.
121
     * @return New array of rules.
122
     */
123
    protected function manipulaterulevalues(array $rules) {
124
        $finalrules = [];
125
 
126
        foreach ($rules as $rule) {
127
            $property = $rule->getRule();
128
            $value = $rule->getValue();
129
 
130
            if ($property === 'position' && $value === 'sticky') {
131
                $newrule = clone $rule;
132
                $newrule->setValue('-webkit-sticky');
133
                $finalrules[] = $newrule;
134
            } else if (
135
                $property === 'background-image' &&
136
                $value instanceof CSSFunction &&
137
                $value->getName() === 'linear-gradient'
138
            ) {
139
 
140
                foreach (['-webkit-', '-o-'] as $prefix) {
141
                    $newfunction = clone $value;
142
                    $newfunction->setName($prefix . $value->getName());
143
                    $newrule = clone $rule;
144
                    $newrule->setValue($newfunction);
145
                    $finalrules[] = $newrule;
146
                }
147
            }
148
 
149
            $finalrules[] = $rule;
150
        }
151
 
152
        return $finalrules;
153
    }
154
 
155
    /**
156
     * Prefix all the things!
157
     */
158
    public function prefix() {
159
        $this->processBlock($this->tree);
160
    }
161
 
162
    /**
163
     * Process block.
164
     *
165
     * @param object $block A block.
166
     * @param object $parent The parent of the block.
167
     */
168
    protected function processblock($block) {
169
        foreach ($block->getContents() as $node) {
170
            if ($node instanceof AtRule) {
171
 
172
                $name = $node->atRuleName();
173
                if (isset(self::$atrules[$name])) {
174
                    foreach (self::$atrules[$name] as $prefix) {
175
                        $newname = $prefix . $name;
176
                        $newnode = clone $node;
177
 
178
                        if ($node instanceof KeyFrame) {
179
                            $newnode->setVendorKeyFrame($newname);
180
                            $block->insert($newnode, $node);
181
                        } else {
182
                            debugging('Unhandled atRule prefixing.', DEBUG_DEVELOPER);
183
                        }
184
                    }
185
                }
186
            }
187
 
188
            if ($node instanceof CSSList) {
189
                $this->processblock($node);
190
            } else if ($node instanceof RuleSet) {
191
                $this->processdeclaration($node, $block);
192
            }
193
        }
194
    }
195
 
196
    /**
197
     * Process declaration.
198
     *
199
     * @param object $node The declaration block.
200
     * @param object $parent The parent.
201
     */
202
    protected function processdeclaration($node, $parent) {
203
        $rules = [];
204
 
205
        foreach ($node->getRules() as $key => $rule) {
206
            $name = $rule->getRule();
207
            $seen[$name] = true;
208
 
209
            if (!isset(self::$rules[$name])) {
210
                $rules[] = $rule;
211
                continue;
212
            }
213
 
214
            foreach (self::$rules[$name] as $prefix) {
215
                $newname = $prefix . $name;
216
                if (isset($seen[$newname])) {
217
                    continue;
218
                }
219
                $newrule = clone $rule;
220
                $newrule->setRule($newname);
221
                $rules[] = $newrule;
222
            }
223
 
224
            $rules[] = $rule;
225
        }
226
 
227
        $node->setRules($this->manipulateRuleValues($rules));
228
 
229
        if ($node instanceof DeclarationBlock) {
230
            $selectors = $node->getSelectors();
231
            foreach ($selectors as $key => $selector) {
232
 
233
                $matches = [];
234
                if (preg_match($this->pseudosregex, $selector->getSelector(), $matches)) {
235
 
236
                    $newnode = clone $node;
237
                    foreach (self::$pseudos[$matches[1]] as $newpseudo) {
238
                        $newselector = new Selector(str_replace($matches[1], $newpseudo, $selector->getSelector()));
239
                        $selectors[$key] = $newselector;
240
                        $newnode = clone $node;
241
                        $newnode->setSelectors($selectors);
242
                        $parent->insert($newnode, $node);
243
                    }
244
 
245
                    // We're only expecting one affected pseudo class per block.
246
                    break;
247
                }
248
            }
249
        }
250
    }
251
}