Skip to content

Commit

Permalink
Merge pull request #7623 from ckeditor/i/7608
Browse files Browse the repository at this point in the history
Other (engine): Changed arguments of `Element#is()`, `Text#is()`, `TextProxy#is()`, `AttributeElement#is()`, `ContainerElement#is()`, `EditableElement#is()`, `EmptyElement#is()`, `UIElement#is()` methods and all it's usages. Closes #7608.

Internal: Usages of `is()` methods updated to the latest API change.

MAJOR BREAKING CHANGE (engine): Changed expected argument for model's [`Text#is()`](https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_model_text-Text.html#function-is) and [`TextProxy#is()`](https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_model_textproxy-TextProxy.html#function-is) methods (`'text'` replaced with `'$text'` and `'textProxy'` replaced with `'$textProxy'`).

MAJOR BREAKING CHANGE (engine): Changed expected argument for views's [`Text#is()`](https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_text-Text.html#function-is) and [`TextProxy#is()`](https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_view_textproxy-TextProxy.html#function-is) methods (`'text'` replaced with `'$text'` and `'textProxy'` replaced with `'$textProxy'`).

MAJOR BREAKING CHANGE (engine): Changed expected arguments for model's `Element#is()`, `Text#is()` and `TextProxy#is()` (element name is no longer accepted as a first argument, type argument is required for name checks).

MAJOR BREAKING CHANGE (engine): Changed expected arguments for view's `Element#is()`, `Text#is()`, `TextProxy#is()`, `AttributeElement#is()`, `ContainerElement#is()`, `EditableElement#is()`, `EmptyElement#is()`, `UIElement#is()` (element name is no longer accepted as a first argument, type argument is required for name checks).
  • Loading branch information
