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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attrs, body) ⇒ TagBuilder

Returns a new instance of TagBuilder.



202
203
204
205
206
207
208
# File 'lib/goat/html.rb', line 202

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



198
199
200
# File 'lib/goat/html.rb', line 198

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

Instance Method Details

#a_tagObject



228
229
230
231
232
233
234
# File 'lib/goat/html.rb', line 228

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

  build_node
end

#build_nodeObject



224
225
226
# File 'lib/goat/html.rb', line 224

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

#dispatchObject



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

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

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

#input_tagObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/goat/html.rb', line 236

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



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/goat/html.rb', line 210

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