@@ -314,17 +314,41 @@ def self.regexp(schemes = nil)
314
314
TBLDECWWWCOMP_ [ '+' ] = ' '
315
315
TBLDECWWWCOMP_ . freeze
316
316
317
- # Encodes given +str+ to URL-encoded form data .
317
+ # Returns a URL-encoded string derived from the given string +str+ .
318
318
#
319
- # This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP
320
- # (ASCII space) to + and converts others to %XX.
319
+ # The returned string:
321
320
#
322
- # If +enc+ is given, convert +str+ to the encoding before percent encoding.
321
+ # - Preserves:
323
322
#
324
- # This is an implementation of
325
- # https://www.w3.org/TR/2013/CR-html5-20130806/forms.html#url-encoded-form-data.
323
+ # - Characters <tt>'*'</tt>, <tt>'.'</tt>, <tt>'-'</tt>, and <tt>'_'</tt>.
324
+ # - Character in ranges <tt>'a'..'z'</tt>, <tt>'A'..'Z'</tt>,
325
+ # and <tt>'0'..'9'</tt>.
326
+ #
327
+ # Example:
328
+ #
329
+ # URI.encode_www_form_component('*.-_azAZ09')
330
+ # # => "*.-_azAZ09"
331
+ #
332
+ # - Converts:
333
+ #
334
+ # - Character <tt>' '</tt> to character <tt>'+'</tt>.
335
+ # - Any other character to "percent notation";
336
+ # the percent notation for character <i>c</i> is <tt>'%%%X' % c.ord</tt>.
337
+ #
338
+ # Example:
339
+ #
340
+ # URI.encode_www_form_component('Here are some punctuation characters: ,;?:')
341
+ # # => "Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A"
342
+ #
343
+ # Encoding:
344
+ #
345
+ # - If +str+ has encoding Encoding::ASCII_8BIT, argument +enc+ is ignored.
346
+ # - Otherwise +str+ is converted first to Encoding::UTF_8
347
+ # (with suitable character replacements),
348
+ # and then to encoding +enc+.
349
+ #
350
+ # In either case, the returned string has forced encoding Encoding::US_ASCII.
326
351
#
327
- # See URI.decode_www_form_component, URI.encode_www_form.
328
352
def self . encode_www_form_component ( str , enc = nil )
329
353
_encode_uri_component ( /[^*\- .0-9A-Z_a-z]/ , TBLENCWWWCOMP_ , str , enc )
330
354
end
@@ -372,33 +396,98 @@ def self._decode_uri_component(regexp, str, enc)
372
396
end
373
397
private_class_method :_decode_uri_component
374
398
375
- # Generates URL-encoded form data from given +enum+.
399
+ # Returns a URL-encoded string derived from the given
400
+ # {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
401
+ # +enum+.
376
402
#
377
- # This generates application/x-www-form-urlencoded data defined in HTML5
378
- # from given an Enumerable object.
403
+ # The result is suitable for use as form data
404
+ # for an \HTTP request whose <tt>Content-Type</tt> is
405
+ # <tt>'application/x-www-form-urlencoded'</tt>.
379
406
#
380
- # This internally uses URI.encode_www_form_component(str).
407
+ # The returned string consists of the elements of +enum+,
408
+ # each converted to one or more URL-encoded strings,
409
+ # and all joined with character <tt>'&'</tt>.
381
410
#
382
- # This method doesn't convert the encoding of given items, so convert them
383
- # before calling this method if you want to send data as other than original
384
- # encoding or mixed encoding data. (Strings which are encoded in an HTML5
385
- # ASCII incompatible encoding are converted to UTF-8.)
411
+ # Simple examples:
386
412
#
387
- # This method doesn't handle files. When you send a file, use
388
- # multipart/form-data.
413
+ # URI.encode_www_form([['foo', 0], ['bar', 1], ['baz', 2]])
414
+ # # => "foo=0&bar=1&baz=2"
415
+ # URI.encode_www_form({foo: 0, bar: 1, baz: 2})
416
+ # # => "foo=0&bar=1&baz=2"
389
417
#
390
- # This refers https://url.spec.whatwg.org/#concept-urlencoded-serializer
418
+ # When +enum+ is Array-like, each element +ele+ is converted to a field:
391
419
#
392
- # URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
393
- # #=> "q=ruby&lang=en"
394
- # URI.encode_www_form("q" => "ruby", "lang" => "en")
395
- # #=> "q=ruby&lang=en"
396
- # URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
397
- # #=> "q=ruby&q=perl&lang=en"
398
- # URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
399
- # #=> "q=ruby&q=perl&lang=en"
420
+ # - If +ele+ is an array of two or more elements,
421
+ # the field is formed from its first two elements
422
+ # (and any additional elements are ignored):
423
+ #
424
+ # name = URI.encode_www_form_component(ele[0], enc)
425
+ # value = URI.encode_www_form_component(ele[1], enc)
426
+ # "#{name}=#{value}"
427
+ #
428
+ # Examples:
429
+ #
430
+ # URI.encode_www_form([%w[foo bar], %w[baz bat bah]])
431
+ # # => "foo=bar&baz=bat"
432
+ # URI.encode_www_form([['foo', 0], ['bar', :baz, 'bat']])
433
+ # # => "foo=0&bar=baz"
434
+ #
435
+ # - If +ele+ is an array of one element,
436
+ # the field is formed from <tt>ele[0]</tt>:
437
+ #
438
+ # URI.encode_www_form_component(ele[0])
439
+ #
440
+ # Example:
441
+ #
442
+ # URI.encode_www_form([['foo'], [:bar], [0]])
443
+ # # => "foo&bar&0"
444
+ #
445
+ # - Otherwise the field is formed from +ele+:
446
+ #
447
+ # URI.encode_www_form_component(ele)
448
+ #
449
+ # Example:
450
+ #
451
+ # URI.encode_www_form(['foo', :bar, 0])
452
+ # # => "foo&bar&0"
453
+ #
454
+ # The elements of an Array-like +enum+ may be mixture:
455
+ #
456
+ # URI.encode_www_form([['foo', 0], ['bar', 1, 2], ['baz'], :bat])
457
+ # # => "foo=0&bar=1&baz&bat"
458
+ #
459
+ # When +enum+ is Hash-like,
460
+ # each +key+/+value+ pair is converted to one or more fields:
461
+ #
462
+ # - If +value+ is
463
+ # {Array-convertible}[https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html#label-Array-Convertible+Objects],
464
+ # each element +ele+ in +value+ is paired with +key+ to form a field:
465
+ #
466
+ # name = URI.encode_www_form_component(key, enc)
467
+ # value = URI.encode_www_form_component(ele, enc)
468
+ # "#{name}=#{value}"
469
+ #
470
+ # Example:
471
+ #
472
+ # URI.encode_www_form({foo: [:bar, 1], baz: [:bat, :bam, 2]})
473
+ # # => "foo=bar&foo=1&baz=bat&baz=bam&baz=2"
474
+ #
475
+ # - Otherwise, +key+ and +value+ are paired to form a field:
476
+ #
477
+ # name = URI.encode_www_form_component(key, enc)
478
+ # value = URI.encode_www_form_component(value, enc)
479
+ # "#{name}=#{value}"
480
+ #
481
+ # Example:
482
+ #
483
+ # URI.encode_www_form({foo: 0, bar: 1, baz: 2})
484
+ # # => "foo=0&bar=1&baz=2"
485
+ #
486
+ # The elements of a Hash-like +enum+ may be mixture:
487
+ #
488
+ # URI.encode_www_form({foo: [0, 1], bar: 2})
489
+ # # => "foo=0&foo=1&bar=2"
400
490
#
401
- # See URI.encode_www_form_component, URI.decode_www_form.
402
491
def self . encode_www_form ( enum , enc = nil )
403
492
enum . map do |k , v |
404
493
if v . nil?
0 commit comments