/** * VisualEditor data model Converter class. * * @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * Converter between HTML DOM and VisualEditor linear data. * * @class * @constructor * @param {Object} options Conversion options */ ve.dm.Converter = function ( nodeFactory, annotationFactory ) { // Properties this.nodeFactory = nodeFactory; this.annotationFactory = annotationFactory; this.elements = { 'toDomElement': {}, 'toDataElement': {}, 'dataElementTypes': {} }; this.annotations = { 'toDomElement': {}, 'toDataAnnotation': {} }; // Events this.nodeFactory.addListenerMethod( this, 'register', 'onNodeRegister' ); this.annotationFactory.addListenerMethod( this, 'register', 'onAnnotationRegister' ); }; /* Static Methods */ /** * Get linear model data from a string optionally applying annotations * * @param {String} text Plain text to convert * @param {Array} [annotations] Array of annotation objects to apply * @returns {Array} Linear model data, one element per character */ ve.dm.Converter.getDataContentFromText = function ( text, annotations ) { var characters = text.split( '' ), annotationMap = {}, i; if ( !annotations || annotations.length === 0 ) { return characters; } // Build annotation map for ( i = 0; i < annotations.length; i++ ) { if ( annotations[i].data && ve.isEmptyObject( annotations[i].data ) ) { // Cleanup empty data property delete annotations[i].data; } annotationMap[ve.getHash( annotations[i] )] = annotations[i]; } // Apply annotations to characters for ( i = 0; i < characters.length; i++ ) { // Make a shallow copy of the annotationMap object, otherwise adding an annotation to one // character automatically adds it to all of others as well, annotations should be treated // as immutable, so it's OK to share references, but annotation maps are not immutable, so // its not safe to share references - each annotated character needs its own map characters[i] = [characters[i], ve.extendObject( {}, annotationMap )]; } return characters; }; /* Methods */ /** * Responds to register events from the node factory. * * If a node is special; such as document, alienInline, alienBlock and text; its {converters} * property should be set to null, as to distinguish it from a new node type that someone has simply * forgotten to implement converters for. * * @method * @param {String} type Node type * @param {Function} constructor Node constructor * @throws 'Missing conversion data in node implementation of {type}' */ ve.dm.Converter.prototype.onNodeRegister = function ( dataElementType, constructor ) { if ( constructor.converters === undefined ) { throw new Error( 'Missing conversion data in node implementation of ' + dataElementType ); } else if ( constructor.converters !== null ) { var i, domElementTypes = constructor.converters.domElementTypes, toDomElement = constructor.converters.toDomElement, toDataElement = constructor.converters.toDataElement; // Registration this.elements.toDomElement[dataElementType] = toDomElement; for ( i = 0; i < domElementTypes.length; i++ ) { this.elements.toDataElement[domElementTypes[i]] = toDataElement; this.elements.dataElementTypes[domElementTypes[i]] = dataElementType; } } }; /** * Responds to register events from the annotation factory. * * @method * @param {String} type Base annotation type * @param {Function} constructor Annotation constructor * @throws 'Missing conversion data in annotation implementation of {type}' */ ve.dm.Converter.prototype.onAnnotationRegister = function ( dataElementType, constructor ) { if ( constructor.converters === undefined ) { throw new Error( 'Missing conversion data in annotation implementation of ' + dataElementType ); } else if ( constructor.converters !== null ) { var i, domElementTypes = constructor.converters.domElementTypes, toDomElement = constructor.converters.toDomElement, toDataAnnotation = constructor.converters.toDataAnnotation; // Registration this.annotations.toDomElement[dataElementType] = toDomElement; for ( i = 0; i < domElementTypes.length; i++ ) { this.annotations.toDataAnnotation[domElementTypes[i]] = toDataAnnotation; } } }; /** * Get the DOM element for a given linear model element. * * This invokes the toDomElement function registered for the element type. * NOTE: alienBlock and alienInline elements are not supported, if you pass them this function * will return false. (Opposite of District 9: no aliens allowed.) * * @method * @param {Object} dataElement Linear model element * @returns {HTMLElement|false} DOM element, or false if this element cannot be converted */ ve.dm.Converter.prototype.getDomElementFromDataElement = function ( dataElement ) { var key, domElement, dataElementAttributes, dataElementType = dataElement.type; if ( // Aliens dataElementType === 'alienInline' || dataElementType === 'alienBlock' || // Unsupported elements !( dataElementType in this.elements.toDomElement) ) { return false; } domElement = this.elements.toDomElement[dataElementType]( dataElementType, dataElement ); dataElementAttributes = dataElement.attributes; if ( dataElementAttributes ) { for ( key in dataElementAttributes ) { // Only include 'html/*' attributes and strip the 'html/' from the beginning of the name if ( key.indexOf( 'html/' ) === 0 ) { domElement.setAttribute( key.substr( 5 ), dataElementAttributes[key] ); } } } return domElement; }; /** * Get the linear model data element for a given DOM element. * * This invokes the toDataElement function registered for the element type, after checking that * there is no data-mw-gc attribute. * * @method * @param {HTMLElement} domElement DOM element * @returns {Object|false} Linear model element, or false if this node cannot be converted */ ve.dm.Converter.prototype.getDataElementFromDomElement = function ( domElement ) { var dataElement, domElementAttributes, dataElementAttributes, domElementAttribute, i, domElementType = domElement.nodeName.toLowerCase(); if ( // Generated elements domElement.hasAttribute( 'data-mw-gc' ) || // Unsupported elements !( domElementType in this.elements.toDataElement ) ) { return false; } dataElement = this.elements.toDataElement[domElementType]( domElementType, domElement ); domElementAttributes = domElement.attributes; if ( domElementAttributes.length ) { dataElementAttributes = dataElement.attributes = dataElement.attributes || {}; // Inlcude all attributes and prepend 'html/' to each attribute name for ( i = 0; i < domElementAttributes.length; i++ ) { domElementAttribute = domElementAttributes[i]; dataElementAttributes['html/' + domElementAttribute.name] = domElementAttribute.value; } } return dataElement; }; /** * Check if an HTML DOM node represents an annotation, and if so, build an annotation object for it. * * @example Annotation Object * { 'type': 'type', data: { 'key': 'value', ... } } * * @param {HTMLElement} domElement HTML DOM node * @returns {Object|false} Annotation object, or false if this node is not an annotation */ ve.dm.Converter.prototype.getDataAnnotationFromDomElement = function ( domElement ) { var domElementType = domElement.nodeName.toLowerCase(), toDataAnnotation = this.annotations.toDataAnnotation[domElementType]; if ( typeof toDataAnnotation === 'function' ) { return toDataAnnotation( domElementType, domElement ); } return false; }; /** * Build an HTML DOM node for a linear model annotation. * * @method * @param {Object} dataAnnotation Annotation object * @returns {HTMLElement|false} HTML DOM node, or false if this annotation is not known */ ve.dm.Converter.prototype.getDomElementFromDataAnnotation = function ( dataAnnotation ) { var split = dataAnnotation.type.split( '/', 2 ), baseType = split[0], subType = split.slice( 1 ).join( '/' ), toDomElement = this.annotations.toDomElement[baseType]; if ( typeof toDomElement === 'function' ) { return toDomElement( subType, dataAnnotation ); } return false; }; /** * Convert an HTML DOM tree to a linear model. * * Do not use the annotations, dataElement and path parameters, they're used for internal * recursion only. * * @method * @param {HTMLElement} domElement Wrapper div containing the HTML to convert * @param {Array} [annotations] Array of annotations (objects) to apply to the generated data * @param {Object} [dataElement] Data element to wrap the returned data in * @param {Array} [path] Array of linear model element types * @returns {Array} Linear model data */ ve.dm.Converter.prototype.getDataFromDom = function ( domElement, annotations, dataElement, path, alreadyWrapped ) { function createAlien( domElement, isInline ) { var type = isInline ? 'alienInline' : 'alienBlock'; return [ { 'type': type, 'attributes': { 'html': $( '