All files / src/ui/elements ve.ui.PreviewElement.js

91.48% Statements 43/47
70% Branches 7/10
80% Functions 8/10
91.48% Lines 43/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                                        1x 2x     2x     2x   2x   2x 1x       2x                     1x   1x         1x                       1x 2x 2x               1x     3x     3x               1x 1x 1x 1x     1x           1x     1x 1x 1x         1x           1x   2x 2x 2x 2x   2x         2x   1x 1x     1x                   1x   2x 2x     2x               1x 5x    
/*!
 * VisualEditor UserInterface PreviewElement class.
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */
 
/**
 * Creates a ve.ui.PreviewElement object.
 *
 * @class
 * @extends OO.ui.Element
 * @mixes OO.EventEmitter
 *
 * @constructor
 * @param {ve.dm.Node} [model] Model from which to create a preview
 * @param {Object} [config] Configuration options
 * @param {boolean} [config.useView=false] Use the view HTML, and don't bother generating model HTML, which
 *  is a bit slower
 */
ve.ui.PreviewElement = function VeUiPreviewElement( model, config ) {
	config = config || {};
 
	// Parent constructor
	ve.ui.PreviewElement.super.call( this, config );
 
	// Mixin constructor
	OO.EventEmitter.call( this );
 
	this.useView = !!config.useView;
 
	if ( model ) {
		this.setModel( model );
	}
 
	// Initialize
	this.$element.addClass( 've-ui-previewElement' );
};
 
/**
 * The element rendering has been updated
 *
 * @event ve.ui.PreviewElement#render
 */
 
/* Inheritance */
 
OO.inheritClass( ve.ui.PreviewElement, OO.ui.Element );
 
OO.mixinClass( ve.ui.PreviewElement, OO.EventEmitter );
 
/**
 * Destroy the preview node.
 */
ve.ui.PreviewElement.prototype.destroy = function () {
	if ( this.view ) {
		this.view.destroy();
		this.view = null;
	}
};
 
/**
 * Set the model node for the preview
 *
 * @param {ve.dm.Node} model Model from which to create a preview
 */
ve.ui.PreviewElement.prototype.setModel = function ( model ) {
	this.model = model;
	this.updatePreview();
};
 
/**
 * Modify DOM node before appending to the preview
 *
 * @param {HTMLElement} element Element to be appended
 */
ve.ui.PreviewElement.prototype.beforeAppend = function ( element ) {
	// Remove slugs and nails. This used to be done in CSS but triggered
	// a catastrophic browser bug in Chrome (T341901)
	Array.prototype.forEach.call( element.querySelectorAll( '.ve-ce-nail, .ve-ce-branchNode-slug' ), ( el ) => {
		el.parentNode.removeChild( el );
	} );
	ve.targetLinksToNewWindow( element );
};
 
/**
 * Replace the content of the body with the model DOM
 *
 * Doesn't use jQuery to avoid document switching performance bug
 */
ve.ui.PreviewElement.prototype.replaceWithModelDom = function () {
	const htmlDocument = ve.dm.converter.getDomFromNode( this.model, ve.dm.Converter.static.PREVIEW_MODE ),
		body = htmlDocument.body,
		element = this.$element[ 0 ];
 
	// Resolve attributes (in particular, expand 'href' and 'src' using the right base)
	ve.resolveAttributes(
		body,
		this.model.getDocument().getHtmlDocument(),
		ve.dm.Converter.static.computedAttributes
	);
 
	this.beforeAppend( body );
 
	// Move content to element
	element.innerHTML = '';
	while ( body.childNodes.length ) {
		element.appendChild(
			element.ownerDocument.adoptNode( body.childNodes[ 0 ] )
		);
	}
 
	this.afterRender();
};
 
/**
 * Update the preview
 */
ve.ui.PreviewElement.prototype.updatePreview = function () {
	// Initial CE node
	this.view = ve.ce.nodeFactory.createFromModel( this.model );
	this.beforeAppend( this.view.$element[ 0 ] );
	this.$element.append( this.view.$element );
	this.view.setLive( true );
 
	ve.ce.GeneratedContentNode.static.awaitGeneratedContent( this.view )
		.then( () => {
			// When all children are rerendered, replace with DM DOM for a better preview.
			// Conversion should be pretty fast, but avoid this (by setting useView to true)
			// if you generating a lot of previews, e.g. in a list
			if ( !this.useView ) {
				// Verify that the PreviewElement hasn't been destroyed.
				Eif ( this.view ) {
					this.replaceWithModelDom();
				}
			} else {
				this.afterRender();
			}
		} );
};
 
/**
 * Cleanup and emit events after render
 *
 * @fires ve.ui.PreviewElement#render
 */
ve.ui.PreviewElement.prototype.afterRender = function () {
	// Cleanup
	this.view.destroy();
	this.view = null;
 
	// Event
	this.emit( 'render' );
};
 
/**
 * Check if the preview is still generating
 *
 * @return {boolean} Still generating
 */
ve.ui.PreviewElement.prototype.isGenerating = function () {
	return !!this.view;
};