UNPKG

1.84 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5
6/**
7 * @module list/documentlistproperties/converters
8 */
9
10/**
11 * Returns a converter that consumes the `style`, `reversed`, and `start` attributes.
12 * In `style`, it searches for the `list-style-type` definition.
13 * If not found, the `"default"` value will be used.
14 *
15 * @protected
16 * @param {module:list/documentlistproperties/documentlistpropertiesediting~AttributeStrategy} strategy
17 * @returns {Function}
18 */
19export function listPropertiesUpcastConverter( strategy ) {
20 return ( evt, data, conversionApi ) => {
21 const { writer, schema, consumable } = conversionApi;
22
23 // If there is no view consumable to consume, set the default attribute value to be able to reconvert nested lists on parent change.
24 // So abort converting if attribute was directly consumed.
25 if ( consumable.test( data.viewItem, strategy.viewConsumables ) === false ) {
26 return;
27 }
28
29 if ( !data.modelRange ) {
30 Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
31 }
32
33 let applied = false;
34
35 for ( const item of data.modelRange.getItems( { shallow: true } ) ) {
36 if ( !schema.checkAttribute( item, strategy.attributeName ) ) {
37 continue;
38 }
39
40 if ( !strategy.appliesToListItem( item ) ) {
41 continue;
42 }
43
44 // Set list attributes only on same level items, those nested deeper are already handled by the recursive conversion.
45 if ( item.hasAttribute( strategy.attributeName ) ) {
46 continue;
47 }
48
49 writer.setAttribute( strategy.attributeName, strategy.getAttributeOnUpcast( data.viewItem ), item );
50 applied = true;
51 }
52
53 if ( applied ) {
54 consumable.consume( data.viewItem, strategy.viewConsumables );
55 }
56 };
57}