Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unescape escaped characters when value is not quoted #421

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/dotenv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,9 @@ def parse_line(line)
def parse_value(value)
# Remove surrounding quotes
value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')

if Regexp.last_match(1) == '"'
value = unescape_characters(expand_newlines(value))
end

if Regexp.last_match(1) != "'"
self.class.substitutions.each do |proc|
value = proc.call(value, @hash, @is_load)
end
end
maybe_quote = Regexp.last_match(1)
value = unescape_value(value, maybe_quote)
value = perform_substitutions(value, maybe_quote)
value
end

Expand All @@ -94,5 +87,24 @@ def expand_newlines(value)
def variable_not_set?(line)
!line.split[1..-1].all? { |var| @hash.member?(var) }
end

def unescape_value(value, maybe_quote)
if maybe_quote == '"'
unescape_characters(expand_newlines(value))
elsif maybe_quote.nil?
unescape_characters(value)
else
value
end
end

def perform_substitutions(value, maybe_quote)
if maybe_quote != "'"
self.class.substitutions.each do |proc|
value = proc.call(value, @hash, @is_load)
end
end
value
end
end
end
4 changes: 4 additions & 0 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def env(string)
expect(env("FOO= bar")).to eql("FOO" => "bar")
end

it "parses unquoted escape characters correctly" do
expect(env("FOO=bar\\ bar")).to eql("FOO" => "bar bar")
end

it "parses values with spaces around equal sign" do
expect(env("FOO =bar")).to eql("FOO" => "bar")
expect(env("FOO= bar")).to eql("FOO" => "bar")
Expand Down