From 2ae077973e523d2456555ddf5342c1e3185d65da Mon Sep 17 00:00:00 2001 From: akianonymus Date: Sun, 26 Feb 2023 16:45:06 +0530 Subject: fix: hsl: for multiple spaces in between --- lua/colorizer/color.lua | 9 +++++---- lua/colorizer/utils.lua | 8 ++++++++ test/expectation.txt | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lua/colorizer/color.lua b/lua/colorizer/color.lua index 37f8f30..fe3e15b 100644 --- a/lua/colorizer/color.lua +++ b/lua/colorizer/color.lua @@ -12,6 +12,7 @@ local utils = require "colorizer.utils" local byte_is_alphanumeric = utils.byte_is_alphanumeric local byte_is_hex = utils.byte_is_hex local byte_is_valid_colorchar = utils.byte_is_valid_colorchar +local count = utils.count local parse_hex = utils.parse_hex local percent_or_hex = utils.percent_or_hex @@ -100,7 +101,7 @@ function color.hsl_function_parser(line, i, opts) local min_commas, min_spaces = 2, 2 local pattern = "^" .. opts.prefix - .. "%(%s*([.%d]+)([deg]*)([turn]*)(,?)(%s?)%s*(%d+)%%(,?)(%s?)%s*(%d+)%%%s*(/?,?)%s*([.%d]*)([%%]?)%s*%)()" + .. "%(%s*([.%d]+)([deg]*)([turn]*)(%s?)%s*(,?)%s*(%d+)%%(%s?)%s*(,?)%s*(%d+)%%%s*(/?,?)%s*([.%d]*)([%%]?)%s*%)()" if opts.prefix == "hsl" then min_len = CSS_HSL_FN_MINIMUM_LENGTH @@ -110,7 +111,7 @@ function color.hsl_function_parser(line, i, opts) return end - local h, deg, turn, csep1, ssep1, s, csep2, ssep2, l, sep3, a, percent_sign, match_end = line:sub(i):match(pattern) + local h, deg, turn, ssep1, csep1, s, ssep2, csep2, l, sep3, a, percent_sign, match_end = line:sub(i):match(pattern) if not match_end then return end @@ -129,11 +130,11 @@ function color.hsl_function_parser(line, i, opts) local s_seps = ("%s%s"):format(ssep1, ssep2) -- comma separator syntax if c_seps:match "," then - if not (#c_seps:match ",*" == min_commas) then + if not (count(c_seps, ",") == min_commas) then return end -- space separator syntax with decimal or percentage alpha - elseif #s_seps:match "%s*" >= min_spaces then + elseif count(s_seps, "%s") >= min_spaces then if a then if not (c_seps == "/") then return diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua index 282e405..724b46e 100644 --- a/lua/colorizer/utils.lua +++ b/lua/colorizer/utils.lua @@ -71,6 +71,14 @@ function utils.byte_is_valid_colorchar(byte) return utils.byte_is_alphanumeric(byte) or byte == ("-"):byte() end +---Count the number of character in a string +---@param str string +---@param pattern string +---@return number +function utils.count(str, pattern) + return select(2, string.gsub(str, pattern, "")) +end + --- Get last modified time of a file ---@param path string: file path ---@return number|nil: modified time diff --git a/test/expectation.txt b/test/expectation.txt index 37472e0..e3cc609 100644 --- a/test/expectation.txt +++ b/test/expectation.txt @@ -25,7 +25,7 @@ hsl(990 80% 50% / 0.4) hsl(720 80% 50% / 0.4) hsl(1turn 80% 50% / 0.4) hsl(0.4turn 80% 50% / 0.4) hsl(1.4turn 80% 50% / 0.4) hsla(300 50% 50%) hsla(300 50% 50% / 1) hsla(300 50% 50% / 0.4) hsla(300,50%,50%,05) -hsla(360,50%,50%,1.0000000000000001) +hsla(360 , 50% , 50% , 1.0000000000000001) ]] --[[ FAIL -- cgit v1.2.3-70-g09d2