Class: Kitchen::NoteElement

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

Overview

An element for a note

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) ⇒ NoteElement

Creates a new NoteElement

Parameters:



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

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

Class Method Details

.short_typeSymbol

Returns the short type

Returns:

  • (Symbol)


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

def self.short_type
  :note
end

Instance Method Details

#autogenerated_titleString

Get the autogenerated title for this note

Returns:



68
69
70
71
72
73
74
# File 'lib/kitchen/note_element.rb', line 68

def autogenerated_title
  if indicates_autogenerated_title?
    I18n.t(:"notes.#{detected_note_title_key}")
  else
    "unknown title for note with classes #{classes}"
  end
end

#first_note_body_childObject



47
48
49
50
# File 'lib/kitchen/note_element.rb', line 47

def first_note_body_child
  note_body = first('div.os-note-body')
  note_body ? note_body.element_children[0] : element_children[0]
end

#get_first_grandchild_for_title(first_child) ⇒ Object



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

def get_first_grandchild_for_title(first_child)
  first_child&.element_children&.[](0) if first_child.name == 'p'
end

#indicates_autogenerated_title?Boolean

Returns true if the note’s title is autogenerated

Returns:

  • (Boolean)


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

def indicates_autogenerated_title?
  detected_note_title_key != 0 && detected_note_title_key.present?
end

#titleElement?

Returns the note’s title element

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kitchen/note_element.rb', line 30

def title
  block_error_if(block_given?)
  # An element with data-type="title" is ambiguous; it is only the note's title if:
  # 1. it is the note body's first child
  # 2. it is the first child's first child and the first child is a paragraph
  first_child = first_note_body_child
  return unless first_child

  first_grandchild = get_first_grandchild_for_title(first_child)

  if first_child.data_type == 'title'
    first_child
  elsif first_grandchild&.data_type == 'title'
    first_grandchild
  end
end