1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/cgi/{core,html}.rb : Update define tagmaker

because to delete eval.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
xibbar 2013-04-11 08:04:27 +00:00
parent 3ae53e4943
commit f75da76ffc
2 changed files with 120 additions and 143 deletions

View file

@ -832,29 +832,23 @@ class CGI
when "html3" when "html3"
require 'cgi/html' require 'cgi/html'
extend Html3 extend Html3
element_init()
extend HtmlExtension extend HtmlExtension
when "html4" when "html4"
require 'cgi/html' require 'cgi/html'
extend Html4 extend Html4
element_init()
extend HtmlExtension extend HtmlExtension
when "html4Tr" when "html4Tr"
require 'cgi/html' require 'cgi/html'
extend Html4Tr extend Html4Tr
element_init()
extend HtmlExtension extend HtmlExtension
when "html4Fr" when "html4Fr"
require 'cgi/html' require 'cgi/html'
extend Html4Tr extend Html4Tr
element_init()
extend Html4Fr extend Html4Fr
element_init()
extend HtmlExtension extend HtmlExtension
when "html5" when "html5"
require 'cgi/html' require 'cgi/html'
extend Html5 extend Html5
element_init()
extend HtmlExtension extend HtmlExtension
end end
end end

View file

@ -8,55 +8,64 @@ class CGI
# Generate code for an element with required start and end tags. # Generate code for an element with required start and end tags.
# #
# - - # - -
def nn_element_def(element) def nn_element(element, attributes = {})
nOE_element_def(element, <<-END) s = nOE_element(element, attributes)
if block_given? if block_given?
yield.to_s s << yield.to_s
else else
"" ""
end + end
"</#{element.upcase}>" s << "</#{element.upcase}>"
END end
def nn_element_def(attributes = {}, &block)
nn_element(__callee__, attributes, &block)
end end
# Generate code for an empty element. # Generate code for an empty element.
# #
# - O EMPTY # - O EMPTY
def nOE_element_def(element, append = nil) def nOE_element(element, attributes = {})
s = <<-END attributes={attributes=>nil} if attributes.kind_of?(String)
attributes={attributes=>nil} if attributes.kind_of?(String) s = "<#{element.upcase}"
"<#{element.upcase}" + attributes.collect{|name, value| attributes.each do|name, value|
next unless value next unless value
" " + CGI::escapeHTML(name.to_s) + s << " "
if true == value s << CGI::escapeHTML(name.to_s)
"" if value != true
else s << '="'
'="' + CGI::escapeHTML(value.to_s) + '"' s << CGI::escapeHTML(value.to_s)
end s << '"'
}.join + ">" end
END end
s.sub!(/\Z/, " +") << append if append s << ">"
s
end 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 # Generate code for an element for which the end (and possibly the
# start) tag is optional. # start) tag is optional.
# #
# O O or - O # O O or - O
def nO_element_def(element) def nO_element(element, attributes = {})
nOE_element_def(element, <<-END) s = nOE_element(element)
if block_given? if block_given?
yield.to_s + "</#{element.upcase}>" s << yield.to_s
else s << "</#{element.upcase}>"
"" end
end s
END end
def nO_element_def(attributes = {}, &block)
nO_element(__callee__, attributes, &block)
end end
end # TagMaker end # TagMaker
#
# Mixin module providing HTML generation methods. # Mixin module providing HTML generation methods.
# #
# For example, # For example,
@ -836,50 +845,38 @@ class CGI
# Mixin module for HTML version 3 generation methods. # Mixin module for HTML version 3 generation methods.
module Html3 # :nodoc: module Html3 # :nodoc:
include TagMaker
# The DOCTYPE declaration for this version of HTML # The DOCTYPE declaration for this version of HTML
def doctype def doctype
%|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">| %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">|
end end
# Initialise the HTML generation methods for this version. instance_method(:nn_element_def).tap do |m|
def element_init
extend TagMaker
return if defined?(html)
methods = ""
# - - # - -
for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG 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 DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV CENTER MAP
APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT TABLE TITLE APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT TABLE TITLE
STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE
CAPTION ] CAPTION ]
methods << <<-BEGIN + nn_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
end
instance_method(:nOE_element_def).tap do |m|
# - O EMPTY # - O EMPTY
for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
ISINDEX META ] ISINDEX META ]
methods << <<-BEGIN + nOE_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
end
instance_method(:nO_element_def).tap do |m|
# O O or - O # O O or - O
for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION TR for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION TR
TH TD ] TH TD ]
methods << <<-BEGIN + nO_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
eval(methods)
end end
end # Html3 end # Html3
@ -887,6 +884,7 @@ class CGI
# Mixin module for HTML version 4 generation methods. # Mixin module for HTML version 4 generation methods.
module Html4 # :nodoc: module Html4 # :nodoc:
include TagMaker
# The DOCTYPE declaration for this version of HTML # The DOCTYPE declaration for this version of HTML
def doctype def doctype
@ -894,42 +892,30 @@ class CGI
end end
# Initialise the HTML generation methods for this version. # Initialise the HTML generation methods for this version.
def element_init # - -
extend TagMaker instance_method(:nn_element_def).tap do |m|
return if defined?(html)
methods = ""
# - -
for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD 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 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 H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP
FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
TEXTAREA FORM A BLOCKQUOTE CAPTION ] TEXTAREA FORM A BLOCKQUOTE CAPTION ]
methods << <<-BEGIN + nn_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 ] for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ]
methods << <<-BEGIN + nOE_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
COLGROUP TR TH TD HEAD ] COLGROUP TR TH TD HEAD ]
methods << <<-BEGIN + nO_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
eval(methods)
end end
end # Html4 end # Html4
@ -937,6 +923,7 @@ class CGI
# Mixin module for HTML version 4 transitional generation methods. # Mixin module for HTML version 4 transitional generation methods.
module Html4Tr # :nodoc: module Html4Tr # :nodoc:
include TagMaker
# The DOCTYPE declaration for this version of HTML # The DOCTYPE declaration for this version of HTML
def doctype def doctype
@ -944,44 +931,32 @@ class CGI
end end
# Initialise the HTML generation methods for this version. # Initialise the HTML generation methods for this version.
def element_init # - -
extend TagMaker instance_method(:nn_element_def).tap do |m|
return if defined?(html)
methods = ""
# - -
for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN 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 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 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 INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET
LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT
NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ] NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ]
methods << <<-BEGIN + nn_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
COL ISINDEX META ] COL ISINDEX META ]
methods << <<-BEGIN + nOE_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
COLGROUP TR TH TD HEAD ] COLGROUP TR TH TD HEAD ]
methods << <<-BEGIN + nO_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
eval(methods)
end end
end # Html4Tr end # Html4Tr
@ -989,6 +964,7 @@ class CGI
# Mixin module for generating HTML version 4 with framesets. # Mixin module for generating HTML version 4 with framesets.
module Html4Fr # :nodoc: module Html4Fr # :nodoc:
include TagMaker
# The DOCTYPE declaration for this version of HTML # The DOCTYPE declaration for this version of HTML
def doctype def doctype
@ -996,27 +972,18 @@ class CGI
end end
# Initialise the HTML generation methods for this version. # Initialise the HTML generation methods for this version.
def element_init # - -
return if defined?(frameset) instance_method(:nn_element_def).tap do |m|
methods = ""
# - -
for element in %w[ FRAMESET ] for element in %w[ FRAMESET ]
methods << <<-BEGIN + nn_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
end
# - O EMPTY # - O EMPTY
instance_method(:nOE_element_def).tap do |m|
for element in %w[ FRAME ] for element in %w[ FRAME ]
methods << <<-BEGIN + nOE_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
eval(methods)
end end
end # Html4Fr end # Html4Fr
@ -1024,6 +991,7 @@ class CGI
# Mixin module for HTML version 5 generation methods. # Mixin module for HTML version 5 generation methods.
module Html5 # :nodoc: module Html5 # :nodoc:
include TagMaker
# The DOCTYPE declaration for this version of HTML # The DOCTYPE declaration for this version of HTML
def doctype def doctype
@ -1031,11 +999,8 @@ class CGI
end end
# Initialise the HTML generation methods for this version. # Initialise the HTML generation methods for this version.
def element_init # - -
extend TagMaker instance_method(:nn_element_def).tap do |m|
return if defined?(html)
methods = ""
# - -
for element in %w[ SECTION NAV ARTICLE ASIDE HGROUP HEADER for element in %w[ SECTION NAV ARTICLE ASIDE HGROUP HEADER
FOOTER FIGURE FIGCAPTION S TIME U MARK RUBY BDI IFRAME FOOTER FIGURE FIGCAPTION S TIME U MARK RUBY BDI IFRAME
VIDEO AUDIO CANVAS DATALIST OUTPUT PROGRESS METER DETAILS VIDEO AUDIO CANVAS DATALIST OUTPUT PROGRESS METER DETAILS
@ -1044,34 +1009,52 @@ class CGI
H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT
FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
TEXTAREA FORM A BLOCKQUOTE CAPTION ] TEXTAREA FORM A BLOCKQUOTE CAPTION ]
methods += <<-BEGIN + nn_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META
COMMAND EMBED KEYGEN SOURCE TRACK WBR ] COMMAND EMBED KEYGEN SOURCE TRACK WBR ]
methods += <<-BEGIN + nOE_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end 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 for element in %w[ HTML HEAD BODY P DT DD LI OPTION THEAD TFOOT TBODY
OPTGROUP COLGROUP RT RP TR TH TD ] OPTGROUP COLGROUP RT RP TR TH TD ]
methods += <<-BEGIN + nO_element_def(element) + <<-END define_method(element.downcase, m)
def #{element.downcase}(attributes = {})
BEGIN
end
END
end end
eval(methods)
end end
end # Html5 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 end