summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/sass-embedded-1.101.0-arm64-darwin/lib/sass/compiler/session/path.rb
blob: f5b109fa0ad8ff47b83480f0906111e8883cde4d (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
# frozen_string_literal: true

module Sass
  class Compiler
    class Session
      # @see https://pub.dev/documentation/path/latest/path/
      module Path
        module_function

        # @see https://pub.dev/documentation/path/latest/path/Context/prettyUri.html
        def pretty_uri(uri)
          return uri unless uri&.start_with?('file:')

          absolute_path = Uri.file_uri_to_path(uri)
          relative_path = Uri.decode_uri_component(Uri.relative(uri, Uri.pwd))
          relative_path.count('/') > absolute_path.count('/') ? absolute_path : relative_path
        end

        def pretty_formatted!(formatted, uri)
          index = formatted.index(uri)
          return formatted unless index

          replacement = pretty_uri(uri)
          return formatted if uri == replacement

          formatted[index, uri.length] = replacement
          formatted
        end
      end

      private_constant :Path
    end
  end
end