summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/sass-embedded-1.101.0-arm64-darwin/lib/sass/uri.rb
blob: 2e0f9b9f7a75859968d7a38ed4d0194e3113d4fd (plain)
1
2
3
4
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
54
55
56
# frozen_string_literal: true

require 'uri'

module Sass
  # The {Uri} class.
  #
  # It follows RFC3986 to match the behavior of Uri class from Dart.
  #
  # @see https://www.rfc-editor.org/info/rfc3986/
  module Uri
    module_function

    def decode_uri_component(str)
      str.b.gsub(/%\h\h/, ::URI::TBLDECWWWCOMP_).force_encoding(str.encoding)
    end

    def encode_uri_component(str)
      str.b.gsub(/[^0-9A-Za-z\-._~]/n, ::URI::TBLENCURICOMP_).force_encoding(str.encoding)
    end

    def encode_uri_path_component(str)
      str.b.gsub(%r{[^0-9A-Za-z\-._~!$&'()*+,;=:@/]}n, ::URI::TBLENCURICOMP_).force_encoding(str.encoding)
    end

    def encode_uri_query_component(str)
      str.b.gsub(%r{[^0-9A-Za-z\-._~!$&'()*+,;=:@/?]}n, ::URI::TBLENCURICOMP_).force_encoding(str.encoding)
    end

    def file_uri_to_path(uri)
      path = decode_uri_component(::URI::RFC3986_PARSER.parse(uri).path)
      if path.start_with?('/')
        windows_path = path[1..]
        path = windows_path if File.absolute_path?(windows_path)
      end
      path
    end

    def path_to_file_uri(path)
      path = "/#{path}" unless path.start_with?('/')
      "file://#{encode_uri_path_component(path)}"
    end

    def pwd
      pwd = Dir.pwd
      pwd += '/' unless pwd.end_with?('/')
      path_to_file_uri(pwd)
    end

    def relative(to, from)
      ::URI::RFC3986_PARSER.parse(to).route_from(from).to_s
    end
  end

  private_constant :Uri
end