/** * Parses the import rule. * * @param string $ruleString */ protected function parseRuleString($ruleString) { $charset = $this->getCharset(); // Remove at-rule and unnecessary white-spaces $ruleString = preg_replace('/^[ \\r\\n\\t\\f]*@import[ \\r\\n\\t\\f]+/i', '', $ruleString); $ruleString = trim($ruleString, " \r\n\t\f"); // Remove trailing semicolon $ruleString = rtrim($ruleString, ";"); $isEscaped = false; $inFunction = false; $url = ""; $mediaQuery = ""; $currentPart = ""; for ($i = 0, $j = mb_strlen($ruleString, $charset); $i < $j; $i++) { $char = mb_substr($ruleString, $i, 1, $charset); if ($char === "\\") { if ($isEscaped === false) { $isEscaped = true; } else { $isEscaped = false; } } else { if ($char === " ") { if ($isEscaped === false) { if ($inFunction == false) { $currentPart = trim($currentPart, " \r\n\t\f"); if ($currentPart !== "") { if ($url === "") { $url = trim($currentPart, " \r\n\t\f"); } else { $mediaQuery .= trim($currentPart, " \r\n\t\f"); $mediaQuery .= $char; } $currentPart = ""; } } } else { $currentPart .= $char; } } elseif ($isEscaped === false && $char === "(") { $inFunction = true; $currentPart .= $char; } elseif ($isEscaped === false && $char === ")") { $inFunction = false; $currentPart .= $char; } else { $currentPart .= $char; } } // Reset escaped flag if ($isEscaped === true && $char !== "\\") { $isEscaped = false; } } if ($currentPart !== "") { $currentPart = trim($currentPart, " \r\n\t\f"); if ($currentPart !== "") { if ($url === "") { $url = trim($currentPart, " \r\n\t\f"); } else { $mediaQuery .= trim($currentPart, " \r\n\t\f"); } } } // Get URL value $url = Url::extractUrl($url); $this->setUrl($url); // Process media query $mediaRule = new MediaRule($mediaQuery); $this->setQueries($mediaRule->getQueries()); }
/** * Parses the namespace rule. * * @param string $ruleString */ protected function parseRuleString($ruleString) { if (is_string($ruleString)) { $charset = $this->getCharset(); // Remove at-rule and unnecessary white-spaces $ruleString = preg_replace('/^[ \\r\\n\\t\\f]*@namespace[ \\r\\n\\t\\f]+/i', '', $ruleString); $ruleString = trim($ruleString, " \r\n\t\f"); // Remove trailing semicolon $ruleString = rtrim($ruleString, ";"); $isEscaped = false; $inFunction = false; $parts = []; $currentPart = ""; for ($i = 0, $j = mb_strlen($ruleString, $charset); $i < $j; $i++) { $char = mb_substr($ruleString, $i, 1, $charset); if ($char === "\\") { if ($isEscaped === false) { $isEscaped = true; } else { $isEscaped = false; } } else { if ($char === " ") { if ($isEscaped === false) { if ($inFunction == false) { $currentPart = trim($currentPart, " \r\n\t\f"); if ($currentPart !== "") { $parts[] = trim($currentPart, " \r\n\t\f"); $currentPart = ""; } } } else { $currentPart .= $char; } } elseif ($isEscaped === false && $char === "(") { $inFunction = true; $currentPart .= $char; } elseif ($isEscaped === false && $char === ")") { $inFunction = false; $currentPart .= $char; } else { $currentPart .= $char; } } // Reset escaped flag if ($isEscaped === true && $char !== "\\") { $isEscaped = false; } } if ($currentPart !== "") { $currentPart = trim($currentPart, " \r\n\t\f"); if ($currentPart !== "") { $parts[] = trim($currentPart, " \r\n\t\f"); } } foreach ($parts as $key => $value) { $parts[$key] = Placeholder::replaceStringPlaceholders($value); } $countParts = count($parts); if ($countParts === 2) { $this->setPrefix($parts[0]); // Get URL value $name = Url::extractUrl($parts[1]); $this->setName($name); } elseif ($countParts === 1) { // Get URL value $name = Url::extractUrl($parts[0]); $this->setName($name); } else { // ERROR } } else { throw new \InvalidArgumentException("Invalid type '" . gettype($ruleString) . "' for argument 'ruleString' given."); } }