https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113045
Revision: 113045
Author: gwicke
Date: 2012-03-05 16:23:00 +0000 (Mon, 05 Mar 2012)
Log Message:
-----------
Support external images wrapped in a clickable link using bracketed external
link syntax. 265 tests passing.
Modified Paths:
--------------
trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
Modified: trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
2012-03-05 15:53:25 UTC (rev 113044)
+++ trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
2012-03-05 16:23:00 UTC (rev 113045)
@@ -45,7 +45,7 @@
//console.warn('content: ' + JSON.stringify( content, null, 2 )
);
// XXX: handle trail
if ( content.length ) {
- var out = []
+ var out = [];
for ( var i = 0, l = content.length; i < l ; i++ ) {
out = out.concat( content[i].v );
if ( i < l - 1 ) {
@@ -61,9 +61,9 @@
}
obj.attribs.push( new KV('data-mw-type', 'internal') );
- var out = [obj].concat( content, new EndTagTk( 'a' ) );
- //console.warn( JSON.stringify( out, null, 2 ) );
- return { tokens: out };
+ return {
+ tokens: [obj].concat( content, new EndTagTk( 'a' ) )
+ };
}
};
@@ -124,7 +124,7 @@
var oContent = content[i],
oText = manager.env.tokensToString( oContent, true );
if ( oText.constructor === String ) {
- var oText = oText.trim();
+ oText = oText.trim();
if ( this._simpleImageOptions[ oText ] ) {
options.push( new KV( this._simpleImageOptions[
oText ],
oText ) );
@@ -193,7 +193,7 @@
this.manager = manager;
this.manager.addTransform( this.onUrlLink.bind( this ), this.rank,
'tag', 'urllink' );
this.manager.addTransform( this.onExtLink.bind( this ),
- this.rank, 'tag', 'extlink' );
+ this.rank - 0.001, 'tag', 'extlink' );
// create a new peg parser for image options..
if ( !this.imageParser ) {
// Actually the regular tokenizer, but we'll call it with the
@@ -213,7 +213,7 @@
var bits = href.split( '.' );
return bits.length > 1 &&
this._imageExtensions[ bits[bits.length - 1] ] &&
- href.substr(0, 4) === 'http';
+ href.match( /^https?:\/\// );
};
ExternalLinkHandler.prototype.onUrlLink = function ( token, manager, cb ) {
@@ -222,7 +222,7 @@
return { token: new SelfclosingTagTk( 'img',
[
new KV( 'alt', href.split('/').last() ),
- new KV( 'src', href ),
+ new KV( 'src', href )
]
)
};
@@ -240,9 +240,24 @@
// Bracketed external link
ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) {
var href = this.manager.env.lookupKV( token.attribs, 'href' ).v,
- content= this.manager.env.lookupKV( token.attribs, 'content' ).v;
+ content= this.manager.env.lookupKV( token.attribs, 'content'
).v;
+ //console.warn( 'content: ' + JSON.stringify( content, null, 2 ) );
// validate the href
if ( this.imageParser.parseURL( href ) ) {
+ if ( content.length === 1 &&
+ content[0].constructor === String &&
+ this.imageParser.parseURL( content[0] ) &&
+ this._isImageLink( content[0] ) )
+ {
+ var src = content[0];
+ content = [ new SelfclosingTagTk( 'img',
+ [
+ new KV( 'alt', src.split('/').last() ),
+ new KV( 'src', src )
+ ] )
+ ];
+ }
+
return {
tokens:
[
@@ -250,7 +265,7 @@
new TagTk( 'a', [
new KV('href',
href),
new
KV('data-mw-type', 'external')
- ] ),
+ ] )
].concat( content, [ new EndTagTk( 'a' )])
};
} else {
Modified:
trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
===================================================================
---
trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
2012-03-05 15:53:25 UTC (rev 113044)
+++
trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
2012-03-05 16:23:00 UTC (rev 113045)
@@ -196,7 +196,7 @@
// strip comments and newlines
} else {
if ( strict ) {
- return [out.join(''), null];
+ return [out.join(''), tokens.slice( i )];
}
var tstring = JSON.stringify( token );
this.dp ( 'MWParserEnvironment.tokensToString, non-text
token: ' +
Modified: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
2012-03-05 15:53:25 UTC (rev 113044)
+++ trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
2012-03-05 16:23:00 UTC (rev 113045)
@@ -585,6 +585,7 @@
text = [ "[" + linkCount + "]" ];
linkCount++;
}
+ //console.warn( 'extlink text: ' + pp( text ) );
return [
new SelfclosingTagTk( 'extlink', [
new KV('href', target),
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs