schema.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Schema from '../../src/model/schema';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import Model from '../../src/model/model';
  8. import Element from '../../src/model/element';
  9. import Position from '../../src/model/position';
  10. import Text from '../../src/model/text';
  11. import DocumentFragment from '../../src/model/documentfragment';
  12. import { setData, getData, stringify } from '../../src/dev-utils/model';
  13. import AttributeDelta from '../../src/model/delta/attributedelta';
  14. describe( 'Schema', () => {
  15. let schema, root1, r1p1, r1p2, r1bQ, r1bQp, root2;
  16. beforeEach( () => {
  17. schema = new Schema();
  18. root1 = new Element( '$root', null, [
  19. new Element( 'paragraph', null, 'foo' ),
  20. new Element( 'paragraph', { align: 'right' }, 'bar' ),
  21. new Element( 'blockQuote', null, [
  22. new Element( 'paragraph', null, 'foo' )
  23. ] )
  24. ] );
  25. r1p1 = root1.getChild( 0 );
  26. r1p2 = root1.getChild( 1 );
  27. r1bQ = root1.getChild( 2 );
  28. r1bQp = r1bQ.getChild( 0 );
  29. root2 = new Element( '$root2' );
  30. } );
  31. describe( 'register()', () => {
  32. it( 'allows registering an item', () => {
  33. schema.register( 'foo' );
  34. expect( schema.getRule( 'foo' ) ).to.be.an( 'object' );
  35. } );
  36. it( 'copies rules', () => {
  37. const rules = {};
  38. schema.register( 'foo', rules );
  39. rules.isBlock = true;
  40. expect( schema.getRules().foo ).to.not.have.property( 'isBlock' );
  41. } );
  42. it( 'throws when trying to register for a single item twice', () => {
  43. schema.register( 'foo' );
  44. expect( () => {
  45. schema.register( 'foo' );
  46. } ).to.throw( CKEditorError, /^schema-cannot-register-item-twice:/ );
  47. } );
  48. } );
  49. describe( 'extend()', () => {
  50. it( 'allows extending item\'s rules', () => {
  51. schema.register( 'foo' );
  52. schema.extend( 'foo', {
  53. isBlock: true
  54. } );
  55. expect( schema.getRule( 'foo' ) ).to.have.property( 'isBlock', true );
  56. } );
  57. it( 'copies rules', () => {
  58. schema.register( 'foo', {} );
  59. const rules = {};
  60. schema.extend( 'foo', rules );
  61. rules.isBlock = true;
  62. expect( schema.getRules().foo ).to.not.have.property( 'isBlock' );
  63. } );
  64. it( 'throws when trying to extend a not yet registered item', () => {
  65. expect( () => {
  66. schema.extend( 'foo' );
  67. } ).to.throw( CKEditorError, /^schema-cannot-extend-missing-item:/ );
  68. } );
  69. } );
  70. describe( 'getRules()', () => {
  71. it( 'returns compiled rules', () => {
  72. schema.register( '$root' );
  73. schema.register( 'foo', {
  74. allowIn: '$root'
  75. } );
  76. schema.extend( 'foo', {
  77. isBlock: true
  78. } );
  79. const rules = schema.getRules();
  80. expect( rules.foo ).to.deep.equal( {
  81. name: 'foo',
  82. allowIn: [ '$root' ],
  83. allowAttributes: [],
  84. isBlock: true
  85. } );
  86. } );
  87. it( 'copies all is* types', () => {
  88. schema.register( 'foo', {
  89. isBlock: true,
  90. isFoo: true
  91. } );
  92. schema.extend( 'foo', {
  93. isBar: true,
  94. isFoo: false // Just to check that the last one wins.
  95. } );
  96. const rules = schema.getRules();
  97. expect( rules.foo ).to.have.property( 'isBlock', true );
  98. expect( rules.foo ).to.have.property( 'isFoo', false );
  99. expect( rules.foo ).to.have.property( 'isBar', true );
  100. } );
  101. it( 'does not recompile rules if not needed', () => {
  102. schema.register( 'foo' );
  103. expect( schema.getRules() ).to.equal( schema.getRules() );
  104. } );
  105. it( 'ensures no duplicates in allowIn', () => {
  106. schema.register( '$root' );
  107. schema.register( 'foo', {
  108. allowIn: '$root'
  109. } );
  110. schema.extend( 'foo', {
  111. allowIn: '$root'
  112. } );
  113. const rules = schema.getRules();
  114. expect( rules.foo ).to.deep.equal( {
  115. name: 'foo',
  116. allowIn: [ '$root' ],
  117. allowAttributes: []
  118. } );
  119. } );
  120. it( 'ensures no unregistered items in allowIn', () => {
  121. schema.register( 'foo', {
  122. allowIn: '$root'
  123. } );
  124. const rules = schema.getRules();
  125. expect( rules.foo ).to.deep.equal( {
  126. name: 'foo',
  127. allowIn: [],
  128. allowAttributes: []
  129. } );
  130. } );
  131. it( 'ensures no duplicates in allowAttributes', () => {
  132. schema.register( 'paragraph', {
  133. allowAttributes: 'foo'
  134. } );
  135. schema.extend( 'paragraph', {
  136. allowAttributes: 'foo'
  137. } );
  138. const rules = schema.getRules();
  139. expect( rules.paragraph ).to.deep.equal( {
  140. name: 'paragraph',
  141. allowIn: [],
  142. allowAttributes: [ 'foo' ]
  143. } );
  144. } );
  145. it( 'ensures no duplicates in allowAttributes duplicated by allowAttributesOf', () => {
  146. schema.register( 'paragraph', {
  147. allowAttributes: 'foo',
  148. allowAttributesOf: '$block'
  149. } );
  150. schema.register( '$block', {
  151. allowAttributes: 'foo'
  152. } );
  153. const rules = schema.getRules();
  154. expect( rules.paragraph ).to.deep.equal( {
  155. name: 'paragraph',
  156. allowIn: [],
  157. allowAttributes: [ 'foo' ]
  158. } );
  159. } );
  160. } );
  161. describe( 'getRule()', () => {
  162. // TODO
  163. } );
  164. describe( 'isRegistered()', () => {
  165. it( 'returns true if an item was registered', () => {
  166. schema.register( 'foo' );
  167. expect( schema.isRegistered( 'foo' ) ).to.be.true;
  168. } );
  169. it( 'returns false if an item was not registered', () => {
  170. expect( schema.isRegistered( 'foo' ) ).to.be.false;
  171. } );
  172. } );
  173. describe( 'isBlock()', () => {
  174. it( 'returns true if an item was registered as a block', () => {
  175. schema.register( 'foo', {
  176. isBlock: true
  177. } );
  178. expect( schema.isBlock( 'foo' ) ).to.be.true;
  179. } );
  180. it( 'returns false if an item was not registered as a block', () => {
  181. schema.register( 'foo' );
  182. expect( schema.isBlock( 'foo' ) ).to.be.false;
  183. } );
  184. it( 'returns false if an item was not registered at all', () => {
  185. expect( schema.isBlock( 'foo' ) ).to.be.false;
  186. } );
  187. } );
  188. describe( 'isLimit()', () => {
  189. it( 'returns true if an item was registered as a limit element', () => {
  190. schema.register( 'foo', {
  191. isLimit: true
  192. } );
  193. expect( schema.isLimit( 'foo' ) ).to.be.true;
  194. } );
  195. it( 'returns false if an item was not registered as a limit element', () => {
  196. schema.register( 'foo' );
  197. expect( schema.isLimit( 'foo' ) ).to.be.false;
  198. } );
  199. it( 'returns false if an item was not registered at all', () => {
  200. expect( schema.isLimit( 'foo' ) ).to.be.false;
  201. } );
  202. } );
  203. describe( 'isObject()', () => {
  204. it( 'returns true if an item was registered as an object', () => {
  205. schema.register( 'foo', {
  206. isObject: true
  207. } );
  208. expect( schema.isObject( 'foo' ) ).to.be.true;
  209. } );
  210. it( 'returns false if an item was not registered as an object', () => {
  211. schema.register( 'foo' );
  212. expect( schema.isObject( 'foo' ) ).to.be.false;
  213. } );
  214. it( 'returns false if an item was not registered at all', () => {
  215. expect( schema.isObject( 'foo' ) ).to.be.false;
  216. } );
  217. } );
  218. describe( 'checkChild()', () => {
  219. beforeEach( () => {
  220. schema.register( '$root' );
  221. schema.register( 'paragraph', {
  222. allowIn: '$root'
  223. } );
  224. schema.register( '$text', {
  225. allowIn: 'paragraph'
  226. } );
  227. } );
  228. it( 'accepts an element as a context and a node name as a child', () => {
  229. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  230. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  231. } );
  232. it( 'accepts a position as a context', () => {
  233. const posInRoot = Position.createAt( root1 );
  234. const posInParagraph = Position.createAt( r1p1 );
  235. expect( schema.checkChild( posInRoot, 'paragraph' ) ).to.be.true;
  236. expect( schema.checkChild( posInRoot, '$text' ) ).to.be.false;
  237. expect( schema.checkChild( posInParagraph, '$text' ) ).to.be.true;
  238. expect( schema.checkChild( posInParagraph, 'paragraph' ) ).to.be.false;
  239. } );
  240. // This is a temporary feature which is needed to make the current V->M conversion works.
  241. // It should be removed once V->M conversion uses real positions.
  242. // Of course, real positions have this advantage that we know element attributes at this point.
  243. it( 'accepts an array of element names as a context', () => {
  244. const contextInRoot = [ '$root' ];
  245. const contextInParagraph = [ '$root', 'paragraph' ];
  246. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  247. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  248. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  249. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  250. } );
  251. it( 'accepts an array of elements as a context', () => {
  252. const contextInRoot = [ root1 ];
  253. const contextInParagraph = [ root1, r1p1 ];
  254. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  255. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  256. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  257. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  258. } );
  259. // Again, this is needed temporarily to handle current V->M conversion
  260. it( 'accepts a mixed array of elements and strings as a context', () => {
  261. const contextInParagraph = [ '$root', r1p1 ];
  262. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  263. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  264. } );
  265. it( 'accepts a node as a child', () => {
  266. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  267. expect( schema.checkChild( root1, new Text( 'foo' ) ) ).to.be.false;
  268. } );
  269. // TODO checks fires event
  270. // TODO checks with a custom context array
  271. } );
  272. describe( 'checkAttribute()', () => {
  273. beforeEach( () => {
  274. schema.register( 'paragraph', {
  275. allowAttributes: 'align'
  276. } );
  277. schema.register( '$text', {
  278. allowAttributes: 'bold'
  279. } );
  280. } );
  281. it( 'accepts an element as a context', () => {
  282. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  283. expect( schema.checkAttribute( r1p1, 'bold' ) ).to.be.false;
  284. } );
  285. it( 'accepts a text as a context', () => {
  286. expect( schema.checkAttribute( new Text( 'foo' ), 'bold' ) ).to.be.true;
  287. expect( schema.checkAttribute( new Text( 'foo' ), 'align' ) ).to.be.false;
  288. } );
  289. // TODO checks fires event
  290. // TODO checks with a custom context array
  291. } );
  292. describe( 'getLimitElement()', () => {
  293. let model, doc, root;
  294. beforeEach( () => {
  295. model = new Model();
  296. doc = model.document;
  297. schema = model.schema;
  298. root = doc.createRoot();
  299. schema.register( 'div', {
  300. inheritAllFrom: '$block'
  301. } );
  302. schema.register( 'article', {
  303. inheritAllFrom: '$block',
  304. allowIn: 'section'
  305. } );
  306. schema.register( 'section', {
  307. inheritAllFrom: '$block',
  308. allowIn: 'div'
  309. } );
  310. schema.register( 'paragraph', {
  311. inheritAllFrom: '$block',
  312. allowIn: 'article'
  313. } );
  314. schema.register( 'widget', {
  315. inheritAllFrom: '$block',
  316. allowIn: 'div'
  317. } );
  318. schema.register( 'image', {
  319. inheritAllFrom: '$block',
  320. allowIn: 'widget'
  321. } );
  322. schema.register( 'caption', {
  323. inheritAllFrom: '$block',
  324. allowIn: 'image'
  325. } );
  326. } );
  327. it( 'always returns $root element if any other limit was not defined', () => {
  328. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  329. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  330. } );
  331. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  332. schema.extend( 'article', { isLimit: true } );
  333. schema.extend( 'section', { isLimit: true } );
  334. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  335. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  336. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  337. } );
  338. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  339. schema.extend( 'article', { isLimit: true } );
  340. schema.extend( 'section', { isLimit: true } );
  341. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  342. const section = root.getNodeByPath( [ 0, 0 ] );
  343. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  344. } );
  345. it( 'works fine with multi-range selections', () => {
  346. schema.extend( 'article', { isLimit: true } );
  347. schema.extend( 'widget', { isLimit: true } );
  348. schema.extend( 'div', { isLimit: true } );
  349. setData(
  350. model,
  351. '<div>' +
  352. '<section>' +
  353. '<article>' +
  354. '<paragraph>[foo]</paragraph>' +
  355. '</article>' +
  356. '</section>' +
  357. '<widget>' +
  358. '<image>' +
  359. '<caption>b[a]r</caption>' +
  360. '</image>' +
  361. '</widget>' +
  362. '</div>'
  363. );
  364. const div = root.getNodeByPath( [ 0 ] );
  365. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  366. } );
  367. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  368. setData(
  369. model,
  370. '<div>' +
  371. '<section>' +
  372. '<article>' +
  373. '<paragraph>[foo]</paragraph>' +
  374. '</article>' +
  375. '</section>' +
  376. '</div>' +
  377. '<section>b[]ar</section>'
  378. );
  379. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  380. } );
  381. } );
  382. describe( 'removeDisallowedAttributes()', () => {
  383. let model, doc, root;
  384. beforeEach( () => {
  385. model = new Model();
  386. doc = model.document;
  387. root = doc.createRoot();
  388. schema = model.schema;
  389. schema.register( 'paragraph', {
  390. inheritAllFrom: '$block'
  391. } );
  392. schema.register( 'div', {
  393. inheritAllFrom: '$block'
  394. } );
  395. schema.register( 'image', {
  396. isObject: true
  397. } );
  398. schema.extend( '$block', {
  399. allowIn: 'div'
  400. } );
  401. } );
  402. it( 'should filter out disallowed attributes from given nodes', () => {
  403. schema.extend( '$text', { allowAttributes: 'a' } );
  404. schema.extend( 'image', { allowAttributes: 'b' } );
  405. const text = new Text( 'foo', { a: 1, b: 1 } );
  406. const image = new Element( 'image', { a: 1, b: 1 } );
  407. root.appendChildren( [ text, image ] );
  408. model.change( writer => {
  409. schema.removeDisallowedAttributes( root.getChildren(), writer );
  410. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  411. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  412. expect( writer.batch.deltas ).to.length( 2 );
  413. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  414. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  415. expect( getData( model, { withoutSelection: true } ) )
  416. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  417. } );
  418. } );
  419. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  420. schema.on( 'checkAttribute', ( evt, args ) => {
  421. const ctx = args[ 0 ];
  422. const ctxItem = ctx[ ctx.length - 1 ];
  423. const ctxParent = ctx[ ctx.length - 2 ];
  424. const ctxParent2 = ctx[ ctx.length - 3 ];
  425. const attributeName = args[ 1 ];
  426. // 'a' in div>$text
  427. if ( ctxItem.name == '$text' && ctxParent.name == 'div' && attributeName == 'a' ) {
  428. evt.stop();
  429. evt.return = true;
  430. }
  431. // 'b' in div>paragraph>$text
  432. if ( ctxItem.name == '$text' && ctxParent.name == 'paragraph' && ctxParent2.name == 'div' && attributeName == 'b' ) {
  433. evt.stop();
  434. evt.return = true;
  435. }
  436. // 'a' in div>image
  437. if ( ctxItem.name == 'image' && ctxParent.name == 'div' && attributeName == 'a' ) {
  438. evt.stop();
  439. evt.return = true;
  440. }
  441. // 'b' in div>paragraph>image
  442. if ( ctxItem.name == 'image' && ctxParent.name == 'paragraph' && ctxParent2.name == 'div' && attributeName == 'b' ) {
  443. evt.stop();
  444. evt.return = true;
  445. }
  446. }, { priority: 'high' } );
  447. const foo = new Text( 'foo', { a: 1, b: 1 } );
  448. const bar = new Text( 'bar', { a: 1, b: 1 } );
  449. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  450. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  451. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  452. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  453. root.appendChildren( [ div ] );
  454. model.change( writer => {
  455. schema.removeDisallowedAttributes( root.getChildren(), writer );
  456. expect( writer.batch.deltas ).to.length( 4 );
  457. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  458. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  459. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  460. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  461. expect( getData( model, { withoutSelection: true } ) )
  462. .to.equal(
  463. '<div>' +
  464. '<paragraph>' +
  465. '<$text b="1">foo</$text>' +
  466. '<image b="1"></image>' +
  467. '</paragraph>' +
  468. '<$text a="1">bar</$text>' +
  469. '<image a="1"></image>' +
  470. '</div>'
  471. );
  472. } );
  473. } );
  474. } );
  475. describe( 'rules compilation', () => {
  476. describe( 'allowIn cases', () => {
  477. it( 'passes $root>paragraph', () => {
  478. schema.register( '$root' );
  479. schema.register( 'paragraph', {
  480. allowIn: '$root'
  481. } );
  482. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  483. } );
  484. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  485. schema.register( '$root' );
  486. schema.register( '$root2' );
  487. schema.register( 'paragraph', {
  488. allowIn: [ '$root', '$root2' ]
  489. } );
  490. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  491. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  492. } );
  493. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  494. schema.register( '$root' );
  495. schema.register( 'paragraph', {
  496. allowIn: '$root'
  497. } );
  498. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  499. } );
  500. it( 'passes $root>div>div – in case of circular refs', () => {
  501. schema.register( '$root' );
  502. schema.register( 'div', {
  503. allowIn: [ '$root', 'div' ]
  504. } );
  505. const div = new Element( 'div' );
  506. root1.appendChildren( div );
  507. const div2 = new Element( 'div' );
  508. expect( schema.checkChild( div, div2 ) ).to.be.true;
  509. } );
  510. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  511. schema.register( '$root' );
  512. schema.register( 'div', {
  513. allowIn: [ '$root', 'div' ]
  514. } );
  515. const div = new Element( 'div' );
  516. root1.appendChildren( div );
  517. expect( schema.checkChild( div, div ) ).to.be.true;
  518. } );
  519. it( 'rejects $root>paragraph – unregistered paragraph', () => {
  520. schema.register( '$root' );
  521. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  522. } );
  523. it( 'rejects $root>paragraph – registered different item', () => {
  524. schema.register( '$root' );
  525. schema.register( 'paragraph' );
  526. schema.register( 'listItem', {
  527. allowIn: '$root'
  528. } );
  529. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  530. } );
  531. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  532. schema.register( '$root' );
  533. schema.register( '$fancyRoot' );
  534. schema.register( 'paragraph', {
  535. allowIn: '$fancyRoot'
  536. } );
  537. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  538. } );
  539. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  540. schema.register( '$root' );
  541. schema.register( 'paragraph', {
  542. allowIn: '$root'
  543. } );
  544. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  545. } );
  546. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  547. schema.register( '$root' );
  548. schema.register( 'blockQuote', {
  549. allowIn: '$root'
  550. } );
  551. schema.register( 'paragraph', {
  552. allowIn: '$root'
  553. } );
  554. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  555. } );
  556. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  557. schema.register( '$root' );
  558. schema.register( 'paragraph', {
  559. allowIn: '$root'
  560. } );
  561. schema.register( '$text', {
  562. allowIn: 'paragraph'
  563. } );
  564. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  565. } );
  566. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  567. schema.register( '$root' );
  568. schema.register( 'blockQuote' );
  569. schema.register( 'paragraph', {
  570. allowIn: [ 'blockQuote', '$root' ]
  571. } );
  572. schema.register( '$text', {
  573. allowIn: 'paragraph'
  574. } );
  575. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  576. } );
  577. } );
  578. describe( 'allowWhere cases', () => {
  579. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  580. schema.register( '$root' );
  581. schema.register( '$block', {
  582. allowIn: '$root'
  583. } );
  584. schema.register( 'paragraph', {
  585. allowWhere: '$block'
  586. } );
  587. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  588. } );
  589. it( 'supports the array syntax', () => {
  590. schema.register( '$root' );
  591. schema.register( '$root2' );
  592. schema.register( '$block', {
  593. allowIn: '$root'
  594. } );
  595. schema.register( '$block2', {
  596. allowIn: '$root2'
  597. } );
  598. schema.register( 'paragraph', {
  599. allowWhere: [ '$block', '$block2' ]
  600. } );
  601. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  602. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  603. } );
  604. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  605. it( 'passes $root>paragraph – paragraph inherits from $block, order of rules does not matter', () => {
  606. schema.register( '$root' );
  607. schema.register( 'paragraph', {
  608. allowWhere: '$block'
  609. } );
  610. schema.register( '$block', {
  611. allowIn: '$root'
  612. } );
  613. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  614. } );
  615. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  616. schema.register( '$root' );
  617. schema.register( '$block', {
  618. allowIn: '$root'
  619. } );
  620. schema.register( '$specialBlock', {
  621. allowWhere: '$block'
  622. } );
  623. schema.register( 'paragraph', {
  624. allowWhere: '$specialBlock'
  625. } );
  626. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  627. } );
  628. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  629. schema.register( '$root' );
  630. schema.register( '$block' );
  631. schema.register( 'paragraph', {
  632. allowWhere: '$block'
  633. } );
  634. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  635. } );
  636. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  637. schema.register( '$root' );
  638. schema.register( '$block' );
  639. schema.register( 'paragraph', {
  640. allowWhere: '$block'
  641. } );
  642. schema.register( '$text', {
  643. allowIn: 'paragraph'
  644. } );
  645. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  646. } );
  647. } );
  648. describe( 'allowContentOf cases', () => {
  649. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  650. schema.register( '$root' );
  651. schema.register( 'paragraph', {
  652. allowIn: '$root'
  653. } );
  654. schema.register( '$root2', {
  655. allowContentOf: '$root'
  656. } );
  657. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  658. } );
  659. it( 'supports the array syntax', () => {
  660. schema.register( '$root' );
  661. schema.register( '$root2' );
  662. schema.register( 'paragraph', {
  663. allowIn: '$root'
  664. } );
  665. schema.register( 'heading1', {
  666. allowIn: '$root2'
  667. } );
  668. schema.register( '$root3', {
  669. allowContentOf: [ '$root', '$root2' ]
  670. } );
  671. const root3 = new Element( '$root3' );
  672. const heading1 = new Element( 'heading1' );
  673. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  674. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  675. } );
  676. it( 'passes $root2>paragraph – $root2 inherits from $root, order of rules does not matter', () => {
  677. schema.register( '$root' );
  678. schema.register( '$root2', {
  679. allowContentOf: '$root'
  680. } );
  681. schema.register( 'paragraph', {
  682. allowIn: '$root'
  683. } );
  684. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  685. } );
  686. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  687. schema.register( '$root' );
  688. schema.register( '$block' );
  689. schema.register( 'paragraph', {
  690. allowIn: '$root',
  691. allowContentOf: '$block'
  692. } );
  693. schema.register( '$text', {
  694. allowIn: '$block'
  695. } );
  696. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  697. } );
  698. // TODO we need to make it possible to disallow bQ in bQ.
  699. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  700. schema.register( '$root' );
  701. schema.register( 'blockQuote', {
  702. allowIn: '$root',
  703. allowContentOf: '$root'
  704. } );
  705. schema.register( 'paragraph', {
  706. allowIn: '$root'
  707. } );
  708. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  709. } );
  710. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  711. schema.register( '$root' );
  712. schema.register( 'paragraph' );
  713. schema.register( '$root2', {
  714. allowContentOf: '$root'
  715. } );
  716. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  717. } );
  718. } );
  719. describe( 'mix of allowContentOf and allowWhere', () => {
  720. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  721. schema.register( '$root' );
  722. schema.register( '$block', {
  723. allowIn: '$root'
  724. } );
  725. schema.register( 'paragraph', {
  726. allowContentOf: '$block',
  727. allowWhere: '$block'
  728. } );
  729. schema.register( '$text', {
  730. allowIn: '$block'
  731. } );
  732. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  733. } );
  734. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  735. 'and paragraph inherits allowWhere from $block', () => {
  736. schema.register( '$root' );
  737. schema.register( '$root2', {
  738. allowContentOf: '$root'
  739. } );
  740. schema.register( '$block', {
  741. allowIn: '$root'
  742. } );
  743. schema.register( 'paragraph', {
  744. allowWhere: '$block'
  745. } );
  746. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  747. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  748. } );
  749. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  750. schema.register( 'b' );
  751. schema.register( 'a', { allowIn: 'b' } );
  752. schema.register( 'c', { allowContentOf: 'b' } );
  753. schema.register( 'd', { allowContentOf: 'c' } );
  754. const d = new Element( 'd' );
  755. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  756. } );
  757. // This case won't pass becuase we compile the rules in a pretty naive way.
  758. // To make chains like this work we'd need to repeat compilation of allowContentOf rules
  759. // as long as the previous iteration found something to compile.
  760. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  761. // and since we've found something, we'd now try d<-c which would work.
  762. //
  763. // We ignore those situations for now as they are very unlikely to happen and would
  764. // significantly raised the complexity of rule compilation.
  765. //
  766. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  767. // schema.register( 'b' );
  768. // schema.register( 'a', { allowIn: 'b' } );
  769. // schema.register( 'd', { allowContentOf: 'c' } );
  770. // schema.register( 'c', { allowContentOf: 'b' } );
  771. //
  772. // const d = new Element( 'd' );
  773. //
  774. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  775. // } );
  776. } );
  777. describe( 'inheritAllFrom', () => {
  778. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  779. schema.register( '$root' );
  780. schema.register( '$block', {
  781. allowIn: '$root'
  782. } );
  783. schema.register( 'paragraph', {
  784. inheritAllFrom: '$block'
  785. } );
  786. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  787. } );
  788. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  789. schema.register( '$root' );
  790. schema.register( '$block', {
  791. allowIn: '$root'
  792. } );
  793. schema.register( '$text', {
  794. allowIn: '$block'
  795. } );
  796. schema.register( 'paragraph', {
  797. inheritAllFrom: '$block'
  798. } );
  799. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  800. } );
  801. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  802. schema.register( '$root' );
  803. schema.register( '$blockProto', {
  804. allowIn: '$root'
  805. } );
  806. schema.register( '$block', {
  807. allowWhere: '$blockProto'
  808. } );
  809. schema.register( 'paragraph', {
  810. inheritAllFrom: '$block'
  811. } );
  812. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  813. } );
  814. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  815. schema.register( '$root' );
  816. schema.register( '$blockProto' );
  817. schema.register( '$block', {
  818. allowContentOf: '$blockProto',
  819. allowIn: '$root'
  820. } );
  821. schema.register( '$text', {
  822. allowIn: '$blockProto'
  823. } );
  824. schema.register( 'paragraph', {
  825. inheritAllFrom: '$block'
  826. } );
  827. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  828. } );
  829. } );
  830. // We need to handle cases where some independent features registered rules which might use
  831. // optional elements (elements which might not have been registered).
  832. describe( 'missing structure rules', () => {
  833. it( 'does not break when trying to check a child which is not registered', () => {
  834. schema.register( '$root' );
  835. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  836. } );
  837. it( 'does not break when trying to check registered child in a context which contains unregistered elements', () => {
  838. const foo404 = new Element( 'foo404' );
  839. root1.appendChildren( foo404 );
  840. schema.register( '$root' );
  841. schema.register( '$text', {
  842. allowIn: '$root'
  843. } );
  844. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  845. } );
  846. it( 'does not break when used allowedIn pointing to an unregistered element', () => {
  847. schema.register( '$root' );
  848. schema.register( '$text', {
  849. allowIn: 'foo404'
  850. } );
  851. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  852. } );
  853. it( 'does not break when used allowWhere pointing to an unregistered element', () => {
  854. schema.register( '$root' );
  855. schema.register( '$text', {
  856. allowWhere: 'foo404'
  857. } );
  858. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  859. } );
  860. it( 'does not break when used allowContentOf pointing to an unregistered element', () => {
  861. schema.register( '$root', {
  862. allowContentOf: 'foo404'
  863. } );
  864. schema.register( '$text', {
  865. allowIn: '$root'
  866. } );
  867. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  868. } );
  869. it( 'checks whether allowIn uses a registered element', () => {
  870. schema.register( 'paragraph', {
  871. allowIn: '$root'
  872. } );
  873. // $root isn't registered!
  874. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  875. } );
  876. it( 'does not break when inheriting all from an unregistered element', () => {
  877. schema.register( 'paragraph', {
  878. inheritAllFrom: '$block'
  879. } );
  880. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  881. } );
  882. } );
  883. describe( 'allowAttributes', () => {
  884. it( 'passes paragraph[align]', () => {
  885. schema.register( 'paragraph', {
  886. allowAttributes: 'align'
  887. } );
  888. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  889. } );
  890. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  891. schema.register( 'paragraph', {
  892. allowAttributes: [ 'align', 'dir' ]
  893. } );
  894. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  895. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  896. } );
  897. it( 'passes paragraph>$text[bold]', () => {
  898. schema.register( 'paragraph' );
  899. schema.register( '$text', {
  900. allowIn: 'paragraph',
  901. allowAttributes: 'bold'
  902. } );
  903. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  904. } );
  905. } );
  906. describe( 'allowAttributesOf', () => {
  907. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  908. schema.register( '$block', {
  909. allowAttributes: 'align'
  910. } );
  911. schema.register( 'paragraph', {
  912. allowAttributesOf: '$block'
  913. } );
  914. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  915. } );
  916. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  917. schema.register( '$block', {
  918. allowAttributes: 'align'
  919. } );
  920. schema.register( '$block2', {
  921. allowAttributes: 'dir'
  922. } );
  923. schema.register( 'paragraph', {
  924. allowAttributesOf: [ '$block', '$block2' ]
  925. } );
  926. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  927. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  928. } );
  929. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  930. schema.register( '$block', {
  931. allowAttributes: 'align'
  932. } );
  933. schema.register( 'paragraph', {
  934. allowAttributes: 'dir',
  935. allowAttributesOf: '$block'
  936. } );
  937. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  938. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  939. } );
  940. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  941. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  942. } );
  943. describe( 'inheritAllFrom', () => {
  944. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  945. schema.register( '$block', {
  946. allowAttributes: 'align'
  947. } );
  948. schema.register( 'paragraph', {
  949. inheritAllFrom: '$block'
  950. } );
  951. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  952. } );
  953. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  954. schema.register( '$blockProto', {
  955. allowAttributes: 'align'
  956. } );
  957. schema.register( '$block', {
  958. allowAttributesOf: '$blockProto'
  959. } );
  960. schema.register( 'paragraph', {
  961. inheritAllFrom: '$block'
  962. } );
  963. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  964. } );
  965. } );
  966. describe( 'missing attribute rules', () => {
  967. it( 'does not crash when checking an attribute of a unregistered element', () => {
  968. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  969. } );
  970. it( 'does not crash when inheriting attributes of a unregistered element', () => {
  971. schema.register( 'paragraph', {
  972. allowAttributesOf: '$block'
  973. } );
  974. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  975. } );
  976. it( 'does not crash when inheriting all from a unregistered element', () => {
  977. schema.register( 'paragraph', {
  978. allowAttributesOf: '$block'
  979. } );
  980. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  981. } );
  982. } );
  983. } );
  984. describe( 'real scenarios', () => {
  985. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  986. const rules = [
  987. () => {
  988. schema.register( 'paragraph', {
  989. allowWhere: '$block',
  990. allowContentOf: '$block',
  991. allowAttributesOf: '$block'
  992. } );
  993. },
  994. () => {
  995. schema.register( 'heading1', {
  996. allowWhere: '$block',
  997. allowContentOf: '$block',
  998. allowAttributesOf: '$block'
  999. } );
  1000. },
  1001. () => {
  1002. schema.register( 'listItem', {
  1003. allowWhere: '$block',
  1004. allowContentOf: '$block',
  1005. allowAttributes: [ 'indent', 'type' ],
  1006. allowAttributesOf: '$block'
  1007. } );
  1008. },
  1009. () => {
  1010. schema.register( 'blockQuote', {
  1011. allowWhere: '$block',
  1012. allowContentOf: '$root'
  1013. } );
  1014. // Disallow blockQuote in blockQuote.
  1015. schema.on( 'checkChild', ( evt, args ) => {
  1016. const context = args[ 0 ];
  1017. const child = args[ 1 ];
  1018. const childRule = schema.getRule( child );
  1019. if ( childRule.name == 'blockQuote' && context[ context.length - 1 ].name == 'blockQuote' ) {
  1020. evt.stop();
  1021. evt.return = false;
  1022. }
  1023. }, { priority: 'high' } );
  1024. },
  1025. () => {
  1026. schema.register( 'image', {
  1027. allowWhere: '$block',
  1028. allowAttributes: [ 'src', 'alt' ]
  1029. } );
  1030. },
  1031. () => {
  1032. schema.register( 'caption', {
  1033. allowIn: 'image',
  1034. allowContentOf: '$block'
  1035. } );
  1036. },
  1037. () => {
  1038. schema.extend( '$text', {
  1039. allowAttributes: [ 'bold', 'italic' ]
  1040. } );
  1041. // Disallow bold in heading1.
  1042. schema.on( 'checkAttribute', ( evt, args ) => {
  1043. const context = args[ 0 ];
  1044. const ctxItem = context[ context.length - 1 ];
  1045. const ctxParent = context[ context.length - 2 ];
  1046. const attributeName = args[ 1 ];
  1047. if ( ctxItem.name == '$text' && ctxParent.name == 'heading1' && attributeName == 'bold' ) {
  1048. evt.stop();
  1049. evt.return = false;
  1050. }
  1051. }, { priority: 'high' } );
  1052. },
  1053. () => {
  1054. schema.extend( '$block', {
  1055. allowAttributes: 'alignment'
  1056. } );
  1057. }
  1058. ];
  1059. beforeEach( () => {
  1060. schema.register( '$root' );
  1061. schema.register( '$block', {
  1062. allowIn: '$root'
  1063. } );
  1064. schema.register( '$text', {
  1065. allowIn: '$block'
  1066. } );
  1067. for ( const rule of rules ) {
  1068. rule();
  1069. }
  1070. // or...
  1071. //
  1072. // Use the below code to shuffle the rules.
  1073. // Don't look here, Szymon!
  1074. //
  1075. // const rulesCopy = rules.slice();
  1076. //
  1077. // while ( rulesCopy.length ) {
  1078. // const r = Math.floor( Math.random() * rulesCopy.length );
  1079. // rulesCopy.splice( r, 1 )[ 0 ]();
  1080. // }
  1081. root1 = new Element( '$root', null, [
  1082. new Element( 'paragraph', null, 'foo' ),
  1083. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1084. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1085. new Element( 'heading1', null, 'foo' ),
  1086. new Element( 'blockQuote', null, [
  1087. new Element( 'paragraph', null, 'foo' ),
  1088. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1089. new Element( 'image', null, [
  1090. new Element( 'caption', null, 'foo' )
  1091. ] )
  1092. ] ),
  1093. new Element( 'image', null, [
  1094. new Element( 'caption', null, 'foo' )
  1095. ] )
  1096. ] );
  1097. r1p1 = root1.getChild( 0 );
  1098. r1p2 = root1.getChild( 1 );
  1099. r1lI = root1.getChild( 2 );
  1100. r1h = root1.getChild( 3 );
  1101. r1bQ = root1.getChild( 4 );
  1102. r1i = root1.getChild( 5 );
  1103. r1bQp = r1bQ.getChild( 0 );
  1104. r1bQlI = r1bQ.getChild( 1 );
  1105. r1bQi = r1bQ.getChild( 2 );
  1106. } );
  1107. it( 'passes $root>paragraph', () => {
  1108. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1109. } );
  1110. it( 'passes $root>paragraph>$text', () => {
  1111. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1112. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1113. } );
  1114. it( 'passes $root>listItem', () => {
  1115. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1116. } );
  1117. it( 'passes $root>listItem>$text', () => {
  1118. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1119. } );
  1120. it( 'passes $root>blockQuote>paragraph', () => {
  1121. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1122. } );
  1123. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1124. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1125. } );
  1126. it( 'passes $root>blockQuote>listItem', () => {
  1127. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1128. } );
  1129. it( 'passes $root>blockQuote>listItem>$text', () => {
  1130. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1131. } );
  1132. it( 'passes $root>blockQuote>image', () => {
  1133. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1134. } );
  1135. it( 'passes $root>blockQuote>image>caption', () => {
  1136. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1137. } );
  1138. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1139. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1140. } );
  1141. it( 'passes $root>image', () => {
  1142. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1143. } );
  1144. it( 'passes $root>image>caption', () => {
  1145. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1146. } );
  1147. it( 'passes $root>image>caption>$text', () => {
  1148. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1149. } );
  1150. it( 'rejects $root>$root', () => {
  1151. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1152. } );
  1153. it( 'rejects $root>$text', () => {
  1154. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1155. } );
  1156. it( 'rejects $root>caption', () => {
  1157. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1158. } );
  1159. it( 'rejects $root>paragraph>paragraph', () => {
  1160. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1161. } );
  1162. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1163. // Edge case because p>p should not exist in the first place.
  1164. // But it's good to know that it blocks also this.
  1165. const p = new Element( 'p' );
  1166. r1p1.appendChildren( p );
  1167. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1168. } );
  1169. it( 'rejects $root>paragraph>$block', () => {
  1170. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1171. } );
  1172. it( 'rejects $root>paragraph>blockQuote', () => {
  1173. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1174. } );
  1175. it( 'rejects $root>paragraph>image', () => {
  1176. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1177. } );
  1178. it( 'rejects $root>paragraph>caption', () => {
  1179. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1180. } );
  1181. it( 'rejects $root>blockQuote>blockQuote', () => {
  1182. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1183. } );
  1184. it( 'rejects $root>blockQuote>caption', () => {
  1185. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1186. } );
  1187. it( 'rejects $root>blockQuote>$text', () => {
  1188. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1189. } );
  1190. it( 'rejects $root>image>$text', () => {
  1191. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1192. } );
  1193. it( 'rejects $root>image>paragraph', () => {
  1194. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1195. } );
  1196. it( 'rejects $root>image>caption>paragraph', () => {
  1197. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1198. } );
  1199. it( 'rejects $root>image>caption>blockQuote', () => {
  1200. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1201. } );
  1202. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1203. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1204. } );
  1205. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1206. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1207. } );
  1208. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1209. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1210. } );
  1211. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1212. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1213. } );
  1214. it( 'accepts attribute $root>listItem[alignment]', () => {
  1215. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1216. } );
  1217. it( 'accepts attribute $root>listItem[indent]', () => {
  1218. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1219. } );
  1220. it( 'accepts attribute $root>listItem[type]', () => {
  1221. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1222. } );
  1223. it( 'accepts attribute $root>image[src]', () => {
  1224. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1225. } );
  1226. it( 'accepts attribute $root>image[alt]', () => {
  1227. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1228. } );
  1229. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1230. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1231. } );
  1232. it( 'rejects attribute $root[indent]', () => {
  1233. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1234. } );
  1235. it( 'rejects attribute $root>paragraph[indent]', () => {
  1236. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1237. } );
  1238. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1239. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1240. } );
  1241. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1242. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1243. } );
  1244. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1245. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1246. } );
  1247. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1248. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1249. } );
  1250. it( 'rejects attribute $root>image[indent]', () => {
  1251. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1252. } );
  1253. it( 'rejects attribute $root>image[alignment]', () => {
  1254. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1255. } );
  1256. } );
  1257. // TODO:
  1258. // * getValidRanges
  1259. // * checkAttributeInSelection
  1260. // * getLimitElement
  1261. // * removeDisallowedAttributes
  1262. // * test checkChild()'s both params normalization
  1263. // * and see insertContent's _checkIsObject()
  1264. // * add normalization to isObject(), isLimit(), isBlock(), isRegistered() and improve the existing code
  1265. // * inheritAllFrom should also inherit is* props (see tests documentselection getNearestSelectionRange())
  1266. // * test the default abstract entities (in model.js)
  1267. // * see clipboardHolder definition (and rename it to the pastebin)
  1268. // * review insertContent's _tryAutoparagraphing()
  1269. // * it doesn't make sense for VCD to get schema as a param (it can get it from the model)
  1270. // * V->M conversion tests might got outdated and would need to be reviewed if someone has a spare week ;)
  1271. // * Do we need both $inline and $text?
  1272. // * Consider reversing context array for writing simpler callbacks
  1273. } );