schema.js 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Schema, { SchemaContext } from '../../src/model/schema';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import Model from '../../src/model/model';
  8. import DocumentFragment from '../../src/model/documentfragment';
  9. import Element from '../../src/model/element';
  10. import Text from '../../src/model/text';
  11. import TextProxy from '../../src/model/textproxy';
  12. import Position from '../../src/model/position';
  13. import Range from '../../src/model/range';
  14. import Selection from '../../src/model/selection';
  15. import { setData, getData, stringify } from '../../src/dev-utils/model';
  16. import AttributeDelta from '../../src/model/delta/attributedelta';
  17. describe( 'Schema', () => {
  18. let schema, root1, r1p1, r1p2, r1bQ, r1bQp, root2;
  19. beforeEach( () => {
  20. schema = new Schema();
  21. root1 = new Element( '$root', null, [
  22. new Element( 'paragraph', null, 'foo' ),
  23. new Element( 'paragraph', { align: 'right' }, 'bar' ),
  24. new Element( 'blockQuote', null, [
  25. new Element( 'paragraph', null, 'foo' )
  26. ] )
  27. ] );
  28. r1p1 = root1.getChild( 0 );
  29. r1p2 = root1.getChild( 1 );
  30. r1bQ = root1.getChild( 2 );
  31. r1bQp = r1bQ.getChild( 0 );
  32. root2 = new Element( '$root2' );
  33. } );
  34. describe( 'register()', () => {
  35. it( 'allows registering an item', () => {
  36. schema.register( 'foo' );
  37. expect( schema.getDefinition( 'foo' ) ).to.be.an( 'object' );
  38. } );
  39. it( 'copies definitions', () => {
  40. const definitions = {};
  41. schema.register( 'foo', definitions );
  42. definitions.isBlock = true;
  43. expect( schema.getDefinitions().foo ).to.not.have.property( 'isBlock' );
  44. } );
  45. it( 'throws when trying to register for a single item twice', () => {
  46. schema.register( 'foo' );
  47. expect( () => {
  48. schema.register( 'foo' );
  49. } ).to.throw( CKEditorError, /^schema-cannot-register-item-twice:/ );
  50. } );
  51. } );
  52. describe( 'extend()', () => {
  53. it( 'allows extending item\'s definitions', () => {
  54. schema.register( 'foo' );
  55. schema.extend( 'foo', {
  56. isBlock: true
  57. } );
  58. expect( schema.getDefinition( 'foo' ) ).to.have.property( 'isBlock', true );
  59. } );
  60. it( 'copies definitions', () => {
  61. schema.register( 'foo', {} );
  62. const definitions = {};
  63. schema.extend( 'foo', definitions );
  64. definitions.isBlock = true;
  65. expect( schema.getDefinitions().foo ).to.not.have.property( 'isBlock' );
  66. } );
  67. it( 'throws when trying to extend a not yet registered item', () => {
  68. expect( () => {
  69. schema.extend( 'foo' );
  70. } ).to.throw( CKEditorError, /^schema-cannot-extend-missing-item:/ );
  71. } );
  72. } );
  73. describe( 'getDefinitions()', () => {
  74. it( 'returns compiled definitions', () => {
  75. schema.register( '$root' );
  76. schema.register( 'foo', {
  77. allowIn: '$root'
  78. } );
  79. schema.extend( 'foo', {
  80. isBlock: true
  81. } );
  82. const definitions = schema.getDefinitions();
  83. expect( definitions.foo ).to.deep.equal( {
  84. name: 'foo',
  85. allowIn: [ '$root' ],
  86. allowAttributes: [],
  87. isBlock: true
  88. } );
  89. } );
  90. it( 'copies all is* types', () => {
  91. schema.register( 'foo', {
  92. isBlock: true,
  93. isFoo: true
  94. } );
  95. schema.extend( 'foo', {
  96. isBar: true,
  97. isFoo: false // Just to check that the last one wins.
  98. } );
  99. const definitions = schema.getDefinitions();
  100. expect( definitions.foo ).to.have.property( 'isBlock', true );
  101. expect( definitions.foo ).to.have.property( 'isFoo', false );
  102. expect( definitions.foo ).to.have.property( 'isBar', true );
  103. } );
  104. it( 'does not recompile definitions if not needed', () => {
  105. schema.register( 'foo' );
  106. expect( schema.getDefinitions() ).to.equal( schema.getDefinitions() );
  107. } );
  108. it( 'ensures no duplicates in allowIn', () => {
  109. schema.register( '$root' );
  110. schema.register( 'foo', {
  111. allowIn: '$root'
  112. } );
  113. schema.extend( 'foo', {
  114. allowIn: '$root'
  115. } );
  116. const definitions = schema.getDefinitions();
  117. expect( definitions.foo ).to.deep.equal( {
  118. name: 'foo',
  119. allowIn: [ '$root' ],
  120. allowAttributes: []
  121. } );
  122. } );
  123. it( 'ensures no non-registered items in allowIn', () => {
  124. schema.register( 'foo', {
  125. allowIn: '$root'
  126. } );
  127. const definitions = schema.getDefinitions();
  128. expect( definitions.foo ).to.deep.equal( {
  129. name: 'foo',
  130. allowIn: [],
  131. allowAttributes: []
  132. } );
  133. } );
  134. it( 'ensures no duplicates in allowAttributes', () => {
  135. schema.register( 'paragraph', {
  136. allowAttributes: 'foo'
  137. } );
  138. schema.extend( 'paragraph', {
  139. allowAttributes: 'foo'
  140. } );
  141. const definitions = schema.getDefinitions();
  142. expect( definitions.paragraph ).to.deep.equal( {
  143. name: 'paragraph',
  144. allowIn: [],
  145. allowAttributes: [ 'foo' ]
  146. } );
  147. } );
  148. it( 'ensures no duplicates in allowAttributes duplicated by allowAttributesOf', () => {
  149. schema.register( 'paragraph', {
  150. allowAttributes: 'foo',
  151. allowAttributesOf: '$block'
  152. } );
  153. schema.register( '$block', {
  154. allowAttributes: 'foo'
  155. } );
  156. const definitions = schema.getDefinitions();
  157. expect( definitions.paragraph ).to.deep.equal( {
  158. name: 'paragraph',
  159. allowIn: [],
  160. allowAttributes: [ 'foo' ]
  161. } );
  162. } );
  163. } );
  164. describe( 'getDefinition()', () => {
  165. it( 'returns a definition based on an item name', () => {
  166. schema.register( 'foo', {
  167. isMe: true
  168. } );
  169. expect( schema.getDefinition( 'foo' ).isMe ).to.be.true;
  170. } );
  171. it( 'returns a definition based on an element name', () => {
  172. schema.register( 'foo', {
  173. isMe: true
  174. } );
  175. expect( schema.getDefinition( new Element( 'foo' ) ).isMe ).to.be.true;
  176. } );
  177. it( 'returns a definition based on a text node', () => {
  178. schema.register( '$text', {
  179. isMe: true
  180. } );
  181. expect( schema.getDefinition( new Text( 'foo' ) ).isMe ).to.be.true;
  182. } );
  183. it( 'returns a definition based on a text proxy', () => {
  184. schema.register( '$text', {
  185. isMe: true
  186. } );
  187. const text = new Text( 'foo' );
  188. const textProxy = new TextProxy( text, 0, 1 );
  189. expect( schema.getDefinition( textProxy ).isMe ).to.be.true;
  190. } );
  191. it( 'returns a definition based on a schema context item', () => {
  192. schema.register( 'foo', {
  193. isMe: true
  194. } );
  195. const ctx = new SchemaContext( [ '$root', 'foo' ] );
  196. expect( schema.getDefinition( ctx.last ).isMe ).to.be.true;
  197. } );
  198. it( 'returns undefined when trying to get an non-registered item', () => {
  199. expect( schema.getDefinition( '404' ) ).to.be.undefined;
  200. } );
  201. } );
  202. describe( 'isRegistered()', () => {
  203. it( 'returns true if an item was registered', () => {
  204. schema.register( 'foo' );
  205. expect( schema.isRegistered( 'foo' ) ).to.be.true;
  206. } );
  207. it( 'returns false if an item was not registered', () => {
  208. expect( schema.isRegistered( 'foo' ) ).to.be.false;
  209. } );
  210. it( 'uses getDefinition()\'s item to definition normalization', () => {
  211. const stub = sinon.stub( schema, 'getDefinition' ).returns( {} );
  212. expect( schema.isRegistered( 'foo' ) ).to.be.true;
  213. expect( stub.calledOnce ).to.be.true;
  214. } );
  215. } );
  216. describe( 'isBlock()', () => {
  217. it( 'returns true if an item was registered as a block', () => {
  218. schema.register( 'foo', {
  219. isBlock: true
  220. } );
  221. expect( schema.isBlock( 'foo' ) ).to.be.true;
  222. } );
  223. it( 'returns false if an item was not registered as a block', () => {
  224. schema.register( 'foo' );
  225. expect( schema.isBlock( 'foo' ) ).to.be.false;
  226. } );
  227. it( 'returns false if an item was not registered at all', () => {
  228. expect( schema.isBlock( 'foo' ) ).to.be.false;
  229. } );
  230. it( 'uses getDefinition()\'s item to definition normalization', () => {
  231. const stub = sinon.stub( schema, 'getDefinition' ).returns( { isBlock: true } );
  232. expect( schema.isBlock( 'foo' ) ).to.be.true;
  233. expect( stub.calledOnce ).to.be.true;
  234. } );
  235. } );
  236. describe( 'isLimit()', () => {
  237. it( 'returns true if an item was registered as a limit element', () => {
  238. schema.register( 'foo', {
  239. isLimit: true
  240. } );
  241. expect( schema.isLimit( 'foo' ) ).to.be.true;
  242. } );
  243. it( 'returns false if an item was not registered as a limit element', () => {
  244. schema.register( 'foo' );
  245. expect( schema.isLimit( 'foo' ) ).to.be.false;
  246. } );
  247. it( 'returns false if an item was not registered at all', () => {
  248. expect( schema.isLimit( 'foo' ) ).to.be.false;
  249. } );
  250. it( 'uses getDefinition()\'s item to definition normalization', () => {
  251. const stub = sinon.stub( schema, 'getDefinition' ).returns( { isLimit: true } );
  252. expect( schema.isLimit( 'foo' ) ).to.be.true;
  253. expect( stub.calledOnce ).to.be.true;
  254. } );
  255. } );
  256. describe( 'isObject()', () => {
  257. it( 'returns true if an item was registered as an object', () => {
  258. schema.register( 'foo', {
  259. isObject: true
  260. } );
  261. expect( schema.isObject( 'foo' ) ).to.be.true;
  262. } );
  263. it( 'returns false if an item was not registered as an object', () => {
  264. schema.register( 'foo' );
  265. expect( schema.isObject( 'foo' ) ).to.be.false;
  266. } );
  267. it( 'returns false if an item was not registered at all', () => {
  268. expect( schema.isObject( 'foo' ) ).to.be.false;
  269. } );
  270. it( 'uses getDefinition()\'s item to definition normalization', () => {
  271. const stub = sinon.stub( schema, 'getDefinition' ).returns( { isObject: true } );
  272. expect( schema.isObject( 'foo' ) ).to.be.true;
  273. expect( stub.calledOnce ).to.be.true;
  274. } );
  275. } );
  276. describe( 'checkChild()', () => {
  277. beforeEach( () => {
  278. schema.register( '$root' );
  279. schema.register( 'paragraph', {
  280. allowIn: '$root'
  281. } );
  282. schema.register( '$text', {
  283. allowIn: 'paragraph'
  284. } );
  285. } );
  286. it( 'accepts an element as a context and a node name as a child', () => {
  287. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  288. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  289. } );
  290. it( 'accepts a position as a context', () => {
  291. const posInRoot = Position.createAt( root1 );
  292. const posInParagraph = Position.createAt( r1p1 );
  293. expect( schema.checkChild( posInRoot, 'paragraph' ) ).to.be.true;
  294. expect( schema.checkChild( posInRoot, '$text' ) ).to.be.false;
  295. expect( schema.checkChild( posInParagraph, '$text' ) ).to.be.true;
  296. expect( schema.checkChild( posInParagraph, 'paragraph' ) ).to.be.false;
  297. } );
  298. // This is a temporary feature which is needed to make the current V->M conversion works.
  299. // It should be removed once V->M conversion uses real positions.
  300. // Of course, real positions have this advantage that we know element attributes at this point.
  301. it( 'accepts an array of element names as a context', () => {
  302. const contextInRoot = [ '$root' ];
  303. const contextInParagraph = [ '$root', 'paragraph' ];
  304. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  305. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  306. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  307. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  308. } );
  309. it( 'accepts an array of elements as a context', () => {
  310. const contextInRoot = [ root1 ];
  311. const contextInParagraph = [ root1, r1p1 ];
  312. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  313. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  314. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  315. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  316. } );
  317. // Again, this is needed temporarily to handle current V->M conversion
  318. it( 'accepts a mixed array of elements and strings as a context', () => {
  319. const contextInParagraph = [ '$root', r1p1 ];
  320. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  321. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  322. } );
  323. it( 'accepts a node as a child', () => {
  324. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  325. expect( schema.checkChild( root1, new Text( 'foo' ) ) ).to.be.false;
  326. } );
  327. it( 'fires the checkChild event with already normalized params', done => {
  328. schema.on( 'checkChild', ( evt, [ ctx, child ] ) => {
  329. expect( ctx ).to.be.instanceof( SchemaContext );
  330. expect( child ).to.equal( schema.getDefinition( 'paragraph' ) );
  331. done();
  332. }, { priority: 'highest' } );
  333. schema.checkChild( root1, r1p1 );
  334. } );
  335. } );
  336. describe( 'checkAttribute()', () => {
  337. beforeEach( () => {
  338. schema.register( 'paragraph', {
  339. allowAttributes: 'align'
  340. } );
  341. schema.register( '$text', {
  342. allowAttributes: 'bold'
  343. } );
  344. } );
  345. it( 'accepts an element as a context', () => {
  346. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  347. expect( schema.checkAttribute( r1p1, 'bold' ) ).to.be.false;
  348. } );
  349. it( 'accepts a text as a context', () => {
  350. expect( schema.checkAttribute( new Text( 'foo' ), 'bold' ) ).to.be.true;
  351. expect( schema.checkAttribute( new Text( 'foo' ), 'align' ) ).to.be.false;
  352. } );
  353. it( 'accepts a position as a context', () => {
  354. const posInRoot = Position.createAt( root1 );
  355. const posInParagraph = Position.createAt( r1p1 );
  356. expect( schema.checkAttribute( posInRoot, 'align' ) ).to.be.false;
  357. expect( schema.checkAttribute( posInParagraph, 'align' ) ).to.be.true;
  358. } );
  359. it( 'accepts an array of node names as a context', () => {
  360. const contextInRoot = [ '$root' ];
  361. const contextInParagraph = [ '$root', 'paragraph' ];
  362. const contextInText = [ '$root', 'paragraph', '$text' ];
  363. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  364. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  365. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  366. } );
  367. it( 'accepts an array of nodes as a context', () => {
  368. const contextInRoot = [ root1 ];
  369. const contextInParagraph = [ root1, r1p1 ];
  370. const contextInText = [ root1, r1p1, r1p1.getChild( 0 ) ];
  371. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  372. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  373. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  374. } );
  375. it( 'fires the checkAttribute event with already normalized context', done => {
  376. schema.on( 'checkAttribute', ( evt, [ ctx, attributeName ] ) => {
  377. expect( ctx ).to.be.instanceof( SchemaContext );
  378. expect( attributeName ).to.equal( 'bold' );
  379. done();
  380. }, { priority: 'highest' } );
  381. schema.checkAttribute( r1p1, 'bold' );
  382. } );
  383. } );
  384. describe( 'addChildCheck()', () => {
  385. beforeEach( () => {
  386. schema.register( '$root' );
  387. schema.register( 'paragraph', {
  388. allowIn: '$root'
  389. } );
  390. } );
  391. it( 'adds a high-priority listener', () => {
  392. const order = [];
  393. schema.on( 'checkChild', () => {
  394. order.push( 'checkChild:high-before' );
  395. }, { priority: 'high' } );
  396. schema.addChildCheck( () => {
  397. order.push( 'addChildCheck' );
  398. } );
  399. schema.on( 'checkChild', () => {
  400. order.push( 'checkChild:high-after' );
  401. }, { priority: 'high' } );
  402. schema.checkChild( root1, r1p1 );
  403. expect( order.join() ).to.equal( 'checkChild:high-before,addChildCheck,checkChild:high-after' );
  404. } );
  405. it( 'stops the event and overrides the return value when callback returned true', () => {
  406. schema.register( '$text' );
  407. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  408. schema.addChildCheck( () => {
  409. return true;
  410. } );
  411. schema.on( 'checkChild', () => {
  412. throw new Error( 'the event should be stopped' );
  413. }, { priority: 'high' } );
  414. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  415. } );
  416. it( 'stops the event and overrides the return value when callback returned false', () => {
  417. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  418. schema.addChildCheck( () => {
  419. return false;
  420. } );
  421. schema.on( 'checkChild', () => {
  422. throw new Error( 'the event should be stopped' );
  423. }, { priority: 'high' } );
  424. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  425. } );
  426. it( 'receives context and child definition as params', () => {
  427. schema.addChildCheck( ( ctx, childDef ) => {
  428. expect( ctx ).to.be.instanceOf( SchemaContext );
  429. expect( childDef ).to.equal( schema.getDefinition( 'paragraph' ) );
  430. } );
  431. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  432. } );
  433. it( 'is not called when checking a non-registered element', () => {
  434. expect( schema.getDefinition( 'foo' ) ).to.be.undefined;
  435. schema.addChildCheck( () => {
  436. throw new Error( 'callback should not be called' );
  437. } );
  438. expect( schema.checkChild( root1, 'foo' ) ).to.be.false;
  439. } );
  440. } );
  441. describe( 'addAttributeCheck()', () => {
  442. beforeEach( () => {
  443. schema.register( 'paragraph', {
  444. allowAttributes: 'foo'
  445. } );
  446. } );
  447. it( 'adds a high-priority listener', () => {
  448. const order = [];
  449. schema.on( 'checkAttribute', () => {
  450. order.push( 'checkAttribute:high-before' );
  451. }, { priority: 'high' } );
  452. schema.addAttributeCheck( () => {
  453. order.push( 'addAttributeCheck' );
  454. } );
  455. schema.on( 'checkAttribute', () => {
  456. order.push( 'checkAttribute:high-after' );
  457. }, { priority: 'high' } );
  458. schema.checkAttribute( r1p1, 'foo' );
  459. expect( order.join() ).to.equal( 'checkAttribute:high-before,addAttributeCheck,checkAttribute:high-after' );
  460. } );
  461. it( 'stops the event and overrides the return value when callback returned true', () => {
  462. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.false;
  463. schema.addAttributeCheck( () => {
  464. return true;
  465. } );
  466. schema.on( 'checkAttribute', () => {
  467. throw new Error( 'the event should be stopped' );
  468. }, { priority: 'high' } );
  469. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.true;
  470. } );
  471. it( 'stops the event and overrides the return value when callback returned false', () => {
  472. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  473. schema.addAttributeCheck( () => {
  474. return false;
  475. } );
  476. schema.on( 'checkAttribute', () => {
  477. throw new Error( 'the event should be stopped' );
  478. }, { priority: 'high' } );
  479. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.false;
  480. } );
  481. it( 'receives context and attribute name as params', () => {
  482. schema.addAttributeCheck( ( ctx, attributeName ) => {
  483. expect( ctx ).to.be.instanceOf( SchemaContext );
  484. expect( attributeName ).to.equal( 'foo' );
  485. } );
  486. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  487. } );
  488. } );
  489. describe( 'checkMerge()', () => {
  490. beforeEach( () => {
  491. schema.register( '$root' );
  492. schema.register( '$block', {
  493. allowIn: '$root',
  494. isBlock: true
  495. } );
  496. schema.register( '$text', {
  497. allowIn: '$block'
  498. } );
  499. schema.register( 'paragraph', {
  500. allowWhere: '$block'
  501. } );
  502. schema.register( 'blockQuote', {
  503. allowWhere: '$block',
  504. allowContentOf: '$root'
  505. } );
  506. schema.register( 'listItem', {
  507. inheritAllFrom: '$block'
  508. } );
  509. } );
  510. it( 'returns false if a block cannot be merged with other block', () => {
  511. const paragraph = new Element( 'paragraph', null, [
  512. new Text( 'xyz' )
  513. ] );
  514. const blockQuote = new Element( 'blockQuote', null, [ paragraph ] );
  515. const listItem = new Element( 'listItem' );
  516. expect( schema.checkMerge( listItem, blockQuote ) ).to.equal( false );
  517. } );
  518. it( 'returns true if a block can be merged with other block', () => {
  519. const listItem = new Element( 'listItem' );
  520. const listItemToMerge = new Element( 'listItem', null, [
  521. new Text( 'xyz' )
  522. ] );
  523. expect( schema.checkMerge( listItem, listItemToMerge ) ).to.equal( true );
  524. } );
  525. } );
  526. describe( 'getLimitElement()', () => {
  527. let model, doc, root;
  528. beforeEach( () => {
  529. model = new Model();
  530. doc = model.document;
  531. schema = model.schema;
  532. root = doc.createRoot();
  533. schema.register( 'div', {
  534. inheritAllFrom: '$block'
  535. } );
  536. schema.register( 'article', {
  537. inheritAllFrom: '$block',
  538. allowIn: 'section'
  539. } );
  540. schema.register( 'section', {
  541. inheritAllFrom: '$block',
  542. allowIn: 'div'
  543. } );
  544. schema.register( 'paragraph', {
  545. inheritAllFrom: '$block',
  546. allowIn: 'article'
  547. } );
  548. schema.register( 'widget', {
  549. inheritAllFrom: '$block',
  550. allowIn: 'div'
  551. } );
  552. schema.register( 'image', {
  553. inheritAllFrom: '$block',
  554. allowIn: 'widget'
  555. } );
  556. schema.register( 'caption', {
  557. inheritAllFrom: '$block',
  558. allowIn: 'image'
  559. } );
  560. } );
  561. it( 'always returns $root element if any other limit was not defined', () => {
  562. schema.extend( '$root', {
  563. isLimit: false
  564. } );
  565. expect( schema.isLimit( '$root' ) ).to.be.false;
  566. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  567. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  568. } );
  569. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  570. schema.extend( 'article', { isLimit: true } );
  571. schema.extend( 'section', { isLimit: true } );
  572. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  573. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  574. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  575. } );
  576. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  577. schema.extend( 'article', { isLimit: true } );
  578. schema.extend( 'section', { isLimit: true } );
  579. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  580. const section = root.getNodeByPath( [ 0, 0 ] );
  581. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  582. } );
  583. it( 'works fine with multi-range selections', () => {
  584. schema.extend( 'article', { isLimit: true } );
  585. schema.extend( 'widget', { isLimit: true } );
  586. schema.extend( 'div', { isLimit: true } );
  587. setData(
  588. model,
  589. '<div>' +
  590. '<section>' +
  591. '<article>' +
  592. '<paragraph>[foo]</paragraph>' +
  593. '</article>' +
  594. '</section>' +
  595. '<widget>' +
  596. '<image>' +
  597. '<caption>b[a]r</caption>' +
  598. '</image>' +
  599. '</widget>' +
  600. '</div>'
  601. );
  602. const div = root.getNodeByPath( [ 0 ] );
  603. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  604. } );
  605. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  606. setData(
  607. model,
  608. '<div>' +
  609. '<section>' +
  610. '<article>' +
  611. '<paragraph>[foo]</paragraph>' +
  612. '</article>' +
  613. '</section>' +
  614. '</div>' +
  615. '<section>b[]ar</section>'
  616. );
  617. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  618. } );
  619. } );
  620. describe( 'checkAttributeInSelection()', () => {
  621. const attribute = 'bold';
  622. let model, doc, schema;
  623. beforeEach( () => {
  624. model = new Model();
  625. doc = model.document;
  626. doc.createRoot();
  627. schema = model.schema;
  628. schema.register( 'p', { inheritAllFrom: '$block' } );
  629. schema.register( 'h1', { inheritAllFrom: '$block' } );
  630. schema.register( 'img', { allowWhere: '$text' } );
  631. schema.register( 'figure', {
  632. allowIn: '$root',
  633. allowAttributes: [ 'name', 'title' ]
  634. } );
  635. schema.addAttributeCheck( ( ctx, attributeName ) => {
  636. // Allow 'bold' on p>$text.
  637. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  638. return true;
  639. }
  640. // Allow 'bold' on $root>p.
  641. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  642. return true;
  643. }
  644. } );
  645. } );
  646. describe( 'when selection is collapsed', () => {
  647. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  648. setData( model, '<p>f[]oo</p>' );
  649. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  650. } );
  651. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  652. setData( model, '<h1>[]</h1>' );
  653. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  654. setData( model, '[]' );
  655. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  656. } );
  657. } );
  658. describe( 'when selection is not collapsed', () => {
  659. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  660. // Simple selection on a few characters.
  661. setData( model, '<p>[foo]</p>' );
  662. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  663. // Selection spans over characters but also include nodes that can't have attribute.
  664. setData( model, '<p>fo[o<img />b]ar</p>' );
  665. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  666. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  667. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  668. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  669. // Selection on empty P. P can have the attribute.
  670. setData( model, '[<p></p>]' );
  671. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  672. } );
  673. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  674. // Selection on DIV which can't have bold text.
  675. setData( model, '[<h1></h1>]' );
  676. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  677. // Selection on two images which can't be bold.
  678. setData( model, '<p>foo[<img /><img />]bar</p>' );
  679. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  680. } );
  681. it( 'should return true when checking element with required attribute', () => {
  682. setData( model, '[<figure name="figure"></figure>]' );
  683. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  684. } );
  685. it( 'should return true when checking element when attribute is already present', () => {
  686. setData( model, '[<figure name="figure" title="title"></figure>]' );
  687. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  688. } );
  689. } );
  690. } );
  691. describe( 'getValidRanges()', () => {
  692. const attribute = 'bold';
  693. let model, doc, root, schema, ranges;
  694. beforeEach( () => {
  695. model = new Model();
  696. doc = model.document;
  697. schema = model.schema;
  698. root = doc.createRoot();
  699. schema.register( 'p', { inheritAllFrom: '$block' } );
  700. schema.register( 'h1', { inheritAllFrom: '$block' } );
  701. schema.register( 'img', {
  702. allowWhere: '$text'
  703. } );
  704. schema.addAttributeCheck( ( ctx, attributeName ) => {
  705. // Allow 'bold' on p>$text.
  706. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  707. return true;
  708. }
  709. // Allow 'bold' on $root>p.
  710. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  711. return true;
  712. }
  713. } );
  714. setData( model, '<p>foo<img />bar</p>' );
  715. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  716. } );
  717. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  718. schema.extend( 'img', { allowAttributes: 'bold' } );
  719. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  720. } );
  721. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  722. schema.extend( 'img', { allowAttributes: 'bold' } );
  723. schema.extend( '$text', { allowIn: 'img' } );
  724. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  725. } );
  726. it( 'should return two ranges when attribute is not allowed on one item', () => {
  727. schema.extend( 'img', { allowAttributes: 'bold' } );
  728. schema.extend( '$text', { allowIn: 'img' } );
  729. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  730. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  731. const sel = new Selection();
  732. sel.setRanges( validRanges );
  733. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  734. } );
  735. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  736. schema.extend( '$text', { allowIn: 'img' } );
  737. schema.addAttributeCheck( ( ctx, attributeName ) => {
  738. // Allow 'bold' on img>$text.
  739. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  740. return true;
  741. }
  742. } );
  743. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  744. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  745. const sel = new Selection();
  746. sel.setRanges( validRanges );
  747. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  748. } );
  749. it( 'should not leak beyond the given ranges', () => {
  750. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  751. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  752. const sel = new Selection();
  753. sel.setRanges( validRanges );
  754. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  755. } );
  756. it( 'should correctly handle a range which ends in a disallowed position', () => {
  757. schema.extend( '$text', { allowIn: 'img' } );
  758. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  759. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  760. const sel = new Selection();
  761. sel.setRanges( validRanges );
  762. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  763. } );
  764. it( 'should split range into two ranges and omit disallowed element', () => {
  765. schema.addAttributeCheck( ( ctx, attributeName ) => {
  766. // Disallow 'bold' on p>img.
  767. if ( ctx.endsWith( 'p img' ) && attributeName == 'bold' ) {
  768. return false;
  769. }
  770. } );
  771. const result = schema.getValidRanges( ranges, attribute );
  772. expect( result ).to.length( 2 );
  773. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  774. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  775. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  776. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  777. } );
  778. } );
  779. describe( 'removeDisallowedAttributes()', () => {
  780. let model, doc, root;
  781. beforeEach( () => {
  782. model = new Model();
  783. doc = model.document;
  784. root = doc.createRoot();
  785. schema = model.schema;
  786. schema.register( 'paragraph', {
  787. inheritAllFrom: '$block'
  788. } );
  789. schema.register( 'div', {
  790. inheritAllFrom: '$block'
  791. } );
  792. schema.register( 'image', {
  793. isObject: true
  794. } );
  795. schema.extend( '$block', {
  796. allowIn: 'div'
  797. } );
  798. } );
  799. it( 'should filter out disallowed attributes from given nodes', () => {
  800. schema.extend( '$text', { allowAttributes: 'a' } );
  801. schema.extend( 'image', { allowAttributes: 'b' } );
  802. const text = new Text( 'foo', { a: 1, b: 1 } );
  803. const image = new Element( 'image', { a: 1, b: 1 } );
  804. root.appendChildren( [ text, image ] );
  805. model.change( writer => {
  806. schema.removeDisallowedAttributes( root.getChildren(), writer );
  807. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  808. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  809. expect( writer.batch.deltas ).to.length( 2 );
  810. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  811. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  812. expect( getData( model, { withoutSelection: true } ) )
  813. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  814. } );
  815. } );
  816. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  817. schema.addAttributeCheck( ( ctx, attributeName ) => {
  818. // Allow 'a' on div>$text.
  819. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  820. return true;
  821. }
  822. // Allow 'b' on div>paragraph>$text.
  823. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  824. return true;
  825. }
  826. // Allow 'a' on div>image.
  827. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  828. return true;
  829. }
  830. // Allow 'b' on div>paragraph>image.
  831. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  832. return true;
  833. }
  834. } );
  835. const foo = new Text( 'foo', { a: 1, b: 1 } );
  836. const bar = new Text( 'bar', { a: 1, b: 1 } );
  837. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  838. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  839. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  840. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  841. root.appendChildren( [ div ] );
  842. model.change( writer => {
  843. schema.removeDisallowedAttributes( root.getChildren(), writer );
  844. expect( writer.batch.deltas ).to.length( 4 );
  845. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  846. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  847. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  848. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  849. expect( getData( model, { withoutSelection: true } ) )
  850. .to.equal(
  851. '<div>' +
  852. '<paragraph>' +
  853. '<$text b="1">foo</$text>' +
  854. '<image b="1"></image>' +
  855. '</paragraph>' +
  856. '<$text a="1">bar</$text>' +
  857. '<image a="1"></image>' +
  858. '</div>'
  859. );
  860. } );
  861. } );
  862. } );
  863. describe( 'definitions compilation', () => {
  864. describe( 'allowIn cases', () => {
  865. it( 'passes $root>paragraph', () => {
  866. schema.register( '$root' );
  867. schema.register( 'paragraph', {
  868. allowIn: '$root'
  869. } );
  870. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  871. } );
  872. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  873. schema.register( '$root' );
  874. schema.register( '$root2' );
  875. schema.register( 'paragraph', {
  876. allowIn: [ '$root', '$root2' ]
  877. } );
  878. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  879. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  880. } );
  881. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  882. schema.register( '$root' );
  883. schema.register( 'paragraph', {
  884. allowIn: '$root'
  885. } );
  886. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  887. } );
  888. it( 'passes $root>div>div – in case of circular refs', () => {
  889. schema.register( '$root' );
  890. schema.register( 'div', {
  891. allowIn: [ '$root', 'div' ]
  892. } );
  893. const div = new Element( 'div' );
  894. root1.appendChildren( div );
  895. const div2 = new Element( 'div' );
  896. expect( schema.checkChild( div, div2 ) ).to.be.true;
  897. } );
  898. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  899. schema.register( '$root' );
  900. schema.register( 'div', {
  901. allowIn: [ '$root', 'div' ]
  902. } );
  903. const div = new Element( 'div' );
  904. root1.appendChildren( div );
  905. expect( schema.checkChild( div, div ) ).to.be.true;
  906. } );
  907. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  908. schema.register( '$root' );
  909. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  910. } );
  911. it( 'rejects $root>paragraph – registered different item', () => {
  912. schema.register( '$root' );
  913. schema.register( 'paragraph' );
  914. schema.register( 'listItem', {
  915. allowIn: '$root'
  916. } );
  917. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  918. } );
  919. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  920. schema.register( '$root' );
  921. schema.register( '$fancyRoot' );
  922. schema.register( 'paragraph', {
  923. allowIn: '$fancyRoot'
  924. } );
  925. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  926. } );
  927. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  928. schema.register( '$root' );
  929. schema.register( 'paragraph', {
  930. allowIn: '$root'
  931. } );
  932. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  933. } );
  934. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  935. schema.register( '$root' );
  936. schema.register( 'blockQuote', {
  937. allowIn: '$root'
  938. } );
  939. schema.register( 'paragraph', {
  940. allowIn: '$root'
  941. } );
  942. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  943. } );
  944. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  945. schema.register( '$root' );
  946. schema.register( 'paragraph', {
  947. allowIn: '$root'
  948. } );
  949. schema.register( '$text', {
  950. allowIn: 'paragraph'
  951. } );
  952. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  953. } );
  954. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  955. schema.register( '$root' );
  956. schema.register( 'blockQuote' );
  957. schema.register( 'paragraph', {
  958. allowIn: [ 'blockQuote', '$root' ]
  959. } );
  960. schema.register( '$text', {
  961. allowIn: 'paragraph'
  962. } );
  963. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  964. } );
  965. } );
  966. describe( 'allowWhere cases', () => {
  967. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  968. schema.register( '$root' );
  969. schema.register( '$block', {
  970. allowIn: '$root'
  971. } );
  972. schema.register( 'paragraph', {
  973. allowWhere: '$block'
  974. } );
  975. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  976. } );
  977. it( 'supports the array syntax', () => {
  978. schema.register( '$root' );
  979. schema.register( '$root2' );
  980. schema.register( '$block', {
  981. allowIn: '$root'
  982. } );
  983. schema.register( '$block2', {
  984. allowIn: '$root2'
  985. } );
  986. schema.register( 'paragraph', {
  987. allowWhere: [ '$block', '$block2' ]
  988. } );
  989. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  990. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  991. } );
  992. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  993. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  994. schema.register( '$root' );
  995. schema.register( 'paragraph', {
  996. allowWhere: '$block'
  997. } );
  998. schema.register( '$block', {
  999. allowIn: '$root'
  1000. } );
  1001. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1002. } );
  1003. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1004. schema.register( '$root' );
  1005. schema.register( '$block', {
  1006. allowIn: '$root'
  1007. } );
  1008. schema.register( '$specialBlock', {
  1009. allowWhere: '$block'
  1010. } );
  1011. schema.register( 'paragraph', {
  1012. allowWhere: '$specialBlock'
  1013. } );
  1014. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1015. } );
  1016. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1017. schema.register( '$root' );
  1018. schema.register( '$block' );
  1019. schema.register( 'paragraph', {
  1020. allowWhere: '$block'
  1021. } );
  1022. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1023. } );
  1024. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1025. schema.register( '$root' );
  1026. schema.register( '$block' );
  1027. schema.register( 'paragraph', {
  1028. allowWhere: '$block'
  1029. } );
  1030. schema.register( '$text', {
  1031. allowIn: 'paragraph'
  1032. } );
  1033. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1034. } );
  1035. } );
  1036. describe( 'allowContentOf cases', () => {
  1037. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1038. schema.register( '$root' );
  1039. schema.register( 'paragraph', {
  1040. allowIn: '$root'
  1041. } );
  1042. schema.register( '$root2', {
  1043. allowContentOf: '$root'
  1044. } );
  1045. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1046. } );
  1047. it( 'supports the array syntax', () => {
  1048. schema.register( '$root' );
  1049. schema.register( '$root2' );
  1050. schema.register( 'paragraph', {
  1051. allowIn: '$root'
  1052. } );
  1053. schema.register( 'heading1', {
  1054. allowIn: '$root2'
  1055. } );
  1056. schema.register( '$root3', {
  1057. allowContentOf: [ '$root', '$root2' ]
  1058. } );
  1059. const root3 = new Element( '$root3' );
  1060. const heading1 = new Element( 'heading1' );
  1061. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1062. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1063. } );
  1064. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1065. schema.register( '$root' );
  1066. schema.register( '$root2', {
  1067. allowContentOf: '$root'
  1068. } );
  1069. schema.register( 'paragraph', {
  1070. allowIn: '$root'
  1071. } );
  1072. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1073. } );
  1074. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1075. schema.register( '$root' );
  1076. schema.register( '$block' );
  1077. schema.register( 'paragraph', {
  1078. allowIn: '$root',
  1079. allowContentOf: '$block'
  1080. } );
  1081. schema.register( '$text', {
  1082. allowIn: '$block'
  1083. } );
  1084. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1085. } );
  1086. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1087. schema.register( '$root' );
  1088. schema.register( 'blockQuote', {
  1089. allowIn: '$root',
  1090. allowContentOf: '$root'
  1091. } );
  1092. schema.register( 'paragraph', {
  1093. allowIn: '$root'
  1094. } );
  1095. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1096. } );
  1097. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1098. schema.register( '$root' );
  1099. schema.register( 'paragraph' );
  1100. schema.register( '$root2', {
  1101. allowContentOf: '$root'
  1102. } );
  1103. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1104. } );
  1105. } );
  1106. describe( 'mix of allowContentOf and allowWhere', () => {
  1107. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1108. schema.register( '$root' );
  1109. schema.register( '$block', {
  1110. allowIn: '$root'
  1111. } );
  1112. schema.register( 'paragraph', {
  1113. allowContentOf: '$block',
  1114. allowWhere: '$block'
  1115. } );
  1116. schema.register( '$text', {
  1117. allowIn: '$block'
  1118. } );
  1119. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1120. } );
  1121. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1122. 'and paragraph inherits allowWhere from $block', () => {
  1123. schema.register( '$root' );
  1124. schema.register( '$root2', {
  1125. allowContentOf: '$root'
  1126. } );
  1127. schema.register( '$block', {
  1128. allowIn: '$root'
  1129. } );
  1130. schema.register( 'paragraph', {
  1131. allowWhere: '$block'
  1132. } );
  1133. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1134. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1135. } );
  1136. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1137. schema.register( 'b' );
  1138. schema.register( 'a', { allowIn: 'b' } );
  1139. schema.register( 'c', { allowContentOf: 'b' } );
  1140. schema.register( 'd', { allowContentOf: 'c' } );
  1141. const d = new Element( 'd' );
  1142. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1143. } );
  1144. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1145. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1146. // as long as the previous iteration found something to compile.
  1147. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1148. // and since we've found something, we'd now try d<-c which would work.
  1149. //
  1150. // We ignore those situations for now as they are very unlikely to happen and would
  1151. // significantly raised the complexity of definition compilation.
  1152. //
  1153. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1154. // schema.register( 'b' );
  1155. // schema.register( 'a', { allowIn: 'b' } );
  1156. // schema.register( 'd', { allowContentOf: 'c' } );
  1157. // schema.register( 'c', { allowContentOf: 'b' } );
  1158. //
  1159. // const d = new Element( 'd' );
  1160. //
  1161. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1162. // } );
  1163. } );
  1164. describe( 'inheritTypesFrom', () => {
  1165. it( 'inherit properties of another item', () => {
  1166. schema.register( '$block', {
  1167. isBlock: true,
  1168. isLimit: true
  1169. } );
  1170. schema.register( 'paragraph', {
  1171. inheritTypesFrom: '$block'
  1172. } );
  1173. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1174. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1175. } );
  1176. it( 'inherit properties of other items – support for arrays', () => {
  1177. schema.register( '$block', {
  1178. isBlock: true
  1179. } );
  1180. schema.register( '$block2', {
  1181. isLimit: true
  1182. } );
  1183. schema.register( 'paragraph', {
  1184. inheritTypesFrom: [ '$block', '$block2' ]
  1185. } );
  1186. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1187. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1188. } );
  1189. it( 'does not override existing props', () => {
  1190. schema.register( '$block', {
  1191. isBlock: true,
  1192. isLimit: true
  1193. } );
  1194. schema.register( 'paragraph', {
  1195. inheritTypesFrom: '$block',
  1196. isLimit: false
  1197. } );
  1198. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1199. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1200. } );
  1201. } );
  1202. describe( 'inheritAllFrom', () => {
  1203. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1204. schema.register( '$root' );
  1205. schema.register( '$block', {
  1206. allowIn: '$root'
  1207. } );
  1208. schema.register( 'paragraph', {
  1209. inheritAllFrom: '$block'
  1210. } );
  1211. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1212. } );
  1213. it( 'paragraph inherit properties of $block', () => {
  1214. schema.register( '$block', {
  1215. isBlock: true
  1216. } );
  1217. schema.register( 'paragraph', {
  1218. inheritAllFrom: '$block'
  1219. } );
  1220. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1221. } );
  1222. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1223. schema.register( '$root' );
  1224. schema.register( '$block', {
  1225. allowIn: '$root'
  1226. } );
  1227. schema.register( '$text', {
  1228. allowIn: '$block'
  1229. } );
  1230. schema.register( 'paragraph', {
  1231. inheritAllFrom: '$block'
  1232. } );
  1233. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1234. } );
  1235. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1236. schema.register( '$root' );
  1237. schema.register( '$blockProto', {
  1238. allowIn: '$root'
  1239. } );
  1240. schema.register( '$block', {
  1241. allowWhere: '$blockProto'
  1242. } );
  1243. schema.register( 'paragraph', {
  1244. inheritAllFrom: '$block'
  1245. } );
  1246. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1247. } );
  1248. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1249. schema.register( '$root' );
  1250. schema.register( '$blockProto' );
  1251. schema.register( '$block', {
  1252. allowContentOf: '$blockProto',
  1253. allowIn: '$root'
  1254. } );
  1255. schema.register( '$text', {
  1256. allowIn: '$blockProto'
  1257. } );
  1258. schema.register( 'paragraph', {
  1259. inheritAllFrom: '$block'
  1260. } );
  1261. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1262. } );
  1263. } );
  1264. // We need to handle cases where some independent features registered definitions which might use
  1265. // optional elements (elements which might not have been registered).
  1266. describe( 'missing structure definitions', () => {
  1267. it( 'does not break when trying to check a child which is not registered', () => {
  1268. schema.register( '$root' );
  1269. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1270. } );
  1271. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1272. const foo404 = new Element( 'foo404' );
  1273. root1.appendChildren( foo404 );
  1274. schema.register( '$root' );
  1275. schema.register( '$text', {
  1276. allowIn: '$root'
  1277. } );
  1278. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1279. } );
  1280. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1281. schema.register( '$root' );
  1282. schema.register( '$text', {
  1283. allowIn: 'foo404'
  1284. } );
  1285. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1286. } );
  1287. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1288. schema.register( '$root' );
  1289. schema.register( '$text', {
  1290. allowWhere: 'foo404'
  1291. } );
  1292. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1293. } );
  1294. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1295. schema.register( '$root', {
  1296. allowContentOf: 'foo404'
  1297. } );
  1298. schema.register( '$text', {
  1299. allowIn: '$root'
  1300. } );
  1301. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1302. } );
  1303. it( 'checks whether allowIn uses a registered element', () => {
  1304. schema.register( 'paragraph', {
  1305. allowIn: '$root'
  1306. } );
  1307. // $root isn't registered!
  1308. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1309. } );
  1310. it( 'does not break when inheriting all from an non-registered element', () => {
  1311. schema.register( 'paragraph', {
  1312. inheritAllFrom: '$block'
  1313. } );
  1314. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1315. } );
  1316. } );
  1317. describe( 'allowAttributes', () => {
  1318. it( 'passes paragraph[align]', () => {
  1319. schema.register( 'paragraph', {
  1320. allowAttributes: 'align'
  1321. } );
  1322. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1323. } );
  1324. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1325. schema.register( 'paragraph', {
  1326. allowAttributes: [ 'align', 'dir' ]
  1327. } );
  1328. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1329. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1330. } );
  1331. it( 'passes paragraph>$text[bold]', () => {
  1332. schema.register( 'paragraph' );
  1333. schema.register( '$text', {
  1334. allowIn: 'paragraph',
  1335. allowAttributes: 'bold'
  1336. } );
  1337. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1338. } );
  1339. } );
  1340. describe( 'allowAttributesOf', () => {
  1341. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1342. schema.register( '$block', {
  1343. allowAttributes: 'align'
  1344. } );
  1345. schema.register( 'paragraph', {
  1346. allowAttributesOf: '$block'
  1347. } );
  1348. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1349. } );
  1350. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1351. schema.register( '$block', {
  1352. allowAttributes: 'align'
  1353. } );
  1354. schema.register( '$block2', {
  1355. allowAttributes: 'dir'
  1356. } );
  1357. schema.register( 'paragraph', {
  1358. allowAttributesOf: [ '$block', '$block2' ]
  1359. } );
  1360. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1361. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1362. } );
  1363. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1364. schema.register( '$block', {
  1365. allowAttributes: 'align'
  1366. } );
  1367. schema.register( 'paragraph', {
  1368. allowAttributes: 'dir',
  1369. allowAttributesOf: '$block'
  1370. } );
  1371. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1372. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1373. } );
  1374. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1375. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1376. } );
  1377. describe( 'inheritAllFrom', () => {
  1378. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1379. schema.register( '$block', {
  1380. allowAttributes: 'align'
  1381. } );
  1382. schema.register( 'paragraph', {
  1383. inheritAllFrom: '$block'
  1384. } );
  1385. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1386. } );
  1387. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1388. schema.register( '$blockProto', {
  1389. allowAttributes: 'align'
  1390. } );
  1391. schema.register( '$block', {
  1392. allowAttributesOf: '$blockProto'
  1393. } );
  1394. schema.register( 'paragraph', {
  1395. inheritAllFrom: '$block'
  1396. } );
  1397. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1398. } );
  1399. } );
  1400. describe( 'missing attribute definitions', () => {
  1401. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1402. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1403. } );
  1404. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1405. schema.register( 'paragraph', {
  1406. allowAttributesOf: '$block'
  1407. } );
  1408. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1409. } );
  1410. it( 'does not crash when inheriting all from a non-registered element', () => {
  1411. schema.register( 'paragraph', {
  1412. allowAttributesOf: '$block'
  1413. } );
  1414. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1415. } );
  1416. } );
  1417. describe( 'missing types definitions', () => {
  1418. it( 'does not crash when inheriting types of an non-registered element', () => {
  1419. schema.register( 'paragraph', {
  1420. inheritTypesFrom: '$block'
  1421. } );
  1422. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1423. } );
  1424. } );
  1425. } );
  1426. describe( 'real scenarios', () => {
  1427. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1428. const definitions = [
  1429. () => {
  1430. schema.register( 'paragraph', {
  1431. inheritAllFrom: '$block'
  1432. } );
  1433. },
  1434. () => {
  1435. schema.register( 'heading1', {
  1436. inheritAllFrom: '$block'
  1437. } );
  1438. },
  1439. () => {
  1440. schema.register( 'listItem', {
  1441. inheritAllFrom: '$block',
  1442. allowAttributes: [ 'indent', 'type' ]
  1443. } );
  1444. },
  1445. () => {
  1446. schema.register( 'blockQuote', {
  1447. allowWhere: '$block',
  1448. allowContentOf: '$root'
  1449. } );
  1450. // Disallow blockQuote in blockQuote.
  1451. schema.addChildCheck( ( ctx, childDef ) => {
  1452. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1453. return false;
  1454. }
  1455. } );
  1456. },
  1457. () => {
  1458. schema.register( 'image', {
  1459. allowWhere: '$block',
  1460. allowAttributes: [ 'src', 'alt' ],
  1461. isObject: true,
  1462. isBlock: true
  1463. } );
  1464. },
  1465. () => {
  1466. schema.register( 'caption', {
  1467. allowIn: 'image',
  1468. allowContentOf: '$block',
  1469. isLimit: true
  1470. } );
  1471. },
  1472. () => {
  1473. schema.extend( '$text', {
  1474. allowAttributes: [ 'bold', 'italic' ]
  1475. } );
  1476. // Disallow bold in heading1.
  1477. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1478. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1479. return false;
  1480. }
  1481. } );
  1482. },
  1483. () => {
  1484. schema.extend( '$block', {
  1485. allowAttributes: 'alignment'
  1486. } );
  1487. }
  1488. ];
  1489. beforeEach( () => {
  1490. schema.register( '$root', {
  1491. isLimit: true
  1492. } );
  1493. schema.register( '$block', {
  1494. allowIn: '$root',
  1495. isBlock: true
  1496. } );
  1497. schema.register( '$text', {
  1498. allowIn: '$block'
  1499. } );
  1500. for ( const definition of definitions ) {
  1501. definition();
  1502. }
  1503. // or...
  1504. //
  1505. // Use the below code to shuffle the definitions.
  1506. // Don't look here, Szymon!
  1507. //
  1508. // const definitionsCopy = definitions.slice();
  1509. //
  1510. // while ( definitionsCopy.length ) {
  1511. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1512. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1513. // }
  1514. root1 = new Element( '$root', null, [
  1515. new Element( 'paragraph', null, 'foo' ),
  1516. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1517. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1518. new Element( 'heading1', null, 'foo' ),
  1519. new Element( 'blockQuote', null, [
  1520. new Element( 'paragraph', null, 'foo' ),
  1521. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1522. new Element( 'image', null, [
  1523. new Element( 'caption', null, 'foo' )
  1524. ] )
  1525. ] ),
  1526. new Element( 'image', null, [
  1527. new Element( 'caption', null, 'foo' )
  1528. ] )
  1529. ] );
  1530. r1p1 = root1.getChild( 0 );
  1531. r1p2 = root1.getChild( 1 );
  1532. r1lI = root1.getChild( 2 );
  1533. r1h = root1.getChild( 3 );
  1534. r1bQ = root1.getChild( 4 );
  1535. r1i = root1.getChild( 5 );
  1536. r1bQp = r1bQ.getChild( 0 );
  1537. r1bQlI = r1bQ.getChild( 1 );
  1538. r1bQi = r1bQ.getChild( 2 );
  1539. } );
  1540. it( 'passes $root>paragraph', () => {
  1541. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1542. } );
  1543. it( 'passes $root>paragraph>$text', () => {
  1544. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1545. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1546. } );
  1547. it( 'passes $root>listItem', () => {
  1548. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1549. } );
  1550. it( 'passes $root>listItem>$text', () => {
  1551. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1552. } );
  1553. it( 'passes $root>blockQuote>paragraph', () => {
  1554. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1555. } );
  1556. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1557. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1558. } );
  1559. it( 'passes $root>blockQuote>listItem', () => {
  1560. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1561. } );
  1562. it( 'passes $root>blockQuote>listItem>$text', () => {
  1563. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1564. } );
  1565. it( 'passes $root>blockQuote>image', () => {
  1566. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1567. } );
  1568. it( 'passes $root>blockQuote>image>caption', () => {
  1569. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1570. } );
  1571. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1572. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1573. } );
  1574. it( 'passes $root>image', () => {
  1575. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1576. } );
  1577. it( 'passes $root>image>caption', () => {
  1578. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1579. } );
  1580. it( 'passes $root>image>caption>$text', () => {
  1581. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1582. } );
  1583. it( 'rejects $root>$root', () => {
  1584. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1585. } );
  1586. it( 'rejects $root>$text', () => {
  1587. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1588. } );
  1589. it( 'rejects $root>caption', () => {
  1590. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1591. } );
  1592. it( 'rejects $root>paragraph>paragraph', () => {
  1593. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1594. } );
  1595. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1596. // Edge case because p>p should not exist in the first place.
  1597. // But it's good to know that it blocks also this.
  1598. const p = new Element( 'p' );
  1599. r1p1.appendChildren( p );
  1600. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1601. } );
  1602. it( 'rejects $root>paragraph>$block', () => {
  1603. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1604. } );
  1605. it( 'rejects $root>paragraph>blockQuote', () => {
  1606. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1607. } );
  1608. it( 'rejects $root>paragraph>image', () => {
  1609. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1610. } );
  1611. it( 'rejects $root>paragraph>caption', () => {
  1612. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1613. } );
  1614. it( 'rejects $root>blockQuote>blockQuote', () => {
  1615. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1616. } );
  1617. it( 'rejects $root>blockQuote>caption', () => {
  1618. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1619. } );
  1620. it( 'rejects $root>blockQuote>$text', () => {
  1621. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1622. } );
  1623. it( 'rejects $root>image>$text', () => {
  1624. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1625. } );
  1626. it( 'rejects $root>image>paragraph', () => {
  1627. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1628. } );
  1629. it( 'rejects $root>image>caption>paragraph', () => {
  1630. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1631. } );
  1632. it( 'rejects $root>image>caption>blockQuote', () => {
  1633. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1634. } );
  1635. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1636. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1637. } );
  1638. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1639. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1640. } );
  1641. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1642. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1643. } );
  1644. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1645. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1646. } );
  1647. it( 'accepts attribute $root>listItem[alignment]', () => {
  1648. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1649. } );
  1650. it( 'accepts attribute $root>listItem[indent]', () => {
  1651. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1652. } );
  1653. it( 'accepts attribute $root>listItem[type]', () => {
  1654. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1655. } );
  1656. it( 'accepts attribute $root>image[src]', () => {
  1657. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1658. } );
  1659. it( 'accepts attribute $root>image[alt]', () => {
  1660. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1661. } );
  1662. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1663. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1664. } );
  1665. it( 'rejects attribute $root[indent]', () => {
  1666. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1667. } );
  1668. it( 'rejects attribute $root>paragraph[indent]', () => {
  1669. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1670. } );
  1671. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1672. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1673. } );
  1674. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1675. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1676. } );
  1677. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1678. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1679. } );
  1680. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1681. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1682. } );
  1683. it( 'rejects attribute $root>image[indent]', () => {
  1684. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1685. } );
  1686. it( 'rejects attribute $root>image[alignment]', () => {
  1687. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1688. } );
  1689. it( '$root is limit', () => {
  1690. expect( schema.isLimit( '$root' ) ).to.be.true;
  1691. expect( schema.isBlock( '$root' ) ).to.be.false;
  1692. expect( schema.isObject( '$root' ) ).to.be.false;
  1693. } );
  1694. it( 'paragraph is block', () => {
  1695. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  1696. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  1697. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  1698. } );
  1699. it( 'heading1 is block', () => {
  1700. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  1701. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  1702. expect( schema.isObject( 'heading1' ) ).to.be.false;
  1703. } );
  1704. it( 'listItem is block', () => {
  1705. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  1706. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  1707. expect( schema.isObject( 'listItem' ) ).to.be.false;
  1708. } );
  1709. it( 'image is block object', () => {
  1710. expect( schema.isLimit( 'image' ) ).to.be.false;
  1711. expect( schema.isBlock( 'image' ) ).to.be.true;
  1712. expect( schema.isObject( 'image' ) ).to.be.true;
  1713. } );
  1714. it( 'caption is limit', () => {
  1715. expect( schema.isLimit( 'caption' ) ).to.be.true;
  1716. expect( schema.isBlock( 'caption' ) ).to.be.false;
  1717. expect( schema.isObject( 'caption' ) ).to.be.false;
  1718. } );
  1719. } );
  1720. } );
  1721. describe( 'SchemaContext', () => {
  1722. let root;
  1723. beforeEach( () => {
  1724. root = new Element( '$root', null, [
  1725. new Element( 'blockQuote', { foo: 1 }, [
  1726. new Element( 'paragraph', { align: 'left' }, [
  1727. new Text( 'foo', { bold: true, italic: true } )
  1728. ] )
  1729. ] )
  1730. ] );
  1731. } );
  1732. describe( 'constructor()', () => {
  1733. it( 'creates context based on an array of strings', () => {
  1734. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1735. expect( ctx.length ).to.equal( 3 );
  1736. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1737. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  1738. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1739. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1740. } );
  1741. it( 'creates context based on an array of elements', () => {
  1742. const blockQuote = root.getChild( 0 );
  1743. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1744. const ctx = new SchemaContext( [ blockQuote, text ] );
  1745. expect( ctx.length ).to.equal( 2 );
  1746. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  1747. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  1748. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1749. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  1750. } );
  1751. it( 'creates context based on a mixed array of strings and elements', () => {
  1752. const blockQuote = root.getChild( 0 );
  1753. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1754. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  1755. expect( ctx.length ).to.equal( 3 );
  1756. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  1757. } );
  1758. it( 'creates context based on a root element', () => {
  1759. const ctx = new SchemaContext( root );
  1760. expect( ctx.length ).to.equal( 1 );
  1761. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  1762. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1763. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1764. } );
  1765. it( 'creates context based on a nested element', () => {
  1766. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  1767. expect( ctx.length ).to.equal( 3 );
  1768. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1769. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1770. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  1771. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  1772. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  1773. } );
  1774. it( 'creates context based on a text node', () => {
  1775. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1776. expect( ctx.length ).to.equal( 4 );
  1777. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1778. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1779. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1780. } );
  1781. it( 'creates context based on a text proxy', () => {
  1782. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  1783. const textProxy = new TextProxy( text, 0, 1 );
  1784. const ctx = new SchemaContext( textProxy );
  1785. expect( ctx.length ).to.equal( 4 );
  1786. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1787. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1788. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1789. } );
  1790. it( 'creates context based on a position', () => {
  1791. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  1792. const ctx = new SchemaContext( pos );
  1793. expect( ctx.length ).to.equal( 3 );
  1794. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1795. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  1796. } );
  1797. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  1798. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  1799. expect( ctx.length ).to.equal( 1 );
  1800. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1801. } );
  1802. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  1803. const p = new Element( 'paragraph' );
  1804. const docFrag = new DocumentFragment();
  1805. docFrag.appendChildren( p );
  1806. const ctx = new SchemaContext( p );
  1807. expect( ctx.length ).to.equal( 1 );
  1808. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1809. } );
  1810. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  1811. const p = new Element( 'paragraph' );
  1812. const docFrag = new DocumentFragment();
  1813. docFrag.appendChildren( p );
  1814. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  1815. expect( ctx.length ).to.equal( 1 );
  1816. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1817. } );
  1818. } );
  1819. describe( 'length', () => {
  1820. it( 'gets the number of items', () => {
  1821. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1822. expect( ctx.length ).to.equal( 3 );
  1823. } );
  1824. } );
  1825. describe( 'last', () => {
  1826. it( 'gets the last item', () => {
  1827. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1828. expect( ctx.last ).to.be.an( 'object' );
  1829. expect( ctx.last.name ).to.equal( 'c' );
  1830. } );
  1831. } );
  1832. describe( 'Symbol.iterator', () => {
  1833. it( 'exists', () => {
  1834. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1835. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  1836. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1837. } );
  1838. } );
  1839. describe( 'getItem()', () => {
  1840. it( 'returns item by index', () => {
  1841. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1842. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  1843. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  1844. } );
  1845. it( 'returns undefined if index exceeds the range', () => {
  1846. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1847. expect( ctx.getItem( 3 ) ).to.be.undefined;
  1848. } );
  1849. } );
  1850. describe( 'getNames()', () => {
  1851. it( 'returns an iterator', () => {
  1852. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1853. expect( ctx.getNames().next ).to.be.a( 'function' );
  1854. } );
  1855. it( 'returns an iterator which returns all item names', () => {
  1856. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1857. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1858. } );
  1859. } );
  1860. describe( 'endsWith()', () => {
  1861. it( 'returns true if the end of the context matches the query - 1 item', () => {
  1862. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1863. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  1864. } );
  1865. it( 'returns true if the end of the context matches the query - 2 items', () => {
  1866. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1867. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  1868. } );
  1869. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  1870. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  1871. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  1872. } );
  1873. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  1874. const ctx = new SchemaContext( [ 'foo' ] );
  1875. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  1876. } );
  1877. it( 'returns true if not only the end of the context matches the query', () => {
  1878. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  1879. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  1880. } );
  1881. it( 'returns false if query matches the middle of the context', () => {
  1882. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1883. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  1884. } );
  1885. it( 'returns false if query matches the start of the context', () => {
  1886. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1887. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  1888. } );
  1889. it( 'returns false if query does not match', () => {
  1890. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1891. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  1892. } );
  1893. it( 'returns false if query is longer than context', () => {
  1894. const ctx = new SchemaContext( [ 'foo' ] );
  1895. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  1896. } );
  1897. } );
  1898. } );