• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ckeditor / ckeditor5 / 25927

pending completion
25927

Pull #14763

CKEditor5 code coverage

web-flow
Merge pull request #14753 from ckeditor/ck/14743-enablePlaceholder-API-should-remain-backward-compatible-for-some-time

Fix (engine): Made the `enablePlaceholder()` API to remain backward compatible for the deprecation period. It will be removed in the future. Closes #14743.
Pull Request #14763: Support for image height attribute

12436 of 12436 branches covered (100.0%)

Branch coverage included in aggregate %.

147 of 147 new or added lines in 10 files covered. (100.0%)

32669 of 32669 relevant lines covered (100.0%)

12002.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/packages/ckeditor5-alignment/src/alignmentcommand.ts
1
/**
2
 * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
3
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
 */
5

6
/**
7
 * @module alignment/alignmentcommand
8
 */
9

10
import { Command } from 'ckeditor5/src/core.js';
11
import { first } from 'ckeditor5/src/utils.js';
12
import type { Element, Writer } from 'ckeditor5/src/engine.js';
13

14
import { isDefault } from './utils.js';
15
import type { SupportedOption } from './alignmentconfig.js';
16

17
const ALIGNMENT = 'alignment';
1✔
18

19
/**
20
 * The alignment command plugin.
21
 */
22
export default class AlignmentCommand extends Command {
23
        /**
24
         * A value of the current block's alignment.
25
         *
26
         * @observable
27
         * @readonly
28
         */
29
        declare public value: SupportedOption;
30

31
        /**
32
         * @inheritDoc
33
         */
34
        public override refresh(): void {
35
                const editor = this.editor;
177✔
36
                const locale = editor.locale;
177✔
37
                const firstBlock = first( this.editor.model.document.selection.getSelectedBlocks() )!;
177✔
38

39
                // As first check whether to enable or disable the command as the value will always be false if the command cannot be enabled.
40
                this.isEnabled = Boolean( firstBlock ) && this._canBeAligned( firstBlock );
177✔
41

42
                if ( this.isEnabled && firstBlock.hasAttribute( 'alignment' ) ) {
177✔
43
                        this.value = firstBlock.getAttribute( 'alignment' ) as SupportedOption;
39✔
44
                } else {
45
                        this.value = locale.contentLanguageDirection === 'rtl' ? 'right' : 'left';
138✔
46
                }
47
        }
48

49
        /**
50
         * Executes the command. Applies the alignment `value` to the selected blocks.
51
         * If no `value` is passed, the `value` is the default one or it is equal to the currently selected block's alignment attribute,
52
         * the command will remove the attribute from the selected blocks.
53
         *
54
         * @param options Options for the executed command.
55
         * @param options.value The value to apply.
56
         * @fires execute
57
         */
58
        public override execute( options: { value?: SupportedOption } = {} ): void {
3✔
59
                const editor = this.editor;
23✔
60
                const locale = editor.locale;
23✔
61
                const model = editor.model;
23✔
62
                const doc = model.document;
23✔
63

64
                const value = options.value!;
23✔
65

66
                model.change( writer => {
23✔
67
                        // Get only those blocks from selected that can have alignment set
68
                        const blocks = Array.from( doc.selection.getSelectedBlocks() ).filter( block => this._canBeAligned( block ) );
33✔
69
                        const currentAlignment = blocks[ 0 ].getAttribute( 'alignment' );
23✔
70

71
                        // Remove alignment attribute if current alignment is:
72
                        // - default (should not be stored in model as it will bloat model data)
73
                        // - equal to currently set
74
                        // - or no value is passed - denotes default alignment.
75
                        const removeAlignment = isDefault( value, locale ) || currentAlignment === value || !value;
23✔
76

77
                        if ( removeAlignment ) {
23✔
78
                                removeAlignmentFromSelection( blocks, writer );
11✔
79
                        } else {
80
                                setAlignmentOnSelection( blocks, writer, value );
12✔
81
                        }
82
                } );
83
        }
84

85
        /**
86
         * Checks whether a block can have alignment set.
87
         *
88
         * @param block The block to be checked.
89
         */
90
        private _canBeAligned( block: Element ) {
91
                return this.editor.model.schema.checkAttribute( block, ALIGNMENT );
175✔
92
        }
93
}
94

95
/**
96
 * Removes the alignment attribute from blocks.
97
 */
98
function removeAlignmentFromSelection( blocks: Array<Element>, writer: Writer ) {
99
        for ( const block of blocks ) {
11✔
100
                writer.removeAttribute( ALIGNMENT, block );
15✔
101
        }
102
}
103

104
/**
105
 * Sets the alignment attribute on blocks.
106
 */
107
function setAlignmentOnSelection( blocks: Array<Element>, writer: Writer, alignment: string ) {
108
        for ( const block of blocks ) {
12✔
109
                writer.setAttribute( ALIGNMENT, alignment, block );
17✔
110
        }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc