forked from ckeditor/ckeditor5
-
Notifications
You must be signed in to change notification settings - Fork 0
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 ckeditor#14136 from ckeditor/ck/14131-remove-style…
…-tag-when-pasting-table-from-google-sheets Fix (paste-from-office): Remove the `<style>` tag that comes with the table pasted from Google Sheets. Closes ckeditor#14131.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/ckeditor5-paste-from-office/src/filters/removestyleblock.ts
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,23 @@ | ||
/** | ||
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paste-from-office/filters/removestyleblock | ||
*/ | ||
|
||
import type { UpcastWriter, ViewDocumentFragment } from 'ckeditor5/src/engine'; | ||
|
||
/** | ||
* Removes `<style>` block added by Google Sheets to a copied content. | ||
* | ||
* @param documentFragment element `data.content` obtained from clipboard | ||
*/ | ||
export default function removeStyleBlock( documentFragment: ViewDocumentFragment, writer: UpcastWriter ): void { | ||
for ( const child of Array.from( documentFragment.getChildren() ) ) { | ||
if ( child.is( 'element', 'style' ) ) { | ||
writer.remove( child ); | ||
} | ||
} | ||
} |
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
File renamed without changes.
80 changes: 80 additions & 0 deletions
80
packages/ckeditor5-paste-from-office/tests/filters/removestyleblock.js
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,80 @@ | ||
/** | ||
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor'; | ||
import removeStyleBlock from '../../src/filters/removestyleblock'; | ||
import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter'; | ||
import Document from '@ckeditor/ckeditor5-engine/src/view/document'; | ||
import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap'; | ||
|
||
describe( 'PasteFromOffice - filters', () => { | ||
const htmlDataProcessor = new HtmlDataProcessor( new Document( new StylesProcessor() ) ); | ||
|
||
describe( 'removeStyleBlock', () => { | ||
let writer, viewDocument; | ||
|
||
before( () => { | ||
viewDocument = new Document(); | ||
writer = new UpcastWriter( viewDocument ); | ||
} ); | ||
|
||
it( 'should remove <style> element', () => { | ||
const inputData = | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>' + | ||
'<table>' + | ||
'<tbody>' + | ||
'<tr>' + | ||
'<td>123</td>' + | ||
'</tr>' + | ||
'</tbody>' + | ||
'</table>'; | ||
|
||
const documentFragment = htmlDataProcessor.toView( inputData ); | ||
|
||
removeStyleBlock( documentFragment, writer ); | ||
|
||
expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<table><tbody><tr><td>123</td></tr></tbody></table>' ); | ||
} ); | ||
|
||
it( 'works with multiple consecutive <style> tags', () => { | ||
const inputData = | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>' + | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>' + | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>' + | ||
'<table>' + | ||
'<tbody>' + | ||
'<tr>' + | ||
'<td>123</td>' + | ||
'</tr>' + | ||
'</tbody>' + | ||
'</table>'; | ||
|
||
const documentFragment = htmlDataProcessor.toView( inputData ); | ||
|
||
removeStyleBlock( documentFragment, writer ); | ||
|
||
expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<table><tbody><tr><td>123</td></tr></tbody></table>' ); | ||
} ); | ||
|
||
it( 'works with multiple non-consecutive <style> tags', () => { | ||
const inputData = | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>' + | ||
'<table>' + | ||
'<tbody>' + | ||
'<tr>' + | ||
'<td>123</td>' + | ||
'</tr>' + | ||
'</tbody>' + | ||
'</table>' + | ||
'<style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style>'; | ||
|
||
const documentFragment = htmlDataProcessor.toView( inputData ); | ||
|
||
removeStyleBlock( documentFragment, writer ); | ||
|
||
expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<table><tbody><tr><td>123</td></tr></tbody></table>' ); | ||
} ); | ||
} ); | ||
} ); |