-
Notifications
You must be signed in to change notification settings - Fork 18
Prevent of bolding entire content pasted from google docs #62
Changes from 34 commits
7a296c4
53152ab
ea75c23
4e21458
60a5e53
ac4c635
f3dbf34
1900a9f
529b6e3
f25f38c
4b05a67
d4805f6
ca4b62f
ffd320b
f7e6dbd
a6a6ad3
aa541e4
24a349c
a907b16
7d32bc8
5c925dd
0017a4b
318b67c
0948fc1
a63bcc1
751f615
c2727b4
67d017a
e3ae28e
f566580
df6b1b8
2ae8f69
ac1c0a1
84762a8
7eaee98
219aac7
dd1fa0d
1b9829c
614005a
11ae6a1
8bc14fe
201e07c
b055de3
1bbdbd1
a81e493
a27e2e4
d79a224
ee7e3a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paste-from-office/filters/common | ||
*/ | ||
|
||
/** | ||
* Removes `<b>` tag wrapper added by Google Docs to a copied content. | ||
* | ||
* @param {module:engine/view/documentfragment~DocumentFragment} documentFragment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong parameters in the docs. |
||
*/ | ||
export function removeBoldTagWrapper( { documentFragment, writer } ) { | ||
for ( const childWithWrapper of documentFragment.getChildren() ) { | ||
if ( childWithWrapper.is( 'b' ) && childWithWrapper.getStyle( 'font-weight' ) === 'normal' ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const childIndex = documentFragment.getChildIndex( childWithWrapper ); | ||
const removedElement = writer.remove( childWithWrapper )[ 0 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd avoid such constructs if possible: writer.remove( child );
writer.insertChild( index, child.getChildren(), docuemntFragment ); also will work. |
||
|
||
writer.insertChild( childIndex, removedElement.getChildren(), documentFragment ); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paste-from-office/normalizer | ||
*/ | ||
|
||
/** | ||
* Interface defining a content transformation pasted from an external editor. | ||
* | ||
* Normalizers are registered by the {@link module:paste-from-office/pastefromoffice~PasteFromOffice} plugin and run on | ||
* {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. They detect environment-specific | ||
* quirks and transform it into a form compatible with other CKEditor features. | ||
* | ||
* @interface Normalizer | ||
*/ | ||
|
||
/** | ||
* Must return `true` if the `htmlString` contains content which this normalizer can transform. | ||
* | ||
* @method #isActive | ||
* @param {String} htmlString full content of `dataTransfer.getData( 'text/html' )` | ||
* @returns {Boolean} | ||
*/ | ||
|
||
/** | ||
* Executes the normalization of a given data. | ||
* | ||
* @method #exec | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might forgot about it - it should be full form: |
||
* @param {Object} data object obtained from | ||
* {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paste-from-office/normalizer/googledocsnormalizer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The module path should be |
||
*/ | ||
|
||
import { removeBoldTagWrapper } from '../filters/common'; | ||
import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter'; | ||
|
||
/** | ||
* Normalizer for the content pasted from Google Docs. | ||
* | ||
* @implements module:paste-from-office/normalizer~Normalizer | ||
*/ | ||
export default class GoogleDocsNormalizer { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
isActive( htmlString ) { | ||
return /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( htmlString ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
exec( data ) { | ||
const writer = new UpcastWriter(); | ||
|
||
removeBoldTagWrapper( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's stick with a similar API as the rest of the filters to have this consistent: |
||
writer, | ||
documentFragment: data.content | ||
} ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paste-from-office/normalizer/mswordnormalizer | ||
*/ | ||
|
||
import { parseHtml } from '../filters/parse'; | ||
import { transformListItemLikeElementsIntoLists } from '../filters/list'; | ||
import { replaceImagesSourceWithBase64 } from '../filters/image'; | ||
|
||
/** | ||
* Normalizer for the content pasted from Microsoft Word. | ||
* | ||
* @implements module:paste-from-office/normalizer~Normalizer | ||
*/ | ||
export default class MSWordNormalizer { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
isActive( htmlString ) { | ||
return /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( htmlString ) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we know why there are two checks? We can move the RegExps as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ps.: Also for Google Docs we could move the regexp out of the method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Honestly IDK, however, the scope of this PR is related to Google Docs. MS Word is touched only to unify content processing and there is no logic modification. So I would prefer to remain those rules untouched for now. |
||
/xmlns:o="urn:schemas-microsoft-com/i.test( htmlString ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
exec( data ) { | ||
const html = data.dataTransfer.getData( 'text/html' ); | ||
|
||
data.content = normalizeWordInput( html, data.dataTransfer ); | ||
} | ||
} | ||
|
||
// | ||
// Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}. | ||
// | ||
// @private | ||
// @param {String} input Word input. | ||
// @param {module:clipboard/datatransfer~DataTransfer} dataTransfer Data transfer instance. | ||
// @returns {module:engine/view/documentfragment~DocumentFragment} Normalized input. | ||
// | ||
function normalizeWordInput( input, dataTransfer ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this method so both normalizers |
||
const { body, stylesString } = parseHtml( input ); | ||
|
||
transformListItemLikeElementsIntoLists( body, stylesString ); | ||
replaceImagesSourceWithBase64( body, dataTransfer.getData( 'text/rtf' ) ); | ||
|
||
return body; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import simpleText from './simple-text/input.html'; | ||
import simpleTextWindows from './simple-text-windows/input.html'; | ||
|
||
import simpleTextNormalized from './simple-text/normalized.html'; | ||
import simpleTextWindowsNormalized from './simple-text-windows/normalized.html'; | ||
|
||
import simpleTextModel from './simple-text/model.html'; | ||
import simpleTextWindowsModel from './simple-text-windows/model.html'; | ||
|
||
export const fixtures = { | ||
input: { | ||
simpleText, | ||
simpleTextWindows | ||
}, | ||
normalized: { | ||
simpleText: simpleTextNormalized, | ||
simpleTextWindows: simpleTextWindowsNormalized | ||
}, | ||
model: { | ||
simpleText: simpleTextModel, | ||
simpleTextWindows: simpleTextWindowsModel | ||
} | ||
}; | ||
|
||
import simpleTextFirefox from './simple-text/input.firefox.html'; | ||
import simpleTextWindowsFirefox from './simple-text-windows/input.firefox.html'; | ||
|
||
import simpleTextNormalizedFirefox from './simple-text/normalized.firefox.html'; | ||
import simpleTextWindowsNormalizedFirefox from './simple-text-windows/normalized.firefox.html'; | ||
|
||
export const browserFixtures = { | ||
firefox: { | ||
input: { | ||
simpleText: simpleTextFirefox, | ||
simpleTextWindows: simpleTextWindowsFirefox | ||
}, | ||
normalized: { | ||
simpleText: simpleTextNormalizedFirefox, | ||
simpleTextWindows: simpleTextWindowsNormalizedFirefox | ||
}, | ||
model: { | ||
simpleText: simpleTextModel, | ||
simpleTextWindows: simpleTextWindowsModel | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html><body> | ||
<!--StartFragment--><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;" id="docs-internal-guid-f8b26bf1-7fff-40c0-af18-1234567890ab"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p><!--EndFragment--> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
<!--StartFragment--><b style="font-weight:normal;" id="docs-internal-guid-0954d9f2-7fff-2b3c-4978-1234567890ab"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p></b><br class="Apple-interchange-newline"><!--EndFragment--> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<paragraph>Hello world</paragraph> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p dir="ltr" id="docs-internal-guid-f8b26bf1-7fff-40c0-af18-1234567890ab" style="line-height:1.38;margin-bottom:0pt;margin-top:0pt"><span style="background-color:transparent;color:#000000;font-family:Arial;font-size:11pt;font-style:normal;font-variant:normal;font-weight:400;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Hello world</span></p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p dir="ltr" style="line-height:1.38;margin-bottom:0pt;margin-top:0pt"><span style="background-color:transparent;color:#000000;font-family:Arial;font-size:11pt;font-style:normal;font-variant:normal;font-weight:400;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Hello world</span></p><br class="Apple-interchange-newline"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">ol{margin:0;padding:0}table td,table th{padding:0}.c2{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c1{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c0{background-color:#ffffff;max-width:468pt;padding:72pt 72pt 72pt 72pt}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:20pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#000000;font-size:16pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#434343;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c0"><p class="c1"><span>Hello world</span></p></body></html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta charset="utf-8"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;" id="docs-internal-guid-b205dcb1-7fff-2468-bad5-1234567890ab"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta charset='utf-8'><meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p></b><br class="Apple-interchange-newline"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<paragraph>Hello world</paragraph> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p dir="ltr" id="docs-internal-guid-b205dcb1-7fff-2468-bad5-1234567890ab" style="line-height:1.38;margin-bottom:0pt;margin-top:0pt"><span style="background-color:transparent;color:#000000;font-family:Arial;font-size:11pt;font-style:normal;font-variant:normal;font-weight:400;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Hello world</span></p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p><br class="Apple-interchange-newline"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">ol{margin:0;padding:0}table td,table th{padding:0}.c1{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c0{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c2{background-color:#ffffff;max-width:468pt;padding:72pt 72pt 72pt 72pt}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:20pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#000000;font-size:16pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#434343;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c2"><p class="c0"><span class="c1">Hello world</span></p></body></html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is a common filter - so maybe we should just move it to
removeboldtagwrapper.js
.