Skip to content

Commit

Permalink
Restrict SOURCE_MAPPING_PATTERN to only match leading comments
Browse files Browse the repository at this point in the history
The `sourceMappingURL` comments always appear at the beginning of the
line, so we can ignore any comments that appear later in a line when
searching for comments to replace.
  • Loading branch information
rmacklin committed Nov 23, 2021
1 parent d319486 commit 1dd7799
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/propshaft/compilers/source_mapping_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Propshaft::Compilers::SourceMappingUrls
attr_reader :assembly

SOURCE_MAPPING_PATTERN = %r{(//|/\*)# sourceMappingURL=(.*\.map)}
SOURCE_MAPPING_PATTERN = %r{^(//|/\*)# sourceMappingURL=(.*\.map)}

def initialize(assembly)
@assembly = assembly
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/assets/mapped/sourceMappingURL-not-at-start.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class {
color: green; /*# sourceMappingURL=sourceMappingURL-not-at-start.css.map */
}
5 changes: 5 additions & 0 deletions test/propshaft/compilers/source_mapping_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ class Propshaft::Compilers::SourceMappingUrlsTest < ActiveSupport::TestCase
assert_match %r{sourceMappingURL=sourceMappingURL-outside-comment.css.map},
@assembly.compilers.compile(find_asset("sourceMappingURL-outside-comment.css", fixture_path: "mapped"))
end

test "sourceMappingURL not at the beginning of the line should be left alone" do
assert_match %r{sourceMappingURL=sourceMappingURL-not-at-start.css.map},
@assembly.compilers.compile(find_asset("sourceMappingURL-not-at-start.css", fixture_path: "mapped"))
end
end

0 comments on commit 1dd7799

Please sign in to comment.