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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Dec 21, 2017
1 parent 990e424 commit 1a56464
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
41 changes: 30 additions & 11 deletions src/model/schema2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@
*/

import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';

/**
* @module engine/model/schema
*/

/**
* @mixes module:utils/emittermixin~ObservableMixin
*/
export default class Schema {
constructor() {
this._sourceRules = {};

// TODO events
this.decorate( 'checkChild' );
this.decorate( 'checkAttribute' );

this.on( 'checkAttribute', ( evt, args ) => {
args[ 0 ] = getContext( args[ 0 ] );
}, { priority: 'highest' } );

this.on( 'checkChild', ( evt, args ) => {
args[ 0 ] = getContext( args[ 0 ] );
}, { priority: 'highest' } );
}

register( itemName, rules ) {
Expand Down Expand Up @@ -53,9 +70,11 @@ export default class Schema {

if ( typeof item == 'string' ) {
itemName = item;
} else if ( item.is( 'text' ) ) {
} else if ( item.is && item.is( 'text' ) ) {
itemName = '$text';
} else {
}
// Element or context item.
else {
itemName = item.name;
}

Expand All @@ -69,13 +88,11 @@ export default class Schema {
return false;
}

const normalizedContext = normalizeContext( context );

return this._checkContextMatch( rule, normalizedContext );
return this._checkContextMatch( rule, context );
}

checkAttribute( node, attributeName ) {
const rule = this.getRule( node );
checkAttribute( context, attributeName ) {
const rule = this.getRule( context[ context.length - 1 ] );

if ( !rule ) {
return false;
Expand Down Expand Up @@ -124,7 +141,7 @@ export default class Schema {
if ( contextItemIndex == 0 ) {
return true;
} else {
const parentRule = this.getRule( contextItem.name );
const parentRule = this.getRule( contextItem );

return this._checkContextMatch( parentRule, context, contextItemIndex - 1 );
}
Expand All @@ -134,6 +151,8 @@ export default class Schema {
}
}

mix( Schema, ObservableMixin );

function compileBaseItemRule( sourceItemRules, itemName ) {
const itemRule = {
name: itemName,
Expand Down Expand Up @@ -243,9 +262,9 @@ function getAllowedChildren( compiledRules, itemName ) {
return getValues( compiledRules ).filter( rule => rule.allowIn.includes( itemRule.name ) );
}

function normalizeContext( context ) {
if ( context.is && context.is( 'element' ) ) {
return context.getAncestors( { includeSelf: true } ).map( element => {
function getContext( node ) {
if ( node.is && node.is( 'element' ) ) {
return node.getAncestors( { includeSelf: true } ).map( element => {
return {
name: element.name,
* getAttributes() {
Expand Down
20 changes: 17 additions & 3 deletions tests/model/schema/schema2.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ describe( 'Schema', () => {

schema.register( 'foo', rules );

// TODO
rules.isBlock = true;

expect( schema.getRules().foo ).to.not.have.property( 'isBlock' );
} );

it( 'throws when trying to register for a single item twice', () => {
Expand All @@ -65,11 +67,14 @@ describe( 'Schema', () => {
} );

it( 'copies rules', () => {
schema.register( 'foo', {} );

const rules = {};
schema.extend( 'foo', rules );

schema.register( 'foo', rules );
rules.isBlock = true;

// TODO
expect( schema.getRules().foo ).to.not.have.property( 'isBlock' );
} );

it( 'throws when trying to extend a not yet registered item', () => {
Expand Down Expand Up @@ -193,6 +198,10 @@ describe( 'Schema', () => {
} );
} );

describe( 'getRule()', () => {
// TODO
} );

describe( 'checkChild()', () => {
describe( 'allowIn cases', () => {
it( 'passes $root>paragraph', () => {
Expand Down Expand Up @@ -969,4 +978,9 @@ describe( 'Schema', () => {
// TODO:
// * disallow blockQ in blockQ
// * disallow hx>$text[bold]
// * getValidRanges
// * checkAttributeInSelection
// * getLimitElement
// * removeDisallowedAttributes
// * isBlock, isLimit, isObject, isRegistered
} );

0 comments on commit 1a56464

Please sign in to comment.