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, #ancestor, #ancestor_elements, #append, #as_enumerator, #children, #classes, #clone, #config, #contains?, #content, #copied_id, #copy, #count_in, #cut, #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, #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



69
70
71
72
73
# File 'lib/kitchen/page_element.rb', line 69

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

#exercisesElement

Returns the exercises element.

Returns:

Raises:



113
114
115
# File 'lib/kitchen/page_element.rb', line 113

def exercises
  first!('section.exercises')
end

#free_responseElement

Returns the free response questions

Returns:



121
122
123
# File 'lib/kitchen/page_element.rb', line 121

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

#is_appendix?Boolean

Returns true if this page is an appendix

Returns:

  • (Boolean)


87
88
89
# File 'lib/kitchen/page_element.rb', line 87

def is_appendix?
  has_class?('appendix')
end

#is_handbook?Boolean

Returns true if this page is a handbook

Returns:

  • (Boolean)


129
130
131
# File 'lib/kitchen/page_element.rb', line 129

def is_handbook?
  has_class?('handbook')
end

#is_introduction?Boolean

Returns true if this page is an introduction

Returns:

  • (Boolean)


60
61
62
# File 'lib/kitchen/page_element.rb', line 60

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

#is_preface?Boolean

Returns true if this page is a preface

Returns:

  • (Boolean)


79
80
81
# File 'lib/kitchen/page_element.rb', line 79

def is_preface?
  has_class?('preface')
end

#metadataElement

Returns the metadata element.

Returns:

Raises:



96
97
98
# File 'lib/kitchen/page_element.rb', line 96

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

#summaryElement?

Returns the summary element.

Returns:

  • (Element, nil)

    the summary or nil if no summary found



104
105
106
# File 'lib/kitchen/page_element.rb', line 104

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
# 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
    first!(selector, reload: reload)
  end
end

#title_textString

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

Returns:



44
45
46
# File 'lib/kitchen/page_element.rb', line 44

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

#titlesElementEnumerator

Returns an enumerator for titles.

Returns:



52
53
54
# File 'lib/kitchen/page_element.rb', line 52

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