From 496b30e18e2c6618cf5770936d6922ec043a0e7d Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Thu, 14 Sep 2023 09:17:00 +0100 Subject: [PATCH] Avoid adding semantic style from an element which has explicit style (#14781) GitOrigin-RevId: ae36022a9da36be0703c7e8e04e133975fadb893 --- .../extensions/visual/paste-html.ts | 18 ++++++------------ ...demirror-editor-visual-paste-html.spec.tsx | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts b/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts index 8068063d24..0c167f8fa6 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts +++ b/services/web/frontend/js/features/source-editor/extensions/visual/paste-html.ts @@ -449,8 +449,7 @@ const selectors = [ createSelector({ selector: 'b', match: element => - element.style.fontWeight !== 'normal' && - !(parseInt(element.style.fontWeight) < 700) && + !element.style.fontWeight && !isHeading(element.parentElement) && hasContent(element), start: () => '\\textbf{', @@ -468,17 +467,13 @@ const selectors = [ }), createSelector({ selector: 'strong', - match: element => - element.style.fontWeight !== 'normal' && - !(parseInt(element.style.fontWeight) < 700) && - hasContent(element), + match: element => !element.style.fontWeight && hasContent(element), start: () => '\\textbf{', end: () => '}', }), createSelector({ selector: 'i', - match: element => - element.style.fontStyle !== 'normal' && hasContent(element), + match: element => !element.style.fontStyle && hasContent(element), start: () => '\\textit{', end: () => '}', }), @@ -491,14 +486,13 @@ const selectors = [ }), createSelector({ selector: 'em', - match: element => - element.style.fontStyle !== 'normal' && hasContent(element), + match: element => !element.style.fontStyle && hasContent(element), start: () => '\\textit{', end: () => '}', }), createSelector({ selector: 'sup', - match: element => hasContent(element), + match: element => !element.style.verticalAlign && hasContent(element), start: () => '\\textsuperscript{', end: () => '}', }), @@ -511,7 +505,7 @@ const selectors = [ }), createSelector({ selector: 'sub', - match: element => hasContent(element), + match: element => !element.style.verticalAlign && hasContent(element), start: () => '\\textsubscript{', end: () => '}', }), diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx index b8057d4882..7e9024a3f4 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx +++ b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx @@ -479,6 +479,25 @@ describe(' paste HTML in Visual mode', function () { cy.get('.ol-cm-command-textit').should('have.length', 0) }) + it('handles pasted elements with duplicate CSS formatting', function () { + mountEditor() + + const data = [ + 'foo', + 'foo', + 'foo', + 'foo', + 'foo', + ].join(' ') + + const clipboardData = new DataTransfer() + clipboardData.setData('text/html', data) + cy.get('@content').trigger('paste', { clipboardData }) + + cy.get('.ol-cm-command-textbf').should('have.length', 2) + cy.get('.ol-cm-command-textit').should('have.length', 2) + }) + it('removes a non-breaking space when a text node contains no other content', function () { mountEditor()