jodator authored Jul 21, 2020
2 parents ec2b8a7 + d52a22d commit dbee479
Show file tree
Hide file tree
Showing 130 changed files with 385 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function blockAutoformatEditing( editor, plugin, pattern, callbac
const blockToFormat = entry.position.parent;

// Block formatting should trigger only if the entire content of a paragraph is a single text node... (see ckeditor5#5671).
if ( !blockToFormat.is( 'paragraph' ) || blockToFormat.childCount !== 1 ) {
if ( !blockToFormat.is( 'element', 'paragraph' ) || blockToFormat.childCount !== 1 ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function getTextAfterCode( range, model ) {

const text = Array.from( range.getItems() ).reduce( ( rangeText, node ) => {
// Trim text to a last occurrence of an inline element and update range start.
if ( !( node.is( 'text' ) || node.is( 'textProxy' ) ) || node.getAttribute( 'code' ) ) {
if ( !( node.is( '$text' ) || node.is( '$textProxy' ) ) || node.getAttribute( 'code' ) ) {
start = model.createPositionAfter( node );

return '';
Expand Down
11 changes: 7 additions & 4 deletions packages/ckeditor5-block-quote/src/blockquoteediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export default class BlockQuoteEditing extends Plugin {
continue;
}

if ( element.is( 'blockQuote' ) && element.isEmpty ) {
if ( element.is( 'element', 'blockQuote' ) && element.isEmpty ) {
// Added an empty blockQuote - remove it.
writer.remove( element );

return true;
} else if ( element.is( 'blockQuote' ) && !schema.checkChild( entry.position, element ) ) {
} else if ( element.is( 'element', 'blockQuote' ) && !schema.checkChild( entry.position, element ) ) {
// Added a blockQuote in incorrect place - most likely inside another blockQuote. Unwrap it
// so the content inside is not lost.
writer.unwrap( element );
Expand All @@ -78,7 +78,10 @@ export default class BlockQuoteEditing extends Plugin {
const range = writer.createRangeIn( element );

for ( const child of range.getItems() ) {
if ( child.is( 'blockQuote' ) && !schema.checkChild( writer.createPositionBefore( child ), child ) ) {
if (
child.is( 'element', 'blockQuote' ) &&
!schema.checkChild( writer.createPositionBefore( child ), child )
) {
writer.unwrap( child );

return true;
Expand All @@ -88,7 +91,7 @@ export default class BlockQuoteEditing extends Plugin {
} else if ( entry.type == 'remove' ) {
const parent = entry.position.parent;

if ( parent.is( 'blockQuote' ) && parent.isEmpty ) {
if ( parent.is( 'element', 'blockQuote' ) && parent.isEmpty ) {
// Something got removed and now blockQuote is empty. Remove the blockQuote as well.
writer.remove( parent );

Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-clipboard/src/utils/viewtoplaintext.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const smallPaddingElements = [ 'figcaption', 'li' ];
export default function viewToPlainText( viewItem ) {
let text = '';

if ( viewItem.is( 'text' ) || viewItem.is( 'textProxy' ) ) {
if ( viewItem.is( '$text' ) || viewItem.is( '$textProxy' ) ) {
// If item is `Text` or `TextProxy` simple take its text data.
text = viewItem.data;
} else if ( viewItem.is( 'img' ) && viewItem.hasAttribute( 'alt' ) ) {
} else if ( viewItem.is( 'element', 'img' ) && viewItem.hasAttribute( 'alt' ) ) {
// Special case for images - use alt attribute if it is provided.
text = viewItem.getAttribute( 'alt' );
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/ckeditor5-code-block/src/codeblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class CodeBlockCommand extends Command {
_getValue() {
const selection = this.editor.model.document.selection;
const firstBlock = first( selection.getSelectedBlocks() );
const isCodeBlock = !!( firstBlock && firstBlock.is( 'codeBlock' ) );
const isCodeBlock = !!( firstBlock && firstBlock.is( 'element', 'codeBlock' ) );

return isCodeBlock ? firstBlock.getAttribute( 'language' ) : false;
}
Expand Down Expand Up @@ -132,13 +132,13 @@ export default class CodeBlockCommand extends Command {
* @param {Array.<module:engine/model/element~Element>} blocks
*/
_removeCodeBlock( writer, blocks ) {
const codeBlocks = blocks.filter( block => block.is( 'codeBlock' ) );
const codeBlocks = blocks.filter( block => block.is( 'element', 'codeBlock' ) );

for ( const block of codeBlocks ) {
const range = writer.createRangeOn( block );

for ( const item of Array.from( range.getItems() ).reverse() ) {
if ( item.is( 'softBreak' ) && item.parent.is( 'codeBlock' ) ) {
if ( item.is( 'element', 'softBreak' ) && item.parent.is( 'element', 'codeBlock' ) ) {
const { position } = writer.split( writer.createPositionBefore( item ) );

writer.rename( position.nodeAfter, 'paragraph' );
Expand Down
16 changes: 8 additions & 8 deletions packages/ckeditor5-code-block/src/codeblockediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class CodeBlockEditing extends Plugin {
this.listenTo( editor.editing.view.document, 'clipboardInput', ( evt, data ) => {
const modelSelection = model.document.selection;

if ( !modelSelection.anchor.parent.is( 'codeBlock' ) ) {
if ( !modelSelection.anchor.parent.is( 'element', 'codeBlock' ) ) {
return;
}

Expand All @@ -156,7 +156,7 @@ export default class CodeBlockEditing extends Plugin {
this.listenTo( model, 'getSelectedContent', ( evt, [ selection ] ) => {
const anchor = selection.anchor;

if ( selection.isCollapsed || !anchor.parent.is( 'codeBlock' ) || !anchor.hasSameParentAs( selection.focus ) ) {
if ( selection.isCollapsed || !anchor.parent.is( 'element', 'codeBlock' ) || !anchor.hasSameParentAs( selection.focus ) ) {
return;
}

Expand Down Expand Up @@ -210,7 +210,7 @@ export default class CodeBlockEditing extends Plugin {
this.listenTo( editor.editing.view.document, 'enter', ( evt, data ) => {
const positionParent = editor.model.document.selection.getLastPosition().parent;

if ( !positionParent.is( 'codeBlock' ) ) {
if ( !positionParent.is( 'element', 'codeBlock' ) ) {
return;
}

Expand Down Expand Up @@ -248,7 +248,7 @@ function breakLineOnEnter( editor ) {
let leadingWhiteSpaces;

// Figure out the indentation (white space chars) at the beginning of the line.
if ( node && node.is( 'text' ) ) {
if ( node && node.is( '$text' ) ) {
leadingWhiteSpaces = getLeadingWhiteSpaces( node );
}

Expand Down Expand Up @@ -287,7 +287,7 @@ function leaveBlockStartOnEnter( editor, isSoftEnter ) {
return false;
}

if ( !nodeAfter || !nodeAfter.is( 'softBreak' ) ) {
if ( !nodeAfter || !nodeAfter.is( 'element', 'softBreak' ) ) {
return false;
}

Expand Down Expand Up @@ -350,7 +350,7 @@ function leaveBlockEndOnEnter( editor, isSoftEnter ) {
//
// <codeBlock>foo[<softBreak></softBreak>]</codeBlock>
//
if ( nodeBefore.is( 'softBreak' ) ) {
if ( nodeBefore.is( 'element', 'softBreak' ) ) {
emptyLineRangeToRemoveOnEnter = model.createRangeOn( nodeBefore );
}

Expand All @@ -367,10 +367,10 @@ function leaveBlockEndOnEnter( editor, isSoftEnter ) {
// <codeBlock>foo[<softBreak></softBreak> ]</codeBlock>
//
else if (
nodeBefore.is( 'text' ) &&
nodeBefore.is( '$text' ) &&
!nodeBefore.data.match( /\S/ ) &&
nodeBefore.previousSibling &&
nodeBefore.previousSibling.is( 'softBreak' )
nodeBefore.previousSibling.is( 'element', 'softBreak' )
) {
emptyLineRangeToRemoveOnEnter = model.createRange(
model.createPositionBefore( nodeBefore.previousSibling ), model.createPositionAfter( nodeBefore )
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-code-block/src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function dataViewToModelCodeBlockInsertion( editingView, languageDefs ) {
const viewItem = data.viewItem;
const viewChild = viewItem.getChild( 0 );

if ( !viewChild || !viewChild.is( 'code' ) ) {
if ( !viewChild || !viewChild.is( 'element', 'code' ) ) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-code-block/src/outdentcodeblockcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ function getCodeLineTextNodeAtPosition( position ) {

// <codeBlock>foo^</codeBlock>
// <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
if ( !nodeAtPosition || nodeAtPosition.is( 'softBreak' ) ) {
if ( !nodeAtPosition || nodeAtPosition.is( 'element', 'softBreak' ) ) {
nodeAtPosition = position.nodeBefore;
}

// <codeBlock>^</codeBlock>
// <codeBlock>foo^<softBreak></softBreak>bar</codeBlock>
if ( !nodeAtPosition || nodeAtPosition.is( 'softBreak' ) ) {
if ( !nodeAtPosition || nodeAtPosition.is( 'element', 'softBreak' ) ) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-code-block/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function getIndentOutdentPositions( model ) {
} );

for ( const { item } of walker ) {
if ( item.is( 'textProxy' ) && item.parent.is( 'codeBlock' ) ) {
if ( item.is( '$textProxy' ) && item.parent.is( 'element', 'codeBlock' ) ) {
const leadingWhiteSpaces = getLeadingWhiteSpaces( item.textNode );
const { parent, startOffset } = item.textNode;

Expand All @@ -213,5 +213,5 @@ export function getIndentOutdentPositions( model ) {
export function isModelSelectionInCodeBlock( selection ) {
const firstBlock = first( selection.getSelectedBlocks() );

return firstBlock && firstBlock.is( 'codeBlock' );
return firstBlock && firstBlock.is( 'element', 'codeBlock' );
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function downcastCustomClasses( modelElementName ) {
function findViewChild( viewElement, viewElementName, conversionApi ) {
const viewChildren = Array.from( conversionApi.writer.createRangeIn( viewElement ).getItems() );

return viewChildren.find( item => item.is( viewElementName ) );
return viewChildren.find( item => item.is( 'element', viewElementName ) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/ckeditor5-engine/src/conversion/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export default class Conversion {
* viewElement => {
* const fontWeight = viewElement.getStyle( 'font-weight' );
*
* if ( viewElement.is( 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
* if ( viewElement.is( 'element', 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
Expand Down Expand Up @@ -388,7 +388,7 @@ export default class Conversion {
*
* const size = Number( match[ 1 ] );
*
* if ( viewElement.is( 'span' ) && size > 10 ) {
* if ( viewElement.is( 'element', 'span' ) && size > 10 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
Expand All @@ -413,7 +413,7 @@ export default class Conversion {
*
* const size = Number( match[ 1 ] );
*
* if ( viewElement.is( 'span' ) && size < 10 ) {
* if ( viewElement.is( 'element', 'span' ) && size < 10 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ function highlightText( highlightDescriptor ) {
return;
}

if ( !( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) && !data.item.is( 'textProxy' ) ) {
if ( !( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) && !data.item.is( '$textProxy' ) ) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/ckeditor5-engine/src/conversion/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ export default class Mapper {
// viewBlock == viewParent, so we need to calculate the offset in the parent element.

// If the position is a text it is simple ("ba|r" -> 2).
if ( viewParent.is( 'text' ) ) {
if ( viewParent.is( '$text' ) ) {
return viewOffset;
}

Expand Down Expand Up @@ -488,7 +488,7 @@ export default class Mapper {
return callback( viewNode );
} else if ( this._viewToModelMapping.has( viewNode ) ) {
return 1;
} else if ( viewNode.is( 'text' ) ) {
} else if ( viewNode.is( '$text' ) ) {
return viewNode.data.length;
} else if ( viewNode.is( 'uiElement' ) ) {
return 0;
Expand Down Expand Up @@ -537,7 +537,7 @@ export default class Mapper {
let viewOffset = 0;

// In the text node it is simple: offset in the model equals offset in the text.
if ( viewParent.is( 'text' ) ) {
if ( viewParent.is( '$text' ) ) {
return new ViewPosition( viewParent, expectedOffset );
}

Expand Down Expand Up @@ -631,7 +631,7 @@ export default class Mapper {
* const sibling = data.modelPosition.nodeBefore;
*
* // Check if this is the element we are interested in.
* if ( !sibling.is( 'customElement' ) ) {
* if ( !sibling.is( 'element', 'customElement' ) ) {
* return;
* }
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export default class UpcastDispatcher {

if ( viewItem.is( 'element' ) ) {
this.fire( 'element:' + viewItem.name, data, this.conversionApi );
} else if ( viewItem.is( 'text' ) ) {
} else if ( viewItem.is( '$text' ) ) {
this.fire( 'text', data, this.conversionApi );
} else {
this.fire( 'documentFragment', data, this.conversionApi );
Expand Down
10 changes: 5 additions & 5 deletions packages/ckeditor5-engine/src/conversion/viewconsumable.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class ViewConsumable {
let elementConsumables;

// For text nodes and document fragments just mark them as consumable.
if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
if ( element.is( '$text' ) || element.is( 'documentFragment' ) ) {
this._consumables.set( element, true );

return;
Expand Down Expand Up @@ -134,7 +134,7 @@ export default class ViewConsumable {
}

// For text nodes and document fragments return stored boolean value.
if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
if ( element.is( '$text' ) || element.is( 'documentFragment' ) ) {
return elementConsumables;
}

Expand Down Expand Up @@ -172,7 +172,7 @@ export default class ViewConsumable {
*/
consume( element, consumables ) {
if ( this.test( element, consumables ) ) {
if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
if ( element.is( '$text' ) || element.is( 'documentFragment' ) ) {
// For text nodes and document fragments set value to false.
this._consumables.set( element, false );
} else {
Expand Down Expand Up @@ -218,7 +218,7 @@ export default class ViewConsumable {
const elementConsumables = this._consumables.get( element );

if ( elementConsumables !== undefined ) {
if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
if ( element.is( '$text' ) || element.is( 'documentFragment' ) ) {
// For text nodes and document fragments - set consumable to true.
this._consumables.set( element, true );
} else {
Expand Down Expand Up @@ -287,7 +287,7 @@ export default class ViewConsumable {
instance = new ViewConsumable( from );
}

if ( from.is( 'text' ) ) {
if ( from.is( '$text' ) ) {
instance.add( from );

return instance;
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-engine/src/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function stringify( node, selectionOrPositionOrRange = null, markers = nu

downcastDispatcher.on( 'insert:$text', insertText() );
downcastDispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection || data.item.is( 'textProxy' ) ) {
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection || data.item.is( '$textProxy' ) ) {
const converter = wrap( ( modelAttributeValue, viewWriter ) => {
return viewWriter.createAttributeElement(
'model-text-with-attributes',
Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-engine/src/dev-utils/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class RangeParser {
}
}

if ( node.is( 'text' ) ) {
if ( node.is( '$text' ) ) {
const regexp = new RegExp(
`[${ TEXT_RANGE_START_TOKEN }${ TEXT_RANGE_END_TOKEN }\\${ ELEMENT_RANGE_END_TOKEN }\\${ ELEMENT_RANGE_START_TOKEN }]`,
'g'
Expand Down Expand Up @@ -688,7 +688,7 @@ class ViewStringify {
}
}

if ( root.is( 'text' ) ) {
if ( root.is( '$text' ) ) {
callback( this._stringifyTextRanges( root ) );
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-engine/src/model/differ.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ function _getChildrenSnapshot( children ) {
const snapshot = [];

for ( const child of children ) {
if ( child.is( 'text' ) ) {
if ( child.is( '$text' ) ) {
for ( let i = 0; i < child.data.length; i++ ) {
snapshot.push( {
name: '$text',
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-engine/src/model/documentfragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export default class DocumentFragment {
// @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
// @if CK_DEBUG_ENGINE // string += '\n';

// @if CK_DEBUG_ENGINE // if ( child.is( 'text' ) ) {
// @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
// @if CK_DEBUG_ENGINE // const textAttrs = stringifyMap( child._attrs );

// @if CK_DEBUG_ENGINE // string += '\t'.repeat( 1 );
Expand Down
Loading

0 comments on commit dbee479

Please sign in to comment.