Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Parse hex colors with alpha channels #67

Merged
merged 4 commits into from
Jun 22, 2018
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
sudo: false
language: ruby
cache: bundler

install:
# If we're running for a pull request, check out the revision of sass-spec
# referenced by that pull request.
- |
if [ ! -z "$TRAVIS_PULL_REQUEST" -a "$TRAVIS_PULL_REQUEST" != false ]; then
ref=$(extra/sass-spec-ref.sh)
mkdir sass-spec
git -C sass-spec init
git -C sass-spec pull --depth=1 git://github.com/sass/sass-spec \
$(extra/sass-spec-ref.sh)
git -C sass-spec pull --depth=1 git://github.com/sass/sass-spec "$ref"
bundle config local.sass-spec "$(pwd)/sass-spec"
fi

Expand Down
5 changes: 5 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* Add support for importing an `_index.scss` or `_index.sass` file when
importing a directory.

### Backwards Incompatibilities -- Must Read!

* Tokens such as `#abcd` that are ambiguous between ID strings and hex colors
with an alpha channel are now parsed as colors.

## 3.5.6 (22 March 2018)

* Allow `!` in custom property values.
Expand Down
10 changes: 9 additions & 1 deletion extra/sass-spec-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ fi

>&2 echo "Fetching pull request $TRAVIS_PULL_REQUEST..."

JSON=$(curl -L -sS https://api.github.com/repos/sass/sass/pulls/$TRAVIS_PULL_REQUEST)
url=https://api.github.com/repos/sass/ruby-sass/pulls/$TRAVIS_PULL_REQUEST
if [ -z "$GITHUB_AUTH" ]; then
>&2 echo "Fetching pull request info without authentication"
JSON=$(curl -L -sS $url)
else
>&2 echo "Fetching pull request info as sassbot"
JSON=$(curl -u "sassbot:$GITHUB_AUTH" -L -sS $url)
fi
>&2 echo "$JSON"

RE_SPEC_PR="sass\/sass-spec(#|\/pull\/)([0-9]+)"

Expand Down
20 changes: 8 additions & 12 deletions lib/sass/script/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,20 @@ def id
# IDs in properties are used in the Basic User Interface Module
# (http://www.w3.org/TR/css3-ui/).
return unless scan(REGULAR_EXPRESSIONS[:id])
if @scanner[0] =~ /^\#[0-9a-fA-F]+$/
if @scanner[0].length == 4 || @scanner[0].length == 7
return [:color, Script::Value::Color.from_hex(@scanner[0])]
elsif @scanner[0].length == 5 || @scanner[0].length == 9
filename = @options[:filename]
Sass::Util.sass_warn <<MESSAGE
DEPRECATION WARNING on line #{line}, column #{offset}#{" of #{filename}" if filename}:
The value "#{@scanner[0]}" is currently parsed as a string, but it will be parsed as a color in
future versions of Sass. Use "unquote('#{@scanner[0]}')" to continue parsing it as a string.
MESSAGE
end
if @scanner[0] =~ /^\#[0-9a-fA-F]+$/ &&
(@scanner[0].length == 4 || @scanner[0].length == 5 ||
@scanner[0].length == 7 || @scanner[0].length == 9)
return [:color, Script::Value::Color.from_hex(@scanner[0])]
end
[:ident, @scanner[0]]
end

def color
return unless @scanner.match?(REGULAR_EXPRESSIONS[:color])
return unless @scanner[0].length == 4 || @scanner[0].length == 7
unless @scanner[0].length == 4 || @scanner[0].length == 5 ||
@scanner[0].length == 7 || @scanner[0].length == 9
return
end
script_color = Script::Value::Color.from_hex(scan(REGULAR_EXPRESSIONS[:color]))
[:color, script_color]
end
Expand Down
5 changes: 3 additions & 2 deletions lib/sass/script/value/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,14 @@ def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
#
# @return [Color]
def self.from_hex(hex_string, alpha = nil)
unless hex_string =~ /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i ||
hex_string =~ /^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i
unless hex_string =~ /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i ||
hex_string =~ /^#?([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i
raise ArgumentError.new("#{hex_string.inspect} is not a valid hex color.")
end
red = $1.ljust(2, $1).to_i(16)
green = $2.ljust(2, $2).to_i(16)
blue = $3.ljust(2, $3).to_i(16)
alpha = $4.ljust(2, $4).to_i(16).to_f / 0xff if $4

hex_string = "##{hex_string}" unless hex_string[0] == ?#
attrs = {:red => red, :green => green, :blue => blue, :representation => hex_string}
Expand Down
2 changes: 0 additions & 2 deletions test/sass/script_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,6 @@ def test_non_ident_colors_with_wrong_number_of_digits
'Invalid CSS after "": expected expression (e.g. 1px, bold), was "#1"') {eval("#1")}
assert_raise_message(Sass::SyntaxError,
'Invalid CSS after "": expected expression (e.g. 1px, bold), was "#12"') {eval("#12")}
assert_raise_message(Sass::SyntaxError,
'Invalid CSS after "": expected expression (e.g. 1px, bold), was "#1234"') {eval("#1234")}
assert_raise_message(Sass::SyntaxError,
'Invalid CSS after "": expected expression (e.g. 1px, bold), was "#12345"') {eval("#12345")}
assert_raise_message(Sass::SyntaxError, 'Invalid CSS after "": expected expression (e.g. ' \
Expand Down
2 changes: 1 addition & 1 deletion test/sass/value_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_malformed_hex_color
hex_color("green")
end
assert_raises ArgumentError do
hex_color("#abcd")
hex_color("#abcde")
end
end

Expand Down