Skip to content

Commit

Permalink
closes #163
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Oct 12, 2022
1 parent c4e657f commit d10aca2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/vendor/gradient-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

var GradientParser = GradientParser || {}

// https://w3c.github.io/csswg-drafts/css-images-3/#linear-gradients
// It may be omitted; if so, it defaults to to bottom.
const FALLBACK_LINEAR_ORIENTATION = { type: 'directional', value: 'bottom' }

GradientParser.parse = (function () {
var tokens = {
linearGradient: /^(\-(webkit|o|ms|moz)\-)?(linear\-gradient)/i,
Expand Down Expand Up @@ -58,12 +62,14 @@ GradientParser.parse = (function () {
matchGradient(
'linear-gradient',
tokens.linearGradient,
matchLinearOrientation
matchLinearOrientation,
FALLBACK_LINEAR_ORIENTATION
) ||
matchGradient(
'repeating-linear-gradient',
tokens.repeatingLinearGradient,
matchLinearOrientation
matchLinearOrientation,
FALLBACK_LINEAR_ORIENTATION
) ||
matchGradient(
'radial-gradient',
Expand All @@ -78,13 +84,20 @@ GradientParser.parse = (function () {
)
}

function matchGradient(gradientType, pattern, orientationMatcher) {
function matchGradient(
gradientType,
pattern,
orientationMatcher,
fallbackOrientation
) {
return matchCall(pattern, function (captures) {
var orientation = orientationMatcher()
if (orientation) {
if (!scan(tokens.comma)) {
error('Missing comma before color stops')
}
} else {
orientation = fallbackOrientation
}

return {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/gradient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ describe('Gradient', () => {
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support linear-gradient with omitted orientation', async () => {
const svg = await satori(
<div
style={{
backgroundColor: 'green',
backgroundImage: 'linear-gradient(red, blue)',
height: '100%',
width: '100%',
}}
></div>,
{
width: 100,
height: 100,
fonts,
}
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})

describe('radial-gradient', () => {
Expand Down

0 comments on commit d10aca2

Please sign in to comment.