Class: Habaki::Stylesheet
- Includes:
- SelectorMatcher
- Defined in:
- lib/habaki/stylesheet.rb
Overview
main structure
Instance Attribute Summary collapse
Class Method Summary collapse
-
.parse(data) ⇒ Stylesheet
instanciate and parse from data.
-
.parse_file(filename) ⇒ Stylesheet
instanciate and parse from file.
Instance Method Summary collapse
-
#compact! ⇒ Object
remove rules with no declaration.
-
#each_rule(media = nil) {|rule| ... } ⇒ void
traverse all rules (including all media & supports).
-
#each_rules(media = nil) {|rules| ... } ⇒ void
traverse rules.
-
#find_by_selector(selector_str, media = nil) ⇒ Array<Rule>
find rule from selector str.
-
#find_declarations_by_selector(selector_str, media = nil) ⇒ Array<Declarations>
find declarations from selector str.
-
#has_selector?(selector_str, media = nil) ⇒ Boolean
does selector exists ?.
-
#initialize ⇒ Stylesheet
constructor
A new instance of Stylesheet.
-
#parse!(data) ⇒ void
parse from data and append to current stylesheet.
-
#parse_file!(filename) ⇒ void
parse from file and append to current stylesheet.
- #read_from_katana(out) ⇒ void private
- #string(format = Formatter::Base.new) ⇒ String
Methods included from SelectorMatcher
#each_match, #each_matching_rule, #find_matching_declarations, #find_matching_rules, #index_selectors!, #lookup_rules
Methods inherited from Node
Constructor Details
#initialize ⇒ Stylesheet
Returns a new instance of Stylesheet.
11 12 13 14 |
# File 'lib/habaki/stylesheet.rb', line 11 def initialize @rules = Rules.new @errors = [] end |
Instance Attribute Details
Class Method Details
.parse(data) ⇒ Stylesheet
instanciate and parse from data
95 96 97 98 99 |
# File 'lib/habaki/stylesheet.rb', line 95 def self.parse(data) stylesheet = self.new stylesheet.parse!(data) stylesheet end |
.parse_file(filename) ⇒ Stylesheet
instanciate and parse from file
104 105 106 |
# File 'lib/habaki/stylesheet.rb', line 104 def self.parse_file(filename) parse(File.read(filename)) end |
Instance Method Details
#compact! ⇒ Object
remove rules with no declaration
82 83 84 85 86 87 88 89 90 |
# File 'lib/habaki/stylesheet.rb', line 82 def compact! @rules.reject! { |rule| rule.declarations&.empty? || false } @rules.each do |rule| if rule.rules rule.rules.reject! { |emb_rule| emb_rule.declarations&.empty? || false } end end @rules.reject! { |rule| rule.rules&.empty? || false } end |
#each_rule(media = nil) {|rule| ... } ⇒ void
This method returns an undefined value.
traverse all rules (including all media & supports)
33 34 35 36 37 38 39 |
# File 'lib/habaki/stylesheet.rb', line 33 def each_rule(media = nil, &block) each_rules(media) do |rules| rules.each do |rule| block.call rule end end end |
#each_rules(media = nil) {|rules| ... } ⇒ void
This method returns an undefined value.
traverse rules
20 21 22 23 24 25 26 27 |
# File 'lib/habaki/stylesheet.rb', line 20 def each_rules(media = nil, &block) block.call @rules @rules.each do |rule| next unless rule.rules next if rule.is_a?(MediaRule) && !rule.media_match?(media) block.call rule.rules end end |
#find_by_selector(selector_str, media = nil) ⇒ Array<Rule>
find rule from selector str
58 59 60 61 62 63 64 65 66 |
# File 'lib/habaki/stylesheet.rb', line 58 def find_by_selector(selector_str, media = nil) results = [] each_rule(media) do |rule| rule.each_selector do |selector| results << rule if selector_str == selector.to_s end end results end |
#find_declarations_by_selector(selector_str, media = nil) ⇒ Array<Declarations>
find declarations from selector str
72 73 74 75 76 77 78 79 |
# File 'lib/habaki/stylesheet.rb', line 72 def find_declarations_by_selector(selector_str, media = nil) results = [] each_rule(media) do |rule| next unless rule.selectors results << rule.declarations if rule.selectors.map(&:to_s).include?(selector_str) end results end |
#has_selector?(selector_str, media = nil) ⇒ Boolean
does selector exists ?
45 46 47 48 49 50 51 52 |
# File 'lib/habaki/stylesheet.rb', line 45 def has_selector?(selector_str, media = nil) each_rule do |rule| rule.each_selector do |selector| return true if selector_str == selector.to_s end end false end |
#parse!(data) ⇒ void
This method returns an undefined value.
parse from data and append to current stylesheet
111 112 113 114 115 |
# File 'lib/habaki/stylesheet.rb', line 111 def parse!(data) return unless data read_from_katana(Katana.parse(data)) end |
#parse_file!(filename) ⇒ void
This method returns an undefined value.
parse from file and append to current stylesheet
120 121 122 |
# File 'lib/habaki/stylesheet.rb', line 120 def parse_file!(filename) parse(File.read(filename)) end |
#read_from_katana(out) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/habaki/stylesheet.rb', line 133 def read_from_katana(out) @rules.read_from_katana(out.stylesheet.imports) @rules.read_from_katana(out.stylesheet.rules) # keep reference to this stylesheet in each rule each_rule do |rule| rule.stylesheet = self end out.errors.each do |err| @errors << Error.read_from_katana(err) end end |