Class | Sass::CSS
In: | lib/sass/css.rb
Parent: | Object

This class converts CSS documents into Sass or SCSS templates. It works by parsing the CSS document into a {Sass::Tree} structure, and then applying various transformations to the structure to produce more concise and idiomatic Sass/SCSS.

Example usage:

        Sass::CSS.new("p { color: blue }").render(:sass) #=> "p\n  color: blue"
        Sass::CSS.new("p { color: blue }").render(:scss) #=> "p {\n  color: blue; }"

### Methods

new render source_encoding

### Public Class methods

new(template, options = {})

@param template [String] The CSS stylesheet.

      This stylesheet can be encoded using any encoding
      that can be converted to Unicode.
      If the stylesheet contains an `@charset` declaration,
      that overrides the Ruby encoding
      (see {file:SASS_REFERENCE.md#encodings the encoding documentation})

@option options :old [Boolean] (false)

        Whether or not to output old property syntax
        (`:color blue` as opposed to `color: blue`).
        This is only meaningful when generating Sass code,
        rather than SCSS.

[Source]

        # File lib/sass/css.rb, line 28
    28:     def initialize(template, options = {})
    29:       if template.is_a? IO
    30:         template = template.read
    31:       end
    32:
    33:       @options = options.dup
    34:       # Backwards compatibility
    35:       @options[:old] = true if @options[:alternate] == false
    36:       @template = template
    37:     end

### Public Instance methods

render(fmt = :sass)

Converts the CSS template into Sass or SCSS code.

@param fmt [Symbol] `:sass` or `:scss`, designating the format to return. @return [String] The resulting Sass or SCSS code @raise [Sass::SyntaxError] if there‘s an error parsing the CSS template

[Source]

        # File lib/sass/css.rb, line 44
    44:     def render(fmt = :sass)
    45:       check_encoding!
    46:       build_tree.send("to_#{fmt}", @options).strip + "\n"
    47:     rescue Sass::SyntaxError => err
    48:       err.modify_backtrace(:filename => @options[:filename] || '(css)')
    49:       raise err
    50:     end

source_encoding()

Returns the original encoding of the document, or `nil` under Ruby 1.8.

@return [Encoding, nil] @raise [Encoding::UndefinedConversionError] if the source encoding

      cannot be converted to UTF-8

@raise [ArgumentError] if the document uses an unknown encoding with `@charset`

[Source]

        # File lib/sass/css.rb, line 59
    59:     def source_encoding
    60:       check_encoding!
    61:       @original_encoding
    62:     end

[Validate]