All files / src/dm/nodes ve.dm.CheckListItemNode.js

97.36% Statements 37/38
91.66% Branches 11/12
75% Functions 3/4
97.36% Lines 37/38

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                                1x   2x         1x       1x   1x   1x   1x   1x   1x 2x     2x 2x     1x 6x 6x 6x 6x 3x     6x 6x 6x 6x 6x 6x             6x   6x 4x   2x 2x 2x 2x       6x 6x 6x   6x     1x           1x  
/*!
 * VisualEditor DataModel CheckListItemNode class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * DataModel list item node.
 *
 * @class
 * @extends ve.dm.BranchNode
 *
 * @constructor
 * @param {Object} [element] Reference to element in linear model
 * @param {ve.dm.Node[]} [children]
 */
ve.dm.CheckListItemNode = function VeDmCheckListItemNode() {
	// Parent constructor
	ve.dm.CheckListItemNode.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.CheckListItemNode, ve.dm.BranchNode );
 
/* Static Properties */
 
ve.dm.CheckListItemNode.static.name = 'checkListItem';
 
ve.dm.CheckListItemNode.static.parentNodeTypes = [ 'checkList' ];
 
ve.dm.CheckListItemNode.static.matchTagNames = [ 'li' ];
 
ve.dm.CheckListItemNode.static.matchRdfaTypes = [ 've:checkList' ];
 
ve.dm.CheckListItemNode.static.handlesOwnChildren = true;
 
ve.dm.CheckListItemNode.static.toDataElement = function ( domElements, converter ) {
	const checked = domElements[ 0 ].hasAttribute( 'data-checked' ) ||
		// Old HTML format used the invalid attribute "checked"
		domElements[ 0 ].hasAttribute( 'checked' );
	const element = { type: this.name, attributes: { checked: checked } };
	return converter.getDataFromDomClean( domElements[ 0 ], element );
};
 
ve.dm.CheckListItemNode.static.toDomElements = function ( data, doc, converter ) {
	const dataElement = data[ 0 ];
	const listItem = doc.createElement( 'li' );
	listItem.setAttribute( 'rel', 've:checkList' );
	if ( dataElement.attributes.checked ) {
		listItem.setAttribute( 'data-checked', 'checked' );
	}
 
	const contents = data.slice( 1, -1 );
	Eif ( contents.length ) {
		const wrapper = doc.createElement( 'div' );
		converter.getDomSubtreeFromData( contents, wrapper );
		while ( wrapper.firstChild ) {
			listItem.appendChild( wrapper.firstChild );
		}
	}
 
	// Formatting for external paste / preview
	// * Hide the bullet list
	// * Add a unicode checkbox to the text
	const checkboxText = document.createTextNode( dataElement.attributes.checked ? '☑' : '☐' );
	let checkbox;
	if ( converter.isForParser() ) {
		checkbox = checkboxText;
	} else {
		listItem.style.listStyle = 'none';
		checkbox = document.createElement( 'span' );
		checkbox.setAttribute( 'data-ve-ignore', 'true' );
		checkbox.appendChild( checkboxText );
	}
 
	// The first child should be the wrapper paragraph
	const textContainer = listItem.firstChild.nodeType === Node.TEXT_NODE ? listItem : listItem.firstChild;
	textContainer.insertBefore( document.createTextNode( ' ' ), textContainer.firstChild );
	textContainer.insertBefore( checkbox, textContainer.firstChild );
 
	return [ listItem ];
};
 
ve.dm.CheckListItemNode.static.resetAttributesForClone = function ( clonedElement ) {
	clonedElement.attributes.checked = false;
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.CheckListItemNode );