Class: InlineStyle::Selector

Inherits:
Struct
  • Object
show all
Defined in:
lib/inline-style/selector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#declarationsObject

Returns the value of attribute declarations

Returns:

  • (Object)

    the current value of declarations



7
8
9
# File 'lib/inline-style/selector.rb', line 7

def declarations
  @declarations
end

#selector_textObject

Returns the value of attribute selector_text

Returns:

  • (Object)

    the current value of selector_text



7
8
9
# File 'lib/inline-style/selector.rb', line 7

def selector_text
  @selector_text
end

#specificityObject

Returns the value of attribute specificity

Returns:

  • (Object)

    the current value of specificity



7
8
9
# File 'lib/inline-style/selector.rb', line 7

def specificity
  @specificity
end

Instance Method Details

#inline_declarationsObject

For the most part is just declarations unless a pseudo selector. Then it uses the inline pseudo declarations



20
21
22
23
24
25
26
# File 'lib/inline-style/selector.rb', line 20

def inline_declarations
  if pseudo?
    "\n#{ selector_text.gsub /\w(?=:)/, '' } {#{ declarations }}"
  else
    declarations
  end
end

#pseudo?Boolean

Is this selector using a pseudo class?

Returns:

  • (Boolean)


29
30
31
# File 'lib/inline-style/selector.rb', line 29

def pseudo?
  state_based_pseudo_selectors.any? {|p| selector_text.end_with? ":#{p}"} 
end

#searchObject

A slightly adjusted version of the selector_text that should be used for finding nodes. Will remove the pseudo selector and prepend ‘body ’.



11
12
13
14
15
16
# File 'lib/inline-style/selector.rb', line 11

def search
  selector_text.dup.tap do |s|
    state_based_pseudo_selectors.each {|p| s.gsub! /:#{p}$/, ''}
      s.insert(0, 'body ') unless s =~ /^body/
  end
end

#state_based_pseudo_selectorsObject

A list of state based pseudo selectors (like hover) that should be handled based on the pseudo option. Unlike position-based pseudo selectors (like :first-child) which once resolved to the correct node effectively get inlined like a normal selector.



37
38
39
# File 'lib/inline-style/selector.rb', line 37

def state_based_pseudo_selectors
  %w(link visited active hover focus target enabled disabled checked)
end