function preprocess()
{
// 1. экранирование хаков, которые мешают парсить
if (strpos($this->code['edited'], '"\\"}\\""')) {
// разбираемся со страшным хаком "\"}\""
$i = 0;
$this->code['hacks'] = array();
while (strpos($this->code['edited'], '"\\"}\\""')) {
$this->code['hacks'][] = '"\\"}\\""';
$this->code['edited'] = str_replace('"\\"}\\""', 'hack' . $i++ . '__', $this->code['edited']);
}
}
// 2. expressions
if (strpos($this->code['edited'], 'expression(')) {
// разбираемся с expression если они присутствуют
$i = 0;
$this->code['expressions'] = array();
while (strpos($this->code['edited'], 'expression(')) {
// вылавливаем expression
preg_match_all('@(.*)expression\\((.*?)\\)@ism', $this->code['edited'], $match, PREG_SET_ORDER);
$this->code['expressions'][] = $match[0][2];
// собираем значения expression(...)
$this->code['edited'] = str_replace('expression(' . $match[0][2] . ')', 'exp' . $i++ . '__', $this->code['edited']);
}
}
// 3. data uri
if (strpos($this->code['edited'], ';base64,')) {
$i = 0;
$this->code['datauri'] = array();
while (strpos($this->code['edited'], ';base64,')) {
// вылавливаем data uri
preg_match_all('@(url\\(["\']?data:.[^\\)]*["\']?\\))@ism', $this->code['edited'], $match, PREG_SET_ORDER);
$this->code['datauri'][] = $match[0][1];
// собираем значения
$this->code['edited'] = str_replace($match[0][1], 'datauri' . $i++ . '__', $this->code['edited']);
}
}
// 4. Interpolated variables
preg_match_all('@(\\#|\\@){.*?}@ismx', $this->code['edited'], $this->code['interpolations'], PREG_SET_ORDER);
foreach ($this->code['interpolations'] as $key => $value) {
$this->code['edited'] = str_replace($value[0], 'interpolation' . $key . '__', $this->code['edited']);
}
// X. Temporally remove comments
// Magic comment
preg_match_all('@
(\\s*\\/\\*\\s*csscomb:\\s*false\\s*\\*\\/.*?\\/\\*\\s*csscomb:\\s*true\\s*\\*\\/)
|
(\\s*\\/\\/\\s*csscomb:\\s*false[\\n\\r].*?\\/\\/\\s*csscomb:\\s*true[^\\n\\S\\r]*)
@ismx', $this->code['edited'], $this->code['magicComments'], PREG_SET_ORDER);
foreach ($this->code['magicComments'] as $key => $value) {
$this->code['edited'] = str_replace($value[0], "\nmagic: comment" . $key . '__;', $this->code['edited']);
}
// Comments
preg_match_all('@
([\\n\\r]\\s*/\\*.*?\\*/)+
|
(^\\s*/\\*.*?\\*/)+
|
([\\n\\r]\\s*//[^\\n\\r]*)+
|
(^\\s*//[^\\n\\r]*)+
@ismx', $this->code['edited'], $this->code['comments'], PREG_SET_ORDER);
foreach ($this->code['comments'] as $key => $value) {
$this->code['edited'] = str_replace($value[0], 'comment' . $key . '__', $this->code['edited']);
}
// Inline comments
preg_match_all('@
[^\\S\\n\\r]*/\\*.*?\\*/([^\\n\\S\\r]*/\\*.*?\\*/)*
|
(?<!:)[^\\S\\n\\r]*//[^\\n\\r]*
@ismx', $this->code['edited'], $this->code['inlinecomments'], PREG_SET_ORDER);
foreach ($this->code['inlinecomments'] as $key => $value) {
$this->code['edited'] = str_replace($value[0], 'inlinecomment' . $key . '__', $this->code['edited']);
}
// 5. Закрываем сложности парсинга {}
$this->code['edited'] = str_replace('{}', '{ }', $this->code['edited']);
// 6. Закрываем сложности с отсутствующей последней ; перед }
$this->code['edited'] = preg_replace('@(.*?[^\\s;\\{\\}\\/\\*_])(\\s*?})@', '$1;$2', $this->code['edited']);
// Убираем ; у последнего инлайнового комментария
// Инлайновый комментарий может идти только после фигурной скобки или ;
$this->code['edited'] = preg_replace('@([;\\{\\}]+\\s*?//.*?);(\\s*?})@', '$1$2', $this->code['edited']);
// Убираем ; у интерполированных переменных
$this->code['edited'] = preg_replace('/((#\\{\\$|\\@\\{).*?)[;](\\s*?\\})/', '$1$3', $this->code['edited']);
// 8. Entities
if (preg_match_all('@
\\&
\\#?
[\\d\\w]*?[^;]
\\;
@ismx', $this->code['edited'], $entities)) {
$this->code['entities'] = array();
foreach ($entities[0] as $key => $val) {
$this->code['entities'][$key] = $val;
$this->code['edited'] = str_replace($val, 'entity__' . $key, $this->code['edited']);
}
}
}