Просмотр исходного кода

Avoid adding semantic style from an element which has explicit style (#14781)

GitOrigin-RevId: ae36022a9da36be0703c7e8e04e133975fadb893
Alf Eaton 3 лет назад
Родитель
Сommit
496b30e18e

+ 6 - 12
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: () => '}',
   }),

+ 19 - 0
services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-paste-html.spec.tsx

@@ -479,6 +479,25 @@ describe('<CodeMirrorEditor/> 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 = [
+      '<strong style="font-weight:bold">foo</strong>',
+      '<b style="font-weight:bold">foo</b>',
+      '<em style="font-style:italic">foo</em>',
+      '<i style="font-style:italic">foo</i>',
+      '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()