module Temple module HTML # @api public class Fast < Filter XHTML_DOCTYPES = { '1.1' => '', '5' => '', 'html' => '', 'strict' => '', 'frameset' => '', 'mobile' => '', 'basic' => '', 'transitional' => '', 'svg' => '' }.freeze HTML_DOCTYPES = { '5' => '', 'html' => '', 'strict' => '', 'frameset' => '', 'transitional' => '', }.freeze define_options :format => :xhtml, :attr_quote => '"', :autoclose => %w[base basefont bgsound link meta area br embed img keygen wbr input menuitem param source track hr col frame], :js_wrapper => nil HTML = [:html, :html4, :html5] def initialize(opts = {}) super unless [:xhtml, *HTML].include?(options[:format]) raise ArgumentError, "Invalid format #{options[:format].inspect}" end wrapper = options[:js_wrapper] wrapper = xhtml? ? :cdata : :comment if wrapper == :guess @js_wrapper = case wrapper when :comment [ "" ] when :cdata [ "\n//\n" ] when :both [ "" ] when nil when Array wrapper else raise ArgumentError, "Invalid JavaScript wrapper #{wrapper.inspect}" end end def xhtml? options[:format] == :xhtml end def html? !xhtml? end def on_html_doctype(type) type = type.to_s.downcase if type =~ /^xml(\s+(.+))?$/ raise(FilterError, 'Invalid xml directive in html mode') if html? w = options[:attr_quote] str = "" elsif html? str = HTML_DOCTYPES[type] || raise(FilterError, "Invalid html doctype #{type}") else str = XHTML_DOCTYPES[type] || raise(FilterError, "Invalid xhtml doctype #{type}") end [:static, str] end def on_html_comment(content) [:multi, [:static, '']] end def on_html_condcomment(condition, content) on_html_comment [:multi, [:static, "[#{condition}]>"], content, [:static, ''] result << compile(content) if content result << [:static, ""] if !closed result end def on_html_attrs(*attrs) [:multi, *attrs.map {|attr| compile(attr) }] end def on_html_attr(name, value) if html? && empty_exp?(value) [:static, " #{name}"] else [:multi, [:static, " #{name}=#{options[:attr_quote]}"], compile(value), [:static, options[:attr_quote]]] end end def on_html_js(content) if @js_wrapper [:multi, [:static, @js_wrapper.first], compile(content), [:static, @js_wrapper.last]] else compile(content) end end end end end