Class: Nice::HtmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/html_parser.rb

Class Method Summary collapse

Class Method Details

.add_elements_of_current_state(doc, curr_state, upper_root = false) ⇒ Object



24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
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
# File 'lib/nice/html_parser.rb', line 24

def self.add_elements_of_current_state doc, curr_state, upper_root=false

  # get all nodes of the current state      
   doc.css("*[data-state-root]").remove  if upper_root # for upper_root option remove tree below root to shorten the search
  curr_state_nodes = doc.css("[data-state~='#{curr_state}']")
  all_nodes = doc.css("[data-state~='all']")
  

  # get reference nodes in DOM tree for current nodes and generate js insert statements
  stack = curr_state_nodes.reverse.each_with_index.map do |curr_node,index|

     # in case this node is not only assigned to the current state - check if it should be updated
     if curr_node.attribute("data-state").value != curr_state &&
        !curr_node.has_attribute?("data-state-update") then
       next
     end

    ref_id = self.ref_node_uid( curr_state,curr_state_nodes.count - index)
    ref_node_name = "[data-state-uid~=\'#{ref_id}\']"       
    ref_node = doc.css(ref_node_name)
    
    if ref_node.blank?
      next
    end
    
    #get index
    idx = ref_node.attribute("data-state-uid").value.split(" ").find_index(ref_id)

    ref_node_method = ref_node.attribute('data-state-insert-method').value.split(" ")[idx]

    if ref_node_method == "insert"
      js_text = Nice::Js::Caller.generate_js_insert_after curr_node, ref_node_name
    else
      js_text = Nice::Js::Caller.generate_js_insert_inside curr_node, ref_node_name
    end

    # remove unuseful chars which will break the js parser
    js_text = js_text.gsub(/(\r\n|\n|\r|\t|\s\s)/,'')
  end
  
  # nodes with 'all' attribute
  stack += all_nodes.reverse.each_with_index.map do |curr_node,index|

     if !curr_node.has_attribute?("data-state-update") then
       next
     end

    ref_id = self.ref_node_uid( 'all',all_nodes.count - index)
    ref_node_name = "[data-state-uid~=\'#{ref_id}\']"       
    ref_node = doc.css(ref_node_name)
    
    if ref_node.blank?
      next
    end
    
    #get index
    idx = ref_node.attribute("data-state-uid").value.split(" ").find_index(ref_id)

    ref_node_method = ref_node.attribute('data-state-insert-method').value.split(" ")[idx]

    if ref_node_method == "insert"
      js_text = Nice::Js::Caller.generate_js_insert_after curr_node, ref_node_name
    else
      js_text = Nice::Js::Caller.generate_js_insert_inside curr_node, ref_node_name
    end

    # remove unuseful chars which will break the js parser
    js_text = js_text.gsub(/(\r\n|\n|\r|\t|\s\s)/,'')
  end      

  stack
end

.add_root_content(doc) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/nice/html_parser.rb', line 164

def self.add_root_content doc
   if doc.css("[data-state-root]").length == 0
     raise "You must add add a data-state-root in your application.html to your main container"
   else
    ref_node_name = "[data-state-root]"
    js = []
    doc.css("*[data-state-root]").each do |curr_node|
      js << Nice::Js::Caller.replace_node(curr_node, ref_node_name).gsub(/(\r\n|\n|\r|\t|\s\s)/,'')
     end
     js
  end
end

.add_top_css(doc, current_state) ⇒ Object



159
160
161
162
# File 'lib/nice/html_parser.rb', line 159

def self.add_top_css doc, current_state
  doc.css("body").add_class("state-#{current_state}")
  doc
end

.annotate_referencing_nodes(doc) ⇒ Object

generates referencing data attributes in all preceiding or parent nodes of state bound elements which are used later to insert elements correctly This method supports referencing by more than one state bounded node



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/nice/html_parser.rb', line 100

def self.annotate_referencing_nodes doc

  per_state_counter = {}
  doc.css("[data-state]").each do |curr_node|
    
    # each node can have multiple state references which need to be
    # treated separately
    states = curr_node.attribute("data-state").value.split(" ")

    states.each do |state|
      # increase counter per state
      per_state_counter[state] ||= 0
      idx = per_state_counter[state] += 1

      # try using preceding element if one exists otherwise use parent.
      # the referencing node must not be an state bound element otherwise
      # we can not be sure the element is always present.
      prev_node = curr_node.previous_element

      while prev_node && prev_node.has_attribute?("data-state")
         prev_node = prev_node.previous_element
      end 

      if prev_node && !prev_node.has_attribute?("data-state")
        node = prev_node
        method = "insert" 
      else
        par_node = curr_node.parent
        
        while par_node && par_node.has_attribute?("data-state")
           par_node = par_node.parent
        end 

        if par_node && !par_node.has_attribute?("data-state")
          node = curr_node.parent
          method = "append"
        else
          raise "No reference could be created for node #{curr_node}. Make sure this node \
          is preceeded or sourrounded at least by one non state bound element."
        end
        
      end

      next if node == nil

      # add reference to the found element
      a = node.has_attribute?('data-state-uid') ? [node.attribute('data-state-uid').value] : []
  a += [self.ref_node_uid(state,idx)]
  node['data-state-uid'] = a.join(" ")

  m = node.has_attribute?('data-state-insert-method') ? [node.attribute('data-state-insert-method').value] : []
  m += [method]
  node['data-state-insert-method'] = m.join(" ")
end
  end

  doc
end

.remove_elements_not_of_state(state, doc) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nice/html_parser.rb', line 8

def self.remove_elements_not_of_state(state, doc)

  doc.css("[data-state]").each do |node|
    state_list = node.attribute('data-state').value.split(" ")

    if  !state_list.include?('all') &&
        !state_list.include?(state) then
      
      p "removed state: #{state_list}"            
      node.remove
    end
  end

  doc
end