Class: HerbNodeRetainingTextNode

Inherits:
HerbNodeRetainingNode show all
Includes:
TextNode
Defined in:
lib/nodes/herb_node_retaining_text_node.rb

Instance Method Summary collapse

Methods included from TextNode

#amount_of_ending_whitespace, #amount_of_starting_whitespace, #contains_alpha_characters?, #has_leading_or_trailing_whitespace?, #has_variables?, #html?, #remove_leading_and_trailing_whitespace, #strip_whitespace?, #text?, #top_level?, #white_space?

Methods included from NodeProcessing

#flatten

Methods included from BaseNode

#shatter?, #shattered_node_list

Methods inherited from HerbNodeRetainingNode

#<<, #initialize, #nodes, #text_value

Constructor Details

This class inherits a constructor from HerbNodeRetainingNode

Instance Method Details

#add_all(nodes) ⇒ Object



4
5
6
7
8
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 4

def add_all( nodes )
  nodes.each do |node|
    self << node
  end
end

#break_out_start_and_end_tagsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 31

def break_out_start_and_end_tags
  if( last_tag_does_not_match_first_tag? )
    end_tag = extract_trailing_tag
    start_tag = HerbNodeRetainingTextNode.new
    start_tag.add_all( nodes[0..nodes.length - (end_tag.nodes.length + 1 ) ] )
    [start_tag, end_tag]          
  elsif( find_first_non_whitespace_node.node_name == "html_start_tag" && find_last_non_whitespace_node.node_name == "html_end_tag"  ) && ( can_remove_starting_or_ending_html_tags? )
    start_tag = extract_leading_tag
    end_tag = extract_trailing_tag
    middle_tag = HerbNodeRetainingTextNode.new
    middle_tag.add_all( nodes[start_tag.nodes.length..nodes.length - (end_tag.nodes.length + 1 ) ] )
    [start_tag, middle_tag, end_tag]
  elsif(find_last_non_whitespace_node.node_name == "html_start_tag")
    end_tag = extract_trailing_tag
    start_tag = HerbNodeRetainingTextNode.new
    start_tag.add_all( nodes[0..nodes.length - (end_tag.nodes.length + 1 ) ] )
    [start_tag, end_tag]
  elsif(find_first_non_whitespace_node.node_name == "html_end_tag" || find_first_non_whitespace_node.node_name == "erb_block_end")
    start_tag = extract_leading_tag
    end_tag = HerbNodeRetainingTextNode.new
    end_tag.add_all( nodes[start_tag.nodes.length..nodes.length ] )
    [start_tag, end_tag]
  else
    [self]
  end
end

#build_correct_node_retaining_node(array_of_nodes) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 111

def build_correct_node_retaining_node( array_of_nodes )
  text_retaining_node = false
  array_of_nodes.each do |node|
    text_retaining_node = true if node.is_a?(TextNode) && node.contains_alpha_characters?
  end

  if( text_retaining_node )
    to_return = HerbNodeRetainingTextNode.new
    to_return.add_all( array_of_nodes )
  else
    to_return = HerbNodeRetainingNonTextNode.create_from_nodes( array_of_nodes )
  end

  to_return
end

#can_be_combined?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 133

def can_be_combined?
  true
end

#can_be_exploded?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 127

def can_be_exploded?
  # it is possible that this node contains only non text nodes.  If
  # there is no text within this combined node then this should be uncombined
  contains_list_of_elements? || contains_only_non_text_and_non_method_nodes?
end

#can_remove_starting_or_ending_html_tags?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 10

def can_remove_starting_or_ending_html_tags?
  if( find_first_non_whitespace_node.node_name == "html_start_tag" && find_last_non_whitespace_node.node_name == "html_end_tag"  )
    if( find_first_non_whitespace_node.tag_name.text_value == find_last_non_whitespace_node.tag_name.text_value )
      true
    elsif( last_tag_does_not_match_first_tag? )
      true
    else
      false
    end
  elsif( find_last_non_whitespace_node.node_name == "html_start_tag" || find_first_non_whitespace_node.node_name == "html_end_tag" || find_first_non_whitespace_node.node_name == "erb_block_end" )
    true
  else
    false
  end
  
end

#explodeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 69

def explode
  nested_level = nesting_level
  to_return = []
  current_nest = 0
  new_node_list = nil
  
  # Go through and try to build rolled up nodes for all of the node
  # contents that are below the nested list level
  nodes.each do |current_node|
    if( current_node.node_name == "html_start_tag" )
      current_nest += 1
      if( new_node_list.nil? )
        to_return << current_node
      else
        new_node_list << current_node
      end
      if( current_nest == nesting_level + 1 )
        new_node_list = []
      end
    elsif( current_node.node_name == "html_end_tag" )
      if( current_nest == nesting_level + 1 )          
        to_return << build_correct_node_retaining_node( new_node_list )
        to_return << current_node
        new_node_list = nil
      elsif( new_node_list.nil? )
        to_return << current_node
      else
        new_node_list << current_node
      end
      current_nest -= 1
    elsif( new_node_list.nil? )
      to_return << current_node
    elsif( !new_node_list.nil?)
      new_node_list << current_node
    end

  end
  to_return << build_correct_node_retaining_node( new_node_list ) unless new_node_list.nil?   
  
  return to_return
end

#extract_text(text_extractor, node_tree) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 59

def extract_text( text_extractor, node_tree )
  self.nodes.each do |node|
    if( node.is_a?( MethodCallNode ) )
      node.extract_text( text_extractor, node_tree, self.nodes )
    else
      node.extract_text( text_extractor, node_tree )
    end
  end
end

#find_first_non_whitespace_nodeObject



141
142
143
144
145
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 141

def find_first_non_whitespace_node
  nodes.each do |node|
    return node unless node.text_value.strip.empty?
  end
end

#find_last_non_whitespace_nodeObject



147
148
149
150
151
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 147

def find_last_non_whitespace_node
  nodes.reverse.each do |node|
    return node unless node.text_value.strip.empty?
  end
end

#last_tag_does_not_match_first_tag?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 27

def last_tag_does_not_match_first_tag?
  find_last_non_whitespace_node.node_name == "html_end_tag" && find_first_non_whitespace_node.node_name == "html_start_tag" && find_last_non_whitespace_node.tag_name.text_value.downcase != find_first_non_whitespace_node.tag_name.text_value.downcase
end

#to_sObject



137
138
139
# File 'lib/nodes/herb_node_retaining_text_node.rb', line 137

def to_s
  @text_value
end