Module: Hamlit::AttributeBuilder

Defined in:
lib/hamlit/attribute_builder.rb,
ext/hamlit/hamlit.c

Constant Summary collapse

BOOLEAN_ATTRIBUTES =
%w[disabled readonly multiple checked autobuffer
autoplay controls loop selected hidden scoped async
defer reversed ismap seamless muted required
autofocus novalidate formnovalidate open pubdate
itemscope allowfullscreen default inert sortable
truespeed typemustmatch download playsinline].freeze

Class Method Summary collapse

Class Method Details

.build(escape_attrs, quote, format, boolean_attributes, object_ref, *hashes) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/hamlit/hamlit.c', line 493

def build(escape_attrs, quote, format, boolean_attributes, object_ref, *hashes)
  hashes << Hamlit::ObjectRef.parse(object_ref) if object_ref
  buf  = []
  hash = merge_all_attrs(hashes)

  keys = hash.keys.sort!
  keys.each do |key|
    case key
    when 'id'.freeze
      buf << " id=#{quote}#{build_id(escape_attrs, *hash[key])}#{quote}"
    when 'class'.freeze
      buf << " class=#{quote}#{build_class(escape_attrs, *hash[key])}#{quote}"
    when 'data'.freeze
      buf << build_data(escape_attrs, quote, *hash[key])
    when *boolean_attributes, /\Adata-/
      build_boolean!(escape_attrs, quote, format, buf, key, hash[key])
    else
      buf << " #{key}=#{quote}#{escape_html(escape_attrs, hash[key].to_s)}#{quote}"
    end
  end
  buf.join
end

.build_aria(escape_attrs, quote, *hashes) ⇒ Object



471
472
473
# File 'ext/hamlit/hamlit.c', line 471

def build_aria(escape_attrs, quote, *hashes)
  build_data_attribute(:aria, escape_attrs, quote, *hashes)
end

.build_class(escape_attrs, *values) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'ext/hamlit/hamlit.c', line 460

def build_class(escape_attrs, *values)
  if values.size == 1
    value = values.first
    case
    when value.is_a?(String)
      # noop
    when value.is_a?(Array)
      value = value.flatten.select { |v| v }.map(&:to_s).uniq.join(' ')
    when value
      value = value.to_s
    else
      return ''
    end
    return escape_html(escape_attrs, value)
  end

  classes = []
  values.each do |value|
    case
    when value.is_a?(String)
      classes += value.split(' ')
    when value.is_a?(Array)
      classes += value.select { |v| v }
    when value
      classes << value.to_s
    end
  end
  escape_html(escape_attrs, classes.map(&:to_s).uniq.join(' '))
end

.build_data(escape_attrs, quote, *hashes) ⇒ Object



482
483
484
# File 'ext/hamlit/hamlit.c', line 482

def build_data(escape_attrs, quote, *hashes)
  build_data_attribute(:data, escape_attrs, quote, *hashes)
end

.build_id(escape_attrs, *values) ⇒ Object



449
450
451
# File 'ext/hamlit/hamlit.c', line 449

def build_id(escape_attrs, *values)
  escape_html(escape_attrs, values.flatten.select { |v| v }.join('_'))
end