Module: URI::WhatwgParser::Generic

Defined in:
lib/uri/whatwg_parser/generic.rb

Instance Method Summary collapse

Instance Method Details

#check_opaque(v) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/uri/whatwg_parser/generic.rb', line 158

def check_opaque(v)
  return super unless @parsed_by_whatwg_parser

  return v unless v

  if @host || @port || @user
    raise InvalidURIError, "cannot set opaque with host, port, or userinfo"
  end

  self.set_opaque(v)
  # NOTE: WHATWG URL Living Standard doesn't define "opaque" setter. So parse a URL whole.
  parser.parse(to_s)
  true
end

#fragment=(v) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/uri/whatwg_parser/generic.rb', line 135

def fragment=(v)
  return super unless @parsed_by_whatwg_parser

  if v.nil? || v.empty?
    @fragment = nil
    return
  end

  v = v.start_with?("#") ? v[1..-1] : v
  @fragment = +""

  parse_result = parser.split(v, url: self, state_override: :fragment_state)
  @fragment = parse_result[8].to_s
end

#host=(v) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/uri/whatwg_parser/generic.rb', line 77

def host=(v)
  return super unless @parsed_by_whatwg_parser
  return if v.nil?

  if @opaque
    raise InvalidURIError, "cannot set host with opaque"
  end

  parse_result = parser.split(v.to_s, url: self, state_override: :host_state)
  set_host(parse_result[2])
  set_port(parse_result[3])
end

#initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false) ⇒ Object



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
# File 'lib/uri/whatwg_parser/generic.rb', line 6

def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false)
  @parsed_by_whatwg_parser = parser.is_a?(URI::WhatwgParser)
  unless parser.is_a?(URI::WhatwgParser)
    return super(scheme, userinfo, host, port, registry, path, opaque, query, fragment)
  end

  @scheme = nil
  @user = nil
  @password = nil
  @host = nil
  @port = nil
  @path = nil
  @raw_path = nil
  @query = nil
  @opaque = nil
  @fragment = nil
  @parser = parser == DEFAULT_PARSER ? nil : parser

  self.set_scheme(scheme)
  self.set_host(host)
  self.set_port(port)
  self.set_userinfo(userinfo)
  self.set_path(path)
  self.query = query
  self.set_opaque(opaque)
  @fragment = fragment
  @raw_path = parser&.path

  self.set_path("") if !@path && !@opaque
  parser.parse(to_s) if arg_check

  @scheme&.freeze
  self.set_port(self.default_port) if self.default_port && !@port
end

#merge(oth) ⇒ Object Also known as: +



41
42
43
44
45
# File 'lib/uri/whatwg_parser/generic.rb', line 41

def merge(oth)
  return super unless @parsed_by_whatwg_parser

  parser.join(self.to_s, oth.to_s)
end

#password=(v) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/uri/whatwg_parser/generic.rb', line 67

def password=(v)
  return super unless @parsed_by_whatwg_parser
  return v unless v

  if host.nil? || host.empty? || scheme == "file"
    raise InvalidURIError, "cannot set password when host is nil or file schme"
  end
  set_password(parser.utf8_percent_encode_string(v, URI::WhatwgParser::USERINFO_PERCENT_ENCODE_SET))
end

#path=(v) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/uri/whatwg_parser/generic.rb', line 107

def path=(v)
  return super unless @parsed_by_whatwg_parser
  return if v.nil?

  if @opaque
    raise InvalidURIError, "path conflicts with opaque"
  end

  parse_result = parser.split(v.to_s, url: self, state_override: :path_start_state)
  @raw_path = parser.path
  set_path(parse_result[5])
end

#port=(v) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/uri/whatwg_parser/generic.rb', line 90

def port=(v)
  return super unless @parsed_by_whatwg_parser
  return if v.nil?

  if v.to_s.empty?
    set_port(nil)
    return
  end

  if host.nil? || host.empty? || scheme == "file"
    raise InvalidURIError, "cannot set port when host is nil or scheme is file"
  end

  parse_result = parser.split("#{v}:", url: self, state_override: :port_state)
  set_port(parse_result[3])
end

#query=(v) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/uri/whatwg_parser/generic.rb', line 120

def query=(v)
  return super unless @parsed_by_whatwg_parser

  if v.nil? || v.empty?
    @query = nil
    return
  end

  v = v.start_with?("?") ? v[1..-1] : v
  @query = +""

  parse_result = parser.split(v, url: self, state_override: :query_state)
  @query = parse_result[7].to_s
end

#scheme=(v) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/uri/whatwg_parser/generic.rb', line 48

def scheme=(v)
  return super unless @parsed_by_whatwg_parser
  return if v.nil? || v.empty?

  parse_result = parser.split("#{v}:", url: self, state_override: :scheme_start_state)
  set_scheme(parse_result[0])
  set_port(parse_result[3])
end

#to_sObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/uri/whatwg_parser/generic.rb', line 173

def to_s
  return super unless @parsed_by_whatwg_parser

  str = "".dup
  if @scheme
    str << @scheme
    str << ":"
  end

  if @host || %w[file postgres].include?(@scheme)
    str << "//"
  end
  if self.userinfo
    str << self.userinfo
    str << "@"
  end
  if @host
    str << @host
  end
  if @port && @port != self.default_port
    str << ":"
    str << @port.to_s
  end
  if @host.nil? && @opaque.nil? && @raw_path && @raw_path.length > 1 && @raw_path[0] == ""
    str << "/."
  end
  str << @path if @path
  str << @opaque if @opaque
  if @query
    str << "?"
    str << @query
  end

  if @fragment
    str << "#"
    str << @fragment
  end
  str
end

#user=(v) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/uri/whatwg_parser/generic.rb', line 57

def user=(v)
  return super unless @parsed_by_whatwg_parser
  return v unless v

  if host.nil? || host.empty? || scheme == "file"
    raise InvalidURIError, "cannot set user when host is nil or file schme"
  end
  set_user(parser.utf8_percent_encode_string(v, URI::WhatwgParser::USERINFO_PERCENT_ENCODE_SET))
end

#userinfo=(userinfo) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/uri/whatwg_parser/generic.rb', line 150

def userinfo=(userinfo)
  return super unless @parsed_by_whatwg_parser

  user, password = split_userinfo(userinfo)
  self.user = user
  self.password = password
end