mediawiki-extensions-Visual.../modules/ve/dm/nodes/ve.dm.AlienNode.js
Ed Sanders bdb4a116d9 Hack to escape angle brackets in data-parsoid attribute
Parsoid is sending us some unescaped HTML in the data-parsoid
attribute. When we try to rebuild ref nodes (inline aliens)
this confuses Firefox which tries to sanitise the HTML by converting
<ref/> to <ref></span>.

As a temporary fix we can manually escape <>'s inside the
data-parsoid attribute.

Also in this commit the new MWReference nodes have been moved
to experimental as they are incomplete.

Bug: 47417
Change-Id: Ib6a0cfb880e769f28b42c9fa63ddc1abc75c399d
2013-04-25 14:55:17 +01:00

113 lines
3.2 KiB
JavaScript

/*!
* VisualEditor DataModel AlienNode, AlienBlockNode and AlienInlineNode classes.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel alien node.
*
* @class
* @abstract
* @extends 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.AlienNode = function VeDmAlienNode( length, element ) {
// Parent constructor
ve.dm.GeneratedContentNode.call( this, 0, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.AlienNode, ve.dm.GeneratedContentNode );
/* Static members */
ve.dm.AlienNode.static.name = 'alien';
ve.dm.AlienNode.static.storeHtmlAttributes = false;
// TODO: Develop better method to test for generated content
ve.dm.AlienNode.static.generatedContent = true;
ve.dm.AlienNode.static.toDataElement = function ( domElements, converter ) {
var isInline = this.isHybridInline( domElements, converter ),
type = isInline ? 'alienInline' : 'alienBlock',
html = $( '<div>', domElements[0].ownerDocument ).append( $( domElements ).clone() ).html();
return {
'type': type,
'attributes': {
'html': html
}
};
};
ve.dm.AlienNode.static.toDomElements = function ( dataElement, doc ) {
var wrapper = doc.createElement( 'div' );
// Filthy hack: Parsoid is currently sending us unescaped angle brackets
// inside data-parsoid. For some reason FF picks this up as html and tries
// to sanitise it, converting <ref/> to <ref></span> (!?).
// As a *very temporary* fix we can regex replace them here.
$( wrapper ).html(
dataElement.attributes.html.replace(
/data-parsoid="([^"]+)"/g,
function( r0, r1 ) {
return 'data-parsoid="' + r1.replace( /</g, '&lt;' ).replace( />/g, '&gt;' ) + '"';
}
)
);
// Convert wrapper.children to an array
return Array.prototype.slice.call( wrapper.childNodes, 0 );
};
/* Concrete subclasses */
/**
* DataModel alienBlock node.
*
* @class
* @extends ve.dm.AlienNode
* @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.AlienBlockNode = function VeDmAlienBlockNode( length, element ) {
// Parent constructor
ve.dm.AlienNode.call( this, length, element );
};
ve.inheritClass( ve.dm.AlienBlockNode, ve.dm.AlienNode );
ve.dm.AlienBlockNode.static.name = 'alienBlock';
/**
* DataModel alienInline node.
*
* @class
* @extends ve.dm.AlienNode
* @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.AlienInlineNode = function VeDmAlienInlineNode( length, element ) {
// Parent constructor
ve.dm.AlienNode.call( this, length, element );
};
ve.inheritClass( ve.dm.AlienInlineNode, ve.dm.AlienNode );
ve.dm.AlienInlineNode.static.name = 'alienInline';
ve.dm.AlienInlineNode.static.isContent = true;
/* Registration */
ve.dm.modelRegistry.register( ve.dm.AlienNode );
ve.dm.modelRegistry.register( ve.dm.AlienBlockNode );
ve.dm.modelRegistry.register( ve.dm.AlienInlineNode );