ソースを参照

Merge branch 'master' into memory-test

Maciej Gołaszewski 5 年 前
コミット
2578d2f41b

+ 0 - 1
package.json

@@ -146,7 +146,6 @@
     "postchangelog": "node ./scripts/release/update-utils-version.js",
     "release:bump-version": "node ./scripts/release/bump-versions.js",
     "release:publish": "node ./scripts/release/publish.js",
-    "release:stable-branches": "bash ./scripts/update-stable-branches.sh",
     "switch-to-dev-dev": "sh ./scripts/switch-to-dev-dev.sh",
     "clean-up-svg-icons": "sh ./scripts/clean-up-svg-icons.sh"
   },

+ 1 - 0
packages/ckeditor5-engine/src/view/renderer.js

@@ -930,6 +930,7 @@ function addInlineFiller( domDocument, domParentOrArray, offset ) {
 function areSimilar( node1, node2 ) {
 	return isNode( node1 ) && isNode( node2 ) &&
 		!isText( node1 ) && !isText( node2 ) &&
+		node1.nodeType !== Node.COMMENT_NODE && node2.nodeType !== Node.COMMENT_NODE &&
 		node1.tagName.toLowerCase() === node2.tagName.toLowerCase();
 }
 

+ 100 - 1
packages/ckeditor5-engine/tests/view/renderer.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* globals document, window, NodeFilter, MutationObserver */
+/* globals document, window, NodeFilter, MutationObserver, HTMLImageElement */
 
 import View from '../../src/view/view';
 import ViewElement from '../../src/view/element';
@@ -305,6 +305,33 @@ describe( 'Renderer', () => {
 			expect( domRoot.childNodes[ 0 ] ).to.equal( domImg );
 		} );
 
+		it( 'should remove any comment from the DOM element', () => {
+			// This comment should be cleared during render.
+			const domComment = document.createComment( 'some comment from the user-land' );
+			domRoot.appendChild( domComment );
+
+			renderer.markToSync( 'children', viewRoot );
+			renderer.render();
+
+			expect( domRoot.childNodes.length ).to.equal( 0 );
+		} );
+
+		// https://github.com/ckeditor/ckeditor5/issues/5734
+		it( 'should remove the comment and add a child element', () => {
+			const viewImg = new ViewElement( viewDocument, 'img' );
+			viewRoot._appendChild( viewImg );
+
+			// This comment should be cleared during render.
+			const domComment = document.createComment( 'some comment from the user-land' );
+			domRoot.appendChild( domComment );
+
+			renderer.markToSync( 'children', viewRoot );
+			renderer.render();
+
+			expect( domRoot.childNodes.length ).to.equal( 1 );
+			expect( domRoot.childNodes[ 0 ] ).to.be.an.instanceof( HTMLImageElement );
+		} );
+
 		it( 'should change element if it is different', () => {
 			const viewImg = new ViewElement( viewDocument, 'img' );
 			viewRoot._appendChild( viewImg );
@@ -2706,6 +2733,78 @@ describe( 'Renderer', () => {
 				expect( mappings.get( domQULB ) ).to.equal( viewQ.getChild( 0 ).getChild( 0 ).getChild( 1 ) );
 			} );
 
+			// https://github.com/ckeditor/ckeditor5/issues/5734
+			it( 'should not rerender DOM when view replaced with the same structure without a comment', () => {
+				const domContent = '' +
+					'<h2>He<i>ading 1</i></h2>' +
+					'<p>Ph <strong>Bold</strong>' +
+						'<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a>' +
+					'</p>' +
+					'<!-- A comment to be ignored -->' +
+					'<blockquote><ul><li>Quoted <strong>item 1</strong></li></ul></blockquote>';
+				const content = '' +
+					'<container:h2>He' +
+						'<attribute:i>ading 1</attribute:i>' +
+					'</container:h2>' +
+					'<container:p>Ph ' +
+						'<attribute:strong>Bold</attribute:strong>' +
+						'<attribute:a href="https://ckeditor.com">' +
+							'<attribute:strong>Lin<attribute:i>k</attribute:i></attribute:strong>' +
+						'</attribute:a>' +
+					'</container:p>' +
+					'<container:blockquote>' +
+						'<container:ul>' +
+							'<container:li>Quoted <attribute:strong>item 1</attribute:strong></container:li>' +
+						'</container:ul>' +
+					'</container:blockquote>';
+
+				domRoot.innerHTML = domContent;
+				viewRoot._appendChild( parse( content ) );
+
+				const viewH = viewRoot.getChild( 0 );
+				const viewP = viewRoot.getChild( 1 );
+				const viewQ = viewRoot.getChild( 2 );
+
+				const domH = domRoot.childNodes[ 0 ];
+				const domHI = domH.childNodes[ 1 ];
+				const domP = domRoot.childNodes[ 1 ];
+				const domPT = domP.childNodes[ 0 ];
+				const domPABI = domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ];
+				const domC = domRoot.childNodes[ 2 ];
+				const domQ = domRoot.childNodes[ 3 ];
+				const domQULB = domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ];
+
+				renderer.markToSync( 'children', viewRoot );
+				renderer.render();
+
+				// Assert the comment is no longer in the content.
+				expect( domRoot.contains( domC ), 'domRoot should not contain the comment' ).to.be.false;
+
+				// Assert content, without the comment.
+				expect( domRoot.innerHTML ).to.equal( '<h2>He<i>ading 1</i></h2><p>Ph <strong>Bold</strong>' +
+					'<a href="https://ckeditor.com"><strong>Lin<i>k</i></strong></a></p><blockquote><ul><li>' +
+					'Quoted <strong>item 1</strong></li></ul></blockquote>' );
+
+				// Assert if other DOM elements did not change.
+				expect( domRoot.childNodes[ 0 ] ).to.equal( domH );
+				expect( domH.childNodes[ 1 ] ).to.equal( domHI );
+				expect( domRoot.childNodes[ 1 ] ).to.equal( domP );
+				expect( domP.childNodes[ 0 ] ).to.equal( domPT );
+				expect( domP.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domPABI );
+				// Note the shifted index of domQ, from 3 to 2.
+				expect( domRoot.childNodes[ 2 ] ).to.equal( domQ );
+				expect( domQ.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 1 ] ).to.equal( domQULB );
+
+				// Assert mappings.
+				const mappings = renderer.domConverter._domToViewMapping;
+				expect( mappings.get( domH ) ).to.equal( viewH );
+				expect( mappings.get( domHI ) ).to.equal( viewH.getChild( 1 ) );
+				expect( mappings.get( domP ) ).to.equal( viewP );
+				expect( mappings.get( domPABI ) ).to.equal( viewP.getChild( 2 ).getChild( 0 ).getChild( 1 ) );
+				expect( mappings.get( domQ ) ).to.equal( viewQ );
+				expect( mappings.get( domQULB ) ).to.equal( viewQ.getChild( 0 ).getChild( 0 ).getChild( 1 ) );
+			} );
+
 			it( 'should not rerender DOM when view replaced with the same structure without first node', () => {
 				const content = '' +
 					'<container:h2>He' +

+ 2 - 2
packages/ckeditor5-link/package.json

@@ -13,6 +13,7 @@
     "@ckeditor/ckeditor5-core": "^19.0.1",
     "@ckeditor/ckeditor5-engine": "^19.0.1",
     "@ckeditor/ckeditor5-ui": "^19.0.1",
+    "@ckeditor/ckeditor5-utils": "^19.0.1",
     "lodash-es": "^4.17.15"
   },
   "devDependencies": {
@@ -25,8 +26,7 @@
     "@ckeditor/ckeditor5-paragraph": "^19.1.0",
     "@ckeditor/ckeditor5-theme-lark": "^19.1.0",
     "@ckeditor/ckeditor5-typing": "^19.0.1",
-    "@ckeditor/ckeditor5-undo": "^19.0.1",
-    "@ckeditor/ckeditor5-utils": "^19.0.1"
+    "@ckeditor/ckeditor5-undo": "^19.0.1"
   },
   "engines": {
     "node": ">=12.0.0",

+ 12 - 3
packages/ckeditor5-link/src/linkcommand.js

@@ -11,6 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import findLinkRange from './findlinkrange';
 import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
+import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The link command. It is used by the {@link module:link/link~Link link feature}.
@@ -57,13 +58,21 @@ export default class LinkCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		this.value = doc.selection.getAttribute( 'linkHref' );
+		const selectedElement = first( doc.selection.getSelectedBlocks() );
+
+		// A check for the `LinkImage` plugin. If the selection contains an element, get values from the element.
+		// Currently the selection reads attributes from text nodes only. See #7429.
+		if ( selectedElement && selectedElement.is( 'image' ) ) {
+			this.value = selectedElement.getAttribute( 'linkHref' );
+			this.isEnabled = model.schema.checkAttribute( selectedElement, 'linkHref' );
+		} else {
+			this.value = doc.selection.getAttribute( 'linkHref' );
+			this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
+		}
 
 		for ( const manualDecorator of this.manualDecorators ) {
 			manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
 		}
-
-		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
 	}
 
 	/**

+ 13 - 1
packages/ckeditor5-link/src/unlinkcommand.js

@@ -9,6 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import findLinkRange from './findlinkrange';
+import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The unlink command. It is used by the {@link module:link/link~Link link plugin}.
@@ -20,7 +21,18 @@ export default class UnlinkCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		this.isEnabled = this.editor.model.document.selection.hasAttribute( 'linkHref' );
+		const model = this.editor.model;
+		const doc = model.document;
+
+		const selectedImage = first( doc.selection.getSelectedBlocks() );
+
+		// A check for the `LinkImage` plugin. If the selection contains an image element, get values from the element.
+		// Currently the selection reads attributes from text nodes only. See #7429.
+		if ( selectedImage && selectedImage.name === 'image' ) {
+			this.isEnabled = model.schema.checkAttribute( selectedImage, 'linkHref' );
+		} else {
+			this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
+		}
 	}
 
 	/**

+ 101 - 18
packages/ckeditor5-link/tests/linkcommand.js

@@ -67,6 +67,56 @@ describe( 'LinkCommand', () => {
 				setData( model, '<x>[foo]</x>' );
 				expect( command.isEnabled ).to.be.false;
 			} );
+
+			describe( 'for images', () => {
+				beforeEach( () => {
+					model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+				} );
+
+				it( 'should be true when an image is selected', () => {
+					setData( model, '[<image linkHref="foo"></image>]' );
+
+					expect( command.isEnabled ).to.be.true;
+				} );
+
+				it( 'should be true when an image and a text are selected', () => {
+					setData( model, '[<image linkHref="foo"></image>Foo]' );
+
+					expect( command.isEnabled ).to.be.true;
+				} );
+
+				it( 'should be true when a text and an image are selected', () => {
+					setData( model, '[Foo<image linkHref="foo"></image>]' );
+
+					expect( command.isEnabled ).to.be.true;
+				} );
+
+				it( 'should be true when two images are selected', () => {
+					setData( model, '[<image linkHref="foo"></image><image linkHref="foo"></image>]' );
+
+					expect( command.isEnabled ).to.be.true;
+				} );
+
+				it( 'should be false when a fake image is selected', () => {
+					model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
+
+					setData( model, '[<fake></fake>]' );
+
+					expect( command.isEnabled ).to.be.false;
+				} );
+
+				it( 'should be false if an image does not accept the `linkHref` attribute in given context', () => {
+					model.schema.addAttributeCheck( ( ctx, attributeName ) => {
+						if ( ctx.endsWith( '$root image' ) && attributeName == 'linkHref' ) {
+							return false;
+						}
+					} );
+
+					setData( model, '[<image></image>]' );
+
+					expect( command.isEnabled ).to.be.false;
+				} );
+			} );
 		} );
 	} );
 
@@ -98,6 +148,38 @@ describe( 'LinkCommand', () => {
 				expect( command.value ).to.be.undefined;
 			} );
 		} );
+
+		describe( 'for images', () => {
+			beforeEach( () => {
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+			} );
+
+			it( 'should read the value from a selected image', () => {
+				setData( model, '[<image linkHref="foo"></image>]' );
+
+				expect( command.value ).to.be.equal( 'foo' );
+			} );
+
+			it( 'should read the value from a selected image and ignore a text node', () => {
+				setData( model, '[<image linkHref="foo"></image><p><$text linkHref="bar">bar</$text>]</p>' );
+
+				expect( command.value ).to.be.equal( 'foo' );
+			} );
+
+			it( 'should read the value from a selected text node and ignore an image', () => {
+				setData( model, '<p>[<$text linkHref="bar">bar</$text></p><image linkHref="foo"></image>]' );
+
+				expect( command.value ).to.be.equal( 'bar' );
+			} );
+
+			it( 'should be undefined when a fake image is selected', () => {
+				model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
+
+				setData( model, '[<fake></fake>]' );
+
+				expect( command.value ).to.be.undefined;
+			} );
+		} );
 	} );
 
 	describe( 'execute()', () => {
@@ -194,73 +276,74 @@ describe( 'LinkCommand', () => {
 			} );
 
 			it( 'should set `linkHref` attribute to allowed elements', () => {
-				model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
 
-				setData( model, '<p>f[oo<img></img>ba]r</p>' );
+				setData( model, '<p>f[oo<image></image>ba]r</p>' );
 
 				expect( command.value ).to.be.undefined;
 
 				command.execute( 'url' );
 
-				expect( getData( model ) )
-					.to.equal( '<p>f[<$text linkHref="url">oo</$text><img linkHref="url"></img><$text linkHref="url">ba</$text>]r</p>' );
+				expect( getData( model ) ).to.equal(
+					'<p>f[<$text linkHref="url">oo</$text><image linkHref="url"></image><$text linkHref="url">ba</$text>]r</p>'
+				);
 				expect( command.value ).to.equal( 'url' );
 			} );
 
 			it( 'should set `linkHref` attribute to nested allowed elements', () => {
-				model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
 				model.schema.register( 'blockQuote', { allowWhere: '$block', allowContentOf: '$root' } );
 
-				setData( model, '<p>foo</p>[<blockQuote><img></img></blockQuote>]<p>bar</p>' );
+				setData( model, '<p>foo</p>[<blockQuote><image></image></blockQuote>]<p>bar</p>' );
 
 				command.execute( 'url' );
 
 				expect( getData( model ) )
-					.to.equal( '<p>foo</p>[<blockQuote><img linkHref="url"></img></blockQuote>]<p>bar</p>' );
+					.to.equal( '<p>foo</p>[<blockQuote><image linkHref="url"></image></blockQuote>]<p>bar</p>' );
 			} );
 
 			it( 'should set `linkHref` attribute to allowed elements on multi-selection', () => {
-				model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
 
-				setData( model, '<p>[<img></img>][<img></img>]</p>' );
+				setData( model, '<p>[<image></image>][<image></image>]</p>' );
 
 				command.execute( 'url' );
 
 				expect( getData( model ) )
-					.to.equal( '<p>[<img linkHref="url"></img>][<img linkHref="url"></img>]</p>' );
+					.to.equal( '<p>[<image linkHref="url"></image>][<image linkHref="url"></image>]</p>' );
 			} );
 
 			it( 'should set `linkHref` attribute to allowed elements and omit disallowed', () => {
-				model.schema.register( 'img', { isBlock: true, allowWhere: '$text' } );
-				model.schema.register( 'caption', { allowIn: 'img' } );
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text' } );
+				model.schema.register( 'caption', { allowIn: 'image' } );
 				model.schema.extend( '$text', { allowIn: 'caption' } );
 
-				setData( model, '<p>f[oo<img><caption>xxx</caption></img>ba]r</p>' );
+				setData( model, '<p>f[oo<image><caption>xxx</caption></image>ba]r</p>' );
 
 				command.execute( 'url' );
 
 				expect( getData( model ) ).to.equal(
 					'<p>' +
 						'f[<$text linkHref="url">oo</$text>' +
-						'<img><caption><$text linkHref="url">xxx</$text></caption></img>' +
+						'<image><caption><$text linkHref="url">xxx</$text></caption></image>' +
 						'<$text linkHref="url">ba</$text>]r' +
 					'</p>'
 				);
 			} );
 
 			it( 'should set `linkHref` attribute to allowed elements and omit their children even if they accept the attribute', () => {
-				model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
-				model.schema.register( 'caption', { allowIn: 'img' } );
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+				model.schema.register( 'caption', { allowIn: 'image' } );
 				model.schema.extend( '$text', { allowIn: 'caption' } );
 
-				setData( model, '<p>f[oo<img><caption>xxx</caption></img>ba]r</p>' );
+				setData( model, '<p>f[oo<image><caption>xxx</caption></image>ba]r</p>' );
 
 				command.execute( 'url' );
 
 				expect( getData( model ) ).to.equal(
 					'<p>' +
 						'f[<$text linkHref="url">oo</$text>' +
-						'<img linkHref="url"><caption>xxx</caption></img>' +
+						'<image linkHref="url"><caption>xxx</caption></image>' +
 						'<$text linkHref="url">ba</$text>]r' +
 					'</p>'
 				);

+ 50 - 0
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -51,6 +51,56 @@ describe( 'UnlinkCommand', () => {
 
 			expect( command.isEnabled ).to.false;
 		} );
+
+		describe( 'for images', () => {
+			beforeEach( () => {
+				model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
+			} );
+
+			it( 'should be true when an image is selected', () => {
+				setData( model, '[<image linkHref="foo"></image>]' );
+
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should be true when an image and a text are selected', () => {
+				setData( model, '[<image linkHref="foo"></image>Foo]' );
+
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should be true when a text and an image are selected', () => {
+				setData( model, '[Foo<image linkHref="foo"></image>]' );
+
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should be true when two images are selected', () => {
+				setData( model, '[<image linkHref="foo"></image><image linkHref="foo"></image>]' );
+
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should be false when a fake image is selected', () => {
+				model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
+
+				setData( model, '[<fake></fake>]' );
+
+				expect( command.isEnabled ).to.be.false;
+			} );
+
+			it( 'should be false if an image does not accept the `linkHref` attribute in given context', () => {
+				model.schema.addAttributeCheck( ( ctx, attributeName ) => {
+					if ( ctx.endsWith( '$root image' ) && attributeName == 'linkHref' ) {
+						return false;
+					}
+				} );
+
+				setData( model, '[<image></image>]' );
+
+				expect( command.isEnabled ).to.be.false;
+			} );
+		} );
 	} );
 
 	describe( 'execute()', () => {

+ 0 - 20
scripts/update-stable-branches.sh

@@ -1,20 +0,0 @@
-#!/bin/bash
-
-# @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
-# For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
-
-set -e
-
-read -p "Are you sure? " -n 1 -r
-echo ""
-
-if [[ $REPLY =~ ^[Yy]$ ]]
-then
-	# Update the `stable` branch in the `ckeditor5` repository.
-	git checkout stable && git merge master && git checkout master
-
-	# Push the `#stable` branch.
-	git push origin stable master
-
-	echo "Success! 🎂"
-fi