Method: Sprockets::EncodingUtils#detect_html

Defined in:
lib/sprockets/encoding_utils.rb

#detect_html(str) ⇒ Object

Public: Detect charset from HTML document.

Attempts to parse any Unicode BOM otherwise attempt Charlock detection and finally falls back to the environment’s external encoding.

str - String.

Returns a encoded String.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/sprockets/encoding_utils.rb', line 244

def detect_html(str)
  str = detect_unicode_bom(str)

  # Attempt Charlock detection
  if str.encoding == Encoding::BINARY
    charlock_detect(str)
  end

  # Fallback to environment's external encoding
  if str.encoding == Encoding::BINARY
    str.force_encoding(Encoding.default_external)
  end

  str
end