Explorar el Código

Disable auto-link where linkHref is not allowed.

Maciej Gołaszewski hace 5 años
padre
commit
dfd5944a61

+ 14 - 4
packages/ckeditor5-link/src/autolink.js

@@ -42,9 +42,9 @@ const URL_REG_EXP = new RegExp(
 			'(www.|(\\S+@))' +
 			// Host & domain names.
 			'((?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.))+' +
-			// TLD identifier name.
-			'(?:[a-z\\u00a1-\\uffff]{2,})' +
-		')' +
+	// TLD identifier name.
+	'(?:[a-z\\u00a1-\\uffff]{2,})' +
+	')' +
 	')$', 'i' );
 
 const URL_GROUP_IN_MATCH = 2;
@@ -213,8 +213,14 @@ export default class AutoLink extends Plugin {
 	 * @private
 	 */
 	_applyAutoLink( url, range ) {
+		const model = this.editor.model;
+
+		if ( !isLinkAllowedOnRange( range, model ) ) {
+			return;
+		}
+
 		// Enqueue change to make undo step.
-		this.editor.model.enqueueChange( writer => {
+		model.enqueueChange( writer => {
 			const linkHrefValue = isEmail( url ) ? `mailto://${ url }` : url;
 
 			writer.setAttribute( 'linkHref', linkHrefValue, range );
@@ -236,3 +242,7 @@ function getUrlAtTextEnd( text ) {
 function isEmail( linkHref ) {
 	return EMAIL_REG_EXP.exec( linkHref );
 }
+
+function isLinkAllowedOnRange( range, model ) {
+	return model.schema.checkAttributeInSelection( model.createSelection( range ), 'linkHref' );
+}

+ 10 - 0
packages/ckeditor5-link/tests/autolink.js

@@ -67,6 +67,16 @@ describe( 'AutoLink', () => {
 			);
 		} );
 
+		it( 'does not add linkHref attribute if linkHref is not allowed', () => {
+			model.schema.addAttributeCheck( () => false ); // Disable all attributes.
+
+			simulateTyping( 'https://www.cksource.com ' );
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>https://www.cksource.com []</paragraph>'
+			);
+		} );
+
 		it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
 			setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );