mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-23 18:28:51 +00:00
Cleaned up some unused files, added type to es.DocumentModelNode objects for serialization purposes, and added getPlainObject to es.DocumentModelNode which also uses the newly migrated es.DocumentModel.expandContentData
This commit is contained in:
parent
f525e88058
commit
cf5be16248
|
@ -1,69 +0,0 @@
|
|||
/**
|
||||
* Creates content serializer.
|
||||
*
|
||||
* Base object for all serializers, providing basic shared functionality and stubs for required
|
||||
* implementations.
|
||||
*
|
||||
* @class
|
||||
* @constructor
|
||||
* @param context {es.WikiContext} Context of the wiki the document is a part of
|
||||
* @property context {es.WikiContext} Context of the wiki the document is a part of
|
||||
*/
|
||||
es.Document.Serializer = function( context ) {
|
||||
this.context = context;
|
||||
};
|
||||
|
||||
/* Static Methods */
|
||||
|
||||
es.Document.Serializer.repeatString = function( pattern, count ) {
|
||||
if ( count < 1 ) {
|
||||
return '';
|
||||
}
|
||||
var result = '';
|
||||
while ( count > 0 ) {
|
||||
if ( count & 1 ) { result += pattern; }
|
||||
count >>= 1;
|
||||
pattern += pattern;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
es.Document.Serializer.escapeXmlText = function( text ) {
|
||||
return text
|
||||
.replace( /&/g, '&' )
|
||||
.replace( /</g, '<' )
|
||||
.replace( />/g, '>' )
|
||||
.replace( /"/g, '"' )
|
||||
.replace( /'/g, ''' );
|
||||
};
|
||||
|
||||
es.Document.Serializer.buildXmlAttributes = function( attributes, prespace ) {
|
||||
var attr = [];
|
||||
var name;
|
||||
if ( attributes ) {
|
||||
for ( name in attributes ) {
|
||||
attr.push( name + '="' + attributes[name] + '"' );
|
||||
}
|
||||
}
|
||||
return ( prespace && attr.length ? ' ' : '' ) + attr.join( ' ' );
|
||||
};
|
||||
|
||||
es.Document.Serializer.buildXmlOpeningTag = function( tag, attributes ) {
|
||||
return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + '>';
|
||||
};
|
||||
|
||||
es.Document.Serializer.buildXmlClosingTag = function( tag ) {
|
||||
return '</' + tag + '>';
|
||||
};
|
||||
|
||||
es.Document.Serializer.buildXmlTag = function( tag, attributes, value, escape ) {
|
||||
if ( value === false ) {
|
||||
return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + ' />';
|
||||
} else {
|
||||
if ( escape ) {
|
||||
value = wiki.util.xml.esc( value );
|
||||
}
|
||||
return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + '>' +
|
||||
value + '</' + tag + '>';
|
||||
}
|
||||
};
|
|
@ -1,82 +0,0 @@
|
|||
/**
|
||||
* Ordered collection of blocks.
|
||||
*
|
||||
* @class
|
||||
* @constructor
|
||||
* @extends {es.EventEmitter}
|
||||
* @param blocks {Array} List of blocks
|
||||
* @property blocks {Array}
|
||||
* @property width {Integer}
|
||||
*/
|
||||
es.Document = function( blocks ) {
|
||||
es.DomContainer.call( this, 'document', 'blocks', blocks );
|
||||
this.width = null;
|
||||
};
|
||||
|
||||
/* Static Members */
|
||||
|
||||
/**
|
||||
* List of registered document serializers.
|
||||
*/
|
||||
es.Document.serializers = {};
|
||||
|
||||
/* Static Methods */
|
||||
|
||||
/**
|
||||
* Creates new es.Document from a WikiDom Document object
|
||||
*
|
||||
* @method
|
||||
* @param {Object} WikiDom document object
|
||||
* @returns {es.Document} EditSurface document object
|
||||
*/
|
||||
es.Document.newFromWikiDomDocument = function( wikidomDocument ) {
|
||||
var blocks = [];
|
||||
if ( $.isArray( wikidomDocument.blocks ) ) {
|
||||
for ( var i = 0; i < wikidomDocument.blocks.length; i++ ) {
|
||||
blocks.push( es.Block.newFromWikiDomBlock( wikidomDocument.blocks[i] ) );
|
||||
}
|
||||
}
|
||||
return new es.Document( blocks );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
||||
es.Document.prototype.serialize = function( serializer, context, options ) {
|
||||
if ( serializer in es.Document.serializers ) {
|
||||
return es.Document.serializers[serializer]( this.getWikiDomDocument(), context, options );
|
||||
}
|
||||
};
|
||||
|
||||
es.Document.prototype.getSerializers = function() {
|
||||
return es.Document.serializers;
|
||||
};
|
||||
|
||||
/**
|
||||
* Forces all blocks in the document to render.
|
||||
*
|
||||
* @method
|
||||
*/
|
||||
es.Document.prototype.renderBlocks = function() {
|
||||
// Bypass rendering when width has not changed
|
||||
var width = this.$.innerWidth();
|
||||
if ( this.width === width ) {
|
||||
return;
|
||||
}
|
||||
this.width = width;
|
||||
// Render blocks
|
||||
for ( var i = 0; i < this.blocks.length; i++ ) {
|
||||
this.blocks[i].renderContent();
|
||||
}
|
||||
};
|
||||
|
||||
es.Document.prototype.getWikiDomDocument = function() {
|
||||
var wikidom = { blocks: [ ] };
|
||||
for ( var i = 0; i < this.blocks.length; i++ ) {
|
||||
wikidom.blocks.push( this.blocks[i].getWikiDom() );
|
||||
}
|
||||
return wikidom;
|
||||
};
|
||||
|
||||
/* Inheritance */
|
||||
|
||||
$.extend( es, es.Document, es.DomContainer );
|
|
@ -5,13 +5,14 @@
|
|||
* nodes to be used as nodes in a space partitioning tree.
|
||||
*
|
||||
* @class
|
||||
* @abstract
|
||||
* @constructor
|
||||
* @extends {es.DocumentNode}
|
||||
* @extends {es.EventEmitter}
|
||||
* @param {Integer|Array} contents Either Length of content or array of child nodes to append
|
||||
* @property {Integer} contentLength Length of content
|
||||
*/
|
||||
es.DocumentModelNode = function( element, contents ) {
|
||||
es.DocumentModelNode = function( type, element, contents ) {
|
||||
// Inheritance
|
||||
es.DocumentNode.call( this );
|
||||
es.EventEmitter.call( this );
|
||||
|
@ -21,8 +22,9 @@ es.DocumentModelNode = function( element, contents ) {
|
|||
this.emitUpdate = function() {
|
||||
_this.emit( 'update' );
|
||||
};
|
||||
|
||||
|
||||
// Properties
|
||||
this.type = type;
|
||||
this.parent = null;
|
||||
this.root = this;
|
||||
this.element = element || null;
|
||||
|
@ -54,6 +56,30 @@ es.DocumentModelNode.prototype.createView = function() {
|
|||
|
||||
/* Methods */
|
||||
|
||||
/**
|
||||
* Gets a plain object representation of the document's data.
|
||||
*
|
||||
* The resulting object is compatible with es.DocumentModel.newFromPlainObject.
|
||||
*
|
||||
* @method
|
||||
* @returns {Object} Plain object representation
|
||||
*/
|
||||
es.DocumentModelNode.prototype.getPlainObject = function() {
|
||||
var obj = { 'type': this.type };
|
||||
if ( this.element && this.element.attributes ) {
|
||||
obj.attributes = es.copyObject( this.element.attributes );
|
||||
}
|
||||
if ( this.children.length ) {
|
||||
obj.children = [];
|
||||
for ( var i = 0; i < this.children.length; i++ ) {
|
||||
obj.children.push( this.children[i].getPlainObject() );
|
||||
}
|
||||
} else if ( this.getContentLength() ) {
|
||||
obj.content = es.DocumentModel.expandContentData( this.getContent() );
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a node to the end of this node's children.
|
||||
*
|
||||
|
@ -370,7 +396,7 @@ es.DocumentModelNode.prototype.getElementType = function() {
|
|||
* @returns {Mixed} Value of attribute, or null if no such attribute exists
|
||||
*/
|
||||
es.DocumentModelNode.prototype.getElementAttribute = function( key ) {
|
||||
if ( this.element.attributes && key in this.element.attributes ) {
|
||||
if ( this.element && this.element.attributes && key in this.element.attributes ) {
|
||||
return this.element.attributes[key];
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -44,6 +44,8 @@ es.extendObject = $.extend;
|
|||
|
||||
es.isPlainObject = $.isPlainObject;
|
||||
|
||||
es.isEmptyObject = $.isEmptyObject;
|
||||
|
||||
es.isArray = $.isArray;
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
*/
|
||||
es.DocumentModel = function( data, attributes ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, null, data ? data.length : 0 );
|
||||
es.DocumentModelNode.call( this, 'document', null, data ? data.length : 0 );
|
||||
|
||||
// Properties
|
||||
this.data = es.isArray( data ) ? data : [];
|
||||
|
@ -482,6 +482,83 @@ es.DocumentModel.flattenPlainObjectElementNode = function( obj ) {
|
|||
return data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a plain object representation of content data.
|
||||
*
|
||||
* @method
|
||||
* @returns {Object} Plain object representation
|
||||
*/
|
||||
es.DocumentModel.expandContentData = function( data ) {
|
||||
var stack = [];
|
||||
// Text and annotations
|
||||
function start( offset, annotation ) {
|
||||
stack.push( es.extendObject( true, {}, annotation, { 'range': { 'start': offset } } ) );
|
||||
}
|
||||
function end( offset, annotation ) {
|
||||
for ( var i = stack.length - 1; i >= 0; i-- ) {
|
||||
if ( !stack[i].range.end ) {
|
||||
if ( annotation ) {
|
||||
if ( stack[i].type === annotation.type &&
|
||||
es.compareObjects( stack[i].data, annotation.data ) ) {
|
||||
stack[i].range.end = offset;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
stack[i].range.end = offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var left = '',
|
||||
right,
|
||||
leftPlain,
|
||||
rightPlain,
|
||||
obj = { 'text': '' },
|
||||
offset = 0,
|
||||
i,
|
||||
j;
|
||||
for ( i = 0; i < data.length; i++ ) {
|
||||
right = data[i];
|
||||
leftPlain = typeof left === 'string';
|
||||
rightPlain = typeof right === 'string';
|
||||
// Open or close annotations
|
||||
if ( !leftPlain && rightPlain ) {
|
||||
// [formatted][plain] pair, close any annotations for left
|
||||
end( i - offset );
|
||||
} else if ( leftPlain && !rightPlain ) {
|
||||
// [plain][formatted] pair, open any annotations for right
|
||||
for ( j = 1; j < right.length; j++ ) {
|
||||
start( i - offset, right[j] );
|
||||
}
|
||||
} else if ( !leftPlain && !rightPlain ) {
|
||||
// [formatted][formatted] pair, open/close any differences
|
||||
for ( j = 1; j < left.length; j++ ) {
|
||||
if ( es.DocumentModel.getIndexOfAnnotation( data[i] , left[j], true ) === -1 ) {
|
||||
end( i - offset, left[j] );
|
||||
}
|
||||
}
|
||||
for ( j = 1; j < right.length; j++ ) {
|
||||
if ( es.DocumentModel.getIndexOfAnnotation( data[i - 1], right[j], true ) === -1 ) {
|
||||
start( i - offset, right[j] );
|
||||
}
|
||||
}
|
||||
}
|
||||
obj.text += rightPlain ? right : right[0];
|
||||
left = right;
|
||||
}
|
||||
if ( data.length ) {
|
||||
end( i - offset );
|
||||
}
|
||||
if ( stack.length ) {
|
||||
obj.annotation = stack;
|
||||
}
|
||||
// Copy attributes if there are any set
|
||||
if ( !es.isEmptyObject( this.attributes ) ) {
|
||||
obj.attributes = es.extendObject( true, {}, this.attributes );
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a data at a given offset is content.
|
||||
*
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.HeadingModel = function( element, length ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, length );
|
||||
es.DocumentModelNode.call( this, 'heading', element, length );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.ListItemModel = function( element, length ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, length );
|
||||
es.DocumentModelNode.call( this, 'listItem', element, length );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.ListModel = function( element, contents ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, contents );
|
||||
es.DocumentModelNode.call( this, 'list', element, contents );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.ParagraphModel = function( element, length ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, length );
|
||||
es.DocumentModelNode.call( this, 'paragraph', element, length );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.TableCellModel = function( element, contents ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, contents );
|
||||
es.DocumentModelNode.call( this, 'tableCell', element, contents );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.TableModel = function( element, contents ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, contents );
|
||||
es.DocumentModelNode.call( this, 'table', element, contents );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*/
|
||||
es.TableRowModel = function( element, contents ) {
|
||||
// Inheritance
|
||||
es.DocumentModelNode.call( this, element, contents );
|
||||
es.DocumentModelNode.call( this, 'tableRow', element, contents );
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
|
Loading…
Reference in a new issue