/** * Compiles the path * * @param Context $context * @return UrlNode */ public function compilePath(Context $context) { $path = $this->path->compile($context); if (!$path instanceof UrlNode) { $rootPath = $this->currentFileInfo && $this->currentFileInfo->rootPath ? $this->currentFileInfo->rootPath : false; if ($rootPath) { $pathValue = $path->value; // Add the base path if the import is relative if ($pathValue && Util::isPathRelative($pathValue)) { $path->value = $rootPath . $pathValue; } } $path->value = Util::normalizePath($path->value); } return $path; }
/** * Inlines a resource and falls back to url() if the ieCompat option is on * and the resource is too large, or if you use the function in the browser. * If the mime is not given then node uses the mime package to determine the correct mime type. * * @param Node $mimeType A mime type string * @param Node $url The URL of the file to inline. * @return UrlNode * @throws IOException */ public function dataUri(Node $mimeType, Node $filePath = null) { if (func_num_args() < 2) { $path = $mimeType->value; $mime = false; // we will detect it later } else { $path = $filePath->value; $mime = $mimeType->value; } $path = $this->getFilePath($path); list($fragment, $path) = Util::getFragmentAndPath($path); if ($mime === false) { $mime = Mime::lookup($path); if ($mime === 'image/svg+xml') { $useBase64 = false; } else { // use base 64 unless it's an ASCII or UTF-8 format $charset = Mime::charsetsLookup($mime); $useBase64 = !in_array($charset, ['US-ASCII', 'UTF-8']); } if ($useBase64) { $mime .= ';base64'; } } else { $useBase64 = (bool) preg_match('/;base64$/', $mime); } // the file was not found // FIXME: warn if (!is_readable($path)) { $url = new UrlNode($filePath ? $filePath : $mimeType, 0, $this->context->currentFileInfo); return $url->compile($this->context); } $buffer = file_get_contents($path); $buffer = $useBase64 ? base64_encode($buffer) : Util::encodeURIComponent($buffer); $uri = "data:" . $mime . ',' . $buffer . $fragment; // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded // and the ieCompat option is enabled, return normal url() instead. if ($this->context->ieCompat) { if (strlen($uri) >= self::IE8_DATA_URI_MAX) { // FIXME: warn that we cannot use data uri here // FIXME: we don't have access to current index here! $url = new UrlNode($filePath ? $filePath : $mimeType); return $url->compile($this->context); } } // FIXME: we don't have any information about current index here! return new UrlNode(new QuotedNode('"' . $uri . '"', $uri, false)); }