Class: CSSNative::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/css-native/rule.rb,
lib/css-native/rule/stylesheet.rb

Defined Under Namespace

Classes: Stylesheet

Constant Summary collapse

COMBINATORS =

Combinators

{
  descendant: " ",
  child: " > ",
  sibling: " + ",
  adjacent: " + ",
  column: " || "
}
PSEUDO_CLASSES =

pseudo-classes name of value is pseudo-class, array values are acceptad options

Equality is compared by <option> === <value>, allowing 
classes to test for instances or regex matching

If array is empty, there’s no limitations (in the code, foraml CSS may differ) If array is nil, it takes no options

{
  # Linguistic
  dir: ["ltr", "rtl"],
  lang: [],
  # Location
  "any_link": nil,
  link: nil,
  visited: nil,
  "local_link": nil,
  target: nil,
  "target_within": nil,
  scope: nil,
  # User action
  hover: nil,
  active: nil,
  focus: nil,
  "focus_visible": nil,
  "focus_within": nil,
  # Time_dimensional _ ill_defined
  current: nil,
  past: nil,
  future: nil,
  # Resource state
  playing: nil,
  paused: nil,
  # Input
  enabled: nil,
  disabled: nil,
  "read_only": nil,
  "read_write": nil,
  "placeholder_shown": nil,
  default: nil,
  checked: nil,
  indeterminate: nil,
  blank: nil,
  valid: nil,
  invalid: nil,
  "in_range": nil,
  "out_of_range": nil,
  required: nil,
  optional: nil,
  "user_invalid": nil,
  root: nil,
  empty: nil,
  "nth_child": ["odd", "even", /^\d+(n(\s*\+\s*\d+)?)?$/],
  "nth_last_child": ["odd", "even", /^\d+(n(\s*\+\s*\d+)?)?$/],
  "first_child": nil,
  "last_child": nil,
  "only_child": nil,
  "nth_of_type": ["odd", "even", /^\d+(n(\s*\+\s*\d+)?)?$/],
  "nth_last_of_type": ["odd", "even", /^\d+(n(\s*\+\s*\d+)?)?$/],
  "first_of_type": nil,
  "last_of_type": nil,
  "only_of_type": nil,
}
PSEUDO_ELEMENTS =

pseudo-elements definition semantics same as pseudo-classes

{
  after: nil,
  before: nil,
  backdrop: nil,
  cue: nil,
  "cue_region": nil,
  "first_letter": nil,
  "first_line": nil,
  "file_selector_button": nil,
  "grammar_error": nil,
  marker: nil,
  part: [/[-a-zA-Z]+( [-a-zA-Z]+)?/],
  placeholder: nil,
  selection: nil,
  slotted: [],
  "spelling_error": nil,
  "target_text": nil,
}

Instance Method Summary collapse

Constructor Details

#initialize(parent, name = "", previous: nil) ⇒ Rule

Returns a new instance of Rule.



4
5
6
7
8
9
10
# File 'lib/css-native/rule.rb', line 4

