Class CssParser::Parser
In: lib/css_parser/parser.rb
Parent: Object

Parser class

All CSS is converted to UTF-8.

When calling Parser#new there are some configuaration options:

absolute_paths
Convert relative paths to absolute paths (href, src and url(’’). Boolean, default is false.
import
Follow @import rules. Boolean, default is true.
io_exceptions
Throw an exception if a link can not be found. Boolean, default is true.

Methods

Constants

USER_AGENT = "Ruby CSS Parser/#{CssParser::VERSION} (https://github.com/premailer/css_parser)"
STRIP_CSS_COMMENTS_RX = /\/\*.*?\*\//m
STRIP_HTML_COMMENTS_RX = /\<\!\-\-|\-\-\>/m
RE_AT_IMPORT_RULE = /\@import\s*(?:url\s*)?(?:\()?(?:\s*)["']?([^'"\s\)]*)["']?\)?([\w\s\,^\]\(\)]*)\)?[;\n]?/   Initial parsing
MAX_REDIRECTS = 3

Attributes

folded_declaration_cache  [R] 
loaded_uris  [R]  Array of CSS files that have been loaded.

Public Class methods

Public Instance methods

[](selector, media_types = :all)

Alias for find_by_selector

Add a raw block of CSS.

In order to follow +@import+ rules you must supply either a +:base_dir+ or +:base_uri+ option.

Use the +:media_types+ option to set the media type(s) for this block. Takes an array of symbols.

Use the +:only_media_types+ option to selectively follow +@import+ rules. Takes an array of symbols.

Example

  css = <<-EOT
    body { font-size: 10pt }
    p { margin: 0px; }
    @media screen, print {
      body { line-height: 1.2 }
    }
  EOT

  parser = CssParser::Parser.new
  parser.add_block!(css)

Add a CSS rule by setting the selectors, declarations and media_types.

media_types can be a symbol or an array of symbols.

Add a CssParser RuleSet object.

media_types can be a symbol or an array of symbols.

Add a CSS rule by setting the selectors, declarations, filename, offset and media_types.

filename can be a string or uri pointing to the file or url location. offset should be Range object representing the start and end byte locations where the rule was found in the file. media_types can be a symbol or an array of symbols.

Iterate through RuleSet objects.

media_types can be a symbol or an array of symbols.

Iterate through CSS selectors.

media_types can be a symbol or an array of symbols. See RuleSet#each_selector for options.

Get declarations by selector.

media_types are optional, and can be a symbol or an array of symbols. The default value is :all.

Examples

 find_by_selector('#content')
 => 'font-size: 13px; line-height: 1.2;'

 find_by_selector('#content', [:screen, :handheld])
 => 'font-size: 13px; line-height: 1.2;'

 find_by_selector('#content', :print)
 => 'font-size: 11pt; line-height: 1.2;'

Returns an array of declarations.

Finds the rule sets that match the given selectors

Load a remote CSS file.

You can also pass in file://test.css

See add_block! for options.

Deprecated: originally accepted three params: `uri`, `base_uri` and `media_types`

Remove a CssParser RuleSet object.

media_types can be a symbol or an array of symbols.

A hash of { :media_query => rule_sets }

Output all CSS rules as a Hash

Output all CSS rules as a single stylesheet.

Protected Instance methods

Check that a path hasn‘t been loaded already

Raises a CircularReferenceError exception if io_exceptions are on, otherwise returns true/false.

Remove a pattern from a given string

Returns a string.

[Validate]