Sha256: d20d6b2b69808ca01482378cc2a9d7d16182ac20cf3bcc642a6e1cca25aaaa8f

Contents?: true

Size: 885 Bytes

Versions: 2

Compression:

Stored size: 885 Bytes

Contents

module Deas; end
module Deas::ErbTags

  module Utils

    ESCAPE_ATTRS = {
      "&" => "&",
      "<" => "&lt;",
      '"' => "&quot;"
    }
    ESCAPE_ATTRS_PATTERN = Regexp.union(*ESCAPE_ATTRS.keys)
    def self.escape_attr_value(value)
      value.to_s.gsub(ESCAPE_ATTRS_PATTERN){|c| ESCAPE_ATTRS[c] }
    end

    def self.html_attrs(attrs="", ns=nil)
      return attrs.to_s if !attrs.kind_of?(::Hash)

      attrs.collect do |k_v|
        [ns ? "#{ns}_#{k_v.first}" : k_v.first.to_s, k_v.last]
      end.sort.collect do |k_v|
        if k_v.last.kind_of?(::Hash)
          html_attrs(k_v.last, k_v.first)
        elsif k_v.last.kind_of?(::Array)
          " #{k_v.first}=\"#{escape_attr_value(k_v.last.join(' '))}\""
        else
          " #{k_v.first}=\"#{escape_attr_value(k_v.last)}\""
        end
      end.join
    end

  end

  # alias for brevity
  U = Utils

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deas-erbtags-0.3.0 lib/deas-erbtags/utils.rb
deas-erbtags-0.2.0 lib/deas-erbtags/utils.rb