From 6996babee3cded22b937adf0d4acac972b1b4877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Fri, 22 Sep 2023 01:34:47 -0700 Subject: [PATCH] Create file urls to relative path helper --- lib/sassc/embedded.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/sassc/embedded.rb b/lib/sassc/embedded.rb index 355e0e6..e67560e 100644 --- a/lib/sassc/embedded.rb +++ b/lib/sassc/embedded.rb @@ -49,7 +49,7 @@ def render source_mapping_url = if source_map_embed? "data:application/json;base64,#{[@source_map].pack('m0')}" else - URL.unescape(URL.parse(source_map_file_url).route_from(url).to_s) + URL.file_urls_to_relative_path(source_map_file_url, url) end css += "\n/*# sourceMappingURL=#{source_mapping_url} */" end @@ -61,7 +61,7 @@ def render line += 1 unless line.nil? url = e.span&.url path = if url&.start_with?(Protocol::FILE) - URL.unescape(URL.parse(url).route_from(URL.path_to_file_url("#{Dir.pwd}/")).to_s) + URL.file_urls_to_relative_path(url, URL.path_to_file_url("#{Dir.pwd}/")) end raise SyntaxError.new(e.full_message, filename: path, line: line) end @@ -79,10 +79,10 @@ def source_map url = URL.parse(source_map_file_url || file_url) data = JSON.parse(@source_map) - data['file'] = URL.unescape(URL.parse(output_url).route_from(url).to_s) if output_url + data['file'] = URL.file_urls_to_relative_path(output_url, url) if output_url data['sources'].map! do |source| if source.start_with?(Protocol::FILE) - URL.unescape(URL.parse(source).route_from(url).to_s) + URL.file_urls_to_relative_path(source, url) else source end @@ -309,7 +309,7 @@ def canonicalize(url, context) @parent_urls.push(canonical_url) canonical_url elsif url.start_with?(Protocol::FILE) - path = URL.unescape(URL.parse(url).route_from(@parent_urls.last).to_s) + path = URL.file_urls_to_relative_path(url, @parent_urls.last) parent_path = URL.file_url_to_path(@parent_urls.last) imports = @importer.imports(path, parent_path) @@ -354,7 +354,7 @@ def load_paths end def resolve_file_url(url, parent_url, from_import) - path = URL.unescape(URL.parse(url).route_from(parent_url).to_s) + path = URL.file_urls_to_relative_path(url, parent_url) parent_path = URL.file_url_to_path(parent_url) [File.dirname(parent_path)].concat(load_paths).each do |load_path| resolved = FileImporter.resolve_path(File.absolute_path(path, load_path), from_import) @@ -568,6 +568,10 @@ def unescape(str) PARSER.unescape(str) end + def file_urls_to_relative_path(url, from_url) + URL.unescape(URL.parse(url).route_from(from_url).to_s) + end + def file_url_to_path(url) return if url.nil?