lib/erector/html.rb in erector-0.8.2 vs lib/erector/html.rb in erector-0.8.3

- old
+ new

@@ -175,10 +175,11 @@ end # Return a character given its unicode code point or unicode name. def character(code_point_or_name) if code_point_or_name.is_a?(Symbol) + require "erector/unicode" found = Erector::CHARACTERS[code_point_or_name] if found.nil? raise "Unrecognized character #{code_point_or_name}" end raw("&#x#{sprintf '%x', found};") @@ -201,68 +202,58 @@ # the output includes the opening condition and closing "[endif]". See # http://www.quirksmode.org/css/condcom.html # # Since "Authors should avoid putting two or more adjacent hyphens inside comments," # we emit a warning if you do that. - def comment(text = '', &block) + def comment(text = '') puts "Warning: Authors should avoid putting two or more adjacent hyphens inside comments." if text =~ /--/ conditional = text =~ /\[if .*\]/ rawtext "<!--" rawtext text rawtext ">" if conditional - if block + if block_given? rawtext "\n" - block.call + yield rawtext "\n" end rawtext "<![endif]" if conditional rawtext "-->\n" end # Emits a javascript block inside a +script+ tag, wrapped in CDATA # doohickeys like all the cool JS kids do. - def javascript(*args, &block) - if args.length > 2 - raise ArgumentError, "Cannot accept more than two arguments" + def javascript(value = nil, attributes = {}) + if value.is_a?(Hash) + attributes = value + value = nil + elsif block_given? && value + raise ArgumentError, "You can't pass both a block and a value to javascript -- please choose one." end - attributes, value = nil, nil - arg0 = args[0] - if arg0.is_a?(Hash) - attributes = arg0 - else - value = arg0 - arg1 = args[1] - if arg1.is_a?(Hash) - attributes = arg1 + + script(attributes.merge(:type => "text/javascript")) do + # Shouldn't this be a "cdata" HtmlPart? + # (maybe, but the syntax is specific to javascript; it isn't + # really a generic XML CDATA section. Specifically, + # ]]> within value is not treated as ending the + # CDATA section by Firefox2 when parsing text/html, + # although I guess we could refuse to generate ]]> + # there, for the benefit of XML/XHTML parsers). + output << raw("\n// <![CDATA[\n") + if block_given? + yield + else + output << raw(value) end + output << raw("\n// ]]>") + output.append_newline # this forces a newline even if we're not in pretty mode end - attributes ||= {} - attributes[:type] = "text/javascript" - open_tag 'script', attributes - # Shouldn't this be a "cdata" HtmlPart? - # (maybe, but the syntax is specific to javascript; it isn't - # really a generic XML CDATA section. Specifically, - # ]]> within value is not treated as ending the - # CDATA section by Firefox2 when parsing text/html, - # although I guess we could refuse to generate ]]> - # there, for the benefit of XML/XHTML parsers). - rawtext "\n// <![CDATA[\n" - if block - instance_eval(&block) - else - rawtext value - end - rawtext "\n// ]]>" - output.append_newline # this forces a newline even if we're not in pretty mode - - close_tag 'script' - rawtext "\n" + output << raw("\n") end protected def __element__(raw, tag_name, *args, &block) if args.length > 2 @@ -320,28 +311,28 @@ value = value.join(' ') end results << "#{key}=\"#{h(value)}\"" end end - return results.join(' ') + results.join(' ') end def sorted(attributes) stringized = [] attributes.each do |key, value| stringized << [key.to_s, value] end - return stringized.sort + stringized.sort end def sort_for_xml_declaration(attributes) # correct order is "version, encoding, standalone" (XML 1.0 section 2.8). # But we only try to put version before encoding for now. stringized = [] attributes.each do |key, value| stringized << [key.to_s, value] end - return stringized.sort{|a, b| b <=> a} + stringized.sort{|a, b| b <=> a} end NON_NEWLINEY = {'i' => true, 'b' => true, 'small' => true, 'img' => true, 'span' => true, 'a' => true, 'input' => true, 'textarea' => true, 'button' => true, 'select' => true