Class: REXML::CSSSelector::PseudoClassDef

Inherits:
Object
  • Object
show all
Defined in:
lib/rexml/css_selector/compiler.rb,
lib/rexml/css_selector/pseudo_class_def.rb

Overview

PseudoClassDef is a definition of a pseudo class.

Constant Summary collapse

FIRST_CHILD =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":first-child must not take an argument" if pseudo_class.argument
  Queries::NthChildQuery.new(cont:, a: 0, b: 1)
end
LAST_CHILD =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":last-child must not take an argument" if pseudo_class.argument
  Queries::NthLastChildQuery.new(cont:, a: 0, b: 1)
end
ONLY_CHILD =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":only-child must not take an argument" if pseudo_class.argument
  Queries::OnlyChildQuery.new(cont:)
end
NTH_CHILD =
PseudoClassDef.new(:nth_of_selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":nth-child must take an argument" unless pseudo_class.argument
  case pseudo_class.argument
  in NthOfSelectorList[nth:, selector_list: nil]
    a, b = compiler.nth_value(nth)
    Queries::NthChildQuery.new(cont:, a:, b:)
  in NthOfSelectorList[nth:, selector_list:]
    if selector_list.selectors.any? { _1.is_a?(ComplexSelector) }
      raise CompileError, ":nth-child agument must not take a complex selector"
    end
    a, b = compiler.nth_value(nth)
    query = compiler.compile(selector_list)
    Queries::NthChildOfQuery.new(cont:, a:, b:, query:)
  end
end
NTH_LAST_CHILD =
PseudoClassDef.new(:nth_of_selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":nth-last-child must take an argument" unless pseudo_class.argument
  case pseudo_class.argument
  in NthOfSelectorList[nth:, selector_list: nil]
    a, b = compiler.nth_value(nth)
    Queries::NthLastChildQuery.new(cont:, a:, b:)
  in NthOfSelectorList[nth:, selector_list:]
    if selector_list.selectors.any? { _1.is_a?(ComplexSelector) }
      raise CompileError, ":nth-last-child agument must not take a complex selector"
    end
    a, b = compiler.nth_value(nth)
    query = compiler.compile(selector_list)
    Queries::NthLastChildOfQuery.new(cont:, a:, b:, query:)
  end
end
FIRST_OF_TYPE =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":first-of-type must not take an argument" if pseudo_class.argument
  Queries::NthOfTypeQuery.new(cont:, a: 0, b: 1)
end
LAST_OF_TYPE =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":last-of-type must not take an argument" if pseudo_class.argument
  Queries::NthLastOfTypeQuery.new(cont:, a: 0, b: 1)
end
ONLY_OF_TYPE =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":only-of-type must not take an argument" if pseudo_class.argument
  Queries::OnlyOfTypeQuery.new(cont:)
end
NTH_OF_TYPE =
PseudoClassDef.new(:nth) do |cont, pseudo_class, compiler|
  raise CompileError, ":nth-of-type must take an argument" unless pseudo_class.argument
  a, b = compiler.nth_value(pseudo_class.argument)
  Queries::NthOfTypeQuery.new(cont:, a:, b:)
end
NTH_LAST_OF_TYPE =
PseudoClassDef.new(:nth) do |cont, pseudo_class, compiler|
  raise CompileError, ":nth-last-of-type must take an argument" unless pseudo_class.argument
  a, b = compiler.nth_value(pseudo_class.argument)
  Queries::NthLastOfTypeQuery.new(cont:, a:, b:)
end
ROOT =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":root must not take an argument" if pseudo_class.argument
  Queries::RootQuery.new(cont:)
end
IS =
PseudoClassDef.new(:selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":is must take an argument" unless pseudo_class.argument
  selector_list = pseudo_class.argument
  if selector_list.selectors.any? { _1.is_a?(ComplexSelector) }
    raise CompileError, ":is agument must not take a complex selector"
  end
  query = compiler.compile(selector_list)
  Queries::NestedQuery.new(cont:, query:)
end
WHERE =
PseudoClassDef.new(:selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":where must take an argument" unless pseudo_class.argument
  selector_list = pseudo_class.argument
  if selector_list.selectors.any? { _1.is_a?(ComplexSelector) }
    raise CompileError, ":where agument must not take a complex selector"
  end
  query = compiler.compile(selector_list)
  Queries::NestedQuery.new(cont:, query:)
end
NOT =
PseudoClassDef.new(:selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":not must take an argument" unless pseudo_class.argument
  selector_list = pseudo_class.argument
  if selector_list.selectors.any? { _1.is_a?(ComplexSelector) }
    raise CompileError, ":not agument must not take a complex selector"
  end
  query = compiler.compile(selector_list)
  Queries::NotQuery.new(cont:, query:)
end
SCOPE =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":scope must not take an argument" if pseudo_class.argument
  Queries::ScopeQuery.new(cont:)
end
HAS =
PseudoClassDef.new(:relative_selector_list) do |cont, pseudo_class, compiler|
  raise CompileError, ":has must take an argument" unless pseudo_class.argument
  relative_selector_list = pseudo_class.argument
  needs_parent = false
  selectors =
    relative_selector_list.selectors.map do |relative_selector|
      scope =
        CompoundSelector[
          type: nil,
          subclasses: [PseudoClass[name: "scope", argument: nil]],
          pseudo_elements: []
        ]
      case relative_selector.combinator
      when :sibling, :adjacent
        needs_parent = true
      end
      ComplexSelector[left: scope, combinator: relative_selector.combinator, right: relative_selector.right]
    end
  query = compiler.compile(SelectorList[selectors:])
  Queries::HasQuery.new(cont:, query:, needs_parent:)
end
EMPTY =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":empty must not take an argument" if pseudo_class.argument
  Queries::EmptyQuery.new(cont:)
end
CHECKED =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":checked must not take an argument" if pseudo_class.argument
  Queries::CheckedQuery.new(cont:)
end
DISABLED =
PseudoClassDef.new do |cont, pseudo_class, _compiler|
  raise CompileError, ":disabled must not take an argument" if pseudo_class.argument
  Queries::DisabledQuery.new(cont:)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argument_kind = nil, &compile) ⇒ PseudoClassDef

Returns a new instance of PseudoClassDef.



7
8
9
10
# File 'lib/rexml/css_selector/pseudo_class_def.rb', line 7

def initialize(argument_kind = nil, &compile)
  @argument_kind = argument_kind
  @compile = compile
end

Instance Attribute Details

#argument_kindObject (readonly)

Returns the value of attribute argument_kind.



12
13
14
# File 'lib/rexml/css_selector/pseudo_class_def.rb', line 12

def argument_kind
  @argument_kind
end

Instance Method Details

#compile(cont, pseudo_class, compiler) ⇒ Object



14
15
16
# File 'lib/rexml/css_selector/pseudo_class_def.rb', line 14

def compile(cont, pseudo_class, compiler)
  @compile.call(cont, pseudo_class, compiler)
end