Class: Slim::Splat::Builder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/slim/splat/builder.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

INVALID_ATTRIBUTE_NAME_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

/[ \0"'>\/=]/

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.



10
11
12
13
# File 'lib/slim/splat/builder.rb', line 10

def initialize(options)
  @options = options
  @attrs = {}
end

Instance Method Details

#attr(name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/slim/splat/builder.rb', line 32

def attr(name, value)
  if name =~ INVALID_ATTRIBUTE_NAME_REGEX
    raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered"
  end
  if @attrs[name]
    if delim = @options[:merge_attrs][name]
      @attrs[name] = @attrs[name].to_s + delim + value.to_s
    else
      raise("Multiple #{name} attributes specified")
    end
  else
    @attrs[name] = value
  end
end

#build_attrsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/slim/splat/builder.rb', line 58

def build_attrs
  attrs = @options[:sort_attrs] ? @attrs.sort_by(&:first) : @attrs
  attrs.map do |k, v|
    if v == true
      if @options[:format] == :xhtml
        " #{k}=#{@options[:attr_quote]}#{@options[:attr_quote]}"
      else
        " #{k}"
      end
    else
      " #{k}=#{@options[:attr_quote]}#{v}#{@options[:attr_quote]}"
    end
  end.join
end

#build_tag(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
# File 'lib/slim/splat/builder.rb', line 47

def build_tag(&block)
  tag = @attrs.delete('tag').to_s
  tag = @options[:default_tag] if tag.empty?
  if block
    content = capture_block_content(&block)
    "<#{tag}#{build_attrs}>#{content}</#{tag}>"
  else
    "<#{tag}#{build_attrs} />"
  end
end

#code_attr(name, escape, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
18
19
20
21
22
23
24
# File 'lib/slim/splat/builder.rb', line 15

def code_attr(name, escape, value)
  if delim = @options[:merge_attrs][name]
    value = Array === value ? value.join(delim) : value.to_s
    attr(name, escape_html(escape, value)) unless value.empty?
  elsif @options[:hyphen_attrs].include?(name) && Hash === value
    hyphen_attr(name, escape, value)
  elsif value != false && value != nil
    attr(name, escape_html(escape, value))
  end
end

#splat_attrs(splat) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
# File 'lib/slim/splat/builder.rb', line 26

def splat_attrs(splat)
  splat.each do |name, value|
    code_attr(name.to_s, true, value)
  end
end