2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model Converter class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
/**
|
|
|
|
* Converter between HTML DOM and VisualEditor linear data.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} options Conversion options
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter = function ( nodeFactory, annotationFactory ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
// Properties
|
|
|
|
this.nodeFactory = nodeFactory;
|
|
|
|
this.annotationFactory = annotationFactory;
|
2012-06-07 00:47:27 +00:00
|
|
|
this.elements = { 'toDomElement': {}, 'toDataElement': {}, 'dataElementTypes': {} };
|
|
|
|
this.annotations = { 'toDomElement': {}, 'toDataAnnotation': {} };
|
2012-05-31 23:50:16 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.nodeFactory.addListenerMethod( this, 'register', 'onNodeRegister' );
|
|
|
|
this.annotationFactory.addListenerMethod( this, 'register', 'onAnnotationRegister' );
|
|
|
|
};
|
|
|
|
|
2012-06-07 00:47:27 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.getDataContentFromText = function ( text, annotations ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
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
|
2012-06-20 23:01:02 +00:00
|
|
|
// its not safe to share references - each annotated character needs its own map
|
2012-06-07 00:47:27 +00:00
|
|
|
characters[i] = [characters[i], ve.extendObject( {}, annotationMap )];
|
|
|
|
}
|
|
|
|
return characters;
|
|
|
|
};
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responds to register events from the node factory.
|
|
|
|
*
|
2012-06-20 23:01:02 +00:00
|
|
|
* 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
|
2012-05-31 23:50:16 +00:00
|
|
|
* 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}'
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.onNodeRegister = function ( dataElementType, constructor ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
if ( constructor.converters === undefined ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Missing conversion data in node implementation of ' + dataElementType );
|
2012-05-31 23:50:16 +00:00
|
|
|
} else if ( constructor.converters !== null ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i,
|
|
|
|
domElementTypes = constructor.converters.domElementTypes,
|
2012-06-07 00:47:27 +00:00
|
|
|
toDomElement = constructor.converters.toDomElement,
|
|
|
|
toDataElement = constructor.converters.toDataElement;
|
2012-05-31 23:50:16 +00:00
|
|
|
// Registration
|
2012-06-07 00:47:27 +00:00
|
|
|
this.elements.toDomElement[dataElementType] = toDomElement;
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( i = 0; i < domElementTypes.length; i++ ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
this.elements.toDataElement[domElementTypes[i]] = toDataElement;
|
|
|
|
this.elements.dataElementTypes[domElementTypes[i]] = dataElementType;
|
2012-05-31 23:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}'
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.onAnnotationRegister = function ( dataElementType, constructor ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
if ( constructor.converters === undefined ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Missing conversion data in annotation implementation of ' + dataElementType );
|
2012-05-31 23:50:16 +00:00
|
|
|
} else if ( constructor.converters !== null ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i,
|
|
|
|
domElementTypes = constructor.converters.domElementTypes,
|
2012-06-07 00:47:27 +00:00
|
|
|
toDomElement = constructor.converters.toDomElement,
|
|
|
|
toDataAnnotation = constructor.converters.toDataAnnotation;
|
2012-05-31 23:50:16 +00:00
|
|
|
// Registration
|
2012-06-07 00:47:27 +00:00
|
|
|
this.annotations.toDomElement[dataElementType] = toDomElement;
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( i = 0; i < domElementTypes.length; i++ ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
this.annotations.toDataAnnotation[domElementTypes[i]] = toDataAnnotation;
|
2012-05-31 23:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-06 17:17:30 +00:00
|
|
|
/**
|
2012-06-08 04:58:56 +00:00
|
|
|
* 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.)
|
2012-06-06 17:17:30 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-08 04:58:56 +00:00
|
|
|
* @param {Object} dataElement Linear model element
|
|
|
|
* @returns {HTMLElement|false} DOM element, or false if this element cannot be converted
|
2012-06-06 17:17:30 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDomElementFromDataElement = function ( dataElement ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var key, domElement, dataElementAttributes,
|
|
|
|
dataElementType = dataElement.type;
|
2012-06-08 04:58:56 +00:00
|
|
|
if (
|
|
|
|
// Aliens
|
|
|
|
dataElementType === 'alienInline' || dataElementType === 'alienBlock' ||
|
|
|
|
// Unsupported elements
|
|
|
|
!( dataElementType in this.elements.toDomElement)
|
|
|
|
) {
|
|
|
|
return false;
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
2012-08-02 18:46:13 +00:00
|
|
|
domElement = this.elements.toDomElement[dataElementType]( dataElementType, dataElement );
|
|
|
|
dataElementAttributes = dataElement.attributes;
|
2012-06-07 00:47:27 +00:00
|
|
|
if ( dataElementAttributes ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( key in dataElementAttributes ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
// Only include 'html/*' attributes and strip the 'html/' from the beginning of the name
|
2012-06-06 17:17:30 +00:00
|
|
|
if ( key.indexOf( 'html/' ) === 0 ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
domElement.setAttribute( key.substr( 5 ), dataElementAttributes[key] );
|
2012-06-06 17:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
|
|
|
return domElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-08 04:58:56 +00:00
|
|
|
* 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.
|
2012-06-07 00:47:27 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-08 04:58:56 +00:00
|
|
|
* @param {HTMLElement} domElement DOM element
|
|
|
|
* @returns {Object|false} Linear model element, or false if this node cannot be converted
|
2012-06-07 00:47:27 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDataElementFromDomElement = function ( domElement ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var dataElement, domElementAttributes, dataElementAttributes, domElementAttribute, i,
|
|
|
|
domElementType = domElement.nodeName.toLowerCase();
|
2012-06-07 00:47:27 +00:00
|
|
|
if (
|
|
|
|
// Generated elements
|
|
|
|
domElement.hasAttribute( 'data-mw-gc' ) ||
|
|
|
|
// Unsupported elements
|
|
|
|
!( domElementType in this.elements.toDataElement )
|
|
|
|
) {
|
2012-06-07 21:16:10 +00:00
|
|
|
return false;
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
2012-08-02 18:46:13 +00:00
|
|
|
dataElement = this.elements.toDataElement[domElementType]( domElementType, domElement );
|
|
|
|
domElementAttributes = domElement.attributes;
|
2012-06-07 00:47:27 +00:00
|
|
|
if ( domElementAttributes.length ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
dataElementAttributes = dataElement.attributes = dataElement.attributes || {};
|
2012-06-07 00:47:27 +00:00
|
|
|
// Inlcude all attributes and prepend 'html/' to each attribute name
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( i = 0; i < domElementAttributes.length; i++ ) {
|
|
|
|
domElementAttribute = domElementAttributes[i];
|
2012-06-07 00:47:27 +00:00
|
|
|
dataElementAttributes['html/' + domElementAttribute.name] = domElementAttribute.value;
|
2012-06-06 17:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDataAnnotationFromDomElement = function ( domElement ) {
|
2012-07-20 00:24:54 +00:00
|
|
|
var domElementType = domElement.nodeName.toLowerCase(),
|
2012-06-07 00:47:27 +00:00
|
|
|
toDataAnnotation = this.annotations.toDataAnnotation[domElementType];
|
|
|
|
if ( typeof toDataAnnotation === 'function' ) {
|
|
|
|
return toDataAnnotation( domElementType, domElement );
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-06 17:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-08 04:58:56 +00:00
|
|
|
* Build an HTML DOM node for a linear model annotation.
|
2012-06-06 17:17:30 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-08 04:58:56 +00:00
|
|
|
* @param {Object} dataAnnotation Annotation object
|
|
|
|
* @returns {HTMLElement|false} HTML DOM node, or false if this annotation is not known
|
2012-06-06 17:17:30 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDomElementFromDataAnnotation = function ( dataAnnotation ) {
|
2012-08-14 00:50:43 +00:00
|
|
|
var split = dataAnnotation.type.split( '/' ),
|
2012-06-08 04:58:56 +00:00
|
|
|
baseType = split[0],
|
|
|
|
subType = split.slice( 1 ).join( '/' ),
|
|
|
|
toDomElement = this.annotations.toDomElement[baseType];
|
|
|
|
if ( typeof toDomElement === 'function' ) {
|
|
|
|
return toDomElement( subType, dataAnnotation );
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-06 17:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-08 04:58:56 +00:00
|
|
|
* 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.
|
2012-06-06 17:17:30 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-08 04:58:56 +00:00
|
|
|
* @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
|
2012-06-06 17:17:30 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDataFromDom = function ( domElement, annotations, dataElement, path, alreadyWrapped ) {
|
2012-06-14 19:18:06 +00:00
|
|
|
function createAlien( domElement, isInline ) {
|
|
|
|
var type = isInline ? 'alienInline' : 'alienBlock';
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'type': type,
|
|
|
|
'attributes': {
|
|
|
|
'html': $( '<div>' ).append( $( domElement ).clone() ).html()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/' + type }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2012-06-07 00:47:27 +00:00
|
|
|
// Fallback to defaults
|
|
|
|
annotations = annotations || [];
|
|
|
|
path = path || ['document'];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
var i, childDomElement, annotation, childDataElement, text, childTypes, matches, paragraph,
|
|
|
|
wrapperElement = dataElement,
|
2012-08-02 18:46:13 +00:00
|
|
|
data = [],
|
2012-06-08 22:16:55 +00:00
|
|
|
branchType = path[path.length - 1],
|
2012-06-14 19:18:06 +00:00
|
|
|
branchIsContent = ve.dm.nodeFactory.canNodeContainContent( branchType ),
|
2012-06-08 22:16:55 +00:00
|
|
|
wrapping = false;
|
2012-06-07 00:47:27 +00:00
|
|
|
// Open element
|
|
|
|
if ( dataElement ) {
|
|
|
|
data.push( dataElement );
|
|
|
|
}
|
|
|
|
// Add contents
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( i = 0; i < domElement.childNodes.length; i++ ) {
|
|
|
|
childDomElement = domElement.childNodes[i];
|
2012-06-07 00:47:27 +00:00
|
|
|
switch ( childDomElement.nodeType ) {
|
|
|
|
case Node.ELEMENT_NODE:
|
2012-06-14 19:18:06 +00:00
|
|
|
// Detect generated content and wrap it in an alien node
|
|
|
|
if ( childDomElement.hasAttribute( 'data-mw-gc' ) ) {
|
2012-07-26 03:46:57 +00:00
|
|
|
// FIXME Parsoid outputs RDFa now, address this in API rewrite
|
2012-06-14 19:18:06 +00:00
|
|
|
data = data.concat( createAlien( childDomElement, branchIsContent ) );
|
2012-06-07 21:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Detect and handle annotated content
|
2012-08-02 18:46:13 +00:00
|
|
|
annotation = this.getDataAnnotationFromDomElement( childDomElement );
|
2012-06-07 00:47:27 +00:00
|
|
|
if ( annotation ) {
|
|
|
|
// Start auto-wrapping of bare content
|
2012-06-14 19:18:06 +00:00
|
|
|
if ( !wrapping && !alreadyWrapped && !branchIsContent ) {
|
2012-08-16 20:06:18 +00:00
|
|
|
// Mark this paragraph as having been generated by
|
|
|
|
// us, so we can strip it on the way out
|
|
|
|
data.push( {
|
|
|
|
'type': 'paragraph',
|
|
|
|
'internal': { 'generated': 'wrapper' }
|
|
|
|
} );
|
2012-06-07 00:47:27 +00:00
|
|
|
wrapping = true;
|
|
|
|
}
|
|
|
|
// Append child element data
|
|
|
|
data = data.concat(
|
|
|
|
this.getDataFromDom(
|
2012-06-08 22:16:55 +00:00
|
|
|
childDomElement, annotations.concat( annotation ), undefined, path, wrapping || alreadyWrapped
|
2012-06-07 00:47:27 +00:00
|
|
|
)
|
|
|
|
);
|
2012-06-07 21:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// End auto-wrapping of bare content
|
|
|
|
if ( wrapping ) {
|
|
|
|
data.push( { 'type': '/paragraph' } );
|
|
|
|
wrapping = false;
|
|
|
|
}
|
|
|
|
// Append child element data
|
2012-08-02 18:46:13 +00:00
|
|
|
childDataElement = this.getDataElementFromDomElement( childDomElement );
|
2012-06-07 21:16:10 +00:00
|
|
|
if ( childDataElement ) {
|
2012-06-07 00:47:27 +00:00
|
|
|
data = data.concat(
|
|
|
|
this.getDataFromDom(
|
|
|
|
childDomElement,
|
|
|
|
[],
|
|
|
|
childDataElement,
|
2012-06-08 22:16:55 +00:00
|
|
|
path.concat( childDataElement.type ),
|
|
|
|
wrapping || alreadyWrapped
|
2012-06-07 00:47:27 +00:00
|
|
|
)
|
|
|
|
);
|
2012-06-07 21:16:10 +00:00
|
|
|
break;
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
2012-06-14 19:18:06 +00:00
|
|
|
// We don't know what this is, fall back to alien
|
|
|
|
data = data.concat( createAlien( childDomElement, branchIsContent ) );
|
2012-06-07 00:47:27 +00:00
|
|
|
break;
|
|
|
|
case Node.TEXT_NODE:
|
2012-08-03 19:12:09 +00:00
|
|
|
// HACK: strip trailing newline in <li> tags. Workaround for a Parsoid bug
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
// TODO kill this in favor of the fringe whitespace preservation
|
|
|
|
// code below
|
2012-08-02 18:46:13 +00:00
|
|
|
text = childDomElement.data;
|
2012-06-12 00:32:28 +00:00
|
|
|
if ( domElement.nodeName.toLowerCase() === 'li' ) {
|
2012-08-03 19:12:09 +00:00
|
|
|
text = text.replace( /\n$/, '' );
|
2012-06-15 02:35:39 +00:00
|
|
|
}
|
2012-06-15 05:46:04 +00:00
|
|
|
if ( !branchIsContent ) {
|
|
|
|
// If it's bare content, strip leading and trailing newlines
|
2012-08-08 23:07:50 +00:00
|
|
|
// FIXME these newlines should be preserved somehow
|
2012-06-15 05:46:04 +00:00
|
|
|
text = text.replace( /^\n+/, '' ).replace( /\n+$/, '' );
|
2012-08-03 19:12:09 +00:00
|
|
|
}
|
|
|
|
if ( text === '' ) {
|
|
|
|
// Don't produce an empty text node or an empty paragraph
|
|
|
|
break;
|
2012-06-15 05:46:04 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 00:47:27 +00:00
|
|
|
// Start auto-wrapping of bare content
|
2012-06-14 19:18:06 +00:00
|
|
|
if ( !wrapping && !alreadyWrapped && !branchIsContent ) {
|
2012-08-16 20:06:18 +00:00
|
|
|
// Mark this paragraph as having been generated by
|
|
|
|
// us, so we can strip it on the way out
|
|
|
|
paragraph = {
|
|
|
|
'type': 'paragraph',
|
|
|
|
'internal': { 'generated': 'wrapper' }
|
|
|
|
};
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
data.push( paragraph );
|
2012-06-07 00:47:27 +00:00
|
|
|
wrapping = true;
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
wrapperElement = paragraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip leading and trailing whitespace
|
2012-08-16 17:53:33 +00:00
|
|
|
// (but only in non-annotation nodes)
|
|
|
|
// and store it so it can be restored later.
|
|
|
|
// whitespace = [ outerPre, innerPre, innerPost, outerPost ]
|
|
|
|
// <tag> text </tag> <nextTag>
|
|
|
|
// ^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^
|
|
|
|
// outerPre innerPre innerPost outerPost
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
if ( annotations.length === 0 && i === 0 && wrapperElement ) {
|
|
|
|
// Strip leading whitespace from the first child
|
|
|
|
matches = text.match( /^\s+/ );
|
|
|
|
if ( matches && matches[0] !== '' ) {
|
2012-08-16 17:53:33 +00:00
|
|
|
if ( !wrapperElement.internal ) {
|
|
|
|
wrapperElement.internal = {};
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
}
|
2012-08-16 17:53:33 +00:00
|
|
|
if ( !wrapperElement.internal.whitespace ) {
|
|
|
|
wrapperElement.internal.whitespace = [];
|
|
|
|
}
|
|
|
|
wrapperElement.internal.whitespace[1] = matches[0];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
text = text.substring( matches[0].length );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
annotations.length === 0 &&
|
|
|
|
i === domElement.childNodes.length - 1 &&
|
|
|
|
wrapperElement
|
|
|
|
) {
|
|
|
|
// Strip trailing whitespace from the last child
|
|
|
|
matches = text.match( /\s+$/ );
|
|
|
|
if ( matches && matches[0] !== '' ) {
|
2012-08-16 17:53:33 +00:00
|
|
|
if ( !wrapperElement.internal ) {
|
|
|
|
wrapperElement.internal = {};
|
|
|
|
}
|
|
|
|
if ( !wrapperElement.internal.whitespace ) {
|
|
|
|
wrapperElement.internal.whitespace = [];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
}
|
2012-08-16 17:53:33 +00:00
|
|
|
wrapperElement.internal.whitespace[2] = matches[0];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
text = text.substring( 0,
|
|
|
|
text.length - matches[0].length );
|
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
2012-06-12 00:32:28 +00:00
|
|
|
|
2012-06-07 00:47:27 +00:00
|
|
|
// Annotate the text and output it
|
|
|
|
data = data.concat(
|
2012-06-12 00:32:28 +00:00
|
|
|
ve.dm.Converter.getDataContentFromText( text, annotations )
|
2012-06-07 00:47:27 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case Node.COMMENT_NODE:
|
|
|
|
// TODO: Preserve comments by inserting them into the linear model too
|
2012-08-03 19:12:09 +00:00
|
|
|
// Could use placeholders for this too, although they'd need to be
|
|
|
|
// inline in certain cases
|
2012-06-07 00:47:27 +00:00
|
|
|
break;
|
2012-06-06 17:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
// End auto-wrapping of bare content
|
|
|
|
if ( wrapping ) {
|
|
|
|
data.push( { 'type': '/paragraph' } );
|
|
|
|
}
|
2012-06-19 21:36:35 +00:00
|
|
|
|
|
|
|
// If we're closing a node that doesn't have any children, but could contain a paragraph,
|
|
|
|
// add a paragraph. This prevents things like empty list items
|
2012-08-02 18:46:13 +00:00
|
|
|
childTypes = ve.dm.nodeFactory.getChildNodeTypes( branchType );
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( branchType !== 'paragraph' && dataElement && data[data.length - 1] === dataElement &&
|
2012-06-19 21:36:35 +00:00
|
|
|
!wrapping && !ve.dm.nodeFactory.canNodeContainContent( branchType ) &&
|
|
|
|
!ve.dm.nodeFactory.isNodeContent( branchType ) &&
|
2012-08-11 08:14:56 +00:00
|
|
|
( childTypes === null || ve.indexOf( 'paragraph', childTypes ) !== -1 )
|
2012-06-19 21:36:35 +00:00
|
|
|
) {
|
2012-08-16 20:06:18 +00:00
|
|
|
data.push( { 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } } );
|
2012-06-19 21:36:35 +00:00
|
|
|
data.push( { 'type': '/paragraph' } );
|
|
|
|
}
|
|
|
|
|
2012-06-07 00:47:27 +00:00
|
|
|
// Close element
|
|
|
|
if ( dataElement ) {
|
|
|
|
data.push( { 'type': '/' + dataElement.type } );
|
|
|
|
}
|
2012-06-19 00:38:42 +00:00
|
|
|
// Don't return an empty document
|
|
|
|
if ( branchType === 'document' && data.length === 0 ) {
|
2012-08-16 20:06:18 +00:00
|
|
|
return [
|
|
|
|
{ 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } },
|
|
|
|
{ 'type': '/paragraph' }
|
|
|
|
];
|
2012-06-19 00:38:42 +00:00
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
return data;
|
2012-06-06 17:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-08 04:58:56 +00:00
|
|
|
* Convert linear model data to an HTML DOM
|
2012-06-06 17:17:30 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-06-08 04:58:56 +00:00
|
|
|
* @param {Array} data Linear model data
|
|
|
|
* @returns {HTMLElement} Wrapper div containing the resulting HTML
|
2012-06-06 17:17:30 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Converter.prototype.getDomFromData = function ( data ) {
|
2012-08-16 20:06:18 +00:00
|
|
|
var text, i, j, annotations, hash, annotationElement, done, dataElement, wrapper,
|
|
|
|
childDomElement, pre, post, parentDomElement,
|
2012-08-02 18:46:13 +00:00
|
|
|
container = document.createElement( 'div' ),
|
|
|
|
domElement = container,
|
|
|
|
annotationStack = {}; // { hash: DOMnode }
|
|
|
|
for ( i = 0; i < data.length; i++ ) {
|
2012-06-08 04:58:56 +00:00
|
|
|
if ( typeof data[i] === 'string' ) {
|
2012-06-07 22:02:25 +00:00
|
|
|
// Text
|
2012-06-08 04:58:56 +00:00
|
|
|
text = '';
|
|
|
|
// Continue forward as far as the plain text goes
|
|
|
|
while ( typeof data[i] === 'string' ) {
|
|
|
|
text += data[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// i points to the first non-text thing, go back one so we don't skip this later
|
|
|
|
i--;
|
|
|
|
// Add text
|
|
|
|
domElement.appendChild( document.createTextNode( text ) );
|
|
|
|
} else if (
|
|
|
|
ve.isArray( data[i] ) ||
|
|
|
|
(
|
|
|
|
data[i].annotations !== undefined &&
|
|
|
|
ve.dm.nodeFactory.isNodeContent( data[i].type )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Annotated text
|
|
|
|
text = '';
|
|
|
|
while (
|
|
|
|
ve.isArray( data[i] ) ||
|
|
|
|
(
|
|
|
|
data[i].annotations !== undefined &&
|
|
|
|
ve.dm.nodeFactory.isNodeContent( data[i].type )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
annotations = data[i].annotations || data[i][1];
|
|
|
|
// Check for closed annotations
|
|
|
|
for ( hash in annotationStack ) {
|
|
|
|
if ( !( hash in annotations ) ) {
|
|
|
|
// It's closed
|
|
|
|
// Traverse up until we hit the node we need to close, and then
|
|
|
|
// traverse up one more time to close that node
|
|
|
|
done = false;
|
|
|
|
while ( !done ) {
|
|
|
|
done = domElement === annotationStack[hash];
|
|
|
|
// Remove the annotation from the stack
|
|
|
|
delete annotationStack[domElement.veAnnotationHash];
|
|
|
|
// Remove the temporary veAnnotationHash property
|
|
|
|
delete domElement.veAnnotationHash;
|
|
|
|
// Add text if needed
|
|
|
|
if ( text.length > 0 ) {
|
|
|
|
domElement.appendChild( document.createTextNode( text ) );
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
// Traverse up
|
|
|
|
domElement = domElement.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for opened annotations
|
|
|
|
for ( hash in annotations ) {
|
|
|
|
if ( !( hash in annotationStack ) ) {
|
|
|
|
// It's opened
|
|
|
|
annotationElement = this.getDomElementFromDataAnnotation( annotations[hash] );
|
|
|
|
// Temporary property, will remove this when closing the annotation
|
|
|
|
annotationElement.veAnnotationHash = hash;
|
|
|
|
// Add to the annotation stack
|
|
|
|
annotationStack[hash] = annotationElement;
|
|
|
|
// Add text if needed
|
|
|
|
if ( text.length > 0 ) {
|
|
|
|
domElement.appendChild( document.createTextNode( text ) );
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
// Attach new node and descend into it
|
|
|
|
domElement.appendChild( annotationElement );
|
|
|
|
domElement = annotationElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( data[i].annotations === undefined ) {
|
|
|
|
text += data[i][0];
|
|
|
|
} else {
|
|
|
|
// Add text if needed
|
|
|
|
if ( text.length > 0 ) {
|
|
|
|
domElement.appendChild( document.createTextNode( text ) );
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
// Insert the element
|
|
|
|
domElement.appendChild( this.getDomElementFromDataElement( data[i] ) );
|
|
|
|
// Increment i once more so we skip over the closing as well
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// We're now at the first non-annotated thing, go back one so we don't skip this later
|
|
|
|
i--;
|
|
|
|
|
|
|
|
// Add any gathered text
|
|
|
|
if ( text.length > 0 ) {
|
|
|
|
domElement.appendChild( document.createTextNode( text ) );
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
// Close any remaining annotation nodes
|
|
|
|
while ( domElement.veAnnotationHash !== undefined ) {
|
2012-08-06 20:51:51 +00:00
|
|
|
delete annotationStack[domElement.veAnnotationHash];
|
2012-06-08 04:58:56 +00:00
|
|
|
delete domElement.veAnnotationHash;
|
|
|
|
domElement = domElement.parentNode;
|
|
|
|
}
|
|
|
|
} else if ( data[i].type !== undefined ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
dataElement = data[i];
|
2012-06-07 22:02:25 +00:00
|
|
|
// Element
|
2012-06-08 04:58:56 +00:00
|
|
|
if ( dataElement.type === 'alienBlock' || dataElement.type === 'alienInline' ) {
|
|
|
|
// Create nodes from source
|
2012-08-02 18:46:13 +00:00
|
|
|
wrapper = document.createElement( 'div' );
|
2012-06-08 04:58:56 +00:00
|
|
|
wrapper.innerHTML = dataElement.attributes.html;
|
|
|
|
// Add element - adds all child elements, but there really should only be 1
|
|
|
|
while ( wrapper.firstChild ) {
|
|
|
|
domElement.appendChild( wrapper.firstChild );
|
|
|
|
}
|
|
|
|
// Make sure the alien closing is skipped
|
|
|
|
i++;
|
|
|
|
} else if ( dataElement.type.charAt( 0 ) === '/' ) {
|
2012-08-16 17:53:33 +00:00
|
|
|
// Process inner whitespace
|
|
|
|
// whitespace = [ outerPre, innerPre, innerPost, outerPost ]
|
|
|
|
if ( domElement.veInternal && domElement.veInternal.whitespace ) {
|
|
|
|
pre = domElement.veInternal.whitespace[1];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
if ( pre ) {
|
|
|
|
if ( domElement.firstChild.nodeType === 3 ) {
|
|
|
|
// First child is a TextNode, prepend to it
|
|
|
|
domElement.firstChild.insertData( 0, pre );
|
|
|
|
} else {
|
|
|
|
// Prepend a TextNode
|
|
|
|
domElement.insertBefore(
|
|
|
|
document.createTextNode( pre ),
|
|
|
|
domElement.firstChild
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-08-16 17:53:33 +00:00
|
|
|
post = domElement.veInternal.whitespace[2];
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
if ( post ) {
|
|
|
|
if ( domElement.lastChild.nodeType === 3 ) {
|
|
|
|
// Last child is a TextNode, append to it
|
|
|
|
domElement.lastChild.appendData( post );
|
|
|
|
} else {
|
|
|
|
// Append a TextNode
|
|
|
|
domElement.appendChild(
|
|
|
|
document.createTextNode( post )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-16 20:06:18 +00:00
|
|
|
|
|
|
|
// If closing a generated wrapper node, unwrap it
|
|
|
|
// It would be nicer if we could avoid generating in the first
|
|
|
|
// place, but then remembering where we have to skip ascending
|
|
|
|
// to the parent would be tricky.
|
|
|
|
parentDomElement = domElement.parentNode;
|
|
|
|
if ( domElement.veInternal && domElement.veInternal.generated === 'wrapper' ) {
|
|
|
|
for ( j = 0; j < domElement.childNodes.length; j++ ) {
|
|
|
|
parentDomElement.insertBefore(
|
|
|
|
domElement.childNodes[j],
|
|
|
|
domElement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
parentDomElement.removeChild( domElement );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the internal data
|
2012-08-16 17:53:33 +00:00
|
|
|
delete domElement.veInternal;
|
2012-06-08 04:58:56 +00:00
|
|
|
// Ascend to parent node
|
2012-08-16 20:06:18 +00:00
|
|
|
domElement = parentDomElement;
|
2012-06-08 04:58:56 +00:00
|
|
|
} else {
|
|
|
|
// Create node from data
|
2012-08-02 18:46:13 +00:00
|
|
|
childDomElement = this.getDomElementFromDataElement( dataElement );
|
2012-08-16 20:06:18 +00:00
|
|
|
// Add reference to internal data
|
2012-08-16 17:53:33 +00:00
|
|
|
if ( dataElement.internal ) {
|
|
|
|
childDomElement.veInternal = dataElement.internal;
|
Strip and preserve inner leading&trailing whitespace in the linear model
This makes things like
== Foo ==
* Bar
render without the leading and trailing spaces, while still
round-tripping those spaces.
* Added a .fringeWhitespace property to the linear model and ve.dm.Node
** Object containing innerPre, innerPost, outerPre, outerPost
** Only inner* are used right now, outer* are planned for future use
** Like .attributes , it's suppressed if it's an empty object
* In getDataFromDom():
** Store the stripped whitespace in .fringeWhitespace
** Move emptiness check up: empty elements with .fringeWhitespace have
to be preserved
** Move paragraph wrapping up: .fringeWhitespace has to be applied to
the generated paragraph, not its parent
** Add wrapperElement to keep track of the element .fringeWhitespace has
to be added to; this is either dataElement or the generated paragraph
or nothing, but we can't modify dataElement because it's used later
* In getDomFromData():
** When processing an opening, store the fringeWhitespace data in the
generated DOM node
** When processing a closing, add the stored whitespace back in
* In the ve.dm.Document constructor, pass through .fringeWhitespace from
the linear model data to the generated nodes
Tests:
* Change one existing test case to account for this change
* Add three new test cases for this behavior
* Add normalizedHtml field so I can test behavior with bare content
Change-Id: I0411544652dd72b923c831c495d69ee4322a2c14
2012-08-10 21:09:04 +00:00
|
|
|
}
|
2012-06-08 04:58:56 +00:00
|
|
|
// Add element
|
|
|
|
domElement.appendChild( childDomElement );
|
|
|
|
// Descend into child node
|
|
|
|
domElement = childDomElement;
|
|
|
|
}
|
2012-06-07 22:02:25 +00:00
|
|
|
}
|
2012-06-07 00:47:27 +00:00
|
|
|
}
|
2012-06-08 04:58:56 +00:00
|
|
|
return container;
|
2012-06-06 17:17:30 +00:00
|
|
|
};
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
ve.dm.converter = new ve.dm.Converter( ve.dm.nodeFactory, ve.dm.annotationFactory );
|