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

Commit

Permalink
Converted more schem.allow() calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Dec 27, 2017
1 parent 7381028 commit 0756e1b
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 139 deletions.
11 changes: 6 additions & 5 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ export default class Model {
this.schema.register( '$block', { allowIn: '$root' } );
this.schema.register( '$text', { allowIn: '$block' } );

// TMP!
// TODO review
// Create an "all allowed" context in the schema for processing the pasted content.
// Read: https://github.com/ckeditor/ckeditor5-engine/issues/638#issuecomment-255086588
//
// TODO
// this.schema.register( '$clipboardHolder', '$root' );
// this.schema.allow( { name: '$inline', inside: '$clipboardHolder' } );

this.schema.register( '$clipboardHolder', {
allowContentOf: '$root'
} );
this.schema.extend( '$text', { allowIn: '$clipboardHolder' } );
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export default class Schema {
return element;
}

removeDisallowedAttributes() {
// TODO
}

_clearCache() {
this._compiledRules = null;
}
Expand Down
4 changes: 1 addition & 3 deletions src/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,7 @@ function isUnvisitedBlockContainer( element, visited ) {

visited.add( element );

// TODO https://github.com/ckeditor/ckeditor5-engine/issues/532#issuecomment-278900072.
// This should not be a `$block` check.
return element.document.model.schema.itemExtends( element.name, '$block' ) && element.parent;
return element.document.model.schema.isBlock( element.name ) && element.parent;
}

// Finds the lowest element in position's ancestors which is a block.
Expand Down
2 changes: 1 addition & 1 deletion src/model/utils/deletecontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function mergeBranches( writer, startPos, endPos ) {

function shouldAutoparagraph( schema, position ) {
const isTextAllowed = schema.checkChild( position, '$text' );
const isParagraphAllowed = schema.check( position, 'paragraph' );
const isParagraphAllowed = schema.checkChild( position, 'paragraph' );

return !isTextAllowed && isParagraphAllowed;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe( 'DataController', () => {

it( 'should set paragraphs with bold', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
schema.extend( '$text', {
allowAttributes: [ 'bold' ]
} );

buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
Expand Down
70 changes: 42 additions & 28 deletions tests/conversion/buildviewconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import buildViewConverter from '../../src/conversion/buildviewconverter';

import Model from '../../src/model/model';
import ModelSchema from '../../src/model/schema';
import ModelDocumentFragment from '../../src/model/documentfragment';
import ModelElement from '../../src/model/element';
import ModelTextProxy from '../../src/model/textproxy';
Expand Down Expand Up @@ -60,36 +59,49 @@ function modelToString( item ) {
return result;
}

const textAttributes = [ undefined, 'linkHref', 'linkTitle', 'bold', 'italic', 'style' ];
const pAttributes = [ undefined, 'class', 'important', 'theme', 'decorated', 'size' ];
const textAttributes = [ 'linkHref', 'linkTitle', 'bold', 'italic', 'style' ];
const pAttributes = [ 'class', 'important', 'theme', 'decorated', 'size' ];

describe( 'View converter builder', () => {
let dispatcher, schema, additionalData;

const model = new Model();
let dispatcher, model, schema, additionalData;

beforeEach( () => {
model = new Model();

// `additionalData` parameter for `.convert` calls.
additionalData = { context: [ '$root' ] };

schema = new ModelSchema();

schema.register( 'paragraph', { inheritAllFrom: '$block' } );
schema.register( 'div', { inheritAllFrom: '$block' } );
schema.register( 'customP', { inheritAllFrom: 'paragraph' } );
schema.register( 'image', { inheritAllFrom: '$inline' } );
schema.register( 'span', { inheritAllFrom: '$inline' } );
schema.register( 'MEGATRON', { inheritAllFrom: '$inline' } ); // Yes, folks, we are building MEGATRON.
schema.register( 'abcd', { inheritAllFrom: '$inline' } );
schema.allow( { name: '$inline', attributes: textAttributes, inside: '$root' } );
schema.allow( { name: 'image', attributes: [ 'src' ], inside: '$root' } );
schema.allow( { name: 'image', attributes: [ 'src' ], inside: '$block' } );
schema.extend( '$text', { allowIn: '$inline' } );
schema.allow( { name: '$text', attributes: textAttributes, inside: '$block' } );
schema.allow( { name: '$text', attributes: textAttributes, inside: '$root' } );
schema.allow( { name: 'paragraph', attributes: pAttributes, inside: '$root' } );
schema.allow( { name: 'span', attributes: [ 'transformer' ], inside: '$root' } );
schema.allow( { name: 'div', attributes: [ 'class' ], inside: '$root' } );
schema = model.schema;

schema.register( 'paragraph', {
inheritAllFrom: '$block',
allowAttributes: pAttributes
} );
schema.register( 'div', {
inheritAllFrom: '$block',
allowAttributes: 'class'
} );
schema.register( 'customP', {
inheritAllFrom: 'paragraph'
} );
schema.register( 'image', {
inheritAllFrom: '$text',
allowAttributes: 'src'
} );
schema.register( 'span', {
inheritAllFrom: '$text',
allowAttributes: 'transformer'
} );
// Yes, folks, we are building MEGATRON.
schema.register( 'MEGATRON', {
inheritAllFrom: '$text'
} );
schema.register( 'abcd', {
inheritAllFrom: '$text'
} );
schema.extend( '$text', {
allowAttributes: textAttributes
} );

dispatcher = new ViewConversionDispatcher( model, { schema } );
dispatcher.on( 'text', convertText() );
Expand Down Expand Up @@ -152,7 +164,7 @@ describe( 'View converter builder', () => {
} );

it( 'should convert from view attribute and key to model attribute', () => {
schema.allow( { name: 'paragraph', attributes: [ 'type' ], inside: '$root' } );
schema.extend( 'paragraph', { allowAttributes: 'type' } );

dispatcher.on( 'documentFragment', convertToModelFragment() );

Expand Down Expand Up @@ -481,7 +493,8 @@ describe( 'View converter builder', () => {
buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );

schema.disallow( { name: 'div', inside: '$root' } );
// TODO this requires a callback
// schema.xdisallow( { name: 'div', inside: '$root' } );

dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );

Expand All @@ -500,7 +513,8 @@ describe( 'View converter builder', () => {
buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );

schema.disallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
// TODO this requires a callback
// schema.xdisallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );

dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );

Expand Down Expand Up @@ -535,7 +549,7 @@ describe( 'View converter builder', () => {
} );

it( 'should stop to attribute conversion if creating function returned null', () => {
schema.allow( { name: 'paragraph', attributes: [ 'type' ], inside: '$root' } );
schema.extend( 'paragraph', { allowAttributes: 'type' } );

buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );

Expand Down
13 changes: 8 additions & 5 deletions tests/conversion/definition-based-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ describe( 'definition-based-converters', () => {
setupViewToModelTests();

schema.register( 'div', { inheritAllFrom: '$block' } );

schema.allow( { name: '$inline', attributes: [ 'foo' ], inside: '$root' } );
schema.extend( '$text', { allowIn: '$root' } );
schema.extend( '$text', {
allowIn: '$root',
allowAttributes: 'foo'
} );

dispatcher.on( 'text', convertText() );
} );
Expand Down Expand Up @@ -381,8 +382,10 @@ describe( 'definition-based-converters', () => {
schema.register( 'bar', { inheritAllFrom: '$block' } );
schema.register( 'baz', { inheritAllFrom: '$block' } );

schema.allow( { name: '$inline', attribute: [ 'foo' ], inside: '$root' } );
schema.extend( '$text', { allowIn: '$inline' } );
schema.extend( '$text', {
allowIn: '$root',
allowAttributes: 'foo'
} );

dispatcher.on( 'text', convertText() );
} );
Expand Down
3 changes: 2 additions & 1 deletion tests/conversion/view-to-model-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ describe( 'view-to-model-converters', () => {
} );

it( 'should not convert text if it is wrong with schema', () => {
schema.disallow( { name: '$text', inside: '$root' } );
// TODO this requires a callback
// schema.xdisallow( { name: '$text', inside: '$root' } );

const viewText = new ViewText( 'foobar' );
dispatcher.on( 'text', convertText() );
Expand Down
30 changes: 18 additions & 12 deletions tests/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ describe( 'model test utils', () => {
sandbox = sinon.sandbox.create();
selection.removeAllRanges();

model.schema.register( 'a', { inheritAllFrom: '$inline' } );
model.schema.extend( 'a', { allowIn: '$root' } );
model.schema.allow( { name: 'a', inside: '$root', attributes: [ 'bar', 'car', 'foo' ] } );
model.schema.register( 'a', {
allowWhere: '$text',
allowIn: '$root',
allowAttributes: [ 'bar', 'car', 'foo' ]
} );

model.schema.register( 'b', { inheritAllFrom: '$inline' } );
model.schema.extend( 'b', { allowIn: '$root' } );
model.schema.allow( { name: 'b', inside: '$root', attributes: [ 'barFoo', 'fooBar', 'x' ] } );
model.schema.register( 'b', {
allowWhere: '$text',
allowIn: '$root',
allowAttributes: [ 'barFoo', 'fooBar', 'x' ]
} );

model.schema.register( 'c', { inheritAllFrom: '$inline' } );
model.schema.extend( 'c', { allowIn: '$root' } );
model.schema.register( 'c', {
allowWhere: '$text',
allowIn: [ '$root', 'b' ]
} );

model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
model.schema.extend( '$text', { allowIn: '$root' } );
model.schema.extend( '$text', { allowIn: 'a' } );
model.schema.extend( '$text', { allowIn: 'b' } );
model.schema.extend( 'c', { allowIn: 'b' } );

model.schema.extend( '$text', {
allowIn: [ '$root', 'a', 'b' ]
} );
} );

afterEach( () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/manual/nestededitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class NestedEditable extends Plugin {
schema.register( 'figcaption' );
schema.extend( 'figure', { allowIn: '$root' } );
schema.extend( 'figcaption', { allowIn: 'figure' } );

schema.extend( '$inline', { allowIn: 'figure' } );
schema.extend( '$inline', { allowIn: 'figcaption' } );
schema.extend( '$text', {
allowIn: [ 'figure', 'figcaption' ]
} );

buildModelConverter().for( data.modelToView, editing.modelToView )
.fromElement( 'figure' )
Expand Down
13 changes: 7 additions & 6 deletions tests/manual/tickets/1088/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ ClassicEditor
.then( editor => {
window.editor = editor;

const schema = editor.model.schema;
// const schema = editor.model.schema;

schema.disallow( { name: '$text', attributes: [ 'linkHref', 'italic' ], inside: 'heading1' } );
schema.disallow( { name: '$text', attributes: [ 'italic' ], inside: 'heading2' } );
schema.disallow( { name: '$text', attributes: [ 'linkHref' ], inside: 'blockQuote listItem' } );
schema.disallow( { name: '$text', attributes: [ 'bold' ], inside: 'paragraph' } );
schema.disallow( { name: 'heading3', inside: '$root' } );
// TODO this requires a callback
// schema.xdisallow( { name: '$text', attributes: [ 'linkHref', 'italic' ], inside: 'heading1' } );
// schema.xdisallow( { name: '$text', attributes: [ 'italic' ], inside: 'heading2' } );
// schema.xdisallow( { name: '$text', attributes: [ 'linkHref' ], inside: 'blockQuote listItem' } );
// schema.xdisallow( { name: '$text', attributes: [ 'bold' ], inside: 'paragraph' } );
// schema.xdisallow( { name: 'heading3', inside: '$root' } );
} )
.catch( err => {
console.error( err.stack );
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/tickets/475/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Link extends Plugin {
const editing = editor.editing;

// Allow bold attribute on all inline nodes.
editor.model.schema.allow( { name: '$inline', attributes: [ 'link' ] } );
editor.model.schema.extend( '$text', { allowAttributes: 'link' } );

// Build converter from model to view for data and editing pipelines.
buildModelConverter().for( data.modelToView, editing.modelToView )
Expand Down
7 changes: 4 additions & 3 deletions tests/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,12 +947,13 @@ describe( 'DocumentSelection', () => {
} );
model.schema.extend( 'image', { allowIn: '$root' } );
model.schema.extend( 'image', { allowIn: '$block' } );
model.schema.extend( '$inline', { allowIn: 'image' } );

model.schema.register( 'caption' );
model.schema.extend( '$inline', { allowIn: 'caption' } );
model.schema.extend( 'caption', { allowIn: 'image' } );
model.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
model.schema.extend( '$text', {
allowIn: [ 'image', 'caption' ],
allowAttributes: 'bold'
} );
} );

it( 'ignores attributes inside an object if selection contains that object', () => {
Expand Down
3 changes: 3 additions & 0 deletions tests/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,4 +1440,7 @@ describe( 'Schema', () => {
// * test the default abstract entities (in model.js)
// * see clipboardHolder definition (and rename it to the pastebin)
// * review insertContent's _tryAutoparagraphing()
// * it doesn't make sense for VCD to get schema as a param (it can get it from the model)
// * V->M conversion tests might got outdated and would need to be reviewed if someone has a spare week ;)
// * Do we need both $inline and $text?
} );
5 changes: 3 additions & 2 deletions tests/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,9 @@ describe( 'Selection', () => {
} );

it( 'returns false when the entire content except an empty element is selected', () => {
model.schema.register( 'img', { inheritAllFrom: '$inline' } );
model.schema.extend( 'img', { allowIn: 'p' } );
model.schema.register( 'img', {
allowIn: 'p'
} );

setData( model, '<p><img></img>[Foo]</p>' );

Expand Down
Loading

0 comments on commit 0756e1b

Please sign in to comment.