8
0

schema.js 48 KB

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