Ticket #127 (new defect)

Opened 11 months ago

refactor of tag! in builder.rb

Reported by: gregwebs Owned by: why
Priority: major Milestone:
Component: lib/hpricot Version:
Keywords: builder Cc:

Description

refactor *args parsing portion of tag! method to go from 3 * O(N) to O(N)

Index: hpricot/builder.rb
===================================================================
--- hpricot/builder.rb  (revision 157)
+++ hpricot/builder.rb  (working copy)
@@ -88,21 +88,21 @@
       end
 
       # turn arguments into children or attributes
-      childs = []
-      attrs = args.grep(Hash)
-      childs.concat((args - attrs).map do |x|
-        if x.respond_to? :to_html
-          Hpricot.make(x.to_html)
-        elsif x
-          Text.new(x.fast_xs)
+      attrs = {}; childs = []
+      args.each do |arg|
+        if arg.is_a? Hash
+          arg.each do |k,v|
+            attrs[k] = Hpricot.xs(v.to_s) if v
+          end
+        else
+          childs.push(  if x.respond_to? :to_html
+                          Hpricot.make(x.to_html)
+                        elsif x
+                          Text.new(x.fast_xs)
+                        end )
         end
-      end.flatten)
-      attrs = attrs.inject({}) do |hsh, ath|
-        ath.each do |k, v|
-          hsh[k] = v.to_s.fast_xs if v
-        end
-        hsh
       end
+      childs.flatten!
 
       # create the element itself
       f = Elem.new(STag.new(tag, attrs), childs, ETag.new(tag))
Note: See TracTickets for help on using tickets.