Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/278048

Change subject: Detect the type of plain pasted links
......................................................................

Detect the type of plain pasted links

Using the same logic we used for converting pasted URLs, detect
internal links by matching their base.

Currently link pasting is still disabled in the VE target, but
has been enabled elsewhere (Flow).

Change-Id: Iebd61abbe1fe82fd18d129e1dbc815ca75f44e87
---
M modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
M modules/ve-mw/tests/dm/ve.dm.mwExample.js
M modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
3 files changed, 99 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/48/278048/1

diff --git a/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js 
b/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
index 5ab1301..a5e6887 100644
--- a/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
+++ b/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js
@@ -39,11 +39,20 @@
        return !rel || rel === 'mw:ExtLink';
 };
 
-ve.dm.MWExternalLinkAnnotation.static.toDataElement = function ( domElements ) 
{
-       // Parent method
-       var dataElement = 
ve.dm.MWExternalLinkAnnotation.super.static.toDataElement.apply( this, 
arguments );
+ve.dm.MWExternalLinkAnnotation.static.toDataElement = function ( domElements, 
converter ) {
+       var dataElement, annotation,
+               rel = domElements[ 0 ].getAttribute( 'rel' );
 
-       dataElement.attributes.rel = domElements[ 0 ].getAttribute( 'rel' );
+       // If a plain link is pasted, auto-convert it to the correct type 
(internal/external)
+       if ( !rel ) {
+               annotation = ve.ui.MWLinkAction.static.getLinkAnnotation( 
domElements[ 0 ].getAttribute( 'href' ), converter.getHtmlDocument() );
+               return annotation.element;
+       }
+
+       // Parent method
+       dataElement = 
ve.dm.MWExternalLinkAnnotation.super.static.toDataElement.apply( this, 
arguments );
+
+       dataElement.attributes.rel = rel;
        return dataElement;
 };
 
diff --git a/modules/ve-mw/tests/dm/ve.dm.mwExample.js 
b/modules/ve-mw/tests/dm/ve.dm.mwExample.js
index 66f67ae..73d8c43 100644
--- a/modules/ve-mw/tests/dm/ve.dm.mwExample.js
+++ b/modules/ve-mw/tests/dm/ve.dm.mwExample.js
@@ -1516,8 +1516,8 @@
                },
                normalizedBody: '<h1></h1>'
        },
-       'Plain links (e.g. on paste) are converted to link/mwExternal': {
-               body: 'Foo<a href="Bar">bar</a>',
+       'Plain external links (e.g. on paste) are converted to 
link/mwExternal': {
+               body: '<a href="http://www.mediawiki.org/";>ab</a>',
                data: [
                        {
                                type: 'paragraph',
@@ -1525,34 +1525,21 @@
                                        generated: 'wrapper'
                                }
                        },
-                       'F', 'o', 'o',
-                       [
-                               'b',
-                               [ {
-                                       type: 'link/mwExternal',
-                                       attributes: {
-                                               href: 'Bar',
-                                               rel: null
-                                       }
-                               } ]
-                       ],
                        [
                                'a',
                                [ {
                                        type: 'link/mwExternal',
                                        attributes: {
-                                               href: 'Bar',
-                                               rel: null
+                                               href: 
'http://www.mediawiki.org/'
                                        }
                                } ]
                        ],
                        [
-                               'r',
+                               'b',
                                [ {
                                        type: 'link/mwExternal',
                                        attributes: {
-                                               href: 'Bar',
-                                               rel: null
+                                               href: 
'http://www.mediawiki.org/'
                                        }
                                } ]
                        ],
@@ -1566,6 +1553,52 @@
                                type: '/internalList'
                        }
                ],
