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 8
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 #18 from ckeditor/t/11
Fix: Plain text data is now available in clipboard when copying or cutting editor contents. Closes #11.
- Loading branch information
Showing
5 changed files
with
219 additions
and
6 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
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,53 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module clipboard/utils/viewtoplaintext | ||
*/ | ||
|
||
/** | ||
* Deeply converts {@link module:engine/model/view/item view item} to plain text. | ||
* | ||
* @param {module:engine/model/view/item} viewItem View item to convert. | ||
* @returns {String} Plain text representation of `viewItem`. | ||
*/ | ||
export default function viewToPlainText( viewItem ) { | ||
let text = ''; | ||
|
||
if ( viewItem.is( 'text' ) || viewItem.is( 'textProxy' ) ) { | ||
// If item is `Text` or `TextProxy` simple take its text data. | ||
text = viewItem.data; | ||
} else if ( viewItem.is( 'img' ) && viewItem.hasAttribute( 'alt' ) ) { | ||
// Special case for images - use alt attribute if it is provided. | ||
text = viewItem.getAttribute( 'alt' ); | ||
} else { | ||
// Other elements are document fragments, attribute elements or container elements. | ||
// They don't have their own text value, so convert their children. | ||
let prev = null; | ||
|
||
for ( let child of viewItem.getChildren() ) { | ||
const childText = viewToPlainText( child ); | ||
|
||
// Separate container element children with one or more new-line characters. | ||
if ( prev && ( prev.is( 'containerElement' ) || child.is( 'containerElement' ) ) ) { | ||
if ( smallPaddingElements.includes( prev.name ) || smallPaddingElements.includes( child.name ) ) { | ||
text += '\n'; | ||
} else { | ||
text += '\n\n'; | ||
} | ||
} | ||
|
||
text += childText; | ||
prev = child; | ||
} | ||
} | ||
|
||
return text; | ||
} | ||
|
||
// Elements which should not have empty-line padding. | ||
// Most `view.ContainerElement` want to be separate by new-line, but some are creating one structure | ||
// together (like `<li>`) so it is better to separate them by only one "\n". | ||
const smallPaddingElements = [ 'figcaption', 'li' ]; |
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
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,72 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import viewToPlainText from '../../src/utils/viewtoplaintext'; | ||
|
||
import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view'; | ||
|
||
describe( 'viewToPlainText', () => { | ||
function test( viewString, expectedText ) { | ||
const view = parseView( viewString ); | ||
const text = viewToPlainText( view ); | ||
|
||
expect( text ).to.equal( expectedText ); | ||
} | ||
|
||
it( 'should output text contents of given view', () => { | ||
test( | ||
'<container:p>Foo<strong>Bar</strong>Xyz</container:p>', | ||
'FooBarXyz' | ||
); | ||
} ); | ||
|
||
it( 'should put empty line between container elements', () => { | ||
test( | ||
'<container:h1>Header</container:h1>' + | ||
'<container:p>Foo</container:p>' + | ||
'<container:p>Bar</container:p>' + | ||
'Abc' + | ||
'<container:div>Xyz</container:div>', | ||
|
||
'Header\n\nFoo\n\nBar\n\nAbc\n\nXyz' | ||
); | ||
} ); | ||
|
||
it( 'should output alt attribute of image elements', () => { | ||
test( | ||
'<container:p>Foo</container:p>' + | ||
'<img src="foo.jpg" alt="Alt" />', | ||
|
||
'Foo\n\nAlt' | ||
); | ||
} ); | ||
|
||
it( 'should not put empty line after li (if not needed)', () => { | ||
test( | ||
'<container:p>Foo</container:p>' + | ||
'<container:ul>' + | ||
'<container:li>A</container:li>' + | ||
'<container:li>B</container:li>' + | ||
'<container:li>C</container:li>' + | ||
'</container:ul>' + | ||
'<container:p>Bar</container:p>', | ||
|
||
'Foo\n\nA\nB\nC\n\nBar' | ||
); | ||
} ); | ||
|
||
it( 'should not put empty line before/after figcaption (if not needed)', () => { | ||
test( | ||
'<container:p>Foo</container:p>' + | ||
'<container:figure>' + | ||
'<img src="foo.jpg" alt="Alt" />' + | ||
'<container:figcaption>Caption</container:figcaption>' + | ||
'</container:figure>' + | ||
'<container:p>Bar</container:p>', | ||
|
||
'Foo\n\nAlt\nCaption\n\nBar' | ||
); | ||
} ); | ||
} ); |