Module: URI

Defined in:
opal/stdlib/uri.rb

Overview

backtick_javascript: true frozen_string_literal: true

Class Method Summary collapse

Class Method Details

.decode_www_form(str, enc = undefined, separator: '&', use__charset_: false, isindex: false) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'opal/stdlib/uri.rb', line 5

def self.decode_www_form(str, enc = undefined, separator: '&', use__charset_: false, isindex: false)
  raise ArgumentError, "the input of #{name}.#{__method__} must be ASCII only string" unless str.ascii_only?

  %x{
    var ary = [], key, val;
    if (str.length == 0)
      return ary;
    if (enc)
      #{enc = Encoding.find(enc)};

    var parts = str.split(#{separator});
    for (var i = 0; i < parts.length; i++) {
      var string = parts[i];
      var splitIndex = string.indexOf('=')

      if (splitIndex >= 0) {
        key = string.substr(0, splitIndex);
        val = string.substr(splitIndex + 1);
      } else {
        key = string;
        val = '';
      }

      if (isindex) {
        if (splitIndex < 0) {
          key = '';
          val = string;
        }
        isindex = false;
      }

      key = decodeURIComponent(key.replace(/\+/g, ' '));
      if (val) {
        val = decodeURIComponent(val.replace(/\+/g, ' '));
      } else {
        val = '';
      }

      if (enc) {
        key = #{`key`.force_encoding(enc)}
        val = #{`val`.force_encoding(enc)}
      }

      ary.push([key, val]);
    }

    return ary;
  }
end