module Crystal module BasicHtmlHelper BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer autoplay controls loop selected hidden scoped async defer reversed ismap seemless muted required autofocus novalidate formnovalidate open).to_set BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attr| attr.to_sym }) # # authenticity_token # def authenticity_token; workspace.request.session['authenticity_token'] end # # JavaScript & StyleSheets # def stylesheet_link_tag *stylesheets Array(stylesheets).collect{|ssheet| tag :link, '', :href => "#{config && config.url_root!}#{ssheet}", :media => "screen", :rel => "stylesheet", :type => "text/css" }.join("\n") end # # Assets # def image_tag src, opt = {} opt[:src] = src tag :img, '', opt end # # Common HTML tags # def label_tag name, text, options = {} tag :label, text, options.merge(:for => name) end # # Special # def tag name, content_or_options_with_block = nil, options = nil, escape = true, &block if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) content = capture(&block) concat "<#{name}#{tag_options(options)}>#{content}" else content = content_or_options_with_block "<#{name}#{tag_options(options)}>#{content}" end end # instance_methods(false).each do |m| # alias_method "html_#{m}", m # end # Escape def h obj; obj.to_s.html_escape end protected def tag_options options return "" unless options options.must_be.a Hash unless options.blank? attrs = [] options.each_pair do |key, value| if BOOLEAN_ATTRIBUTES.include?(key) attrs << %(#{key}="#{key}") if value elsif !value.nil? final_value = value.is_a?(Array) ? value.join(" ") : value attrs << %(#{key}="#{final_value}") end end " #{attrs.sort * ' '}" unless attrs.empty? end end end end