Skip to content

Commit

Permalink
Merge pull request #15231 from ckeditor/ck/15036-unexpected-html-esca…
Browse files Browse the repository at this point in the history
…pe-characters

Fix (clipboard): Pasting a link address should not convert its parts that look like HTML entities. Closes #15036.
  • Loading branch information
niegowski authored Oct 25, 2023
2 parents f67e41b + 9123b4a commit 89eb065
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ckeditor5-clipboard/src/utils/plaintexttohtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
export default function plainTextToHtml( text: string ): string {
text = text
// Encode &.
.replace( /&/g, '&' )
// Encode <>.
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
Expand Down
27 changes: 27 additions & 0 deletions packages/ckeditor5-clipboard/tests/pasting-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ describe( 'Pasting – integration', () => {
} );
} );
} );

describe( 'links', () => {
// See https://github.com/ckeditor/ckeditor5/issues/15036.
it( 'should not convert parts of the link address which look like HTML entities', () => {
return ClassicTestEditor
.create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } )
.then( editor => {
setData( editor.model, '<paragraph>[]</paragraph>' );

pasteText( editor, 'https://example.com?x=1&quot=2&timestamp=t' );

expect( getData( editor.model ) ).to.equal(
'<paragraph>https://example.com?x=1&quot=2&timestamp=t[]</paragraph>' // keeps "&quot" and "&times" unchanged
);

return editor.destroy();
} );
} );
} );
} );

function pasteHtml( editor, html ) {
Expand All @@ -201,6 +220,14 @@ function pasteHtml( editor, html ) {
} );
}

function pasteText( editor, text ) {
editor.editing.view.document.fire( 'paste', {
dataTransfer: createDataTransfer( { 'text/plain': text } ),
stopPropagation() {},
preventDefault() {}
} );
}

function createDataTransfer( data ) {
return {
getData( type ) {
Expand Down
4 changes: 4 additions & 0 deletions packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe( 'plainTextToHtml()', () => {
expect( plainTextToHtml( 'x y <z>' ) ).to.equal( 'x y &lt;z&gt;' );
} );

it( 'encodes &', () => {
expect( plainTextToHtml( 'x=1&y=2&z=3' ) ).to.equal( 'x=1&amp;y=2&amp;z=3' );
} );

it( 'turns double line breaks into paragraphs (Linux/Mac EOL style)', () => {
expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '<p>x</p><p>y</p><p>z</p>' );
} );
Expand Down

0 comments on commit 89eb065

Please sign in to comment.