jenkins-bot has submitted this change and it was merged.
Change subject: Factor out ve.ui.LinkAction#autolink
......................................................................
Factor out ve.ui.LinkAction#autolink
This refactoring allows us to reuse the autolink machinery to link
other types of content in subclasses of ve.ui.LinkAction.
Also improved the test suite for ve.ui.LinkAction, making it closer
to ve.ui.UrlStringTransferHandler.test.js.
Change-Id: I3dcd289ed7b565b9162ee671038eeb45449e1215
---
M src/ui/actions/ve.ui.LinkAction.js
M tests/ui/actions/ve.ui.LinkAction.test.js
2 files changed, 58 insertions(+), 25 deletions(-)
Approvals:
Jforrester: Looks good to me, but someone else must approve
Esanders: Looks good to me, approved
jenkins-bot: Verified
diff --git a/src/ui/actions/ve.ui.LinkAction.js
b/src/ui/actions/ve.ui.LinkAction.js
index 476a9eb..9ef993c 100644
--- a/src/ui/actions/ve.ui.LinkAction.js
+++ b/src/ui/actions/ve.ui.LinkAction.js
@@ -44,12 +44,36 @@
/* Methods */
/**
- * Autolink the selection (which may have trailing whitespace).
+ * Autolink the selected URL (which may have trailing whitespace).
*
* @method
- * @return {boolean} Action was executed
+ * @return {boolean}
+ * True if the selection is a valid URL and the autolink action was
+ * executed; otherwise false.
*/
ve.ui.LinkAction.prototype.autolinkUrl = function () {
+ return this.autolink( function ( linktext ) {
+ // Make sure we still have a real URL after trail removal, and
not
+ // a bare protocol (or no protocol at all, if we stripped the
last
+ // colon from the protocol)
+ return ve.ui.LinkAction.static.autolinkRegExp.test( linktext +
' ' );
+ } );
+};
+
+/**
+ * Autolink the selection, which may have trailing whitespace.
+ *
+ * @method
+ * @private
+ * @param {Function} validateFunc
+ * A function used to validate the given linktext.
+ * @param {string} validateFunc.linktext
+ * Linktext with trailing whitespace and punctuation stripped.
+ * @param {boolean} validateFunc.return
+ * True iff the given linktext is valid. If false, no linking will be done.
+ * @return {boolean} Selection was valid and link action was executed.
+ */
+ve.ui.LinkAction.prototype.autolink = function ( validateFunc ) {
var range, rangeEnd, linktext, i,
surfaceModel = this.surface.getModel(),
documentModel = surfaceModel.getDocument(),
@@ -74,9 +98,8 @@
// Eliminate trailing punctuation.
linktext = linktext.replace( this.getTrailingPunctuation( linktext ),
'' );
- // Make sure we still have a real URL after trail removal, and not
- // a bare protocol (or no protocol at all, if we stripped the last
colon)
- if ( !ve.ui.LinkAction.static.autolinkRegExp.test( linktext + ' ' ) ) {
+ // Validate the stripped text.
+ if ( !validateFunc( linktext ) ) {
// Don't autolink this.
return false;
}
@@ -129,16 +152,17 @@
};
/**
- * Return an appropriate annotation for the given href.
+ * Return an appropriate annotation for the given link text.
*
* @method
+ * @param {string} linktext The link text to annotate.
* @return {ve.dm.LinkAnnotation} The annotation to use.
*/
-ve.ui.LinkAction.prototype.getLinkAnnotation = function ( href ) {
+ve.ui.LinkAction.prototype.getLinkAnnotation = function ( linktext ) {
return new ve.dm.LinkAnnotation( {
type: 'link',
attributes: {
- href: href
+ href: linktext
}
} );
};
diff --git a/tests/ui/actions/ve.ui.LinkAction.test.js
b/tests/ui/actions/ve.ui.LinkAction.test.js
index 4643642..baa10d1 100644
--- a/tests/ui/actions/ve.ui.LinkAction.test.js
+++ b/tests/ui/actions/ve.ui.LinkAction.test.js
@@ -9,14 +9,17 @@
/* Tests */
function runAutolinkTest( assert, html, method, range, expectedRange,
expectedData, expectedOriginalData, msg ) {
- var status,
+ var status, actualData,
expectFail = /^Don't/.test( msg ),
surface = ve.test.utils.createModelOnlySurfaceFromHtml( html ||
ve.dm.example.html ),
linkAction = new ve.ui.LinkAction( surface ),
data = ve.copy( surface.getModel().getDocument().getFullData()
),
- originalData = ve.copy( data );
+ originalData = ve.copy( data ),
+ makeLinkAnnotation = function ( linktext ) {
+ return linkAction.getLinkAnnotation( linktext ).element;
+ };
- expectedData( data );
+ expectedData( data, makeLinkAnnotation );
if ( expectedOriginalData ) {
expectedOriginalData( originalData );
}
@@ -24,7 +27,9 @@
status = linkAction[ method ]();
assert.equal( status, !expectFail, msg + ': action return value' );
- assert.equalLinearData( surface.getModel().getDocument().getFullData(),
data, msg + ': data models match' );
+ actualData = surface.getModel().getDocument().getFullData();
+ ve.dm.example.postprocessAnnotations( actualData,
surface.getModel().getDocument().getStore() );
+ assert.equalLinearData( actualData, data, msg + ': data models match' );
assert.equalRange( surface.getModel().getSelection().getRange(),
expectedRange, msg + ': ranges match' );
if ( status ) {
@@ -43,10 +48,11 @@
range: new ve.Range( 1, 20 ),
method: 'autolinkUrl',
expectedRange: new ve.Range( 20, 20 ),
- expectedData: function ( data ) {
- var i;
+ expectedData: function ( data, makeAnnotation )
{
+ var i,
+ a = makeAnnotation(
'http://example.com' );
for ( i = 1; i < 19; i++ ) {
- data[ i ] = [ data[ i ], [ 0 ]
];
+ data[ i ] = [ data[ i ], [ a ]
];
}
},
msg: 'Autolink after space'
@@ -56,10 +62,11 @@
range: new ve.Range( 1, 21 ),
method: 'autolinkUrl',
expectedRange: new ve.Range( 21, 21 ),
- expectedData: function ( data ) {
- var i;
+ expectedData: function ( data, makeAnnotation )
{
+ var i,
+ a = makeAnnotation(
'http://example.com' );
for ( i = 1; i < 19; i++ ) {
- data[ i ] = [ data[ i ], [ 0 ]
];
+ data[ i ] = [ data[ i ], [ a ]
];
}
},
msg: 'Autolink after newline'
@@ -69,10 +76,11 @@
range: new ve.Range( 1, 20 ),
method: 'autolinkUrl',
expectedRange: new ve.Range( 20, 20 ),
- expectedData: function ( data ) {
- var i;
+ expectedData: function ( data, makeAnnotation )
{
+ var i,
+ a = makeAnnotation(
'Http://Example.COm' );
for ( i = 1; i < 19; i++ ) {
- data[ i ] = [ data[ i ], [ 0 ]
];
+ data[ i ] = [ data[ i ], [ a ]
];
}
},
msg: 'Autolink with mixed case'
@@ -82,10 +90,11 @@
range: new ve.Range( 1, 22 ),
method: 'autolinkUrl',
expectedRange: new ve.Range( 22, 22 ),
- expectedData: function ( data ) {
- var i;
+ expectedData: function ( data, makeAnnotation )
{
+ var i,
+ a = makeAnnotation(
'http://example.com' );
for ( i = 1; i < 19; i++ ) {
- data[ i ] = [ data[ i ], [ 0 ]
];
+ data[ i ] = [ data[ i ], [ a ]
];
}
},
msg: 'Strip trailing punctuation'
@@ -95,7 +104,7 @@
range: new ve.Range( 1, 11 ),
method: 'autolinkUrl',
expectedRange: new ve.Range( 1, 11 ),
- expectedData: function ( /*data*/ ) {
+ expectedData: function ( /*data,
makeAnnotation*/ ) {
/* no change, no link */
},
msg: 'Don\'t link if stripping leaves bare
protocol'
--
To view, visit https://gerrit.wikimedia.org/r/232389
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I3dcd289ed7b565b9162ee671038eeb45449e1215
Gerrit-PatchSet: 6
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits