Skip to content

Commit

Permalink
Minor refactor of Schema uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed May 13, 2024
1 parent e22cc0f commit cee5c5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
29 changes: 8 additions & 21 deletions packages/ckeditor5-code-block/src/codeblockediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export default class CodeBlockEditing extends Plugin {
const schema = editor.model.schema;
const model = editor.model;
const view = editor.editing.view;
const listEditing: ListEditing | null = editor.plugins.has( 'ListEditing' ) ?
editor.plugins.get( 'ListEditing' ) : null;

const normalizedLanguagesDefs = getNormalizedAndLocalizedLanguageDefinitions( editor );

Expand Down Expand Up @@ -133,32 +131,21 @@ export default class CodeBlockEditing extends Plugin {
schema.register( 'codeBlock', {
allowWhere: '$block',
allowChildren: '$text',
isBlock: true,
allowAttributes: [ 'language' ]
// `$inlineObject` inherits from `$text`, so it would be allowed by `codeBlock` element. This would allow elements like inline
// image inside a code block. This is an incorrect situation, as we want only text inside `codeBlock`.
disallowChildren: '$inlineObject',
allowAttributes: [ 'language' ],
allowAttributesOf: '$listItem',
isBlock: true
} );

// Allow all list* attributes on `codeBlock` (integration with DocumentList).
// Disallow all attributes on $text inside `codeBlock`.
schema.addAttributeCheck( ( context, attributeName ) => {
if (
context.endsWith( 'codeBlock' ) &&
listEditing && listEditing.getListAttributeNames().includes( attributeName )
) {
return true;
}

// Disallow all attributes on `$text` inside `codeBlock`.
schema.addAttributeCheck( ( context ) => {
if ( context.endsWith( 'codeBlock $text' ) ) {
return false;
}
} );

// Disallow object elements inside `codeBlock`. See #9567.
editor.model.schema.addChildCheck( ( context, childDefinition ) => {
if ( context.endsWith( 'codeBlock' ) && childDefinition.isObject ) {
return false;
}
} );

// Conversion.
editor.editing.downcastDispatcher.on<DowncastInsertEvent>(
'insert:codeBlock',
Expand Down
16 changes: 7 additions & 9 deletions packages/ckeditor5-code-block/tests/codeblockediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ describe( 'CodeBlockEditing', () => {
expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.be.false;
} );

it( 'disallows object elements in codeBlock', () => {
// Fake "inline-widget".
model.schema.register( 'inline-widget', {
inheritAllFrom: '$block',
// Allow to be a child of the `codeBlock` element.
allowIn: 'codeBlock',
// And mark as an object.
isObject: true
it( 'disallows $inlineObject', () => {
// `$inlineObject` inherits from `$text`, so it would be allowed by `codeBlock` element. This would allow elements like inline
// image inside a code block. This is an incorrect situation, as we want only text inside `codeBlock`.
model.schema.register( 'inlineWidget', {
inheritAllFrom: '$inlineObject'
} );

expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'inline-widget' ) ).to.be.false;
expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$inlineObject' ) ).to.be.false;
expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'inlineWidget' ) ).to.be.false;
} );

it( 'allows only for $text in codeBlock', () => {
Expand Down
14 changes: 4 additions & 10 deletions packages/ckeditor5-image/src/image/imageinlineediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,10 @@ export default class ImageInlineEditing extends Plugin {
// Converters 'alt' and 'srcset' are added in 'ImageEditing' plugin.
schema.register( 'imageInline', {
inheritAllFrom: '$inlineObject',
allowAttributes: [ 'alt', 'src', 'srcset' ]
} );

// Disallow inline images in captions (for now). This is the best spot to do that because
// independent packages can introduce captions (ImageCaption, TableCaption, etc.) so better this
// be future-proof.
schema.addChildCheck( ( context, childDefinition ) => {
if ( context.endsWith( 'caption' ) && childDefinition.name === 'imageInline' ) {
return false;
}
allowAttributes: [ 'alt', 'src', 'srcset' ],
// Disallow inline images in captions (at least for now).
// This is the best spot to do that because independent packages can introduce captions (ImageCaption, TableCaption, etc.).
disallowIn: [ 'caption' ]
} );

this._setupConversion();
Expand Down

0 comments on commit cee5c5c

Please sign in to comment.