Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EmptyBlock plugin that prevents adding   in exported data. #17756

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
37d57c4
Init `EmptyBlocks` plugin prototype.
Mati365 Jan 15, 2025
9efcd01
Add tests.
Mati365 Jan 15, 2025
fe051c7
Minor fixes in docs after CR.
Mati365 Jan 17, 2025
e57d7f5
Adjust checks
Mati365 Jan 17, 2025
5e61837
Add table tests.
Mati365 Jan 17, 2025
99cef94
Table autoparagraphing fixes.
Mati365 Jan 17, 2025
2e5c624
Add more tests.
Mati365 Jan 17, 2025
19c2a63
Code align improvements.
Mati365 Jan 17, 2025
e6a035e
Add consume downcasted attributes.
Mati365 Jan 20, 2025
df9a25b
Better code split.
Mati365 Jan 20, 2025
581252f
Improve priorities.
Mati365 Jan 20, 2025
182a351
Add data checks for list item tests.
Mati365 Jan 20, 2025
9a930db
Apply table CR remarks.
Mati365 Jan 20, 2025
8187cc9
Adjust docs.
Mati365 Jan 20, 2025
99f91c7
Adjust table cell downcasts after CR.
Mati365 Jan 20, 2025
dcb6876
Simplified implementation.
niegowski Jan 20, 2025
4b6b2d4
Block filler handling adjusted to be able to detect presence of it wh…
niegowski Jan 21, 2025
063f1f2
Fixed block filler handling.
niegowski Jan 21, 2025
bc6bca7
Added tests.
niegowski Jan 22, 2025
e0d570d
Added code comments.
niegowski Jan 22, 2025
b6ed3d5
Added EmptyBlock integration tests.
niegowski Jan 22, 2025
890234f
Added EmptyBlock integration tests.
niegowski Jan 22, 2025
a6c2fd6
Updated tests.
niegowski Jan 22, 2025
6326054
Typo fix.
niegowski Jan 22, 2025
2716c5d
Merge branch 'master' into ck/skip-block-filler-option
niegowski Jan 22, 2025
3a489d0
Added missing dev dependency.
niegowski Jan 23, 2025
48ff505
Fix typos.
Mati365 Jan 24, 2025
192eba5
Add clipboard integration to `EmptyBlock` plugin. (#17805)
Mati365 Jan 24, 2025
b6400a5
Assign editor in manual tests to window variables.
Mati365 Jan 27, 2025
d4b60a8
Add inspector to manual demo.
Mati365 Jan 27, 2025
aafc9b6
Add experimental warning.
Mati365 Jan 28, 2025
7834715
Add GHS empty div test.
Mati365 Jan 28, 2025
72ca176
Fix self import.
Mati365 Jan 28, 2025
4f733a6
Reorder docs.
Mati365 Jan 28, 2025
66fa312
Reorder docs.
Mati365 Jan 28, 2025
5f486b7
Fix newline.
Mati365 Jan 28, 2025
af673d0
Merge remote-tracking branch 'origin' into ck/skip-block-filler-option
Mati365 Jan 30, 2025
d8cafb7
Add configuration for preserving block filler in editing view.
Mati365 Jan 30, 2025
f72b720
Apply CR remarks.
Mati365 Jan 31, 2025
39be496
Apply CR remark.
Mati365 Jan 31, 2025
73d1dbc
Fix typo.
Mati365 Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions packages/ckeditor5-html-support/src/emptyblocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/

/**
* @module html-support/emptyblocks
*/

import { Plugin, type Editor } from 'ckeditor5/src/core.js';
import type {
UpcastElementEvent,
Element,
DowncastDispatcher,
UpcastDispatcher,
DowncastAttributeEvent,
ElementCreatorFunction,
Node
} from 'ckeditor5/src/engine.js';

const EMPTY_BLOCK_MODEL_ATTRIBUTE = 'htmlEmptyBlock';

/**
* This plugin allows for preserving empty block elements in the editor content instead of
* automatically filling them with block fillers (` `).
*
* This is useful when you want to:
*
* * Preserve empty block elements exactly as they were in the source HTML
* * Allow for styling empty blocks with CSS (block fillers can interfere with height/margin)
* * Maintain compatibility with external systems that expect empty blocks to remain empty
*
* For example, this allows for HTML like:
*
* ```html
* <p></p>
* <p class="spacer"></p>
* <td></td>
* ```
* to remain empty instead of being converted to:
*
* ```html
* <p>&nbsp;</p>
* <p class="spacer">&nbsp;</p>
* <td>&nbsp;</td>
* ```
*/
export default class EmptyBlocks extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'EmptyBlocks' as const;
}

/**
* @inheritDoc
*/
public static override get isOfficialPlugin(): true {
return true;
}

