Class: CSSNative

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

Defined Under Namespace

Classes: AttributeError, CSSError, GrammarError, PseudoClassError, PseudoElementError, Rule, RuleError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCSSNative

Returns a new instance of CSSNative.



40
41
42
# File 'lib/css-native.rb', line 40

def initialize
  @rules = []
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



39
40
41
# File 'lib/css-native.rb', line 39

def rules
  @rules
end

Class Method Details

.format_attribute(name, operation = :none, value = nil, case_sensitive: true) ⇒ Object



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

def self.format_attribute(name, operation = :none, value = nil, case_sensitive: true)
  op = case operation.to_sym
       when :none        then ""
       when :equals      then "="
       when :include     then "~="
       when :matches     then "|="
       when :starts_with then "^="
       when :ends_with   then "$="
       when :contains    then "*="
       else
         raise AttributeError.new("undefined comparison '#{operation}' for css attribute selector")
       end
  "[#{name}#{op}#{value.nil? ? "" : "\"#{value}\""}#{case_sensitive ? "" : " i"}]"
end

.format_class(name) ⇒ Object



16
17
18
# File 'lib/css-native.rb', line 16

def self.format_class(name)
  ".#{name}"
end

.format_element(name) ⇒ Object



12
13
14
# File 'lib/css-native.rb', line 12

def self.format_element(name)
  name.to_s
end

.format_id(name) ⇒ Object



20
21
22
# File 'lib/css-native.rb', line 20

def self.format_id(name)
  "##{name}"
end

.stylesheet(&block) ⇒ Object



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

def self.stylesheet(&block)
  sheet = new
  sheet.instance_eval(&block)
  sheet
end

Instance Method Details

#attribute(name, operation = :none, value = nil, case_sensitive: true) ⇒ Object



61
62
63
# File 'lib/css-native.rb', line 61

def attribute(name, operation = :none, value = nil, case_sensitive: true)
    Rule.new(self).with_attribute(name, operation, value, case_sensitive: case_sensitive)
end

#class(name = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/css-native.rb', line 49

def class(name = nil)
  if name.nil?
    klass
  else
    Rule.new(self).with_class(name)
  end
end

#element(name) ⇒ Object



44
45
46
# File 'lib/css-native.rb', line 44

def element(name)
  Rule.new(self).with_element(name)
end

#id(name) ⇒ Object



57
58
59
# File 'lib/css-native.rb', line 57

def id(name)
    Rule.new(self).with_id(name)
end

#klassObject



48
# File 'lib/css-native.rb', line 48

alias_method :klass, :class

#select(name, *args, type: :element) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/css-native.rb', line 65

def select(name, *args, type: :element)
  case type
  when :element
    Rule.new(self).with(name)
  when :class
    self.class(name)
  when :id
    id(name)
  when :attribute
    attribute(name, *args)
  else
    raise RuleError.new("undefined rule type '#{type}' for css selector")
  end
end

#to_sObject



80
81
82
# File 'lib/css-native.rb', line 80

def to_s
  @rules.join("\n")
end