mediawiki-extensions-Visual.../modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js
Roan Kattouw 92c38eab85 The great directory split of 2013
Move all MW-specific files into the ve-mw directory, in preparation
for moving them out into a separate repo.

All MW-specific files were moved into a parallel directory structure
in modules/ve-mw . Files with both generic and MW-specific things were
split up. Files in ve/init/mw/ were moved to ve-mw/init/ rather than
ve-mw/init/mw ; they're still named ve.init.mw.* but we should change
that. Some of the test files for core classes had MW-specific test cases,
so those were split up and the test runner was duplicated; we should
refactor our tests to use data providers so we can add cases more easily.

Split files:
* ve.ce.Node.css
* ve.ce.ContentBranchNode.test.js (MWEntityNode)
* ve.ce.Document.test.js (some core test cases genericized)
* ve.dm.InternalList.test.js (uses mwReference test document)
* ve.dm.SurfaceFragment.test.js, ve.ui.FormatAction.test.js
** Made core tests use heading instead of mwHeading
** Updated core tests because normal headings don't break out of lists
** Moved test runners into ve.test.utils.js
* ve.ui.Icons-*.css
* ve.ui.Dialog.css (MW parts into ve.ui.MWDialog.css)
* ve.ui.Tool.css
* ve.ui.Widget.css (move ve-ui-rtl and ve-ui-ltr to ve.ui.css)

ve.dm.Converter.test.js: Moved runner functions into ve.test.utils.js

ve.dm.example.js:
* Refactored createExampleDocument so mwExample can use it
* Removed wgExtensionAssetsPath detection, moved into mw-preload.js
* Genericized withMeta example document (original version copied to mwExample)
* Moved references example document to mwExample

ve.dm.mwExample.js:
* Move withMeta and references example documents from ve.dm.example.js
* Add createExampleDocument function

ve-mw/test/index.php: Runner for MW-specific tests only

ve-mw/test/mw-preload.js: Sets VE_TESTDIR for Special:JavaScriptTest only

ve.ui.Window.js:
* Remove magic path interpolation in addLocalStyleSheets()
* Pass full(er) paths to addLocalStyleSheets(), here and in subclasses

ve.ui.MWDialog.js: Subclass of Dialog that adds MW versions of stylesheets

ve.ui.MW*Dialog.js:
* Subclass MWDialog rather than Dialog
* Load both core and MW versions of stylesheets that have both

ve.ui.PagedDialog.js: Converted to a mixin rather than an abstract base class
* Don't inherit ve.ui.Dialog
* Rather than overriding initialize(), provide initializePages() which the
  host class is supposed to call from its initialize()
* Rename onOutlineSelect to onPageOutlineSelect

ve.ui.MWMetaDialog.js, ve.ui.MWTransclusionDialog.js:
* Use PagedDialog as a mixin rather than a base class, inherit MWDialog

bullet-icon.png: Unused, deleted

Stuff we should do later:
* Refactor tests to use data providers
* Write utility function for SVG compat check
* Separate omnibus CSS files such as ve.ui.Widget.css
* Separate omnibus RL modules
* Use icon classes in ViewPageTarget

Change-Id: I1b28f8ba7f2d2513e5c634927a854686fb9dd5a5
2013-07-02 20:51:38 -07:00

248 lines
7.1 KiB
JavaScript

