Sha256: 03d6caa5206dfc13a51aac605b1835dffbc37b3189ca4cc19f24120386a5b9ab

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

require 'template'
require 'haml/attribute_builder'

module Haml
  module Buffer
    def rstrip!
      self
    end
  end

  module Helpers
    def self.html_escape(string)
      # http://stackoverflow.com/a/9756789
      %x{
        return ('' + string) /* Forces the conversion to string. */
            .replace(/&/g, '&') /* This MUST be the 1st replacement. */
            .replace(/'/g, ''') /* The 4 other predefined entities, required. */
            .replace(/"/g, '"')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            /*
            You may add other replacements here for HTML only
            (but it's not necessary).
            Or for XML, only if the named entities are defined in its DTD.
            */
            .replace(/\r\n/g, '&#13;') /* Must be before the next replacement. */
            .replace(/[\r\n]/g, '&#13;');
      }
    end
  end
end

class Template
  class OutputBuffer
    alias << append

    # allow tags in haml to have dynamic attributes
    def attributes(class_id, obj_ref, *attributes_hashes)
      attributes = class_id

      attributes_hashes.each do |hash|
        attributes.update hash do |_, oldval, newval|
          Array(oldval) + Array(newval)
        end
      end

      result = []
      _render_attributes attributes, result, ''

      result.join
    end

    private

    def _render_attributes(attributes, out, prefix)
      attributes.each do |attr, value|
        attr_name = prefix + attr

        case value
        when true then out << " #{attr_name}"
        when false, nil then next
        when Hash then _render_attributes(value, out, attr_name + '-')
        else
          value = value.compact.join ' ' if value.is_a? Array
          out << " #{attr_name}='#{_attribute_escape value}'"
        end
      end
    end

    def _attribute_escape(value)
      value.to_s.gsub(/['"&<>]/, '"' => '&quot;', "'" => '&apos;', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opal-haml-0.4.6 lib-opal/opal-haml.rb
opal-haml-0.4.5 lib-opal/opal-haml.rb