Class: Goat::DOMTools::HTMLBuilder::TagBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/goat/dom.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attrs, body) ⇒ TagBuilder

Returns a new instance of TagBuilder.



255
256
257
258
259
260
261
# File 'lib/goat/dom.rb', line 255

def initialize(tag, attrs, body)
  @tag = tag
  @attrs = attrs
  @body = body

  rewrite_attrs
end

Class Method Details

.build(tag, attrs, body) ⇒ Object

TODO: gmail trick of only a single onclick() handler



251
252
253
# File 'lib/goat/dom.rb', line 251

def self.build(tag, attrs, body)
  self.new(tag, attrs, body).dispatch
end

Instance Method Details

#a_tagObject



281
282
283
284
285
286
287
# File 'lib/goat/dom.rb', line 281

def a_tag
  unless @attrs.include?(:href)
    @attrs[:href] = 'javascript:void(0)'
  end

  build_node
end

#build_nodeObject



277
278
279
# File 'lib/goat/dom.rb', line 277

def build_node
  [@tag, @attrs, @body]
end

#dispatchObject



306
307
308
309
310
311
312
313
314
# File 'lib/goat/dom.rb', line 306

def dispatch
  meth = "#{@tag}_tag".to_sym

  if self.respond_to?(meth)
    self.send(meth)
  else
    build_node
  end
end

#input_tagObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/goat/dom.rb', line 289

def input_tag
  unless @attrs.include?(:name)
    $stderr.puts "Warning: no name for <#{@tag} #{@attrs.inspect}>#{@body.inspect}</#{@tag}>"

    # this is somewhat ungainly: we generate a name automatically by hashing the values of the
    # other attrs. this may conflict - ideally would track per-page state for these.
    # purpose is to have name-less inputs preserve their values when user goes back to page.
    # webkit in safari 5 gets confused when inputs are nameless.
    unless (vals = @attrs.values{|k, v| v.kind_of?(String)}).empty?
      $stderr.puts "Generating a name automatically..."
      @attrs[:name] = vals.join.md5[0..10]
    end
  end

  build_node
end

#rewrite_attrsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/goat/dom.rb', line 263

def rewrite_attrs
  new = {}

  @attrs.map_to_hash do |k, v|
    if k == :class && v.kind_of?(Array)
      new[:class] = @attrs[:class].join(' ')
    else
      new[k] = v
    end
  end

  @attrs = new
end