/*!
* VisualEditor DataModel MWTransclusionNode class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel MediaWiki transclusion node.
*
* @class
* @abstract
* @extends ve.dm.LeafNode
* @mixins ve.dm.GeneratedContentNode
*
* @constructor
* @param {number} [length] Length of content data in document; ignored and overridden to 0
* @param {Object} [element] Reference to element in linear model
*/
ve.dm.MWTransclusionNode = function VeDmMWTransclusionNode( length, element ) {
// Parent constructor
ve.dm.LeafNode.call( this, 0, element );
// Mixin constructors
ve.dm.GeneratedContentNode.call( this );
};
/* Inheritance */
ve.inheritClass( ve.dm.MWTransclusionNode, ve.dm.LeafNode );
ve.mixinClass( ve.dm.MWTransclusionNode, ve.dm.GeneratedContentNode );
/* Static members */
ve.dm.MWTransclusionNode.static.name = 'mwTransclusion';
ve.dm.MWTransclusionNode.static.matchTagNames = null;
ve.dm.MWTransclusionNode.static.matchRdfaTypes = [
'mw:Transclusion',
// We're interested in all nodes that have mw:Transclusion, even if they also have other mw:
// types. So we match all mw: types, then use a matchFunction to assert that mw:Transclusion
// is in there.
/^mw:/
];
ve.dm.MWTransclusionNode.static.matchFunction = function ( domElement ) {
return ve.indexOf( 'mw:Transclusion',
( domElement.getAttribute( 'typeof' ) || '' ).split( ' ' )
) !== -1;
};
ve.dm.MWTransclusionNode.static.enableAboutGrouping = true;
ve.dm.MWTransclusionNode.static.getHashObject = function ( dataElement ) {
return {
type: dataElement.type,
mw: dataElement.attributes.mw
};
};
ve.dm.MWTransclusionNode.static.toDataElement = function ( domElements, converter ) {
var dataElement, index,
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
isInline = this.isHybridInline( domElements, converter ),
type = isInline ? 'mwTransclusionInline' : 'mwTransclusionBlock';
dataElement = {
'type': type,
'attributes': {
'mw': mwData,
'originalDomElements': ve.copyArray( domElements ),
'originalMw': mwDataJSON
}
};
index = this.storeDomElements( dataElement, domElements, converter.getStore() );
dataElement.attributes.originalIndex = index;
return dataElement;
};
ve.dm.MWTransclusionNode.static.toDomElements = function ( dataElement, doc, converter ) {
var el,
index = converter.getStore().indexOfHash( ve.getHash( this.getHashObject( dataElement ) ) ),
originalMw = dataElement.attributes.originalMw;
// If the transclusion is unchanged just send back the
// original DOM elements so selser can skip over it
if (
index === dataElement.attributes.originalIndex ||
( originalMw && ve.compare( dataElement.attributes.mw, JSON.parse( originalMw ) ) )
) {
// The object in the store is also used for CE rendering so return a copy
return ve.copyDomElements( dataElement.attributes.originalDomElements, doc );
} else {
el = doc.createElement( 'span' );
// All we need to send back to Parsoid is the original transclusion marker, with a
// reconstructed data-mw property.
el.setAttribute( 'typeof', 'mw:Transclusion' );
el.setAttribute( 'data-mw', JSON.stringify( dataElement.attributes.mw ) );
return [ el ];
}
};
/**
* Escape a template parameter. Helper function for getWikitext().
* @param {string} param Parameter value
* @returns {string} Escaped parameter value
*/
ve.dm.MWTransclusionNode.static.escapeParameter = function ( param ) {
var match, needsNowiki, input = param, output = '',
inNowiki = false, bracketStack = 0;
while ( input.length > 0 ) {
match = input.match( /(?:\{\{)+|(?:\}\})+|\|+|<\/?nowiki>|<nowiki\s*\/>/ );
if ( !match ) {
output += input;
break;
}
output += input.substr( 0, match.index );
input = input.substr( match.index + match[0].length );
if ( inNowiki ) {
if ( match[0] === '</nowiki>' ) {
inNowiki = false;
output += match[0];
} else {
output += match[0];
}
} else {
needsNowiki = true;
if ( match[0] === '<nowiki>' ) {
inNowiki = true;
needsNowiki = false;
} else if ( match[0] === '</nowiki>' || match[0].match( /<nowiki\s*\/>/ ) ) {
needsNowiki = false;
} else if ( match[0].match( /(?:\{\{)+/ ) ) {
bracketStack++;
needsNowiki = false;
} else if ( match[0].match( /(?:\}\})+/ ) ) {
if ( bracketStack > 0 ) {
bracketStack--;
needsNowiki = false;
}
} else if ( match[0].match( /\|+/ ) ) {
if ( bracketStack > 0 ) {
needsNowiki = false;
}
}
if ( needsNowiki ) {
output += '<nowiki>' + match[0] + '</nowiki>';
} else {
output += match[0];
}
}
}
return output;
};
/* Methods */
/**
* Get the wikitext for this transclusion.
*
* @method
* @returns {string} Wikitext like `{{foo|1=bar|baz=quux}}`
*/
ve.dm.MWTransclusionNode.prototype.getWikitext = function () {
var i, len, part, template, param,
content = this.getAttribute( 'mw' ),
wikitext = '';
// Normalize to multi template format
if ( content.params ) {
content = { 'parts': [ { 'template': content } ] };
}
// Build wikitext from content
for ( i = 0, len = content.parts.length; i < len; i++ ) {
part = content.parts[i];
if ( part.template ) {
// Template
template = part.template;
wikitext += '{{' + template.target.wt;
for ( param in template.params ) {
wikitext += '|' + param + '=' +
this.constructor.static.escapeParameter( template.params[param].wt );
}
wikitext += '}}';
} else {
// Plain wikitext
wikitext += part;
}
}
return wikitext;
};
/* Concrete subclasses */
/**
* DataModel MediaWiki transclusion block node.
*
* @class
* @extends ve.dm.MWTransclusionNode
* @constructor
* @param {number} [length] Length of content data in document; ignored and overridden to 0
* @param {Object} [element] Reference to element in linear model
*/
ve.dm.MWTransclusionBlockNode = function VeDmMWTransclusionBlockNode( length, element ) {
// Parent constructor
ve.dm.MWTransclusionNode.call( this, length, element );
};
ve.inheritClass( ve.dm.MWTransclusionBlockNode, ve.dm.MWTransclusionNode );
ve.dm.MWTransclusionBlockNode.static.matchTagNames = [];
ve.dm.MWTransclusionBlockNode.static.name = 'mwTransclusionBlock';
/**
* DataModel MediaWiki transclusion inline node.
*
* @class
* @extends ve.dm.MWTransclusionNode
* @constructor
* @param {number} [length] Length of content data in document; ignored and overridden to 0
* @param {Object} [element] Reference to element in linear model
*/
ve.dm.MWTransclusionInlineNode = function VeDmMWTransclusionInlineNode( length, element ) {
// Parent constructor
ve.dm.MWTransclusionNode.call( this, length, element );
};
ve.inheritClass( ve.dm.MWTransclusionInlineNode, ve.dm.MWTransclusionNode );
ve.dm.MWTransclusionInlineNode.static.matchTagNames = [];
ve.dm.MWTransclusionInlineNode.static.name = 'mwTransclusionInline';
ve.dm.MWTransclusionInlineNode.static.isContent = true;
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWTransclusionNode );
ve.dm.modelRegistry.register( ve.dm.MWTransclusionBlockNode );
ve.dm.modelRegistry.register( ve.dm.MWTransclusionInlineNode );