All files / mobile.editor.overlay identifyLeadParagraph.js

94.11% Statements 16/17
83.33% Branches 5/6
100% Functions 3/3
93.75% Lines 15/16

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                1x       20x         20x 20x   20x       20x 20x 15x   5x     16x 16x 20x 20x 15x     1x    
/* global $ */
/**
 * Find first paragraph that has text content, i.e. paragraphs that are not empty.
 * Keep in sync with MoveLeadParagraphTransform::identifyLeadParagraph().
 *
 * @param {jQuery} $body Where to search for paragraphs
 * @return {Node|null} The lead paragraph
 */
module.exports = function identifyLeadParagraph( $body ) {
	// Keep in sync with MoveLeadParagraphTransform::isNotEmptyNode()
	function isNotEmptyNode( node ) {
		// Ignore VE whitespace characters
		return !/^[\s↵➞]*$/.test( node.textContent );
	}
 
	// Keep in sync with MoveLeadParagraphTransform::isNonLeadParagraph()
	function isNonLeadParagraph( node ) {
		node = node.cloneNode( true );
		var $node = $( node );
		// The paragraph itself can be an invisible template (T293834)
		Iif ( $node.hasClass( 've-ce-focusableNode-invisible' ) ) {
			return true;
		}
		// Ignore non-content nodes, TemplateStyles and coordinates
		$node.find( '.ve-ce-branchNode-inlineSlug, .ve-ce-focusableNode-invisible, style, span#coordinates' ).remove();
		if ( isNotEmptyNode( node ) ) {
			return false;
		}
		return true;
	}
 
	var $paragraphs = $body.children( 'p' );
	for ( var i = 0; i < $paragraphs.length; i++ ) {
		var p = $paragraphs[ i ];
		if ( !isNonLeadParagraph( p ) ) {
			return p;
		}
	}
	return null;
};