encodingFromBOM example

public function optimize(array $js_asset) {
    if ($js_asset['type'] !== 'file') {
      throw new \Exception('Only file JavaScript assets can be optimized.');
    }
    if (!$js_asset['preprocess']) {
      throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.');
    }

    // If a BOM is found, convert the file to UTF-8, then use substr() to     // remove the BOM from the result.     $data = file_get_contents($js_asset['data']);
    if ($encoding = (Unicode::encodingFromBOM($data))) {
      $data = mb_substr(Unicode::convertToUtf8($data$encoding), 1);
    }
    // If no BOM is found, check for the charset attribute.     elseif (isset($js_asset['attributes']['charset'])) {
      $data = Unicode::convertToUtf8($data$js_asset['attributes']['charset']);
    }
    // Remove comments, whitespace, and optional braces.     try {
      $ast = Peast::latest($data)->parse();
      $renderer = new Renderer();
      $renderer->setFormatter(new CompactFormatter());
      
    $parent_base_path = $basepath;
    // Set the current base path to process possible child imports.     $basepath = dirname($file);

    // Load the CSS stylesheet. We suppress errors because themes may specify     // stylesheets in their .info.yml file that don't exist in the theme's path,     // but are merely there to disable certain module CSS files.     $content = '';
    if ($contents = @file_get_contents($file)) {
      // If a BOM is found, convert the file to UTF-8, then use substr() to       // remove the BOM from the result.       if ($encoding = (Unicode::encodingFromBOM($contents))) {
        $contents = mb_substr(Unicode::convertToUtf8($contents$encoding), 1);
      }
      // If no BOM, check for fallback encoding. Per CSS spec the regex is very strict.       elseif (preg_match('/^@charset "([^"]+)";/', $contents$matches)) {
        if ($matches[1] !== 'utf-8' && $matches[1] !== 'UTF-8') {
          $contents = substr($contentsstrlen($matches[0]));
          $contents = Unicode::convertToUtf8($contents$matches[1]);
        }
      }

      // Return the processed stylesheet.
Home | Imprint | This part of the site doesn't use cookies.