2013-08-03 14:17:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWExtensionNode class.
|
|
|
|
*
|
2017-01-03 16:58:33 +00:00
|
|
|
* @copyright 2011-2017 VisualEditor Team and others; see AUTHORS.txt
|
2013-08-03 14:17:16 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki extension node.
|
|
|
|
*
|
2013-09-24 22:03:11 +00:00
|
|
|
* Configuration options for .update():
|
2014-08-22 20:50:48 +00:00
|
|
|
* - extsrc: override the contents of the tag (string)
|
|
|
|
* - attrs: override the attributes of the tag (object)
|
2013-09-24 22:03:11 +00:00
|
|
|
*
|
2013-08-03 14:17:16 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
2015-03-23 15:49:38 +00:00
|
|
|
* @extends ve.ce.LeafNode
|
2013-08-03 14:17:16 +00:00
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @mixins ve.ce.GeneratedContentNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-05-20 11:56:37 +00:00
|
|
|
ve.ce.MWExtensionNode = function VeCeMWExtensionNode() {
|
2015-03-23 15:49:38 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.ce.MWExtensionNode.super.apply( this, arguments );
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
// Mixin constructors
|
2014-07-24 09:54:22 +00:00
|
|
|
ve.ce.FocusableNode.call( this, this.getFocusableElement() );
|
2013-08-03 14:17:16 +00:00
|
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ce.MWExtensionNode, ve.ce.LeafNode );
|
|
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.FocusableNode );
|
|
|
|
OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.GeneratedContentNode );
|
2013-08-03 14:17:16 +00:00
|
|
|
|
2014-06-25 15:58:40 +00:00
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension renders visible content when empty
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {boolean}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.static.rendersEmpty = false;
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2016-10-28 19:02:36 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc ve.ce.GeneratedContentNode
|
|
|
|
*/
|
2013-09-24 22:03:11 +00:00
|
|
|
ve.ce.MWExtensionNode.prototype.generateContents = function ( config ) {
|
2015-06-08 15:03:47 +00:00
|
|
|
var xhr, attr, wikitext,
|
2013-10-17 11:39:27 +00:00
|
|
|
deferred = $.Deferred(),
|
2015-10-01 12:32:38 +00:00
|
|
|
mwData = ve.copy( this.getModel().getAttribute( 'mw' ) ),
|
2013-09-24 22:03:11 +00:00
|
|
|
extsrc = config && config.extsrc !== undefined ? config.extsrc : mwData.body.extsrc,
|
|
|
|
attrs = config && config.attrs || mwData.attrs,
|
2015-06-08 15:03:47 +00:00
|
|
|
tagName = this.getModel().getExtensionName();
|
|
|
|
|
|
|
|
// undefined means omit the attribute, not convert it to string 'undefined'
|
2015-10-01 12:32:38 +00:00
|
|
|
for ( attr in attrs ) {
|
|
|
|
if ( attrs[ attr ] === undefined ) {
|
|
|
|
delete attrs[ attr ];
|
2015-06-08 15:03:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
|
|
|
|
wikitext = mw.html.element( tagName, attrs, new mw.html.Raw( extsrc ) );
|
2013-08-15 09:40:58 +00:00
|
|
|
|
2016-04-01 12:10:48 +00:00
|
|
|
if ( this.constructor.static.rendersEmpty || extsrc.trim() !== '' ) {
|
2015-01-24 00:22:17 +00:00
|
|
|
xhr = new mw.Api().post( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'visualeditor',
|
|
|
|
paction: 'parsefragment',
|
|
|
|
page: mw.config.get( 'wgRelevantPageName' ),
|
|
|
|
wikitext: wikitext
|
2015-01-24 00:22:17 +00:00
|
|
|
} )
|
2014-07-08 22:33:32 +00:00
|
|
|
.done( this.onParseSuccess.bind( this, deferred ) )
|
|
|
|
.fail( this.onParseError.bind( this, deferred ) );
|
2014-06-25 15:58:40 +00:00
|
|
|
return deferred.promise( { abort: xhr.abort } );
|
|
|
|
} else {
|
|
|
|
deferred.resolve( $( '<span> </span>' ).get() );
|
|
|
|
return deferred.promise();
|
|
|
|
}
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
2017-03-30 16:37:54 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.prototype.getRenderedDomElements = function () {
|
|
|
|
// Parent method
|
|
|
|
var elements = ve.ce.GeneratedContentNode.prototype.getRenderedDomElements.apply( this, arguments );
|
|
|
|
|
|
|
|
if ( this.getModelHtmlDocument() ) {
|
|
|
|
ve.init.platform.linkCache.styleParsoidElements(
|
|
|
|
$( elements ),
|
|
|
|
this.getModelHtmlDocument()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
};
|
|
|
|
|
2013-08-03 14:17:16 +00:00
|
|
|
/**
|
|
|
|
* Handle a successful response from the parser for the wikitext fragment.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The Deferred object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.prototype.onParseSuccess = function ( deferred, response ) {
|
2015-01-31 00:41:37 +00:00
|
|
|
var data = response.visualeditor,
|
2015-04-09 23:47:15 +00:00
|
|
|
contentNodes = $( data.content ).get();
|
2013-08-03 14:17:16 +00:00
|
|
|
deferred.resolve( contentNodes );
|
2013-09-26 19:35:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** */
|
|
|
|
ve.ce.MWExtensionNode.prototype.afterRender = function () {
|
2015-08-26 11:03:44 +00:00
|
|
|
var node = this,
|
2015-10-02 13:18:13 +00:00
|
|
|
$images = this.$element
|
|
|
|
.find( 'img:not([width]),img:not([height])' )
|
|
|
|
.addBack( 'img:not([width]),img:not([height])' );
|
2015-08-26 11:03:44 +00:00
|
|
|
|
2015-09-30 20:41:34 +00:00
|
|
|
// Mixin method
|
|
|
|
ve.ce.GeneratedContentNode.prototype.afterRender.call( this );
|
|
|
|
|
2015-08-26 11:03:44 +00:00
|
|
|
// Images missing a dimension change size after load
|
|
|
|
// TODO: Ignore images which have dimensions defined in CSS, if performant
|
|
|
|
if ( $images.length ) {
|
|
|
|
$images.on( 'load', function () {
|
|
|
|
// Mixin method
|
|
|
|
ve.ce.GeneratedContentNode.prototype.afterRender.call( node );
|
|
|
|
} );
|
|
|
|
}
|
2013-08-03 14:17:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an unsuccessful response from the parser for the wikitext fragment.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The promise object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWExtensionNode.prototype.onParseError = function ( deferred ) {
|
|
|
|
deferred.reject();
|
|
|
|
};
|
2014-05-20 11:56:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki inline extension node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2015-03-23 15:49:38 +00:00
|
|
|
* @extends ve.ce.MWExtensionNode
|
2014-05-20 11:56:37 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWInlineExtensionNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2015-03-23 15:49:38 +00:00
|
|
|
ve.ce.MWInlineExtensionNode = function VeCeMWInlineExtensionNode() {
|
2014-05-20 11:56:37 +00:00
|
|
|
// Parent constructor
|
2015-03-23 15:49:38 +00:00
|
|
|
ve.ce.MWInlineExtensionNode.super.apply( this, arguments );
|
2014-05-20 11:56:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2015-03-23 15:49:38 +00:00
|
|
|
OO.inheritClass( ve.ce.MWInlineExtensionNode, ve.ce.MWExtensionNode );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWInlineExtensionNode.prototype.onParseSuccess = function ( deferred, response ) {
|
|
|
|
var data = response.visualeditor,
|
2015-09-29 02:36:48 +00:00
|
|
|
contentNodes = $.parseHTML( data.content );
|
2014-05-20 11:56:37 +00:00
|
|
|
|
2015-09-29 02:36:48 +00:00
|
|
|
// Inline nodes may come back in a wrapper paragraph; in that case, unwrap it
|
2015-09-29 16:35:23 +00:00
|
|
|
if ( contentNodes.length === 1 && contentNodes[ 0 ].nodeName === 'P' ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
contentNodes = Array.prototype.slice.apply( contentNodes[ 0 ].childNodes );
|
2015-03-23 15:49:38 +00:00
|
|
|
}
|
|
|
|
deferred.resolve( contentNodes );
|
|
|
|
};
|
2014-05-20 11:56:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki block extension node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2015-03-23 15:49:38 +00:00
|
|
|
* @extends ve.ce.MWExtensionNode
|
2014-05-20 11:56:37 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWBlockExtensionNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2015-03-23 15:49:38 +00:00
|
|
|
ve.ce.MWBlockExtensionNode = function VeCeMWBlockExtensionNode() {
|
2014-05-20 11:56:37 +00:00
|
|
|
// Parent constructor
|
2015-03-23 15:49:38 +00:00
|
|
|
ve.ce.MWBlockExtensionNode.super.apply( this, arguments );
|
2014-05-20 11:56:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2015-03-23 15:49:38 +00:00
|
|
|
OO.inheritClass( ve.ce.MWBlockExtensionNode, ve.ce.MWExtensionNode );
|