All files / mobile.editor.overlay identifyLeadParagraph.js

100% Statements 17/17
87.5% Branches 7/8
100% Functions 3/3
100% Lines 17/17

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                1x           20x           20x   20x 20x 16x 16x 12x   4x 4x     4x     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 ) {
	var $paragraphs, i, node;
 
	// 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 ) {
		var $coords;
		node = node.cloneNode( true );
		// Ignore non-content nodes
		$( node ).find( '.ve-ce-branchNode-inlineSlug, .ve-ce-focusableNode-invisible' ).remove();
		if ( isNotEmptyNode( node ) ) {
			$coords = $( node ).find( 'span#coordinates' );
			if ( !$coords.length ) {
				return false;
			}
			Eif ( node.textContent ) {
				return node.textContent === $coords[ 0 ].textContent;
			}
		}
		return true;
	}
 
	$paragraphs = $body.children( 'p' );
	for ( i = 0; i < $paragraphs.length; i++ ) {
		node = $paragraphs[ i ];
		if ( !isNonLeadParagraph( node ) ) {
			return node;
		}
	}
	return null;
};