Cscott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/232389
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.
Change-Id: I3dcd289ed7b565b9162ee671038eeb45449e1215
---
M src/ui/actions/ve.ui.LinkAction.js
1 file changed, 31 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/89/232389/1
diff --git a/src/ui/actions/ve.ui.LinkAction.js
b/src/ui/actions/ve.ui.LinkAction.js
index aebe01d..14c155c 100644
--- a/src/ui/actions/ve.ui.LinkAction.js
+++ b/src/ui/actions/ve.ui.LinkAction.js
@@ -44,12 +44,35 @@
/* 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 +97,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;
}
@@ -128,16 +150,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
}
} );
};
--
To view, visit https://gerrit.wikimedia.org/r/232389
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3dcd289ed7b565b9162ee671038eeb45449e1215
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits