Class: URI::WhatwgParser

Inherits:
Object
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/uri/whatwg_parser.rb,
lib/uri/whatwg_parser/error.rb,
lib/uri/whatwg_parser/generic.rb,
lib/uri/whatwg_parser/version.rb,
lib/uri/whatwg_parser/host_parser.rb,
lib/uri/whatwg_parser/parser_helper.rb

Defined Under Namespace

Modules: Generic, ParserHelper Classes: Error, HostParser, ParseError

Constant Summary collapse

SPECIAL_SCHEME =
{ "ftp" => 21, "file" => nil, "http" => 80, "https" => 443, "ws" => 80, "wss" => 443 }
FRAGMENT_PERCENT_ENCODE_SET =
C0_CONTROL_PERCENT_ENCODE_SET | Set[" ", "\"", "<", ">", "`"]
QUERY_PERCENT_ENCODE_SET =
C0_CONTROL_PERCENT_ENCODE_SET | Set[" ", "\"", "#", "<", ">"]
SPECIAL_QUERY_PERCENT_ENCODE_SET =
QUERY_PERCENT_ENCODE_SET | Set["'"]
PATH_PERCENT_ENCODE_SET =
QUERY_PERCENT_ENCODE_SET | Set["?", "^", "`", "{", "}"]
USERINFO_PERCENT_ENCODE_SET =
PATH_PERCENT_ENCODE_SET | Set["/", ":", ";", "=", "@", "[", "\\", "]", "|"]
SINGLE_DOT_PATH_SEGMENTS =
Set[".", "%2e", "%2E"]
DOUBLE_DOT_PATH_SEGMENTS =
Set["..", ".%2e", ".%2E", "%2e.", "%2e%2e", "%2e%2E", "%2E.", "%2E%2e", "%2E%2E"]
WINDOWS_DRIVE_LETTER =
Regexp.new("\\A([a-zA-Z][:|])\\z")
NORMALIZED_WINDOWS_DRIVE_LETTER =
Regexp.new("\\A([a-zA-Z][:])\\z")
STARTS_WITH_WINDOWS_DRIVE_LETTER =
Regexp.new("\\A([a-zA-Z][:|])(?:[/\\?#])?\\z")
VALID_SIGNS_FOR_SCHEME =
Set["+", "-", "."]
DELIMITER_SIGNS =
Set["/", "?", "#"]
WS_SCHEMES =
Set["ws", "wss"]
ASCII_ALPHA_LOWERCASE =
Set.new(("a".."z").to_a)
ASCII_ALPHA_UPPERCASE =
Set.new(("A".."Z").to_a)
ASCII_DIGIT =
Set.new(("0".."9").to_a)
VERSION =
"0.3.0"

Constants included from ParserHelper

ParserHelper::C0_CONTROL_PERCENT_ENCODE_SET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserHelper

#utf8_percent_encode, #utf8_percent_encode_string

Constructor Details

#initializeWhatwgParser

Returns a new instance of WhatwgParser.



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

def initialize
  reset
  @host_parser = HostParser.new
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



39
40
41
# File 'lib/uri/whatwg_parser.rb', line 39

def path
  @path
end

Instance Method Details

#join(*uris) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/uri/whatwg_parser.rb', line 110

def join(*uris)
  return parse(uris[0]) if uris.size == 1

  base, input = uris.shift(2)
  uri = parse(input.to_s, base: base.to_s)
  uris.each do |input|
    uri = parse(input.to_s, base: uri.to_s)
  end

  uri
end

#parse(input, base: nil, url: nil, state_override: nil) ⇒ Object

:nodoc:



50
51
52
# File 'lib/uri/whatwg_parser.rb', line 50

def parse(input, base: nil, url: nil, state_override: nil) # :nodoc:
  URI.for(*self.split(input, base: base, url: url, state_override: state_override), self)
end

#regexpObject



46
47
48
# File 'lib/uri/whatwg_parser.rb', line 46

def regexp
  {}
end

#split(input, base: nil, url: nil, state_override: nil) ⇒ Object

:nodoc:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/uri/whatwg_parser.rb', line 54

def split(input, base: nil, url: nil, state_override: nil) # :nodoc:
  reset
  @base = nil
  if base != nil
    ary = split(base, base: nil)
    @base = { scheme: ary[0], userinfo: ary[1], host: ary[2], port: ary[3], query: ary[7], fragment: ary[8]}
    @base_path = @path
    reset
  end

  if url
    raise ArgumentError, "bad argument (expected URI object)" unless url.is_a?(URI::Generic)
    @parse_result.merge!(url.component.zip(url.send(:component_ary)).to_h)
    @username = url.user
    @password = url.password
    @parse_result.delete(:userinfo)
    @special_url = special_url?(@parse_result[:scheme])
  end

  if state_override
    @state = state_override.to_sym
    @state_override = @state
    raise ArgumentError, "state override is invalid" if !state_override.to_s.end_with?("_state") || !respond_to?(@state_override, private: true)
  else
    raise ParseError, "uri can't be empty" if (input.nil? || input.empty?) && @base.nil?
  end

  input = input.dup

  unless url
    remove_c0_control_or_space!(input)
  end

  input.delete!("\t\n\r") if /[\t\n\r]/.match?(input)

  @input_chars = input.chars
  input_chars_length = @input_chars.length
  @pos = 0

  while @pos <= input_chars_length
    dispatch_state(@input_chars[@pos])
    break if @terminate
    @pos += 1
  end

  userinfo = [@username, @password].compact.reject(&:empty?).join(":")
  if @path
    if @path.is_a?(Array)
      path = "/#{@path.join("/")}"
    else
      opaque = @path
    end
  end
  [@parse_result[:scheme], userinfo, @parse_result[:host], @parse_result[:port], nil, path, opaque, @parse_result[:query], @parse_result[:fragment]]
end