def initialize(parent, name = "", previous: nil)
  @previous = previous
  @parent = parent
  @selector = name.to_s
  @body = {}
  @stylesheet = Stylesheet.new(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/css-native/rule.rb', line 237

def method_missing(m, *args, &block)
  if COMBINATORS.key?(m)
    combinator(m)
  elsif PSEUDO_CLASSES.key?(m)
    pseudo_class(m, *args, &block)
  elsif PSEUDO_ELEMENTS.key?(m)
    pseudo_element(m, *args, &block)
  else
    super(m, *args, &block)
  end
end

Instance Method Details

#all(&block) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/css-native/rule.rb', line 74

def all(&block)
  raise GrammarError.new("*") if previous_selector?
  @previous = :all
  @selector += "*"
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end
end

#combinator(c) ⇒ Object

Raises:



108
109
110
111
112
113
114
# File 'lib/css-native/rule.rb', line 108

def combinator(c)
  m = c.to_sym
  raise GrammarError.new(COMBINATORS[]) if previous_combinator?
  @previous = :combinator
  @selector += COMBINATORS[m]
  self
end

#join(rule = nil) ⇒ Object

Grouping selectors

Raises:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/css-native/rule.rb', line 87

def join(rule = nil)
  raise GrammarError.new(",") if previous_combinator?
  @previous = :join
  @selector += ","
  if rule.kind_of? Rule
    @selector += rule.instance_variable_get(:@selector)
  else
    @selector += rule.to_s
  end
  self
end

#pseudo_class(name, *args, &block) ⇒ Object

Raises:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/css-native/rule.rb', line 178

def pseudo_class(name, *args, &block)
  pc = name.to_s.gsub("_", "-")
  m = name.to_s.gsub("-", "_").to_sym
  raise PseudoClassError.new(method: pc) unless PSEUDO_CLASSES.key?(m)
  arg_defs = PSEUDO_CLASSES[m]
  @previous = :pseudo_class
  args.all? do |arg|
    raise PseudoClassError.new(argument: arg, method: pc) unless matches_arg_defs?(arg_defs, arg.to_s)
  end
  @selector += ":" + pc
  @selector += "(#{args.join(" ")})" unless args.empty?
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end
end

#pseudo_element(name, *args, &block) ⇒ Object

Raises:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/css-native/rule.rb', line 218

def pseudo_element(name, *args, &block)
  pe = name.to_s.gsub("_", "-")
  m = name.to_s.gsub("-", "_").to_sym
  raise PseudoElementError.new(method: pe) unless PSEUDO_ELEMENTS.key?(m)
  arg_defs = PSEUDO_ELEMENTS[m]
  @previous = :pseudo_element
  args.all? do |arg|
    raise PseudoElementError.new(argument: arg, method: ps) unless matches_arg_defs?(arg_defs, arg.to_s) 
  end
  @selector += "::" + pe
  @selector += "(#{args.join(" ")})" unless args.empty?
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end 
end

#respond_to_missing?(m, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
252
253
254
# File 'lib/css-native/rule.rb', line 249

def respond_to_missing?(m, include_all = false)
  COMBINATORS.key?(m) ||
  PSEUDO_CLASSES.key?(m) ||
  PSEUDO_ELEMENTS.key?(m) ||
  super(m, include_all)
end

#to_sObject



256
257
258
# File 'lib/css-native/rule.rb', line 256

def to_s
  "#{@selector} #{@stylesheet}"
end

#with(name, *args, type: :element, &block) ⇒ Object Also known as: select



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/css-native/rule.rb', line 49

def with(name, *args, type: :element, &block)
  case type
  when :element
    name = (name == :all ? "*" : name.to_s)
    raise GrammarError.new(name) if previous_selector?
    @previous = (name == :all ? :all : :element)
    @selector += name
    if block_given?
      @stylesheet.instance_exec(@stylesheet, &block)
      @parent.rules << to_s
    else
      self
    end
  when :class
    with_class(name, &block)
  when :id
    with_id(name, &block)
  when :attribute
    with_attribute(name, *args, &block)
  else
    raise CSSNative::RuleError.new("undefined rule type '#{type}' for css selector")
  end
end

#with_attribute(name, operation = :none, value = nil, case_sensitive: true, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/css-native/rule.rb', line 37

def with_attribute(name, operation = :none, value = nil, case_sensitive: true, &block)
  s = CSSNative::format_attribute(name, operation, value, case_sensitive: case_sensitive)
  @previous = :attr
  @selector += s
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end
end

#with_class(name, &block) ⇒ Object

basic selectors



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/css-native/rule.rb', line 13

def with_class(name, &block)
  s = CSSNative::format_class(name)
  @previous = :class
  @selector += s
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end
end

#with_id(name, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/css-native/rule.rb', line 25

def with_id(name, &block)
  s = CSSNative::format_id(name)
  @previous = :id
  @selector += s
  if block_given?
    @stylesheet.instance_exec(@stylesheet, &block)
    @parent.rules << to_s
  else
    self
  end
end