Skip to content

Commit

Permalink
resolve issue 222 (Suisse Int'l font family transform issue / special…
Browse files Browse the repository at this point in the history
… character transform) (#223)

fix: escape apostrophes in font-family names for CSS

---------

Co-authored-by: jorenbroekema <joren.broekema@gmail.com>
  • Loading branch information
niborium and jorenbroekema committed Nov 16, 2023
1 parent 936deda commit 2307b9d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-wolves-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Refined CSS font name processing: Enhanced escapeApostrophes for accurate apostrophe handling in font names. Updated quoteWrapWhitespacedFont for smart quoting and escaping in multi-word fonts. Ensures better CSS font family compatibility.
11 changes: 10 additions & 1 deletion src/css/transformTypography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ export function isCommaSeparated(value: string): boolean {
return value.includes(',');
}

export function escapeApostrophes(value: string): string {
return value.replace(/'/g, "\\'");
}

function quoteWrapWhitespacedFont(fontString: string) {
return hasWhiteSpace(fontString) && !isAlreadyQuoted(fontString) ? `'${fontString}'` : fontString;
let fontName = fontString.trim();
const isQuoted = isAlreadyQuoted(fontName);
if (!isQuoted) {
fontName = escapeApostrophes(fontName);
}
return hasWhiteSpace(fontName) && !isQuoted ? `'${fontName}'` : fontName;
}

export function processFontFamily(fontFamily: string | undefined) {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/sd-transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('sd-transforms smoke tests', () => {
--sdFontWeightsBodyRegular: 400;
--sdFontSizesH6: 16px;
--sdFontSizesBody: 16px;
--sdHeading6: 700 16px/1 Arial;
--sdHeading6: 700 16px/1 'Arial Black', 'Suisse Int\\'l', sans-serif;
--sdShadowBlur: 10px;
--sdShadow: inset 0 4px 10px 0 rgba(0,0,0,0.4);
--sdBorderWidth: 5px;
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('sd-transforms smoke tests', () => {
--sd-font-weights-body-regular: 400;
--sd-font-sizes-h6: 16px;
--sd-font-sizes-body: 16px;
--sd-heading-6: 700 16px/1 Arial;
--sd-heading-6: 700 16px/1 'Arial Black', 'Suisse Int\\'l', sans-serif;
--sd-shadow-blur: 10px;
--sd-shadow: inset 0 4px 10px 0 rgba(0,0,0,0.4);
--sd-border-width: 5px;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/tokens/sd-transforms.tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"value": {
"fontSize": "{fontSizes.h6}",
"fontWeight": "700",
"fontFamily": "Arial",
"fontFamily": "Arial Black, Suisse Int'l, sans-serif",
"lineHeight": "1"
},
"type": "typography"
Expand Down
16 changes: 12 additions & 4 deletions test/spec/css/transformFontFamilies.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { processFontFamily } from '../../../src/css/transformTypography.js';
import { processFontFamily, escapeApostrophes } from '../../../src/css/transformTypography.js';

describe('process font family', () => {
it('transforms font-family to have single quotes around multi-word font-families', () => {
Expand All @@ -9,8 +9,16 @@ describe('process font family', () => {
expect(processFontFamily('Arial Black, Times New Roman, Foo, sans-serif')).to.equal(
`'Arial Black', 'Times New Roman', Foo, sans-serif`,
);
expect(processFontFamily(`'Arial Black', Times New Roman, Foo, sans-serif`)).to.equal(
`'Arial Black', 'Times New Roman', Foo, sans-serif`,
);
expect(
processFontFamily(`'Arial Black', Times New Roman, Suisse Int'l, Foo, sans-serif`),
).to.equal(`'Arial Black', 'Times New Roman', 'Suisse Int\\'l', Foo, sans-serif`);
});
});

describe('escape apostrophes', () => {
it('should escape single apostrophes in strings', () => {
expect(escapeApostrophes("Suisse Int'l")).to.equal("Suisse Int\\'l");
expect(escapeApostrophes("Font's Example")).to.equal("Font\\'s Example");
expect(escapeApostrophes('NoEscape')).to.equal('NoEscape');
});
});

0 comments on commit 2307b9d

Please sign in to comment.