Sha256: cf2bc580b076cf2020421ee52875cff21439e15b87c93d65d59b0fc51c6245d4

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'erb'
require 'set'

class ERB
  module Util
    HTML_ESCAPE = { '&' => '&amp;', '"' => '&quot;', '>' => '&gt;', '<' => '&lt;' }

    def html_escape(s)
      s.to_s.gsub(/[&\"><]/) { |special| HTML_ESCAPE[special] }
    end
  end
end

module Webby
module Helpers #:nodoc:

# Provides methods to generate HTML tags programmatically.
# By default, they output XHTML compliant tags.
#
module TagHelper
  include ERB::Util

  BOOLEAN_ATTRIBUTES = Set.new(%w(disabled readonly multiple))

  # Returns an escaped version of +html+ without affecting existing escaped
  # entities.
  #
  # ==== Examples
  #   escape_once("1 > 2 &amp; 3")
  #   # => "1 &lt; 2 &amp; 3"
  #
  #   escape_once("&lt;&lt; Accept & Checkout")
  #   # => "&lt;&lt; Accept &amp; Checkout"
  #
  def escape_once( html )
    html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
  end

  private

  def tag_options( options, escape = true )
    unless options.empty?
      attrs = []
      if escape
        options.each do |key, value|
          next if value.nil?
          key = key.to_s
          value = BOOLEAN_ATTRIBUTES.include?(key) ? key : escape_once(value)
          attrs << %Q(#{key}="#{value}")
        end
      else
        attrs = options.map {|key, value| %Q(#{key}="#{value}")}
      end
      %Q( #{attrs.sort * ' '}) unless attrs.empty?
    end
  end

end  # module TagHelper
end  # module Helpers
end  # module Webby

# EOF

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webby-0.7.2 lib/webby/helpers/tag_helper.rb
webby-0.7.3 lib/webby/helpers/tag_helper.rb
webby-0.7.4 lib/webby/helpers/tag_helper.rb