Class: Kitchen::PageElement

Inherits:
ElementBase show all
Defined in:
lib/kitchen/page_element.rb

Overview

An element for a page

Instance Attribute Summary

Attributes inherited from ElementBase

#ancestors, #document, #enumerator_class, #search_query_that_found_me, #short_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ElementBase

#[], #[]=, #add_ancestor, #add_ancestors, #add_class, #add_platform_media, #ancestor, #ancestor_elements, #append, #as_enumerator, #children, #classes, #clone, #config, #contains?, #contains_blockish?, #content, #copy, #count_in, #custom_target_label_for_modules, #cut, #data_sm, #data_sm_formatted, #data_source, #data_type, descendant, descendant!, #element_children, #first, #first!, #has_ancestor?, #has_class?, #href, #href=, #id, #id=, #inner_html=, #inspect, #is?, is_the_element_class_for?, #key?, #mark_as_current_location!, #name, #name=, #pages, #pantry, #parent, #paste, #path, #preceded_by_text, #prepend, #previous, #raw, #raw_search, #remember_that_a_sub_element_was_counted, #remove_attribute, #remove_class, #replace_children, #rex_link, #say_source_or_nil, #search, #search_history, #selectors, #set, #sub_header_name, #target_label, #text, #to_html, #to_s, #to_xhtml, #to_xml, #trash, #uncount, #wrap, #wrap_children

Methods included from Mixins::BlockErrorIf

#block_error_if

Constructor Details

#initialize(node:, document: nil) ⇒ PageElement

Creates a new PageElement

Parameters:



13
14
15
16
17
# File 'lib/kitchen/page_element.rb', line 13

def initialize(node:, document: nil)
  super(node: node,
        document: document,
        enumerator_class: PageElementEnumerator)
end

Class Method Details

.short_typeSymbol

Returns the short type

Returns:

  • (Symbol)


22
23
24
# File 'lib/kitchen/page_element.rb', line 22

def self.short_type
  :page
end

Instance Method Details

#count_in_chapter_without_intro_pageInteger

Returns replaces generic call to page.count_in(:chapter)

Returns:

Raises:

  • (StandardError)

    if called on an introduction page



83
84
85
86
87
# File 'lib/kitchen/page_element.rb', line 83

def count_in_chapter_without_intro_page
  raise 'Introduction pages cannot be counted with this method' if is_introduction?

  count_in(:chapter) - (ancestor(:chapter).has_introduction? ? 1 : 0)
end

#free_responseElement

Returns the free response questions

Returns:



126
127
128
# File 'lib/kitchen/page_element.rb', line 126

def free_response
  search('section.free-response')
end

#is_appendix?Boolean

Returns true if this page is an appendix

Returns:

  • (Boolean)


101
102
103
# File 'lib/kitchen/page_element.rb', line 101

def is_appendix?
  has_class?('appendix')
end

#is_handbook?Boolean

Returns true if this page is a handbook

Returns:

  • (Boolean)


134
135
136
# File 'lib/kitchen/page_element.rb', line 134

def is_handbook?
  has_class?('handbook')
end

#is_introduction?Boolean

Returns true if this page is an introduction

Returns:

  • (Boolean)


74
75
76
# File 'lib/kitchen/page_element.rb', line 74

def is_introduction?
  @is_introduction ||= has_class?('introduction')
end

#is_preface?Boolean

Returns true if this page is a preface

Returns:

  • (Boolean)


93
94
95
# File 'lib/kitchen/page_element.rb', line 93

def is_preface?
  has_class?('preface')
end

#metadataElement

Returns the metadata element.

Returns:

Raises:



110
111
112
# File 'lib/kitchen/page_element.rb', line 110

def 
  first!("div[data-type='metadata']")
end

#summaryElement?

Returns the summary element.

Returns:

  • (Element, nil)

    the summary or nil if no summary found



118
119
120
# File 'lib/kitchen/page_element.rb', line 118

def summary
  first(selectors.page_summary)
end

#title(reload: false) ⇒ Element

Returns the title element. This method is aware that the title of the introduction page moves during the baking process.

Returns:

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kitchen/page_element.rb', line 32

def title(reload: false)
  # The selector for intro titles changes during the baking process
  @title ||= begin
    selector = is_introduction? ? selectors.title_in_introduction_page : selectors.title_in_page
    search(selector, reload: reload).map do |title|
      next if title.parent[:'data-type'] == 'metadata'

      return title
    end
    raise "Title not found for page id=#{id}"
  end
end

#title_childrenElement

Returns the title’s primary children regardless of whether the title has been baked

Returns:



57
58
59
60
# File 'lib/kitchen/page_element.rb', line 57

def title_children
  title_os_text = title.search('.os-text').first # module titles which are already baked
  title_os_text ? title_os_text.children : title.children
end

#title_textString

Returns the title’s text regardless of whether the title has been baked

Returns:



49
50
51
# File 'lib/kitchen/page_element.rb', line 49

def title_text
  title.children.one? ? title.text : title.first('.os-text').text
end

#titlesElementEnumerator

Returns an enumerator for titles.

Returns:



66
67
68
# File 'lib/kitchen/page_element.rb', line 66

def titles
  search("div[data-type='document-title']")
end