/**
* @inheritDoc
*/
public afterInit(): void {
const { model, conversion } = this.editor;
const schema = model.schema;

schema.extend( '$block', {
allowAttributes: [ EMPTY_BLOCK_MODEL_ATTRIBUTE ]
} );
Mati365 marked this conversation as resolved.
Show resolved Hide resolved

schema.extend( '$container', {
allowAttributes: [ EMPTY_BLOCK_MODEL_ATTRIBUTE ]
} );

// Downcasts.
conversion.for( 'dataDowncast' ).add( createEmptyBlocksDowncastDispatcher() );
conversion.for( 'editingDowncast' ).add( createEmptyBlocksDowncastDispatcher() );

// Upcasts.
conversion.for( 'upcast' ).add( createEmptyBlocksUpcastDispatcher( this.editor ) );

// Table related converters.
if ( schema.isRegistered( 'tableCell' ) ) {
schema.extend( 'tableCell', {
allowAttributes: [ EMPTY_BLOCK_MODEL_ATTRIBUTE ]
} );

conversion.for( 'dataDowncast' ).elementToElement( {
model: 'paragraph',
view: convertEmptyBlockParagraphInTableCell(),
converterPriority: 'highest'
} );
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/**
* Converts paragraphs in empty table cells during the downcast conversion.
*/
function convertEmptyBlockParagraphInTableCell(): ElementCreatorFunction {
return ( modelElement, { writer } ) => {
const parentCell = modelElement.parent;

if ( !parentCell!.is( 'element', 'tableCell' ) ) {
return null;
}

if ( parentCell.childCount != 1 ||
!parentCell.hasAttribute( EMPTY_BLOCK_MODEL_ATTRIBUTE ) ||
hasAnyAttribute( modelElement )
) {
return null;
}

const viewElement = writer.createContainerElement( 'p' );

viewElement.getFillerOffset = () => null;
writer.setCustomProperty( 'dataPipeline:transparentRendering', true, viewElement );

return viewElement;
};
}

/**
* Creates a downcast dispatcher for handling empty blocks.
* The dispatcher prevents filler elements from being added to elements marked as empty blocks.
*
* @returns A function that sets up the downcast conversion dispatcher.
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
*/
function createEmptyBlocksDowncastDispatcher() {
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
return ( dispatcher: DowncastDispatcher ) => {
dispatcher.on<DowncastAttributeEvent<Element>>( `attribute:${ EMPTY_BLOCK_MODEL_ATTRIBUTE }`, ( evt, data, conversionApi ) => {
const { mapper, consumable } = conversionApi;
const { item } = data;

if ( !consumable.consume( item, evt.name ) ) {
return;
}

const viewElement = mapper.toViewElement( item as Element );

if ( viewElement && data.attributeNewValue ) {
viewElement.getFillerOffset = () => null;
}
} );
};
}

/**
* Creates an upcast dispatcher for handling empty blocks.
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
* The dispatcher detects empty elements and marks them with the empty block attribute.
*
* @param editor - The editor instance.
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
* @returns A function that sets up the upcast conversion dispatcher.
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
*/
function createEmptyBlocksUpcastDispatcher( editor: Editor ) {
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
const { schema } = editor.model;

return ( dispatcher: UpcastDispatcher ) => {
dispatcher.on<UpcastElementEvent>( 'element', ( evt, data, conversionApi ) => {
const { viewItem, modelRange } = data;

if ( !viewItem.is( 'element' ) || !viewItem.isEmpty ) {
return;
}

const modelElement = modelRange && modelRange.start.nodeAfter as Element;

if ( modelElement && schema.checkAttribute( modelElement, EMPTY_BLOCK_MODEL_ATTRIBUTE ) && viewItem.isEmpty ) {
Mati365 marked this conversation as resolved.
Show resolved Hide resolved
conversionApi.writer.setAttribute( EMPTY_BLOCK_MODEL_ATTRIBUTE, true, modelElement );
}
}, { priority: 'lowest' } );
};
}

/**
* Checks if an element has any attributes set.
*/
function hasAnyAttribute( element: Node ): boolean {
const iteratorItem = element.getAttributeKeys().next();

return !iteratorItem.done;
}
1 change: 1 addition & 0 deletions packages/ckeditor5-html-support/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { default as DataSchema, type DataSchemaBlockElementDefinition } from './
export { default as HtmlComment } from './htmlcomment.js';
export { default as FullPage } from './fullpage.js';
export { default as HtmlPageDataProcessor } from './htmlpagedataprocessor.js';
export { default as EmptyBlocks } from './emptyblocks.js';
export type { GeneralHtmlSupportConfig } from './generalhtmlsupportconfig.js';
export type { default as CodeBlockElementSupport } from './integrations/codeblock.js';
export type { default as CustomElementSupport } from './integrations/customelement.js';
Expand Down
Loading
Loading