Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Changed: Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Sep 4, 2017
1 parent 9628b76 commit 5837e68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/controller/deletecontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export default function deleteContent( selection, batch, options = {} ) {
return;
}

const schema = batch.document.schema;

// 1. Replace the entire content with paragraph.
// See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) {
if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( schema, selection ) ) {
replaceEntireContentWithParagraph( batch, selection );

return;
Expand Down Expand Up @@ -74,14 +76,14 @@ export default function deleteContent( selection, batch, options = {} ) {
//
// e.g. bold is disallowed for <H1>
// <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
batch.document.schema.removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
schema.removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
}

selection.setCollapsedAt( startPos );

// 4. Autoparagraphing.
// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
if ( shouldAutoparagraph( batch.document, startPos ) ) {
if ( shouldAutoparagraph( schema, startPos ) ) {
insertParagraph( batch, startPos, selection );
}

Expand Down Expand Up @@ -158,9 +160,9 @@ function mergeBranches( batch, startPos, endPos ) {
mergeBranches( batch, startPos, endPos );
}

function shouldAutoparagraph( doc, position ) {
const isTextAllowed = doc.schema.check( { name: '$text', inside: position } );
const isParagraphAllowed = doc.schema.check( { name: 'paragraph', inside: position } );
function shouldAutoparagraph( schema, position ) {
const isTextAllowed = schema.check( { name: '$text', inside: position } );
const isParagraphAllowed = schema.check( { name: 'paragraph', inside: position } );

return !isTextAllowed && isParagraphAllowed;
}
Expand Down
12 changes: 5 additions & 7 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ export default class Schema {
/**
* Removes disallowed by {@link module:engine/model/schema~Schema schema} attributes from given nodes.
* When {@link module:engine/model/batch~Batch batch} parameter is provided then attributes will be removed
* by creating {@link module:engine/model/delta/attributedelta~AttributeDelta attributeDeltas} otherwise
* attributes will be removed directly from provided nodes using {@link module:engine/model/node~Node node} API.
* using that batch, by creating {@link module:engine/model/delta/attributedelta~AttributeDelta attribute deltas}.
* Otherwise, attributes will be removed directly from provided nodes using {@link module:engine/model/node~Node node} API.
*
* @param {Iterable.<module:engine/model/node~Node>} nodes Nodes that will be filtered.
* @param {module:engine/model/schema~SchemaPath} inside Path inside which schema will be checked.
Expand All @@ -429,13 +429,12 @@ export default class Schema {
for ( const node of nodes ) {
const name = node.is( 'text' ) ? '$text' : node.name;
const attributes = Array.from( node.getAttributeKeys() );
const normalizedQueryPath = Schema._normalizeQueryPath( inside );
const queryPath = normalizedQueryPath.join( ' ' );
const queryPath = Schema._normalizeQueryPath( inside );

// When node with attributes is not allowed in current position.
if ( !this.check( { name, attributes, inside: queryPath } ) ) {
// Let's remove attributes one by one.
// This should be improved to check all combination of attributes.
// TODO: this should be improved to check all combination of attributes.
for ( const attribute of node.getAttributeKeys() ) {
if ( !this.check( { name, attributes: attribute, inside: queryPath } ) ) {
if ( batch ) {
Expand All @@ -448,8 +447,7 @@ export default class Schema {
}

if ( node.is( 'element' ) ) {
const newQueryPath = normalizedQueryPath.concat( [ node.name ] );
this.removeDisallowedAttributes( node.getChildren(), newQueryPath, batch );
this.removeDisallowedAttributes( node.getChildren(), queryPath.concat( node.name ), batch );
}
}
}
Expand Down

0 comments on commit 5837e68

Please sign in to comment.