Skip to content

Commit

Permalink
background image quoted url
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 authored and gabrielmfern committed Sep 24, 2024
1 parent 51b800d commit 2b43917
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
20 changes: 20 additions & 0 deletions packages/font/src/font.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@ describe("<Font> component", () => {
);
expect(actualOutput).toMatchSnapshot();
});

it("renders with quoted URLs and font names", async () => {
const webFont = {
url: '"http://example.com/font.woff"',
format: "woff",
} as const;

const html = await render(
<Font
fallbackFontFamily={["Helvetica Neue", "Arial"]}
fontFamily="My 'Custom' Font"
webFont={webFont}
/>,
);

expect(html).toContain("font-family: 'My \\'Custom\\' Font';");
expect(html).toContain("Helvetica Neue, Arial");
expect(html).toContain('src: url("http://example.com/font.woff") format(\'woff\');');
});

});
14 changes: 7 additions & 7 deletions packages/font/src/font.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ export const Font: React.FC<Readonly<FontProps>> = ({
fontWeight = 400,
}) => {
const src = webFont
? `src: url(${webFont.url}) format('${webFont.format}');`
? `src: url(${webFont.url.replace(/"/g, '\\"')}) format('${webFont.format}');`
: "";

const style = `
@font-face {
font-family: '${fontFamily}';
font-family: '${fontFamily.replace(/'/g, "\\'")}';
font-style: ${fontStyle};
font-weight: ${fontWeight};
mso-font-alt: '${
Array.isArray(fallbackFontFamily)
? fallbackFontFamily[0]
: fallbackFontFamily
? fallbackFontFamily[0].replace(/'/g, "\\'")
: fallbackFontFamily.replace(/'/g, "\\'")
}';
${src}
}
* {
font-family: '${fontFamily}', ${
font-family: '${fontFamily.replace(/'/g, "\\'")}', ${
Array.isArray(fallbackFontFamily)
? fallbackFontFamily.join(", ")
: fallbackFontFamily
? fallbackFontFamily.map(font => font.replace(/'/g, "\\'")).join(", ")
: fallbackFontFamily.replace(/'/g, "\\'")
};
}
`;
Expand Down

0 comments on commit 2b43917

Please sign in to comment.