Move internal-link detection into the InternalLinkAnnotationWidget

If internal link detection is in the LinkAnnotationInspector, it falls down
when a valid URL which isn't also a valid page title (e.g. percent-encoded
titles) is pasted into the input. This is fixed by moving the detection to the
input's change handler before any validation can occur.

Bug: T119431
Change-Id: I1eb2040dd918fdcc22c28594b5cbad835cf384a8
This commit is contained in:
David Lynch 2016-07-15 13:35:56 -05:00
parent c075a17ff7
commit ef7652fdfc
2 changed files with 24 additions and 18 deletions

View file

@ -111,24 +111,7 @@ ve.ui.MWLinkAnnotationInspector.prototype.isExternal = function () {
* @param {ve.dm.MWInternalLinkAnnotation} annotation Annotation
*/
ve.ui.MWLinkAnnotationInspector.prototype.onInternalLinkChange = function ( annotation ) {
var targetData,
href = annotation ? annotation.getAttribute( 'title' ) : '',
// Have to check that this.getFragment() is defined because parent class's teardown
// invokes setAnnotation( null ) which calls this code after fragment is unset
htmlDoc = this.getFragment() && this.getFragment().getDocument().getHtmlDocument();
if ( htmlDoc && ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( href ) ) {
// Check if the 'external' link is in fact a page on the same wiki
// e.g. http://en.wikipedia.org/wiki/Target -> Target
targetData = ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref(
href,
htmlDoc
);
if ( targetData.isInternal ) {
this.internalAnnotationInput.getTextInputWidget().setValue( targetData.title );
return;
}
}
var href = annotation ? annotation.getAttribute( 'title' ) : '';
if (
!this.allowProtocolInInternal &&

View file

@ -82,3 +82,26 @@ ve.ui.MWInternalLinkAnnotationWidget.prototype.getHref = function () {
var title = ve.ui.MWInternalLinkAnnotationWidget.super.prototype.getHref.call( this );
return mw.util.getUrl( title );
};
/**
* @inheritdoc
*/
ve.ui.MWInternalLinkAnnotationWidget.prototype.onTextChange = function ( value ) {
var targetData,
htmlDoc = this.getElementDocument();
// Specific thing we want to check: has a valid URL for an internal page
// been pasted into here, in which case we want to convert it to just the
// page title. This has to happen /here/ because a URL can reference a
// valid page while not being a valid Title (e.g. if it contains a "%").
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
targetData = ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref(
value,
htmlDoc
);
if ( targetData.isInternal ) {
value = targetData.title;
this.input.query.setValue( targetData.title );
}
}
return ve.ui.MWInternalLinkAnnotationWidget.super.prototype.onTextChange.call( this, value );
};