Skip to content

Commit

Permalink
css: improve RGB hex color and length matching
Browse files Browse the repository at this point in the history
  - Allow RGB hexadecimal colors in multiple formats per the spec. For
    example, all of the following formats are valid.

      #123
      #123456
      #12345678

    https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb_colors

  - Allow decimal lengths without a leading zero. For example, the all of
    the following formats are valid.

      .5em
      0.5em
      0
      1em

    https://developer.mozilla.org/en-US/docs/Web/CSS/length#syntax
  • Loading branch information
hochhaus committed Apr 16, 2022
1 parent 078c4be commit bdb77cf
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions css/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func CaretColorHandler(value string) bool {
if in(splitVals, colorValues) {
return true
}
reg := regexp.MustCompile(`#[0-9abcdef]{6}`)
reg := regexp.MustCompile(`#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})`)
reg.Longest()
if reg.FindString(value) == value && value != "" {
return true
Expand Down Expand Up @@ -865,7 +865,7 @@ func ColorHandler(value string) bool {
if in(splitVals, colorValues) {
return true
}
reg := regexp.MustCompile(`#[0-9abcdef]{6}`)
reg := regexp.MustCompile(`#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})`)
reg.Longest()
if reg.FindString(value) == value && value != "" {
return true
Expand Down Expand Up @@ -1298,7 +1298,7 @@ func GridColumnGapHandler(value string) bool {
}

func LengthHandler(value string) bool {
reg := regexp.MustCompile(`[\-]?[0-9]+[\.]?[0-9]*(%|cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vh|vmin|vmax|deg|rad|turn)?`)
reg := regexp.MustCompile(`[\-]?([0-9]+|[0-9]*[\.][0-9]+)(%|cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vh|vmin|vmax|deg|rad|turn)?`)
reg.Longest()
return reg.FindString(value) == value && value != ""
}
Expand Down

0 comments on commit bdb77cf

Please sign in to comment.