[前][次][番号順一覧][スレッド一覧]

ruby-changes:28185

From: xibbar <ko1@a...>
Date: Thu, 11 Apr 2013 17:04:36 +0900 (JST)
Subject: [ruby-changes:28185] xibbar:r40237 (trunk): * lib/cgi/{core,html}.rb : Update define tagmaker

xibbar	2013-04-11 17:04:27 +0900 (Thu, 11 Apr 2013)

  New Revision: 40237

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40237

  Log:
    * lib/cgi/{core,html}.rb : Update define tagmaker
      because to delete eval.

  Modified files:
    trunk/lib/cgi/core.rb
    trunk/lib/cgi/html.rb

Index: lib/cgi/html.rb
===================================================================
--- lib/cgi/html.rb	(revision 40236)
+++ lib/cgi/html.rb	(revision 40237)
@@ -8,55 +8,64 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L8
     # 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
+      else
+        ""
+      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}"
+      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)
+      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,
@@ -836,50 +845,38 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L845
 
   # 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,6 +884,7 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L884
 
   # Mixin module for HTML version 4 generation methods.
   module Html4 # :nodoc:
+    include TagMaker
 
     # The DOCTYPE declaration for this version of HTML
     def doctype
@@ -894,42 +892,30 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L892
     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 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
+    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
+    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 +923,7 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L923
 
   # 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 +931,32 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L931
     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 +964,7 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L964
 
   # 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 +972,18 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L972
     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 +991,7 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L991
 
   # 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 +999,8 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L999
     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[ SECTION NAV ARTICLE ASIDE HGROUP HEADER
         FOOTER FIGURE FIGCAPTION S TIME U MARK RUBY BDI IFRAME
         VIDEO AUDIO CANVAS DATALIST OUTPUT PROGRESS METER DETAILS
@@ -1044,34 +1009,52 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/html.rb#L1009
         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
Index: lib/cgi/core.rb
===================================================================
--- lib/cgi/core.rb	(revision 40236)
+++ lib/cgi/core.rb	(revision 40237)
@@ -832,29 +832,23 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/core.rb#L832
     when "html3"
       require 'cgi/html'
       extend Html3
-      element_init()
       extend HtmlExtension
     when "html4"
       require 'cgi/html'
       extend Html4
-      element_init()
       extend HtmlExtension
     when "html4Tr"
       require 'cgi/html'
       extend Html4Tr
-      element_init()
       extend HtmlExtension
     when "html4Fr"
       require 'cgi/html'
       extend Html4Tr
-      element_init()
       extend Html4Fr
-      element_init()
       extend HtmlExtension
     when "html5"
       require 'cgi/html'
       extend Html5
-      element_init()
       extend HtmlExtension
     end
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]