module HTMLComponent

Constants

FORBIDDEN_ATTRIBUTES

These attributes are deprecated or outright forbidden. However, some people might still try to use them. These attributes are expressly disallowed during generation, and won't be included, even if provided.

LIMITED_ATTRIBUTES

A list of all limited attributes in HTML5. Any attribute not listed here are free to use in any element, as long as it is not in FORBIDDEN_ATTRIBUTES.

Any attributes listed here will only be allowed in the associated elements.

TAG_LIST

A list of all valid HTML5 elements. These are used to generate generator methods within HTMLComponent contexts.

Public Class Methods

singleton(&block) click to toggle source

Creates a module that encompasses the given block in an HTMLComponent context. This gives access to methods in the block as though the block was declared as the `render` function in a module extending HTMLComponent (pretty much because it is).

# File lib/html-native.rb, line 39
def self.singleton(&block)
  Module.new do
    extend HTMLComponent
    define_singleton_method :render, &block
  end
end

Public Instance Methods

_if(bool, &block) click to toggle source
# File lib/html-native/logic.rb, line 20
def _if(bool, &block)
  if bool
    Builder.new(block.call)
  else
    Builder.new
  end
end
_label(attrs = {}, &block) click to toggle source
# File lib/html-native.rb, line 25
def _label(attrs = {}, &block)
  attrs ||= {}
  if block
    body = block.call
    Builder.new("<label#{attributes_list(:label, attrs)}>") + body + "</label>"
  else
    Builder.new("<label#{attributes_list(:label, attrs)}/>") 
  end
end
_unless(bool, &block) click to toggle source
# File lib/html-native/logic.rb, line 28
def _unless(bool, &block)
  unless bool
    Builder.new(block.call)
  else
    Builder.new
  end
end
doctype(type) click to toggle source
# File lib/html-native.rb, line 21
def doctype(type)
  Builder.new("<!DOCTYPE #{type}>")
end
valid_attribute?(tag, attribute) click to toggle source

Checks if the attribute is valid for a given tag.

For example, `class` and `hidden` are valid for everything, but `autoplay` is valid for only `video` and `audio` tags, and invalid for all other tags.

# File lib/html-native.rb, line 50
def valid_attribute?(tag, attribute)
  if LIMITED_ATTRIBUTES.key?(attribute.to_sym)
    return LIMITED_ATTRIBUTES[attribute.to_sym].include?(tag.to_sym)
  end
  return !FORBIDDEN_ATTRIBUTES.include?(attribute)
end

Private Instance Methods

attributes_list(tag, attrs) click to toggle source

Given a tag and a set of attributes as a hash, format the attributes to HTML-valid form. If an attribute doesn't have a value or the value is empty, it's treated as a boolean attribute and formatted as such.

# File lib/html-native.rb, line 62
def attributes_list(tag, attrs)
  formatted = attrs.filter{|opt, value| valid_attribute?(tag, opt)}.map do |k,v| 
    if v&.to_s.empty?
      k.to_s
    else
      "#{k}=\"#{v}\"" # render this appropriately for numeric fields (might already)
    end
  end.join(" ")
  formatted.prepend(" ") unless formatted.empty?
end