diff options
Diffstat (limited to 'lib/cgi/html.rb')
| -rw-r--r-- | lib/cgi/html.rb | 303 |
1 files changed, 131 insertions, 172 deletions
diff --git a/lib/cgi/html.rb b/lib/cgi/html.rb index 3054279b54..1543943320 100644 --- a/lib/cgi/html.rb +++ b/lib/cgi/html.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class CGI # Base module for HTML-generation mixins. # @@ -8,55 +9,62 @@ class CGI # Generate code for an element with required start and end tags. # # - - - def nn_element_def(element) - nOE_element_def(element, <<-END) - if block_given? - yield.to_s - else - "" - end + - "</#{element.upcase}>" - END + def nn_element(element, attributes = {}) + s = nOE_element(element, attributes) + if block_given? + s << yield.to_s + end + s << "</#{element.upcase}>" + end + + def nn_element_def(attributes = {}, &block) + nn_element(__callee__, attributes, &block) end # Generate code for an empty element. # # - O EMPTY - def nOE_element_def(element, append = nil) - s = <<-END - attributes={attributes=>nil} if attributes.kind_of?(String) - "<#{element.upcase}" + attributes.collect{|name, value| - next unless value - " " + CGI::escapeHTML(name.to_s) + - if true == value - "" - else - '="' + CGI::escapeHTML(value.to_s) + '"' - end - }.join + ">" - END - s.sub!(/\Z/, " +") << append if append - s + def nOE_element(element, attributes = {}) + attributes={attributes=>nil} if attributes.kind_of?(String) + s = "<#{element.upcase}".dup + attributes.each do|name, value| + next unless value + s << " " + s << CGI.escapeHTML(name.to_s) + if value != true + s << '="' + s << CGI.escapeHTML(value.to_s) + s << '"' + end + end + s << ">" + end + + def nOE_element_def(attributes = {}, &block) + nOE_element(__callee__, attributes, &block) end + # Generate code for an element for which the end (and possibly the # start) tag is optional. # # O O or - O - def nO_element_def(element) - nOE_element_def(element, <<-END) - if block_given? - yield.to_s + "</#{element.upcase}>" - else - "" - end - END + def nO_element(element, attributes = {}) + s = nOE_element(element, attributes) + if block_given? + s << yield.to_s + s << "</#{element.upcase}>" + end + s + end + + def nO_element_def(attributes = {}, &block) + nO_element(__callee__, attributes, &block) end end # TagMaker - # # Mixin module providing HTML generation methods. # # For example, @@ -92,11 +100,7 @@ class CGI else href end - if block_given? - super(attributes){ yield } - else - super(attributes) - end + super(attributes) end # Generate a Document Base URI element as a String. @@ -114,11 +118,7 @@ class CGI else href end - if block_given? - super(attributes){ yield } - else - super(attributes) - end + super(attributes) end # Generate a BlockQuote element as a string. @@ -137,11 +137,7 @@ class CGI else cite end - if block_given? - super(attributes){ yield } - else - super(attributes) - end + super(attributes) end @@ -161,11 +157,7 @@ class CGI else align end - if block_given? - super(attributes){ yield } - else - super(attributes) - end + super(attributes) end @@ -416,7 +408,7 @@ class CGI end pretty = attributes.delete("PRETTY") pretty = " " if true == pretty - buf = "" + buf = "".dup if attributes.has_key?("DOCTYPE") if attributes["DOCTYPE"] @@ -428,14 +420,10 @@ class CGI buf << doctype end - if block_given? - buf << super(attributes){ yield } - else - buf << super(attributes) - end + buf << super(attributes) if pretty - CGI::pretty(buf, pretty) + CGI.pretty(buf, pretty) else buf end @@ -824,11 +812,7 @@ class CGI else name end - if block_given? - super(attributes){ yield } - else - super(attributes) - end + super(attributes) end end # HtmlExtension @@ -836,50 +820,38 @@ class CGI # Mixin module for HTML version 3 generation methods. module Html3 # :nodoc: + include TagMaker # The DOCTYPE declaration for this version of HTML def doctype %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">| end - # Initialise the HTML generation methods for this version. - def element_init - extend TagMaker - return if defined?(html) - methods = "" + instance_method(:nn_element_def).tap do |m| # - - for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV CENTER MAP APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT TABLE TITLE STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE CAPTION ] - methods << <<-BEGIN + nn_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end + instance_method(:nOE_element_def).tap do |m| # - O EMPTY for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT ISINDEX META ] - methods << <<-BEGIN + nOE_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end + instance_method(:nO_element_def).tap do |m| # O O or - O for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION TR TH TD ] - methods << <<-BEGIN + nO_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end - eval(methods) end end # Html3 @@ -887,49 +859,38 @@ class CGI # Mixin module for HTML version 4 generation methods. module Html4 # :nodoc: + include TagMaker # The DOCTYPE declaration for this version of HTML def doctype %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">| end - # Initialise the HTML generation methods for this version. - def element_init - extend TagMaker - return if defined?(html) - methods = "" - # - - + # Initialize the HTML generation methods for this version. + # - - + instance_method(:nn_element_def).tap do |m| for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD VAR CITE ABBR ACRONYM SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ] - methods << <<-BEGIN + nn_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # - O EMPTY + # - O EMPTY + instance_method(:nOE_element_def).tap do |m| for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ] - methods << <<-BEGIN + nOE_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # O O or - O + # O O or - O + instance_method(:nO_element_def).tap do |m| for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY COLGROUP TR TH TD HEAD ] - methods << <<-BEGIN + nO_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end - eval(methods) end end # Html4 @@ -937,6 +898,7 @@ class CGI # Mixin module for HTML version 4 transitional generation methods. module Html4Tr # :nodoc: + include TagMaker # The DOCTYPE declaration for this version of HTML def doctype @@ -944,44 +906,32 @@ class CGI end # Initialise the HTML generation methods for this version. - def element_init - extend TagMaker - return if defined?(html) - methods = "" - # - - + # - - + instance_method(:nn_element_def).tap do |m| for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN CODE SAMP KBD VAR CITE ABBR ACRONYM FONT SUB SUP SPAN BDO ADDRESS DIV CENTER MAP OBJECT APPLET H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ] - methods << <<-BEGIN + nn_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # - O EMPTY + # - O EMPTY + instance_method(:nOE_element_def).tap do |m| for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT COL ISINDEX META ] - methods << <<-BEGIN + nOE_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # O O or - O + # O O or - O + instance_method(:nO_element_def).tap do |m| for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY COLGROUP TR TH TD HEAD ] - methods << <<-BEGIN + nO_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end - eval(methods) end end # Html4Tr @@ -989,6 +939,7 @@ class CGI # Mixin module for generating HTML version 4 with framesets. module Html4Fr # :nodoc: + include TagMaker # The DOCTYPE declaration for this version of HTML def doctype @@ -996,27 +947,18 @@ class CGI end # Initialise the HTML generation methods for this version. - def element_init - return if defined?(frameset) - methods = "" - # - - + # - - + instance_method(:nn_element_def).tap do |m| for element in %w[ FRAMESET ] - methods << <<-BEGIN + nn_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # - O EMPTY + # - O EMPTY + instance_method(:nOE_element_def).tap do |m| for element in %w[ FRAME ] - methods << <<-BEGIN + nOE_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end - eval(methods) end end # Html4Fr @@ -1024,6 +966,7 @@ class CGI # Mixin module for HTML version 5 generation methods. module Html5 # :nodoc: + include TagMaker # The DOCTYPE declaration for this version of HTML def doctype @@ -1031,11 +974,9 @@ class CGI end # Initialise the HTML generation methods for this version. - def element_init - extend TagMaker - methods = "" - # - - - for element in %w[ SECTION NAV ARTICLE ASIDE HGROUP + # - - + instance_method(:nn_element_def).tap do |m| + for element in %w[ SECTION NAV ARTICLE ASIDE HGROUP HEADER FOOTER FIGURE FIGCAPTION S TIME U MARK RUBY BDI IFRAME VIDEO AUDIO CANVAS DATALIST OUTPUT PROGRESS METER DETAILS SUMMARY MENU DIALOG I B SMALL EM STRONG DFN CODE SAMP KBD @@ -1043,34 +984,52 @@ class CGI H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ] - methods += <<-BEGIN + nn_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # - O EMPTY + # - O EMPTY + instance_method(:nOE_element_def).tap do |m| for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META COMMAND EMBED KEYGEN SOURCE TRACK WBR ] - methods += <<-BEGIN + nOE_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end + end - # O O or - O + # O O or - O + instance_method(:nO_element_def).tap do |m| for element in %w[ HTML HEAD BODY P DT DD LI OPTION THEAD TFOOT TBODY OPTGROUP COLGROUP RT RP TR TH TD ] - methods += <<-BEGIN + nO_element_def(element) + <<-END - def #{element.downcase}(attributes = {}) - BEGIN - end - END + define_method(element.downcase, m) end - eval(methods) end end # Html5 + + class HTML3 + include Html3 + include HtmlExtension + end + + class HTML4 + include Html4 + include HtmlExtension + end + + class HTML4Tr + include Html4Tr + include HtmlExtension + end + + class HTML4Fr + include Html4Tr + include Html4Fr + include HtmlExtension + end + + class HTML5 + include Html5 + include HtmlExtension + end + end |
