From b84c62fc745d4957a04bb80b9be0cfd2e5c76546 Mon Sep 17 00:00:00 2001 From: Nick the Sick Date: Fri, 17 May 2024 05:35:52 +0200 Subject: [PATCH] fix: revert `font-family` escaping introduced by #4545 Using `CSS.escape` is the wrong tool for the job here: - it is meant for CSS selectors and does not handle CSS variables properly. - you can't use `var(--title)` as a font-family because it was getting escaped to `var\(--title\)` Instead this introduces using a regex to see if we should quote the font-family. Quoting when: - font-family includes at least one number or whitespace character --- .../src/Extensions/FontFamily/React/index.jsx | 17 ++++++- .../Extensions/FontFamily/React/index.spec.js | 44 ++++++++++++++++++- .../Extensions/FontFamily/React/styles.scss | 15 +++++++ .../extension-font-family/src/font-family.ts | 3 +- 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 demos/src/Extensions/FontFamily/React/styles.scss diff --git a/demos/src/Extensions/FontFamily/React/index.jsx b/demos/src/Extensions/FontFamily/React/index.jsx index 6d7bbbdc8ee..9899727fd46 100644 --- a/demos/src/Extensions/FontFamily/React/index.jsx +++ b/demos/src/Extensions/FontFamily/React/index.jsx @@ -1,3 +1,5 @@ +import './styles.scss' + import Document from '@tiptap/extension-document' import FontFamily from '@tiptap/extension-font-family' import Paragraph from '@tiptap/extension-paragraph' @@ -15,6 +17,7 @@ export default () => {

Serious people use serif fonts anyway.

The cool kids can apply monospace fonts aswell.

But hopefully we all can agree, that cursive fonts are the best.

+

Then there are CSS variables, the new hotness.

`, }) @@ -27,6 +30,7 @@ export default () => { @@ -37,28 +41,39 @@ export default () => { ? 'is-active' : '' } + data-test-id="comic-sans" > Comic Sans - + diff --git a/demos/src/Extensions/FontFamily/React/index.spec.js b/demos/src/Extensions/FontFamily/React/index.spec.js index e9af067270a..6e77e857199 100644 --- a/demos/src/Extensions/FontFamily/React/index.spec.js +++ b/demos/src/Extensions/FontFamily/React/index.spec.js @@ -3,5 +3,47 @@ context('/src/Extensions/FontFamily/React/', () => { cy.visit('/src/Extensions/FontFamily/React/') }) - // TODO: Write tests + beforeEach(() => { + cy.get('.tiptap').then(([{ editor }]) => { + editor.commands.setContent('

Example Text

') + }) + cy.get('.tiptap').type('{selectall}') + }) + + it('should set the font-family of the selected text', () => { + cy.get('[data-test-id="monospace"]') + .should('not.have.class', 'is-active') + .click() + .should('have.class', 'is-active') + + cy.get('.tiptap').find('span').should('have.attr', 'style', 'font-family: monospace') + }) + + it('should remove the font-family of the selected text', () => { + cy.get('[data-test-id="monospace"]').click() + + cy.get('.tiptap span').should('exist') + + cy.get('[data-test-id="unsetFontFamily"]').click() + + cy.get('.tiptap span').should('not.exist') + }) + + it('should quote font-family that have spaces in them', () => { + cy.get('[data-test-id="comic-sans"]') + .should('not.have.class', 'is-active') + .click() + .should('have.class', 'is-active') + + cy.get('.tiptap').find('span').should('have.attr', 'style', 'font-family: "Comic Sans MS", "Comic Sans"') + }) + + it('should allow CSS variables as a font-family', () => { + cy.get('[data-test-id="css-variable"]') + .should('not.have.class', 'is-active') + .click() + .should('have.class', 'is-active') + + cy.get('.tiptap').find('span').should('have.attr', 'style', 'font-family: var(--title-font-family)') + }) }) diff --git a/demos/src/Extensions/FontFamily/React/styles.scss b/demos/src/Extensions/FontFamily/React/styles.scss new file mode 100644 index 00000000000..6a1386a78e7 --- /dev/null +++ b/demos/src/Extensions/FontFamily/React/styles.scss @@ -0,0 +1,15 @@ + +html { + --title-font-family: 'Helvetica', sans-serif; +} + +.tiptap { + > * + * { + margin-top: 0.75em; + } + + ul, + ol { + padding: 0 1rem; + } +} diff --git a/packages/extension-font-family/src/font-family.ts b/packages/extension-font-family/src/font-family.ts index 4d85b06ec05..ef55fc1da04 100644 --- a/packages/extension-font-family/src/font-family.ts +++ b/packages/extension-font-family/src/font-family.ts @@ -56,7 +56,8 @@ export const FontFamily = Extension.create({ } return { - style: `font-family: ${attributes.fontFamily.split(',').map((fontFamily: string) => CSS.escape(fontFamily.trim())).join(', ')}`, + style: `font-family: ${attributes.fontFamily.split(',') + .map((fontFamily: string) => (fontFamily.match(/(\d|\s)+/g) ? `"${fontFamily.trim()}"` : fontFamily.trim())).join(', ')}`, } }, },