This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from ckeditor/t/61
Feature: Prevent of bolding entire content pasted from Google Docs. Closes #61.
- Loading branch information
Showing
31 changed files
with
770 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @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/removeboldwrapper | ||
*/ | ||
|
||
/** | ||
* Removes `<b>` tag wrapper added by Google Docs to a copied content. | ||
* | ||
* @param {module:engine/view/documentfragment~DocumentFragment} documentFragment element `data.content` obtained from clipboard | ||
* @param {module:engine/view/upcastwriter~UpcastWriter} writer | ||
*/ | ||
export default function removeBoldWrapper( documentFragment, writer ) { | ||
for ( const child of documentFragment.getChildren() ) { | ||
if ( child.is( 'b' ) && child.getStyle( 'font-weight' ) === 'normal' ) { | ||
const childIndex = documentFragment.getChildIndex( child ); | ||
|
||
writer.remove( child ); | ||
writer.insertChild( childIndex, child.getChildren(), documentFragment ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 #execute | ||
* @param {Object} data object obtained from | ||
* {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @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/normalizers/googledocsnormalizer | ||
*/ | ||
|
||
import removeBoldWrapper from '../filters/removeboldwrapper'; | ||
import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter'; | ||
|
||
const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i; | ||
|
||
/** | ||
* Normalizer for the content pasted from Google Docs. | ||
* | ||
* @implements module:paste-from-office/normalizer~Normalizer | ||
*/ | ||
export default class GoogleDocsNormalizer { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
isActive( htmlString ) { | ||
return googleDocsMatch.test( htmlString ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
execute( data ) { | ||
const writer = new UpcastWriter(); | ||
|
||
removeBoldWrapper( data.content, writer ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @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/normalizers/mswordnormalizer | ||
*/ | ||
|
||
import { parseHtml } from '../filters/parse'; | ||
import { transformListItemLikeElementsIntoLists } from '../filters/list'; | ||
import { replaceImagesSourceWithBase64 } from '../filters/image'; | ||
|
||
const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i; | ||
const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i; | ||
|
||
/** | ||
* Normalizer for the content pasted from Microsoft Word. | ||
* | ||
* @implements module:paste-from-office/normalizer~Normalizer | ||
*/ | ||
export default class MSWordNormalizer { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
isActive( htmlString ) { | ||
return msWordMatch1.test( htmlString ) || msWordMatch2.test( htmlString ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
execute( data ) { | ||
const { body, stylesString } = parseHtml( data.dataTransfer.getData( 'text/html' ) ); | ||
|
||
transformListItemLikeElementsIntoLists( body, stylesString ); | ||
replaceImagesSourceWithBase64( body, data.dataTransfer.getData( 'text/rtf' ) ); | ||
|
||
data.content = body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/input.firefox.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/input.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/model.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<paragraph>Hello world</paragraph> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/normalized.firefox.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/normalized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text-windows/simple-text-windows.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/input.firefox.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/input.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/model.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<paragraph>Hello world</paragraph> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/normalized.firefox.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/normalized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
1 change: 1 addition & 0 deletions
1
tests/_data/paste-from-google-docs/bold-wrapper/simple-text/simple-text.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.