Class: Sprockets::CharsetNormalizer

### Home Classes Methods

### In Files

- lib/sprockets/charset_normalizer.rb

### Parent

Tilt::Template

### Methods

- #evaluate
- #prepare

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::AssetAttributes
- Sprockets::Base
- Sprockets::BundledAsset
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Caching
- Sprockets::CharsetNormalizer
- Sprockets::CircularDependencyError
- Sprockets::ClosureCompressor
- Sprockets::Compressing
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::DirectiveProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsTemplate
- Sprockets::EncodingError
- Sprockets::EngineError
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::Manifest
- Sprockets::Mime
- Sprockets::NotImplementedError
- Sprockets::Paths
- Sprockets::ProcessedAsset
- Sprockets::ProcessedAsset::DependencyFile
- Sprockets::Processing
- Sprockets::Processor
- Sprockets::SafetyColons
- Sprockets::SassCacheStore
- Sprockets::SassCompressor
- Sprockets::SassFunctions
- Sprockets::SassImporter
- Sprockets::SassTemplate
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::StaticAsset
- Sprockets::UglifierCompressor
- Sprockets::UnserializeError
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::CharsetNormalizer

Some browsers have issues with stylesheets that contain multiple `@charset` definitions. The issue surfaces while using Sass since it inserts a `@charset` at the top of each file. Then Sprockets concatenates them together.

The `CharsetNormalizer` processor strips out multiple `@charset` definitions.

The current implementation is naive. It picks the first `@charset` it sees and strips the others. This works for most people because the other definitions are usually `UTF-8`. A more sophisticated approach would be to re-encode stylesheets with mixed encodings.

This behavior can be disabled with:

    environment.unregister_bundle_processor 'text/css', Sprockets::CharsetNormalizer

### Public Instance Methods

evaluate(context, locals, &block) click to toggle source

    # File lib/sprockets/charset_normalizer.rb, line 25
    def evaluate(context, locals, &block)
      charset = nil

      # Find and strip out any `@charset` definitions
      filtered_data = data.gsub(/^@charset "([^"]+)";$/) {
        charset ||= $1; ""
      }

      if charset
        # If there was a charset, move it to the top
        "@charset \"#{charset}\";#{filtered_data}"
      else
        data
      end
    end

prepare() click to toggle source

    # File lib/sprockets/charset_normalizer.rb, line 22
    def prepare
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.