Class: Markaby::Tagset
- Inherits:
-
Object
- Object
- Markaby::Tagset
- Defined in:
- lib/markaby/lib/markaby/tagset.rb
Class Attribute Summary collapse
-
.doctype ⇒ Object
Returns the value of attribute doctype.
-
.forms ⇒ Object
Returns the value of attribute forms.
-
.self_closing ⇒ Object
Returns the value of attribute self_closing.
-
.tags ⇒ Object
Returns the value of attribute tags.
-
.tagset ⇒ Object
Returns the value of attribute tagset.
Class Method Summary collapse
- .can_handle?(tag_name) ⇒ Boolean
- .default_options ⇒ Object
- .handle_tag(tag_name, builder, *args, &block) ⇒ Object
- .transform_attribute_hash(attributes, prefix) ⇒ Object
- .transform_attribute_name(name) ⇒ Object
- .transform_attributes(tag_name, attributes) ⇒ Object
- .valid_attribute_name?(tag_name, attribute_name) ⇒ Boolean
- .validate_and_transform_attributes!(tag_name, *args) ⇒ Object
- .validate_and_transform_tag_name!(tag_name) ⇒ Object
- .validate_attribute!(tag_name, attribute_name) ⇒ Object
Class Attribute Details
.doctype ⇒ Object
Returns the value of attribute doctype.
33 34 35 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 33 def doctype @doctype end |
.forms ⇒ Object
Returns the value of attribute forms.
33 34 35 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 33 def forms @forms end |
.self_closing ⇒ Object
Returns the value of attribute self_closing.
33 34 35 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 33 def self_closing @self_closing end |
.tags ⇒ Object
Returns the value of attribute tags.
33 34 35 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 33 def @tags end |
.tagset ⇒ Object
Returns the value of attribute tagset.
33 34 35 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 33 def @tagset end |
Class Method Details
.can_handle?(tag_name) ⇒ Boolean
39 40 41 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 39 def can_handle? tag_name false end |
.default_options ⇒ Object
35 36 37 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 35 def {tagset: self} end |
.handle_tag(tag_name, builder, *args, &block) ⇒ Object
43 44 45 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 43 def handle_tag tag_name, builder, *args, &block raise NoMethodError.new end |
.transform_attribute_hash(attributes, prefix) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 70 def transform_attribute_hash attributes, prefix values = attributes[prefix] = {} values.each do |suffix, value| name = transform_attribute_name "#{prefix}-#{suffix}" [name] = value end attributes.merge!().delete(prefix) end |
.transform_attribute_name(name) ⇒ Object
66 67 68 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 66 def transform_attribute_name name name.to_s.downcase.tr("_", "-").to_sym end |
.transform_attributes(tag_name, attributes) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 56 def transform_attributes tag_name, attributes attributes[:name] ||= attributes[:id] if forms.include?(tag_name) && attributes[:id] attributes.transform_keys! { |name| transform_attribute_name name } hashed_attributes = attributes.keys.select { |name| attributes[name].is_a? Hash } hashed_attributes.each { |name| transform_attribute_hash attributes, name } attributes.reject! { |name, value| name.nil? || (AttrsBoolean.include?(name) && value.nil?) } attributes.keys.each { |name| validate_attribute! tag_name, name } attributes end |
.valid_attribute_name?(tag_name, attribute_name) ⇒ Boolean
84 85 86 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 84 def valid_attribute_name? tag_name, attribute_name attribute_name.to_s.start_with?(":", "data-", "aria-") || @tagset[tag_name].include?(attribute_name) end |
.validate_and_transform_attributes!(tag_name, *args) ⇒ Object
52 53 54 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 52 def validate_and_transform_attributes! tag_name, *args args.last.respond_to?(:to_hash) ? transform_attributes(tag_name, args.last.to_hash) : {} end |
.validate_and_transform_tag_name!(tag_name) ⇒ Object
47 48 49 50 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 47 def validate_and_transform_tag_name! tag_name raise(InvalidXhtmlError, "no element `#{tag_name}' for #{doctype}") unless @tagset.has_key?(tag_name) tag_name end |
.validate_attribute!(tag_name, attribute_name) ⇒ Object
80 81 82 |
# File 'lib/markaby/lib/markaby/tagset.rb', line 80 def validate_attribute! tag_name, attribute_name raise InvalidXhtmlError, "no attribute `#{attribute_name}' on #{tag_name} elements" unless valid_attribute_name? tag_name, attribute_name end |