cgi 0.3.3-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of cgi might be problematic. Click here for more details.

data/lib/cgi/html.rb ADDED
@@ -0,0 +1,1035 @@
1
+ # frozen_string_literal: true
2
+ class CGI
3
+ # Base module for HTML-generation mixins.
4
+ #
5
+ # Provides methods for code generation for tags following
6
+ # the various DTD element types.
7
+ module TagMaker # :nodoc:
8
+
9
+ # Generate code for an element with required start and end tags.
10
+ #
11
+ # - -
12
+ def nn_element(element, attributes = {})
13
+ s = nOE_element(element, attributes)
14
+ if block_given?
15
+ s << yield.to_s
16
+ end
17
+ s << "</#{element.upcase}>"
18
+ end
19
+
20
+ def nn_element_def(attributes = {}, &block)
21
+ nn_element(__callee__, attributes, &block)
22
+ end
23
+
24
+ # Generate code for an empty element.
25
+ #
26
+ # - O EMPTY
27
+ def nOE_element(element, attributes = {})
28
+ attributes={attributes=>nil} if attributes.kind_of?(String)
29
+ s = "<#{element.upcase}".dup
30
+ attributes.each do|name, value|
31
+ next unless value
32
+ s << " "
33
+ s << CGI.escapeHTML(name.to_s)
34
+ if value != true
35
+ s << '="'
36
+ s << CGI.escapeHTML(value.to_s)
37
+ s << '"'
38
+ end
39
+ end
40
+ s << ">"
41
+ end
42
+
43
+ def nOE_element_def(attributes = {}, &block)
44
+ nOE_element(__callee__, attributes, &block)
45
+ end
46
+
47
+
48
+ # Generate code for an element for which the end (and possibly the
49
+ # start) tag is optional.
50
+ #
51
+ # O O or - O
52
+ def nO_element(element, attributes = {})
53
+ s = nOE_element(element, attributes)
54
+ if block_given?
55
+ s << yield.to_s
56
+ s << "</#{element.upcase}>"
57
+ end
58
+ s
59
+ end
60
+
61
+ def nO_element_def(attributes = {}, &block)
62
+ nO_element(__callee__, attributes, &block)
63
+ end
64
+
65
+ end # TagMaker
66
+
67
+
68
+ # Mixin module providing HTML generation methods.
69
+ #
70
+ # For example,
71
+ # cgi.a("http://www.example.com") { "Example" }
72
+ # # => "<A HREF=\"http://www.example.com\">Example</A>"
73
+ #
74
+ # Modules Html3, Html4, etc., contain more basic HTML-generation methods
75
+ # (+#title+, +#h1+, etc.).
76
+ #
77
+ # See class CGI for a detailed example.
78
+ #
79
+ module HtmlExtension
80
+
81
+
82
+ # Generate an Anchor element as a string.
83
+ #
84
+ # +href+ can either be a string, giving the URL
85
+ # for the HREF attribute, or it can be a hash of
86
+ # the element's attributes.
87
+ #
88
+ # The body of the element is the string returned by the no-argument
89
+ # block passed in.
90
+ #
91
+ # a("http://www.example.com") { "Example" }
92
+ # # => "<A HREF=\"http://www.example.com\">Example</A>"
93
+ #
94
+ # a("HREF" => "http://www.example.com", "TARGET" => "_top") { "Example" }
95
+ # # => "<A HREF=\"http://www.example.com\" TARGET=\"_top\">Example</A>"
96
+ #
97
+ def a(href = "") # :yield:
98
+ attributes = if href.kind_of?(String)
99
+ { "HREF" => href }
100
+ else
101
+ href
102
+ end
103
+ super(attributes)
104
+ end
105
+
106
+ # Generate a Document Base URI element as a String.
107
+ #
108
+ # +href+ can either by a string, giving the base URL for the HREF
109
+ # attribute, or it can be a has of the element's attributes.
110
+ #
111
+ # The passed-in no-argument block is ignored.
112
+ #
113
+ # base("http://www.example.com/cgi")
114
+ # # => "<BASE HREF=\"http://www.example.com/cgi\">"
115
+ def base(href = "") # :yield:
116
+ attributes = if href.kind_of?(String)
117
+ { "HREF" => href }
118
+ else
119
+ href
120
+ end
121
+ super(attributes)
122
+ end
123
+
124
+ # Generate a BlockQuote element as a string.
125
+ #
126
+ # +cite+ can either be a string, give the URI for the source of
127
+ # the quoted text, or a hash, giving all attributes of the element,
128
+ # or it can be omitted, in which case the element has no attributes.
129
+ #
130
+ # The body is provided by the passed-in no-argument block
131
+ #
132
+ # blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }
133
+ # #=> "<BLOCKQUOTE CITE=\"http://www.example.com/quotes/foo.html\">Foo!</BLOCKQUOTE>
134
+ def blockquote(cite = {}) # :yield:
135
+ attributes = if cite.kind_of?(String)
136
+ { "CITE" => cite }
137
+ else
138
+ cite
139
+ end
140
+ super(attributes)
141
+ end
142
+
143
+
144
+ # Generate a Table Caption element as a string.
145
+ #
146
+ # +align+ can be a string, giving the alignment of the caption
147
+ # (one of top, bottom, left, or right). It can be a hash of
148
+ # all the attributes of the element. Or it can be omitted.
149
+ #
150
+ # The body of the element is provided by the passed-in no-argument block.
151
+ #
152
+ # caption("left") { "Capital Cities" }
153
+ # # => <CAPTION ALIGN=\"left\">Capital Cities</CAPTION>
154
+ def caption(align = {}) # :yield:
155
+ attributes = if align.kind_of?(String)
156
+ { "ALIGN" => align }
157
+ else
158
+ align
159
+ end
160
+ super(attributes)
161
+ end
162
+
163
+
164
+ # Generate a Checkbox Input element as a string.
165
+ #
166
+ # The attributes of the element can be specified as three arguments,
167
+ # +name+, +value+, and +checked+. +checked+ is a boolean value;
168
+ # if true, the CHECKED attribute will be included in the element.
169
+ #
170
+ # Alternatively, the attributes can be specified as a hash.
171
+ #
172
+ # checkbox("name")
173
+ # # = checkbox("NAME" => "name")
174
+ #
175
+ # checkbox("name", "value")
176
+ # # = checkbox("NAME" => "name", "VALUE" => "value")
177
+ #
178
+ # checkbox("name", "value", true)
179
+ # # = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
180
+ def checkbox(name = "", value = nil, checked = nil)
181
+ attributes = if name.kind_of?(String)
182
+ { "TYPE" => "checkbox", "NAME" => name,
183
+ "VALUE" => value, "CHECKED" => checked }
184
+ else
185
+ name["TYPE"] = "checkbox"
186
+ name
187
+ end
188
+ input(attributes)
189
+ end
190
+
191
+ # Generate a sequence of checkbox elements, as a String.
192
+ #
193
+ # The checkboxes will all have the same +name+ attribute.
194
+ # Each checkbox is followed by a label.
195
+ # There will be one checkbox for each value. Each value
196
+ # can be specified as a String, which will be used both
197
+ # as the value of the VALUE attribute and as the label
198
+ # for that checkbox. A single-element array has the
199
+ # same effect.
200
+ #
201
+ # Each value can also be specified as a three-element array.
202
+ # The first element is the VALUE attribute; the second is the
203
+ # label; and the third is a boolean specifying whether this
204
+ # checkbox is CHECKED.
205
+ #
206
+ # Each value can also be specified as a two-element
207
+ # array, by omitting either the value element (defaults
208
+ # to the same as the label), or the boolean checked element
209
+ # (defaults to false).
210
+ #
211
+ # checkbox_group("name", "foo", "bar", "baz")
212
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
213
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="bar">bar
214
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
215
+ #
216
+ # checkbox_group("name", ["foo"], ["bar", true], "baz")
217
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
218
+ # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="bar">bar
219
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
220
+ #
221
+ # checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
222
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="1">Foo
223
+ # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="2">Bar
224
+ # # <INPUT TYPE="checkbox" NAME="name" VALUE="Baz">Baz
225
+ #
226
+ # checkbox_group("NAME" => "name",
227
+ # "VALUES" => ["foo", "bar", "baz"])
228
+ #
229
+ # checkbox_group("NAME" => "name",
230
+ # "VALUES" => [["foo"], ["bar", true], "baz"])
231
+ #
232
+ # checkbox_group("NAME" => "name",
233
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
234
+ def checkbox_group(name = "", *values)
235
+ if name.kind_of?(Hash)
236
+ values = name["VALUES"]
237
+ name = name["NAME"]
238
+ end
239
+ values.collect{|value|
240
+ if value.kind_of?(String)
241
+ checkbox(name, value) + value
242
+ else
243
+ if value[-1] == true || value[-1] == false
244
+ checkbox(name, value[0], value[-1]) +
245
+ value[-2]
246
+ else
247
+ checkbox(name, value[0]) +
248
+ value[-1]
249
+ end
250
+ end
251
+ }.join
252
+ end
253
+
254
+
255
+ # Generate an File Upload Input element as a string.
256
+ #
257
+ # The attributes of the element can be specified as three arguments,
258
+ # +name+, +size+, and +maxlength+. +maxlength+ is the maximum length
259
+ # of the file's _name_, not of the file's _contents_.
260
+ #
261
+ # Alternatively, the attributes can be specified as a hash.
262
+ #
263
+ # See #multipart_form() for forms that include file uploads.
264
+ #
265
+ # file_field("name")
266
+ # # <INPUT TYPE="file" NAME="name" SIZE="20">
267
+ #
268
+ # file_field("name", 40)
269
+ # # <INPUT TYPE="file" NAME="name" SIZE="40">
270
+ #
271
+ # file_field("name", 40, 100)
272
+ # # <INPUT TYPE="file" NAME="name" SIZE="40" MAXLENGTH="100">
273
+ #
274
+ # file_field("NAME" => "name", "SIZE" => 40)
275
+ # # <INPUT TYPE="file" NAME="name" SIZE="40">
276
+ def file_field(name = "", size = 20, maxlength = nil)
277
+ attributes = if name.kind_of?(String)
278
+ { "TYPE" => "file", "NAME" => name,
279
+ "SIZE" => size.to_s }
280
+ else
281
+ name["TYPE"] = "file"
282
+ name
283
+ end
284
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
285
+ input(attributes)
286
+ end
287
+
288
+
289
+ # Generate a Form element as a string.
290
+ #
291
+ # +method+ should be either "get" or "post", and defaults to the latter.
292
+ # +action+ defaults to the current CGI script name. +enctype+
293
+ # defaults to "application/x-www-form-urlencoded".
294
+ #
295
+ # Alternatively, the attributes can be specified as a hash.
296
+ #
297
+ # See also #multipart_form() for forms that include file uploads.
298
+ #
299
+ # form{ "string" }
300
+ # # <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
301
+ #
302
+ # form("get") { "string" }
303
+ # # <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
304
+ #
305
+ # form("get", "url") { "string" }
306
+ # # <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
307
+ #
308
+ # form("METHOD" => "post", "ENCTYPE" => "enctype") { "string" }
309
+ # # <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
310
+ def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
311
+ attributes = if method.kind_of?(String)
312
+ { "METHOD" => method, "ACTION" => action,
313
+ "ENCTYPE" => enctype }
314
+ else
315
+ unless method.has_key?("METHOD")
316
+ method["METHOD"] = "post"
317
+ end
318
+ unless method.has_key?("ENCTYPE")
319
+ method["ENCTYPE"] = enctype
320
+ end
321
+ method
322
+ end
323
+ if block_given?
324
+ body = yield
325
+ else
326
+ body = ""
327
+ end
328
+ if @output_hidden
329
+ body << @output_hidden.collect{|k,v|
330
+ "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
331
+ }.join
332
+ end
333
+ super(attributes){body}
334
+ end
335
+
336
+ # Generate a Hidden Input element as a string.
337
+ #
338
+ # The attributes of the element can be specified as two arguments,
339
+ # +name+ and +value+.
340
+ #
341
+ # Alternatively, the attributes can be specified as a hash.
342
+ #
343
+ # hidden("name")
344
+ # # <INPUT TYPE="hidden" NAME="name">
345
+ #
346
+ # hidden("name", "value")
347
+ # # <INPUT TYPE="hidden" NAME="name" VALUE="value">
348
+ #
349
+ # hidden("NAME" => "name", "VALUE" => "reset", "ID" => "foo")
350
+ # # <INPUT TYPE="hidden" NAME="name" VALUE="value" ID="foo">
351
+ def hidden(name = "", value = nil)
352
+ attributes = if name.kind_of?(String)
353
+ { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
354
+ else
355
+ name["TYPE"] = "hidden"
356
+ name
357
+ end
358
+ input(attributes)
359
+ end
360
+
361
+ # Generate a top-level HTML element as a string.
362
+ #
363
+ # The attributes of the element are specified as a hash. The
364
+ # pseudo-attribute "PRETTY" can be used to specify that the generated
365
+ # HTML string should be indented. "PRETTY" can also be specified as
366
+ # a string as the sole argument to this method. The pseudo-attribute
367
+ # "DOCTYPE", if given, is used as the leading DOCTYPE SGML tag; it
368
+ # should include the entire text of this tag, including angle brackets.
369
+ #
370
+ # The body of the html element is supplied as a block.
371
+ #
372
+ # html{ "string" }
373
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>string</HTML>
374
+ #
375
+ # html("LANG" => "ja") { "string" }
376
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML LANG="ja">string</HTML>
377
+ #
378
+ # html("DOCTYPE" => false) { "string" }
379
+ # # <HTML>string</HTML>
380
+ #
381
+ # html("DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">') { "string" }
382
+ # # <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>string</HTML>
383
+ #
384
+ # html("PRETTY" => " ") { "<BODY></BODY>" }
385
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
386
+ # # <HTML>
387
+ # # <BODY>
388
+ # # </BODY>
389
+ # # </HTML>
390
+ #
391
+ # html("PRETTY" => "\t") { "<BODY></BODY>" }
392
+ # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
393
+ # # <HTML>
394
+ # # <BODY>
395
+ # # </BODY>
396
+ # # </HTML>
397
+ #
398
+ # html("PRETTY") { "<BODY></BODY>" }
399
+ # # = html("PRETTY" => " ") { "<BODY></BODY>" }
400
+ #
401
+ # html(if $VERBOSE then "PRETTY" end) { "HTML string" }
402
+ #
403
+ def html(attributes = {}) # :yield:
404
+ if nil == attributes
405
+ attributes = {}
406
+ elsif "PRETTY" == attributes
407
+ attributes = { "PRETTY" => true }
408
+ end
409
+ pretty = attributes.delete("PRETTY")
410
+ pretty = " " if true == pretty
411
+ buf = "".dup
412
+
413
+ if attributes.has_key?("DOCTYPE")
414
+ if attributes["DOCTYPE"]
415
+ buf << attributes.delete("DOCTYPE")
416
+ else
417
+ attributes.delete("DOCTYPE")
418
+ end
419
+ else
420
+ buf << doctype
421
+ end
422
+
423
+ buf << super(attributes)
424
+
425
+ if pretty
426
+ CGI.pretty(buf, pretty)
427
+ else
428
+ buf
429
+ end
430
+
431
+ end
432
+
433
+ # Generate an Image Button Input element as a string.
434
+ #
435
+ # +src+ is the URL of the image to use for the button. +name+
436
+ # is the input name. +alt+ is the alternative text for the image.
437
+ #
438
+ # Alternatively, the attributes can be specified as a hash.
439
+ #
440
+ # image_button("url")
441
+ # # <INPUT TYPE="image" SRC="url">
442
+ #
443
+ # image_button("url", "name", "string")
444
+ # # <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
445
+ #
446
+ # image_button("SRC" => "url", "ALT" => "string")
447
+ # # <INPUT TYPE="image" SRC="url" ALT="string">
448
+ def image_button(src = "", name = nil, alt = nil)
449
+ attributes = if src.kind_of?(String)
450
+ { "TYPE" => "image", "SRC" => src, "NAME" => name,
451
+ "ALT" => alt }
452
+ else
453
+ src["TYPE"] = "image"
454
+ src["SRC"] ||= ""
455
+ src
456
+ end
457
+ input(attributes)
458
+ end
459
+
460
+
461
+ # Generate an Image element as a string.
462
+ #
463
+ # +src+ is the URL of the image. +alt+ is the alternative text for
464
+ # the image. +width+ is the width of the image, and +height+ is
465
+ # its height.
466
+ #
467
+ # Alternatively, the attributes can be specified as a hash.
468
+ #
469
+ # img("src", "alt", 100, 50)
470
+ # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
471
+ #
472
+ # img("SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50)
473
+ # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
474
+ def img(src = "", alt = "", width = nil, height = nil)
475
+ attributes = if src.kind_of?(String)
476
+ { "SRC" => src, "ALT" => alt }
477
+ else
478
+ src
479
+ end
480
+ attributes["WIDTH"] = width.to_s if width
481
+ attributes["HEIGHT"] = height.to_s if height
482
+ super(attributes)
483
+ end
484
+
485
+
486
+ # Generate a Form element with multipart encoding as a String.
487
+ #
488
+ # Multipart encoding is used for forms that include file uploads.
489
+ #
490
+ # +action+ is the action to perform. +enctype+ is the encoding
491
+ # type, which defaults to "multipart/form-data".
492
+ #
493
+ # Alternatively, the attributes can be specified as a hash.
494
+ #
495
+ # multipart_form{ "string" }
496
+ # # <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM>
497
+ #
498
+ # multipart_form("url") { "string" }
499
+ # # <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
500
+ def multipart_form(action = nil, enctype = "multipart/form-data")
501
+ attributes = if action == nil
502
+ { "METHOD" => "post", "ENCTYPE" => enctype }
503
+ elsif action.kind_of?(String)
504
+ { "METHOD" => "post", "ACTION" => action,
505
+ "ENCTYPE" => enctype }
506
+ else
507
+ unless action.has_key?("METHOD")
508
+ action["METHOD"] = "post"
509
+ end
510
+ unless action.has_key?("ENCTYPE")
511
+ action["ENCTYPE"] = enctype
512
+ end
513
+ action
514
+ end
515
+ if block_given?
516
+ form(attributes){ yield }
517
+ else
518
+ form(attributes)
519
+ end
520
+ end
521
+
522
+
523
+ # Generate a Password Input element as a string.
524
+ #
525
+ # +name+ is the name of the input field. +value+ is its default
526
+ # value. +size+ is the size of the input field display. +maxlength+
527
+ # is the maximum length of the inputted password.
528
+ #
529
+ # Alternatively, attributes can be specified as a hash.
530
+ #
531
+ # password_field("name")
532
+ # # <INPUT TYPE="password" NAME="name" SIZE="40">
533
+ #
534
+ # password_field("name", "value")
535
+ # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40">
536
+ #
537
+ # password_field("password", "value", 80, 200)
538
+ # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
539
+ #
540
+ # password_field("NAME" => "name", "VALUE" => "value")
541
+ # # <INPUT TYPE="password" NAME="name" VALUE="value">
542
+ def password_field(name = "", value = nil, size = 40, maxlength = nil)
543
+ attributes = if name.kind_of?(String)
544
+ { "TYPE" => "password", "NAME" => name,
545
+ "VALUE" => value, "SIZE" => size.to_s }
546
+ else
547
+ name["TYPE"] = "password"
548
+ name
549
+ end
550
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
551
+ input(attributes)
552
+ end
553
+
554
+ # Generate a Select element as a string.
555
+ #
556
+ # +name+ is the name of the element. The +values+ are the options that
557
+ # can be selected from the Select menu. Each value can be a String or
558
+ # a one, two, or three-element Array. If a String or a one-element
559
+ # Array, this is both the value of that option and the text displayed for
560
+ # it. If a three-element Array, the elements are the option value, displayed
561
+ # text, and a boolean value specifying whether this option starts as selected.
562
+ # The two-element version omits either the option value (defaults to the same
563
+ # as the display text) or the boolean selected specifier (defaults to false).
564
+ #
565
+ # The attributes and options can also be specified as a hash. In this
566
+ # case, options are specified as an array of values as described above,
567
+ # with the hash key of "VALUES".
568
+ #
569
+ # popup_menu("name", "foo", "bar", "baz")
570
+ # # <SELECT NAME="name">
571
+ # # <OPTION VALUE="foo">foo</OPTION>
572
+ # # <OPTION VALUE="bar">bar</OPTION>
573
+ # # <OPTION VALUE="baz">baz</OPTION>
574
+ # # </SELECT>
575
+ #
576
+ # popup_menu("name", ["foo"], ["bar", true], "baz")
577
+ # # <SELECT NAME="name">
578
+ # # <OPTION VALUE="foo">foo</OPTION>
579
+ # # <OPTION VALUE="bar" SELECTED>bar</OPTION>
580
+ # # <OPTION VALUE="baz">baz</OPTION>
581
+ # # </SELECT>
582
+ #
583
+ # popup_menu("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
584
+ # # <SELECT NAME="name">
585
+ # # <OPTION VALUE="1">Foo</OPTION>
586
+ # # <OPTION SELECTED VALUE="2">Bar</OPTION>
587
+ # # <OPTION VALUE="Baz">Baz</OPTION>
588
+ # # </SELECT>
589
+ #
590
+ # popup_menu("NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
591
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
592
+ # # <SELECT NAME="name" MULTIPLE SIZE="2">
593
+ # # <OPTION VALUE="1">Foo</OPTION>
594
+ # # <OPTION SELECTED VALUE="2">Bar</OPTION>
595
+ # # <OPTION VALUE="Baz">Baz</OPTION>
596
+ # # </SELECT>
597
+ def popup_menu(name = "", *values)
598
+
599
+ if name.kind_of?(Hash)
600
+ values = name["VALUES"]
601
+ size = name["SIZE"].to_s if name["SIZE"]
602
+ multiple = name["MULTIPLE"]
603
+ name = name["NAME"]
604
+ else
605
+ size = nil
606
+ multiple = nil
607
+ end
608
+
609
+ select({ "NAME" => name, "SIZE" => size,
610
+ "MULTIPLE" => multiple }){
611
+ values.collect{|value|
612
+ if value.kind_of?(String)
613
+ option({ "VALUE" => value }){ value }
614
+ else
615
+ if value[value.size - 1] == true
616
+ option({ "VALUE" => value[0], "SELECTED" => true }){
617
+ value[value.size - 2]
618
+ }
619
+ else
620
+ option({ "VALUE" => value[0] }){
621
+ value[value.size - 1]
622
+ }
623
+ end
624
+ end
625
+ }.join
626
+ }
627
+
628
+ end
629
+
630
+ # Generates a radio-button Input element.
631
+ #
632
+ # +name+ is the name of the input field. +value+ is the value of
633
+ # the field if checked. +checked+ specifies whether the field
634
+ # starts off checked.
635
+ #
636
+ # Alternatively, the attributes can be specified as a hash.
637
+ #
638
+ # radio_button("name", "value")
639
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value">
640
+ #
641
+ # radio_button("name", "value", true)
642
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
643
+ #
644
+ # radio_button("NAME" => "name", "VALUE" => "value", "ID" => "foo")
645
+ # # <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
646
+ def radio_button(name = "", value = nil, checked = nil)
647
+ attributes = if name.kind_of?(String)
648
+ { "TYPE" => "radio", "NAME" => name,
649
+ "VALUE" => value, "CHECKED" => checked }
650
+ else
651
+ name["TYPE"] = "radio"
652
+ name
653
+ end
654
+ input(attributes)
655
+ end
656
+
657
+ # Generate a sequence of radio button Input elements, as a String.
658
+ #
659
+ # This works the same as #checkbox_group(). However, it is not valid
660
+ # to have more than one radiobutton in a group checked.
661
+ #
662
+ # radio_group("name", "foo", "bar", "baz")
663
+ # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
664
+ # # <INPUT TYPE="radio" NAME="name" VALUE="bar">bar
665
+ # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
666
+ #
667
+ # radio_group("name", ["foo"], ["bar", true], "baz")
668
+ # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
669
+ # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="bar">bar
670
+ # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
671
+ #
672
+ # radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
673
+ # # <INPUT TYPE="radio" NAME="name" VALUE="1">Foo
674
+ # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="2">Bar
675
+ # # <INPUT TYPE="radio" NAME="name" VALUE="Baz">Baz
676
+ #
677
+ # radio_group("NAME" => "name",
678
+ # "VALUES" => ["foo", "bar", "baz"])
679
+ #
680
+ # radio_group("NAME" => "name",
681
+ # "VALUES" => [["foo"], ["bar", true], "baz"])
682
+ #
683
+ # radio_group("NAME" => "name",
684
+ # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
685
+ def radio_group(name = "", *values)
686
+ if name.kind_of?(Hash)
687
+ values = name["VALUES"]
688
+ name = name["NAME"]
689
+ end
690
+ values.collect{|value|
691
+ if value.kind_of?(String)
692
+ radio_button(name, value) + value
693
+ else
694
+ if value[-1] == true || value[-1] == false
695
+ radio_button(name, value[0], value[-1]) +
696
+ value[-2]
697
+ else
698
+ radio_button(name, value[0]) +
699
+ value[-1]
700
+ end
701
+ end
702
+ }.join
703
+ end
704
+
705
+ # Generate a reset button Input element, as a String.
706
+ #
707
+ # This resets the values on a form to their initial values. +value+
708
+ # is the text displayed on the button. +name+ is the name of this button.
709
+ #
710
+ # Alternatively, the attributes can be specified as a hash.
711
+ #
712
+ # reset
713
+ # # <INPUT TYPE="reset">
714
+ #
715
+ # reset("reset")
716
+ # # <INPUT TYPE="reset" VALUE="reset">
717
+ #
718
+ # reset("VALUE" => "reset", "ID" => "foo")
719
+ # # <INPUT TYPE="reset" VALUE="reset" ID="foo">
720
+ def reset(value = nil, name = nil)
721
+ attributes = if (not value) or value.kind_of?(String)
722
+ { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
723
+ else
724
+ value["TYPE"] = "reset"
725
+ value
726
+ end
727
+ input(attributes)
728
+ end
729
+
730
+ alias scrolling_list popup_menu
731
+
732
+ # Generate a submit button Input element, as a String.
733
+ #
734
+ # +value+ is the text to display on the button. +name+ is the name
735
+ # of the input.
736
+ #
737
+ # Alternatively, the attributes can be specified as a hash.
738
+ #
739
+ # submit
740
+ # # <INPUT TYPE="submit">
741
+ #
742
+ # submit("ok")
743
+ # # <INPUT TYPE="submit" VALUE="ok">
744
+ #
745
+ # submit("ok", "button1")
746
+ # # <INPUT TYPE="submit" VALUE="ok" NAME="button1">
747
+ #
748
+ # submit("VALUE" => "ok", "NAME" => "button1", "ID" => "foo")
749
+ # # <INPUT TYPE="submit" VALUE="ok" NAME="button1" ID="foo">
750
+ def submit(value = nil, name = nil)
751
+ attributes = if (not value) or value.kind_of?(String)
752
+ { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
753
+ else
754
+ value["TYPE"] = "submit"
755
+ value
756
+ end
757
+ input(attributes)
758
+ end
759
+
760
+ # Generate a text field Input element, as a String.
761
+ #
762
+ # +name+ is the name of the input field. +value+ is its initial
763
+ # value. +size+ is the size of the input area. +maxlength+
764
+ # is the maximum length of input accepted.
765
+ #
766
+ # Alternatively, the attributes can be specified as a hash.
767
+ #
768
+ # text_field("name")
769
+ # # <INPUT TYPE="text" NAME="name" SIZE="40">
770
+ #
771
+ # text_field("name", "value")
772
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">
773
+ #
774
+ # text_field("name", "value", 80)
775
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">
776
+ #
777
+ # text_field("name", "value", 80, 200)
778
+ # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
779
+ #
780
+ # text_field("NAME" => "name", "VALUE" => "value")
781
+ # # <INPUT TYPE="text" NAME="name" VALUE="value">
782
+ def text_field(name = "", value = nil, size = 40, maxlength = nil)
783
+ attributes = if name.kind_of?(String)
784
+ { "TYPE" => "text", "NAME" => name, "VALUE" => value,
785
+ "SIZE" => size.to_s }
786
+ else
787
+ name["TYPE"] = "text"
788
+ name
789
+ end
790
+ attributes["MAXLENGTH"] = maxlength.to_s if maxlength
791
+ input(attributes)
792
+ end
793
+
794
+ # Generate a TextArea element, as a String.
795
+ #
796
+ # +name+ is the name of the textarea. +cols+ is the number of
797
+ # columns and +rows+ is the number of rows in the display.
798
+ #
799
+ # Alternatively, the attributes can be specified as a hash.
800
+ #
801
+ # The body is provided by the passed-in no-argument block
802
+ #
803
+ # textarea("name")
804
+ # # = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10)
805
+ #
806
+ # textarea("name", 40, 5)
807
+ # # = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
808
+ def textarea(name = "", cols = 70, rows = 10) # :yield:
809
+ attributes = if name.kind_of?(String)
810
+ { "NAME" => name, "COLS" => cols.to_s,
811
+ "ROWS" => rows.to_s }
812
+ else
813
+ name
814
+ end
815
+ super(attributes)
816
+ end
817
+
818
+ end # HtmlExtension
819
+
820
+
821
+ # Mixin module for HTML version 3 generation methods.
822
+ module Html3 # :nodoc:
823
+ include TagMaker
824
+
825
+ # The DOCTYPE declaration for this version of HTML
826
+ def doctype
827
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">|
828
+ end
829
+
830
+ instance_method(:nn_element_def).tap do |m|
831
+ # - -
832
+ for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG
833
+ DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV CENTER MAP
834
+ APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT TABLE TITLE
835
+ STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE
836
+ CAPTION ]
837
+ define_method(element.downcase, m)
838
+ end
839
+ end
840
+
841
+ instance_method(:nOE_element_def).tap do |m|
842
+ # - O EMPTY
843
+ for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
844
+ ISINDEX META ]
845
+ define_method(element.downcase, m)
846
+ end
847
+ end
848
+
849
+ instance_method(:nO_element_def).tap do |m|
850
+ # O O or - O
851
+ for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION TR
852
+ TH TD ]
853
+ define_method(element.downcase, m)
854
+ end
855
+ end
856
+
857
+ end # Html3
858
+
859
+
860
+ # Mixin module for HTML version 4 generation methods.
861
+ module Html4 # :nodoc:
862
+ include TagMaker
863
+
864
+ # The DOCTYPE declaration for this version of HTML
865
+ def doctype
866
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|
867
+ end
868
+
869
+ # Initialize the HTML generation methods for this version.
870
+ # - -
871
+ instance_method(:nn_element_def).tap do |m|
872
+ for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD
873
+ VAR CITE ABBR ACRONYM SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT
874
+ H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP
875
+ FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
876
+ TEXTAREA FORM A BLOCKQUOTE CAPTION ]
877
+ define_method(element.downcase, m)
878
+ end
879
+ end
880
+
881
+ # - O EMPTY
882
+ instance_method(:nOE_element_def).tap do |m|
883
+ for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ]
884
+ define_method(element.downcase, m)
885
+ end
886
+ end
887
+
888
+ # O O or - O
889
+ instance_method(:nO_element_def).tap do |m|
890
+ for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
891
+ COLGROUP TR TH TD HEAD ]
892
+ define_method(element.downcase, m)
893
+ end
894
+ end
895
+
896
+ end # Html4
897
+
898
+
899
+ # Mixin module for HTML version 4 transitional generation methods.
900
+ module Html4Tr # :nodoc:
901
+ include TagMaker
902
+
903
+ # The DOCTYPE declaration for this version of HTML
904
+ def doctype
905
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|
906
+ end
907
+
908
+ # Initialise the HTML generation methods for this version.
909
+ # - -
910
+ instance_method(:nn_element_def).tap do |m|
911
+ for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN
912
+ CODE SAMP KBD VAR CITE ABBR ACRONYM FONT SUB SUP SPAN BDO
913
+ ADDRESS DIV CENTER MAP OBJECT APPLET H1 H2 H3 H4 H5 H6 PRE Q
914
+ INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET
915
+ LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT
916
+ NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ]
917
+ define_method(element.downcase, m)
918
+ end
919
+ end
920
+
921
+ # - O EMPTY
922
+ instance_method(:nOE_element_def).tap do |m|
923
+ for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
924
+ COL ISINDEX META ]
925
+ define_method(element.downcase, m)
926
+ end
927
+ end
928
+
929
+ # O O or - O
930
+ instance_method(:nO_element_def).tap do |m|
931
+ for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
932
+ COLGROUP TR TH TD HEAD ]
933
+ define_method(element.downcase, m)
934
+ end
935
+ end
936
+
937
+ end # Html4Tr
938
+
939
+
940
+ # Mixin module for generating HTML version 4 with framesets.
941
+ module Html4Fr # :nodoc:
942
+ include TagMaker
943
+
944
+ # The DOCTYPE declaration for this version of HTML
945
+ def doctype
946
+ %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|
947
+ end
948
+
949
+ # Initialise the HTML generation methods for this version.
950
+ # - -
951
+ instance_method(:nn_element_def).tap do |m|
952
+ for element in %w[ FRAMESET ]
953
+ define_method(element.downcase, m)
954
+ end
955
+ end
956
+
957
+ # - O EMPTY
958
+ instance_method(:nOE_element_def).tap do |m|
959
+ for element in %w[ FRAME ]
960
+ define_method(element.downcase, m)
961
+ end
962
+ end
963
+
964
+ end # Html4Fr
965
+
966
+
967
+ # Mixin module for HTML version 5 generation methods.
968
+ module Html5 # :nodoc:
969
+ include TagMaker
970
+
971
+ # The DOCTYPE declaration for this version of HTML
972
+ def doctype
973
+ %|<!DOCTYPE HTML>|
974
+ end
975
+
976
+ # Initialise the HTML generation methods for this version.
977
+ # - -
978
+ instance_method(:nn_element_def).tap do |m|
979
+ for element in %w[ SECTION NAV ARTICLE ASIDE HGROUP HEADER
980
+ FOOTER FIGURE FIGCAPTION S TIME U MARK RUBY BDI IFRAME
981
+ VIDEO AUDIO CANVAS DATALIST OUTPUT PROGRESS METER DETAILS
982
+ SUMMARY MENU DIALOG I B SMALL EM STRONG DFN CODE SAMP KBD
983
+ VAR CITE ABBR SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT
984
+ H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT
985
+ FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
986
+ TEXTAREA FORM A BLOCKQUOTE CAPTION ]
987
+ define_method(element.downcase, m)
988
+ end
989
+ end
990
+
991
+ # - O EMPTY
992
+ instance_method(:nOE_element_def).tap do |m|
993
+ for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META
994
+ COMMAND EMBED KEYGEN SOURCE TRACK WBR ]
995
+ define_method(element.downcase, m)
996
+ end
997
+ end
998
+
999
+ # O O or - O
1000
+ instance_method(:nO_element_def).tap do |m|
1001
+ for element in %w[ HTML HEAD BODY P DT DD LI OPTION THEAD TFOOT TBODY
1002
+ OPTGROUP COLGROUP RT RP TR TH TD ]
1003
+ define_method(element.downcase, m)
1004
+ end
1005
+ end
1006
+
1007
+ end # Html5
1008
+
1009
+ class HTML3
1010
+ include Html3
1011
+ include HtmlExtension
1012
+ end
1013
+
1014
+ class HTML4
1015
+ include Html4
1016
+ include HtmlExtension
1017
+ end
1018
+
1019
+ class HTML4Tr
1020
+ include Html4Tr
1021
+ include HtmlExtension
1022
+ end
1023
+
1024
+ class HTML4Fr
1025
+ include Html4Tr
1026
+ include Html4Fr
1027
+ include HtmlExtension
1028
+ end
1029
+
1030
+ class HTML5
1031
+ include Html5
1032
+ include HtmlExtension
1033
+ end
1034
+
1035
+ end