-               normalizedBody: 'Foo<a href="Bar" rel="mw:ExtLink">bar</a>'
+               normalizedBody: '<a href="http://www.mediawiki.org/"; 
rel="mw:ExtLink">ab</a>'
+       },
+       'Plain internal links (e.g. on paste) are converted to 
link/mwInternal': {
+               body: '<a href="' + ve.dm.mwExample.MWInternalLink.absoluteHref 
+ '">ab</a>',
+               data: [
+                       {
+                               type: 'paragraph',
+                               internal: {
+                                       generated: 'wrapper'
+                               }
+                       },
+                       [
+                               'a',
+                               [ {
+                                       type: 'link/mwInternal',
+                                       attributes: {
+                                               title: 'Foo/Bar',
+                                               normalizedTitle: 'Foo/Bar',
+                                               lookupTitle: 'Foo/Bar'
+                                       }
+                               } ]
+                       ],
+                       [
+                               'b',
+                               [ {
+                                       type: 'link/mwInternal',
+                                       attributes: {
+                                               title: 'Foo/Bar',
+                                               normalizedTitle: 'Foo/Bar',
+                                               lookupTitle: 'Foo/Bar'
+                                       }
+                               } ]
+                       ],
+                       {
+                               type: '/paragraph'
+                       },
+                       {
+                               type: 'internalList'
+                       },
+                       {
+                               type: '/internalList'
+                       }
+               ],
+               normalizedBody: '<a href="Foo/Bar" rel="mw:WikiLink">ab</a>',
+               mwConfig: {
+                       wgArticlePath: '/wiki/$1'
+               }
        }
 };
diff --git a/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js 
b/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
index ba5100c..3d29e59 100644
--- a/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
+++ b/modules/ve-mw/ui/actions/ve.ui.MWLinkAction.js
@@ -33,6 +33,38 @@
  */
 ve.ui.MWLinkAction.static.methods = 
ve.ui.MWLinkAction.super.static.methods.concat( [ 'open', 'autolinkMagicLink' ] 
);
 
+/* Static methods */
+
+/**
+ * Get a link annotation from specified link text
+ *
+ * This is a static version of the method that can be used in the converter.
+ *
+ * @param {string} linktext Link text
+ * @param {HTMLDocument} doc Document
+ * @return {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation} The 
annotation to use
+ */
+ve.ui.MWLinkAction.static.getLinkAnnotation = function ( linktext, doc ) {
+       var title, targetData,
+               href = linktext;
+
+       // Is this a "magic link"?
+       if ( ve.dm.MWMagicLinkNode.static.validateContent( linktext ) ) {
+               return ve.dm.MWMagicLinkNode.static.annotationFromContent( 
linktext );
+       }
+       // Is this an internal link?
+       targetData = 
ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref( href, doc );
+       if ( targetData.isInternal ) {
+               title = mw.Title.newFromText( targetData.title );
+               return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( 
title );
+       }
+       // It's an external link.
+       return new ve.dm.MWExternalLinkAnnotation( {
+               type: 'link/mwExternal',
+               attributes: { href: href }
+       } );
+};
+
 /* Methods */
 
 /**
@@ -59,30 +91,10 @@
 /**
  * @method
  * @inheritdoc
- * @return {ve.dm.MWExternalLinkAnnotation} The annotation to use.
+ * @return {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation} The 
annotation to use
  */
 ve.ui.MWLinkAction.prototype.getLinkAnnotation = function ( linktext ) {
-       var title, targetData,
-               href = linktext;
-
-       // Is this a "magic link"?
-       if ( ve.dm.MWMagicLinkNode.static.validateContent( linktext ) ) {
-               return ve.dm.MWMagicLinkNode.static.annotationFromContent( 
linktext );
-       }
-       // Is this an internal link?
-       targetData = 
ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref(
-               href,
-               this.surface.getModel().getDocument().getHtmlDocument()
-       );
-       if ( targetData.isInternal ) {
-               title = mw.Title.newFromText( targetData.title );
-               return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( 
title );
-       }
-       // It's an external link.
-       return new ve.dm.MWExternalLinkAnnotation( {
-               type: 'link/mwExternal',
-               attributes: { href: href }
-       } );
+       return this.constructor.static.getLinkAnnotation( linktext, 
this.surface.getModel().getDocument().getHtmlDocument() );
 };
 
 /**

-- 
To view, visit https://gerrit.wikimedia.org/r/278048
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iebd61abbe1fe82fd18d129e1dbc815ca75f44e87
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to