schema.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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( 'getLimitElement()', () => {
  490. let model, doc, root;
  491. beforeEach( () => {
  492. model = new Model();
  493. doc = model.document;
  494. schema = model.schema;
  495. root = doc.createRoot();
  496. schema.register( 'div', {
  497. inheritAllFrom: '$block'
  498. } );
  499. schema.register( 'article', {
  500. inheritAllFrom: '$block',
  501. allowIn: 'section'
  502. } );
  503. schema.register( 'section', {
  504. inheritAllFrom: '$block',
  505. allowIn: 'div'
  506. } );
  507. schema.register( 'paragraph', {
  508. inheritAllFrom: '$block',
  509. allowIn: 'article'
  510. } );
  511. schema.register( 'widget', {
  512. inheritAllFrom: '$block',
  513. allowIn: 'div'
  514. } );
  515. schema.register( 'image', {
  516. inheritAllFrom: '$block',
  517. allowIn: 'widget'
  518. } );
  519. schema.register( 'caption', {
  520. inheritAllFrom: '$block',
  521. allowIn: 'image'
  522. } );
  523. } );
  524. it( 'always returns $root element if any other limit was not defined', () => {
  525. schema.extend( '$root', {
  526. isLimit: false
  527. } );
  528. expect( schema.isLimit( '$root' ) ).to.be.false;
  529. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  530. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  531. } );
  532. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  533. schema.extend( 'article', { isLimit: true } );
  534. schema.extend( 'section', { isLimit: true } );
  535. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  536. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  537. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  538. } );
  539. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  540. schema.extend( 'article', { isLimit: true } );
  541. schema.extend( 'section', { isLimit: true } );
  542. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  543. const section = root.getNodeByPath( [ 0, 0 ] );
  544. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  545. } );
  546. it( 'works fine with multi-range selections', () => {
  547. schema.extend( 'article', { isLimit: true } );
  548. schema.extend( 'widget', { isLimit: true } );
  549. schema.extend( 'div', { isLimit: true } );
  550. setData(
  551. model,
  552. '<div>' +
  553. '<section>' +
  554. '<article>' +
  555. '<paragraph>[foo]</paragraph>' +
  556. '</article>' +
  557. '</section>' +
  558. '<widget>' +
  559. '<image>' +
  560. '<caption>b[a]r</caption>' +
  561. '</image>' +
  562. '</widget>' +
  563. '</div>'
  564. );
  565. const div = root.getNodeByPath( [ 0 ] );
  566. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  567. } );
  568. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  569. setData(
  570. model,
  571. '<div>' +
  572. '<section>' +
  573. '<article>' +
  574. '<paragraph>[foo]</paragraph>' +
  575. '</article>' +
  576. '</section>' +
  577. '</div>' +
  578. '<section>b[]ar</section>'
  579. );
  580. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  581. } );
  582. } );
  583. describe( 'checkAttributeInSelection()', () => {
  584. const attribute = 'bold';
  585. let model, doc, schema;
  586. beforeEach( () => {
  587. model = new Model();
  588. doc = model.document;
  589. doc.createRoot();
  590. schema = model.schema;
  591. schema.register( 'p', { inheritAllFrom: '$block' } );
  592. schema.register( 'h1', { inheritAllFrom: '$block' } );
  593. schema.register( 'img', { allowWhere: '$text' } );
  594. schema.register( 'figure', {
  595. allowIn: '$root',
  596. allowAttributes: [ 'name', 'title' ]
  597. } );
  598. schema.addAttributeCheck( ( ctx, attributeName ) => {
  599. // Allow 'bold' on p>$text.
  600. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  601. return true;
  602. }
  603. // Allow 'bold' on $root>p.
  604. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  605. return true;
  606. }
  607. } );
  608. } );
  609. describe( 'when selection is collapsed', () => {
  610. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  611. setData( model, '<p>f[]oo</p>' );
  612. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  613. } );
  614. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  615. setData( model, '<h1>[]</h1>' );
  616. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  617. setData( model, '[]' );
  618. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  619. } );
  620. } );
  621. describe( 'when selection is not collapsed', () => {
  622. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  623. // Simple selection on a few characters.
  624. setData( model, '<p>[foo]</p>' );
  625. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  626. // Selection spans over characters but also include nodes that can't have attribute.
  627. setData( model, '<p>fo[o<img />b]ar</p>' );
  628. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  629. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  630. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  631. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  632. // Selection on empty P. P can have the attribute.
  633. setData( model, '[<p></p>]' );
  634. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  635. } );
  636. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  637. // Selection on DIV which can't have bold text.
  638. setData( model, '[<h1></h1>]' );
  639. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  640. // Selection on two images which can't be bold.
  641. setData( model, '<p>foo[<img /><img />]bar</p>' );
  642. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  643. } );
  644. it( 'should return true when checking element with required attribute', () => {
  645. setData( model, '[<figure name="figure"></figure>]' );
  646. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  647. } );
  648. it( 'should return true when checking element when attribute is already present', () => {
  649. setData( model, '[<figure name="figure" title="title"></figure>]' );
  650. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  651. } );
  652. } );
  653. } );
  654. describe( 'getValidRanges()', () => {
  655. const attribute = 'bold';
  656. let model, doc, root, schema, ranges;
  657. beforeEach( () => {
  658. model = new Model();
  659. doc = model.document;
  660. schema = model.schema;
  661. root = doc.createRoot();
  662. schema.register( 'p', { inheritAllFrom: '$block' } );
  663. schema.register( 'h1', { inheritAllFrom: '$block' } );
  664. schema.register( 'img', {
  665. allowWhere: '$text'
  666. } );
  667. schema.addAttributeCheck( ( ctx, attributeName ) => {
  668. // Allow 'bold' on p>$text.
  669. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  670. return true;
  671. }
  672. // Allow 'bold' on $root>p.
  673. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  674. return true;
  675. }
  676. } );
  677. setData( model, '<p>foo<img />bar</p>' );
  678. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  679. } );
  680. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  681. schema.extend( 'img', { allowAttributes: 'bold' } );
  682. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  683. } );
  684. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  685. schema.extend( 'img', { allowAttributes: 'bold' } );
  686. schema.extend( '$text', { allowIn: 'img' } );
  687. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  688. } );
  689. it( 'should return two ranges when attribute is not allowed on one item', () => {
  690. schema.extend( 'img', { allowAttributes: 'bold' } );
  691. schema.extend( '$text', { allowIn: 'img' } );
  692. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  693. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  694. const sel = new Selection();
  695. sel.setRanges( validRanges );
  696. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  697. } );
  698. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  699. schema.extend( '$text', { allowIn: 'img' } );
  700. schema.addAttributeCheck( ( ctx, attributeName ) => {
  701. // Allow 'bold' on img>$text.
  702. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  703. return true;
  704. }
  705. } );
  706. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  707. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  708. const sel = new Selection();
  709. sel.setRanges( validRanges );
  710. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  711. } );
  712. it( 'should not leak beyond the given ranges', () => {
  713. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  714. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  715. const sel = new Selection();
  716. sel.setRanges( validRanges );
  717. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  718. } );
  719. it( 'should correctly handle a range which ends in a disallowed position', () => {
  720. schema.extend( '$text', { allowIn: 'img' } );
  721. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  722. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  723. const sel = new Selection();
  724. sel.setRanges( validRanges );
  725. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  726. } );
  727. it( 'should split range into two ranges and omit disallowed element', () => {
  728. schema.addAttributeCheck( ( ctx, attributeName ) => {
  729. // Disallow 'bold' on p>img.
  730. if ( ctx.endsWith( 'p img' ) && attributeName == 'bold' ) {
  731. return false;
  732. }
  733. } );
  734. const result = schema.getValidRanges( ranges, attribute );
  735. expect( result ).to.length( 2 );
  736. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  737. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  738. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  739. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  740. } );
  741. } );
  742. describe( 'removeDisallowedAttributes()', () => {
  743. let model, doc, root;
  744. beforeEach( () => {
  745. model = new Model();
  746. doc = model.document;
  747. root = doc.createRoot();
  748. schema = model.schema;
  749. schema.register( 'paragraph', {
  750. inheritAllFrom: '$block'
  751. } );
  752. schema.register( 'div', {
  753. inheritAllFrom: '$block'
  754. } );
  755. schema.register( 'image', {
  756. isObject: true
  757. } );
  758. schema.extend( '$block', {
  759. allowIn: 'div'
  760. } );
  761. } );
  762. it( 'should filter out disallowed attributes from given nodes', () => {
  763. schema.extend( '$text', { allowAttributes: 'a' } );
  764. schema.extend( 'image', { allowAttributes: 'b' } );
  765. const text = new Text( 'foo', { a: 1, b: 1 } );
  766. const image = new Element( 'image', { a: 1, b: 1 } );
  767. root.appendChildren( [ text, image ] );
  768. model.change( writer => {
  769. schema.removeDisallowedAttributes( root.getChildren(), writer );
  770. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  771. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  772. expect( writer.batch.deltas ).to.length( 2 );
  773. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  774. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  775. expect( getData( model, { withoutSelection: true } ) )
  776. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  777. } );
  778. } );
  779. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  780. schema.addAttributeCheck( ( ctx, attributeName ) => {
  781. // Allow 'a' on div>$text.
  782. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  783. return true;
  784. }
  785. // Allow 'b' on div>paragraph>$text.
  786. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  787. return true;
  788. }
  789. // Allow 'a' on div>image.
  790. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  791. return true;
  792. }
  793. // Allow 'b' on div>paragraph>image.
  794. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  795. return true;
  796. }
  797. } );
  798. const foo = new Text( 'foo', { a: 1, b: 1 } );
  799. const bar = new Text( 'bar', { a: 1, b: 1 } );
  800. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  801. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  802. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  803. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  804. root.appendChildren( [ div ] );
  805. model.change( writer => {
  806. schema.removeDisallowedAttributes( root.getChildren(), writer );
  807. expect( writer.batch.deltas ).to.length( 4 );
  808. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  809. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  810. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  811. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  812. expect( getData( model, { withoutSelection: true } ) )
  813. .to.equal(
  814. '<div>' +
  815. '<paragraph>' +
  816. '<$text b="1">foo</$text>' +
  817. '<image b="1"></image>' +
  818. '</paragraph>' +
  819. '<$text a="1">bar</$text>' +
  820. '<image a="1"></image>' +
  821. '</div>'
  822. );
  823. } );
  824. } );
  825. } );
  826. describe( 'definitions compilation', () => {
  827. describe( 'allowIn cases', () => {
  828. it( 'passes $root>paragraph', () => {
  829. schema.register( '$root' );
  830. schema.register( 'paragraph', {
  831. allowIn: '$root'
  832. } );
  833. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  834. } );
  835. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  836. schema.register( '$root' );
  837. schema.register( '$root2' );
  838. schema.register( 'paragraph', {
  839. allowIn: [ '$root', '$root2' ]
  840. } );
  841. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  842. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  843. } );
  844. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  845. schema.register( '$root' );
  846. schema.register( 'paragraph', {
  847. allowIn: '$root'
  848. } );
  849. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  850. } );
  851. it( 'passes $root>div>div – in case of circular refs', () => {
  852. schema.register( '$root' );
  853. schema.register( 'div', {
  854. allowIn: [ '$root', 'div' ]
  855. } );
  856. const div = new Element( 'div' );
  857. root1.appendChildren( div );
  858. const div2 = new Element( 'div' );
  859. expect( schema.checkChild( div, div2 ) ).to.be.true;
  860. } );
  861. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  862. schema.register( '$root' );
  863. schema.register( 'div', {
  864. allowIn: [ '$root', 'div' ]
  865. } );
  866. const div = new Element( 'div' );
  867. root1.appendChildren( div );
  868. expect( schema.checkChild( div, div ) ).to.be.true;
  869. } );
  870. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  871. schema.register( '$root' );
  872. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  873. } );
  874. it( 'rejects $root>paragraph – registered different item', () => {
  875. schema.register( '$root' );
  876. schema.register( 'paragraph' );
  877. schema.register( 'listItem', {
  878. allowIn: '$root'
  879. } );
  880. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  881. } );
  882. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  883. schema.register( '$root' );
  884. schema.register( '$fancyRoot' );
  885. schema.register( 'paragraph', {
  886. allowIn: '$fancyRoot'
  887. } );
  888. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  889. } );
  890. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  891. schema.register( '$root' );
  892. schema.register( 'paragraph', {
  893. allowIn: '$root'
  894. } );
  895. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  896. } );
  897. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  898. schema.register( '$root' );
  899. schema.register( 'blockQuote', {
  900. allowIn: '$root'
  901. } );
  902. schema.register( 'paragraph', {
  903. allowIn: '$root'
  904. } );
  905. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  906. } );
  907. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  908. schema.register( '$root' );
  909. schema.register( 'paragraph', {
  910. allowIn: '$root'
  911. } );
  912. schema.register( '$text', {
  913. allowIn: 'paragraph'
  914. } );
  915. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  916. } );
  917. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  918. schema.register( '$root' );
  919. schema.register( 'blockQuote' );
  920. schema.register( 'paragraph', {
  921. allowIn: [ 'blockQuote', '$root' ]
  922. } );
  923. schema.register( '$text', {
  924. allowIn: 'paragraph'
  925. } );
  926. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  927. } );
  928. } );
  929. describe( 'allowWhere cases', () => {
  930. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  931. schema.register( '$root' );
  932. schema.register( '$block', {
  933. allowIn: '$root'
  934. } );
  935. schema.register( 'paragraph', {
  936. allowWhere: '$block'
  937. } );
  938. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  939. } );
  940. it( 'supports the array syntax', () => {
  941. schema.register( '$root' );
  942. schema.register( '$root2' );
  943. schema.register( '$block', {
  944. allowIn: '$root'
  945. } );
  946. schema.register( '$block2', {
  947. allowIn: '$root2'
  948. } );
  949. schema.register( 'paragraph', {
  950. allowWhere: [ '$block', '$block2' ]
  951. } );
  952. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  953. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  954. } );
  955. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  956. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  957. schema.register( '$root' );
  958. schema.register( 'paragraph', {
  959. allowWhere: '$block'
  960. } );
  961. schema.register( '$block', {
  962. allowIn: '$root'
  963. } );
  964. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  965. } );
  966. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  967. schema.register( '$root' );
  968. schema.register( '$block', {
  969. allowIn: '$root'
  970. } );
  971. schema.register( '$specialBlock', {
  972. allowWhere: '$block'
  973. } );
  974. schema.register( 'paragraph', {
  975. allowWhere: '$specialBlock'
  976. } );
  977. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  978. } );
  979. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  980. schema.register( '$root' );
  981. schema.register( '$block' );
  982. schema.register( 'paragraph', {
  983. allowWhere: '$block'
  984. } );
  985. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  986. } );
  987. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  988. schema.register( '$root' );
  989. schema.register( '$block' );
  990. schema.register( 'paragraph', {
  991. allowWhere: '$block'
  992. } );
  993. schema.register( '$text', {
  994. allowIn: 'paragraph'
  995. } );
  996. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  997. } );
  998. } );
  999. describe( 'allowContentOf cases', () => {
  1000. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1001. schema.register( '$root' );
  1002. schema.register( 'paragraph', {
  1003. allowIn: '$root'
  1004. } );
  1005. schema.register( '$root2', {
  1006. allowContentOf: '$root'
  1007. } );
  1008. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1009. } );
  1010. it( 'supports the array syntax', () => {
  1011. schema.register( '$root' );
  1012. schema.register( '$root2' );
  1013. schema.register( 'paragraph', {
  1014. allowIn: '$root'
  1015. } );
  1016. schema.register( 'heading1', {
  1017. allowIn: '$root2'
  1018. } );
  1019. schema.register( '$root3', {
  1020. allowContentOf: [ '$root', '$root2' ]
  1021. } );
  1022. const root3 = new Element( '$root3' );
  1023. const heading1 = new Element( 'heading1' );
  1024. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1025. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1026. } );
  1027. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1028. schema.register( '$root' );
  1029. schema.register( '$root2', {
  1030. allowContentOf: '$root'
  1031. } );
  1032. schema.register( 'paragraph', {
  1033. allowIn: '$root'
  1034. } );
  1035. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1036. } );
  1037. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1038. schema.register( '$root' );
  1039. schema.register( '$block' );
  1040. schema.register( 'paragraph', {
  1041. allowIn: '$root',
  1042. allowContentOf: '$block'
  1043. } );
  1044. schema.register( '$text', {
  1045. allowIn: '$block'
  1046. } );
  1047. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1048. } );
  1049. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1050. schema.register( '$root' );
  1051. schema.register( 'blockQuote', {
  1052. allowIn: '$root',
  1053. allowContentOf: '$root'
  1054. } );
  1055. schema.register( 'paragraph', {
  1056. allowIn: '$root'
  1057. } );
  1058. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1059. } );
  1060. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1061. schema.register( '$root' );
  1062. schema.register( 'paragraph' );
  1063. schema.register( '$root2', {
  1064. allowContentOf: '$root'
  1065. } );
  1066. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1067. } );
  1068. } );
  1069. describe( 'mix of allowContentOf and allowWhere', () => {
  1070. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1071. schema.register( '$root' );
  1072. schema.register( '$block', {
  1073. allowIn: '$root'
  1074. } );
  1075. schema.register( 'paragraph', {
  1076. allowContentOf: '$block',
  1077. allowWhere: '$block'
  1078. } );
  1079. schema.register( '$text', {
  1080. allowIn: '$block'
  1081. } );
  1082. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1083. } );
  1084. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1085. 'and paragraph inherits allowWhere from $block', () => {
  1086. schema.register( '$root' );
  1087. schema.register( '$root2', {
  1088. allowContentOf: '$root'
  1089. } );
  1090. schema.register( '$block', {
  1091. allowIn: '$root'
  1092. } );
  1093. schema.register( 'paragraph', {
  1094. allowWhere: '$block'
  1095. } );
  1096. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1097. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1098. } );
  1099. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1100. schema.register( 'b' );
  1101. schema.register( 'a', { allowIn: 'b' } );
  1102. schema.register( 'c', { allowContentOf: 'b' } );
  1103. schema.register( 'd', { allowContentOf: 'c' } );
  1104. const d = new Element( 'd' );
  1105. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1106. } );
  1107. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1108. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1109. // as long as the previous iteration found something to compile.
  1110. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1111. // and since we've found something, we'd now try d<-c which would work.
  1112. //
  1113. // We ignore those situations for now as they are very unlikely to happen and would
  1114. // significantly raised the complexity of definition compilation.
  1115. //
  1116. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1117. // schema.register( 'b' );
  1118. // schema.register( 'a', { allowIn: 'b' } );
  1119. // schema.register( 'd', { allowContentOf: 'c' } );
  1120. // schema.register( 'c', { allowContentOf: 'b' } );
  1121. //
  1122. // const d = new Element( 'd' );
  1123. //
  1124. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1125. // } );
  1126. } );
  1127. describe( 'inheritTypesFrom', () => {
  1128. it( 'inherit properties of another item', () => {
  1129. schema.register( '$block', {
  1130. isBlock: true,
  1131. isLimit: true
  1132. } );
  1133. schema.register( 'paragraph', {
  1134. inheritTypesFrom: '$block'
  1135. } );
  1136. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1137. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1138. } );
  1139. it( 'inherit properties of other items – support for arrays', () => {
  1140. schema.register( '$block', {
  1141. isBlock: true
  1142. } );
  1143. schema.register( '$block2', {
  1144. isLimit: true
  1145. } );
  1146. schema.register( 'paragraph', {
  1147. inheritTypesFrom: [ '$block', '$block2' ]
  1148. } );
  1149. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1150. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1151. } );
  1152. it( 'does not override existing props', () => {
  1153. schema.register( '$block', {
  1154. isBlock: true,
  1155. isLimit: true
  1156. } );
  1157. schema.register( 'paragraph', {
  1158. inheritTypesFrom: '$block',
  1159. isLimit: false
  1160. } );
  1161. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1162. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1163. } );
  1164. } );
  1165. describe( 'inheritAllFrom', () => {
  1166. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1167. schema.register( '$root' );
  1168. schema.register( '$block', {
  1169. allowIn: '$root'
  1170. } );
  1171. schema.register( 'paragraph', {
  1172. inheritAllFrom: '$block'
  1173. } );
  1174. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1175. } );
  1176. it( 'paragraph inherit properties of $block', () => {
  1177. schema.register( '$block', {
  1178. isBlock: true
  1179. } );
  1180. schema.register( 'paragraph', {
  1181. inheritAllFrom: '$block'
  1182. } );
  1183. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1184. } );
  1185. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1186. schema.register( '$root' );
  1187. schema.register( '$block', {
  1188. allowIn: '$root'
  1189. } );
  1190. schema.register( '$text', {
  1191. allowIn: '$block'
  1192. } );
  1193. schema.register( 'paragraph', {
  1194. inheritAllFrom: '$block'
  1195. } );
  1196. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1197. } );
  1198. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1199. schema.register( '$root' );
  1200. schema.register( '$blockProto', {
  1201. allowIn: '$root'
  1202. } );
  1203. schema.register( '$block', {
  1204. allowWhere: '$blockProto'
  1205. } );
  1206. schema.register( 'paragraph', {
  1207. inheritAllFrom: '$block'
  1208. } );
  1209. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1210. } );
  1211. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1212. schema.register( '$root' );
  1213. schema.register( '$blockProto' );
  1214. schema.register( '$block', {
  1215. allowContentOf: '$blockProto',
  1216. allowIn: '$root'
  1217. } );
  1218. schema.register( '$text', {
  1219. allowIn: '$blockProto'
  1220. } );
  1221. schema.register( 'paragraph', {
  1222. inheritAllFrom: '$block'
  1223. } );
  1224. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1225. } );
  1226. } );
  1227. // We need to handle cases where some independent features registered definitions which might use
  1228. // optional elements (elements which might not have been registered).
  1229. describe( 'missing structure definitions', () => {
  1230. it( 'does not break when trying to check a child which is not registered', () => {
  1231. schema.register( '$root' );
  1232. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1233. } );
  1234. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1235. const foo404 = new Element( 'foo404' );
  1236. root1.appendChildren( foo404 );
  1237. schema.register( '$root' );
  1238. schema.register( '$text', {
  1239. allowIn: '$root'
  1240. } );
  1241. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1242. } );
  1243. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1244. schema.register( '$root' );
  1245. schema.register( '$text', {
  1246. allowIn: 'foo404'
  1247. } );
  1248. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1249. } );
  1250. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1251. schema.register( '$root' );
  1252. schema.register( '$text', {
  1253. allowWhere: 'foo404'
  1254. } );
  1255. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1256. } );
  1257. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1258. schema.register( '$root', {
  1259. allowContentOf: 'foo404'
  1260. } );
  1261. schema.register( '$text', {
  1262. allowIn: '$root'
  1263. } );
  1264. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1265. } );
  1266. it( 'checks whether allowIn uses a registered element', () => {
  1267. schema.register( 'paragraph', {
  1268. allowIn: '$root'
  1269. } );
  1270. // $root isn't registered!
  1271. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1272. } );
  1273. it( 'does not break when inheriting all from an non-registered element', () => {
  1274. schema.register( 'paragraph', {
  1275. inheritAllFrom: '$block'
  1276. } );
  1277. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1278. } );
  1279. } );
  1280. describe( 'allowAttributes', () => {
  1281. it( 'passes paragraph[align]', () => {
  1282. schema.register( 'paragraph', {
  1283. allowAttributes: 'align'
  1284. } );
  1285. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1286. } );
  1287. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1288. schema.register( 'paragraph', {
  1289. allowAttributes: [ 'align', 'dir' ]
  1290. } );
  1291. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1292. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1293. } );
  1294. it( 'passes paragraph>$text[bold]', () => {
  1295. schema.register( 'paragraph' );
  1296. schema.register( '$text', {
  1297. allowIn: 'paragraph',
  1298. allowAttributes: 'bold'
  1299. } );
  1300. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1301. } );
  1302. } );
  1303. describe( 'allowAttributesOf', () => {
  1304. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1305. schema.register( '$block', {
  1306. allowAttributes: 'align'
  1307. } );
  1308. schema.register( 'paragraph', {
  1309. allowAttributesOf: '$block'
  1310. } );
  1311. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1312. } );
  1313. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1314. schema.register( '$block', {
  1315. allowAttributes: 'align'
  1316. } );
  1317. schema.register( '$block2', {
  1318. allowAttributes: 'dir'
  1319. } );
  1320. schema.register( 'paragraph', {
  1321. allowAttributesOf: [ '$block', '$block2' ]
  1322. } );
  1323. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1324. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1325. } );
  1326. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1327. schema.register( '$block', {
  1328. allowAttributes: 'align'
  1329. } );
  1330. schema.register( 'paragraph', {
  1331. allowAttributes: 'dir',
  1332. allowAttributesOf: '$block'
  1333. } );
  1334. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1335. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1336. } );
  1337. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1338. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1339. } );
  1340. describe( 'inheritAllFrom', () => {
  1341. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1342. schema.register( '$block', {
  1343. allowAttributes: 'align'
  1344. } );
  1345. schema.register( 'paragraph', {
  1346. inheritAllFrom: '$block'
  1347. } );
  1348. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1349. } );
  1350. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1351. schema.register( '$blockProto', {
  1352. allowAttributes: 'align'
  1353. } );
  1354. schema.register( '$block', {
  1355. allowAttributesOf: '$blockProto'
  1356. } );
  1357. schema.register( 'paragraph', {
  1358. inheritAllFrom: '$block'
  1359. } );
  1360. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1361. } );
  1362. } );
  1363. describe( 'missing attribute definitions', () => {
  1364. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1365. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1366. } );
  1367. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1368. schema.register( 'paragraph', {
  1369. allowAttributesOf: '$block'
  1370. } );
  1371. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1372. } );
  1373. it( 'does not crash when inheriting all from a non-registered element', () => {
  1374. schema.register( 'paragraph', {
  1375. allowAttributesOf: '$block'
  1376. } );
  1377. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1378. } );
  1379. } );
  1380. describe( 'missing types definitions', () => {
  1381. it( 'does not crash when inheriting types of an non-registered element', () => {
  1382. schema.register( 'paragraph', {
  1383. inheritTypesFrom: '$block'
  1384. } );
  1385. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1386. } );
  1387. } );
  1388. } );
  1389. describe( 'real scenarios', () => {
  1390. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1391. const definitions = [
  1392. () => {
  1393. schema.register( 'paragraph', {
  1394. inheritAllFrom: '$block'
  1395. } );
  1396. },
  1397. () => {
  1398. schema.register( 'heading1', {
  1399. inheritAllFrom: '$block'
  1400. } );
  1401. },
  1402. () => {
  1403. schema.register( 'listItem', {
  1404. inheritAllFrom: '$block',
  1405. allowAttributes: [ 'indent', 'type' ]
  1406. } );
  1407. },
  1408. () => {
  1409. schema.register( 'blockQuote', {
  1410. allowWhere: '$block',
  1411. allowContentOf: '$root'
  1412. } );
  1413. // Disallow blockQuote in blockQuote.
  1414. schema.addChildCheck( ( ctx, childDef ) => {
  1415. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1416. return false;
  1417. }
  1418. } );
  1419. },
  1420. () => {
  1421. schema.register( 'image', {
  1422. allowWhere: '$block',
  1423. allowAttributes: [ 'src', 'alt' ],
  1424. isObject: true,
  1425. isBlock: true
  1426. } );
  1427. },
  1428. () => {
  1429. schema.register( 'caption', {
  1430. allowIn: 'image',
  1431. allowContentOf: '$block',
  1432. isLimit: true
  1433. } );
  1434. },
  1435. () => {
  1436. schema.extend( '$text', {
  1437. allowAttributes: [ 'bold', 'italic' ]
  1438. } );
  1439. // Disallow bold in heading1.
  1440. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1441. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1442. return false;
  1443. }
  1444. } );
  1445. },
  1446. () => {
  1447. schema.extend( '$block', {
  1448. allowAttributes: 'alignment'
  1449. } );
  1450. }
  1451. ];
  1452. beforeEach( () => {
  1453. schema.register( '$root', {
  1454. isLimit: true
  1455. } );
  1456. schema.register( '$block', {
  1457. allowIn: '$root',
  1458. isBlock: true
  1459. } );
  1460. schema.register( '$text', {
  1461. allowIn: '$block'
  1462. } );
  1463. for ( const definition of definitions ) {
  1464. definition();
  1465. }
  1466. // or...
  1467. //
  1468. // Use the below code to shuffle the definitions.
  1469. // Don't look here, Szymon!
  1470. //
  1471. // const definitionsCopy = definitions.slice();
  1472. //
  1473. // while ( definitionsCopy.length ) {
  1474. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1475. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1476. // }
  1477. root1 = new Element( '$root', null, [
  1478. new Element( 'paragraph', null, 'foo' ),
  1479. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1480. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1481. new Element( 'heading1', null, 'foo' ),
  1482. new Element( 'blockQuote', null, [
  1483. new Element( 'paragraph', null, 'foo' ),
  1484. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1485. new Element( 'image', null, [
  1486. new Element( 'caption', null, 'foo' )
  1487. ] )
  1488. ] ),
  1489. new Element( 'image', null, [
  1490. new Element( 'caption', null, 'foo' )
  1491. ] )
  1492. ] );
  1493. r1p1 = root1.getChild( 0 );
  1494. r1p2 = root1.getChild( 1 );
  1495. r1lI = root1.getChild( 2 );
  1496. r1h = root1.getChild( 3 );
  1497. r1bQ = root1.getChild( 4 );
  1498. r1i = root1.getChild( 5 );
  1499. r1bQp = r1bQ.getChild( 0 );
  1500. r1bQlI = r1bQ.getChild( 1 );
  1501. r1bQi = r1bQ.getChild( 2 );
  1502. } );
  1503. it( 'passes $root>paragraph', () => {
  1504. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1505. } );
  1506. it( 'passes $root>paragraph>$text', () => {
  1507. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1508. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1509. } );
  1510. it( 'passes $root>listItem', () => {
  1511. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1512. } );
  1513. it( 'passes $root>listItem>$text', () => {
  1514. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1515. } );
  1516. it( 'passes $root>blockQuote>paragraph', () => {
  1517. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1518. } );
  1519. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1520. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1521. } );
  1522. it( 'passes $root>blockQuote>listItem', () => {
  1523. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1524. } );
  1525. it( 'passes $root>blockQuote>listItem>$text', () => {
  1526. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1527. } );
  1528. it( 'passes $root>blockQuote>image', () => {
  1529. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1530. } );
  1531. it( 'passes $root>blockQuote>image>caption', () => {
  1532. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1533. } );
  1534. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1535. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1536. } );
  1537. it( 'passes $root>image', () => {
  1538. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1539. } );
  1540. it( 'passes $root>image>caption', () => {
  1541. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1542. } );
  1543. it( 'passes $root>image>caption>$text', () => {
  1544. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1545. } );
  1546. it( 'rejects $root>$root', () => {
  1547. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1548. } );
  1549. it( 'rejects $root>$text', () => {
  1550. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1551. } );
  1552. it( 'rejects $root>caption', () => {
  1553. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1554. } );
  1555. it( 'rejects $root>paragraph>paragraph', () => {
  1556. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1557. } );
  1558. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1559. // Edge case because p>p should not exist in the first place.
  1560. // But it's good to know that it blocks also this.
  1561. const p = new Element( 'p' );
  1562. r1p1.appendChildren( p );
  1563. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1564. } );
  1565. it( 'rejects $root>paragraph>$block', () => {
  1566. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1567. } );
  1568. it( 'rejects $root>paragraph>blockQuote', () => {
  1569. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1570. } );
  1571. it( 'rejects $root>paragraph>image', () => {
  1572. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1573. } );
  1574. it( 'rejects $root>paragraph>caption', () => {
  1575. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1576. } );
  1577. it( 'rejects $root>blockQuote>blockQuote', () => {
  1578. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1579. } );
  1580. it( 'rejects $root>blockQuote>caption', () => {
  1581. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1582. } );
  1583. it( 'rejects $root>blockQuote>$text', () => {
  1584. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1585. } );
  1586. it( 'rejects $root>image>$text', () => {
  1587. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1588. } );
  1589. it( 'rejects $root>image>paragraph', () => {
  1590. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1591. } );
  1592. it( 'rejects $root>image>caption>paragraph', () => {
  1593. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1594. } );
  1595. it( 'rejects $root>image>caption>blockQuote', () => {
  1596. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1597. } );
  1598. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1599. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1600. } );
  1601. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1602. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1603. } );
  1604. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1605. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1606. } );
  1607. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1608. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1609. } );
  1610. it( 'accepts attribute $root>listItem[alignment]', () => {
  1611. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1612. } );
  1613. it( 'accepts attribute $root>listItem[indent]', () => {
  1614. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1615. } );
  1616. it( 'accepts attribute $root>listItem[type]', () => {
  1617. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1618. } );
  1619. it( 'accepts attribute $root>image[src]', () => {
  1620. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1621. } );
  1622. it( 'accepts attribute $root>image[alt]', () => {
  1623. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1624. } );
  1625. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1626. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1627. } );
  1628. it( 'rejects attribute $root[indent]', () => {
  1629. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1630. } );
  1631. it( 'rejects attribute $root>paragraph[indent]', () => {
  1632. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1633. } );
  1634. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1635. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1636. } );
  1637. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1638. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1639. } );
  1640. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1641. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1642. } );
  1643. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1644. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1645. } );
  1646. it( 'rejects attribute $root>image[indent]', () => {
  1647. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1648. } );
  1649. it( 'rejects attribute $root>image[alignment]', () => {
  1650. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1651. } );
  1652. it( '$root is limit', () => {
  1653. expect( schema.isLimit( '$root' ) ).to.be.true;
  1654. expect( schema.isBlock( '$root' ) ).to.be.false;
  1655. expect( schema.isObject( '$root' ) ).to.be.false;
  1656. } );
  1657. it( 'paragraph is block', () => {
  1658. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  1659. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  1660. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  1661. } );
  1662. it( 'heading1 is block', () => {
  1663. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  1664. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  1665. expect( schema.isObject( 'heading1' ) ).to.be.false;
  1666. } );
  1667. it( 'listItem is block', () => {
  1668. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  1669. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  1670. expect( schema.isObject( 'listItem' ) ).to.be.false;
  1671. } );
  1672. it( 'image is block object', () => {
  1673. expect( schema.isLimit( 'image' ) ).to.be.false;
  1674. expect( schema.isBlock( 'image' ) ).to.be.true;
  1675. expect( schema.isObject( 'image' ) ).to.be.true;
  1676. } );
  1677. it( 'caption is limit', () => {
  1678. expect( schema.isLimit( 'caption' ) ).to.be.true;
  1679. expect( schema.isBlock( 'caption' ) ).to.be.false;
  1680. expect( schema.isObject( 'caption' ) ).to.be.false;
  1681. } );
  1682. } );
  1683. } );
  1684. describe( 'SchemaContext', () => {
  1685. let root;
  1686. beforeEach( () => {
  1687. root = new Element( '$root', null, [
  1688. new Element( 'blockQuote', { foo: 1 }, [
  1689. new Element( 'paragraph', { align: 'left' }, [
  1690. new Text( 'foo', { bold: true, italic: true } )
  1691. ] )
  1692. ] )
  1693. ] );
  1694. } );
  1695. describe( 'constructor()', () => {
  1696. it( 'creates context based on an array of strings', () => {
  1697. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1698. expect( ctx.length ).to.equal( 3 );
  1699. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1700. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  1701. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1702. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1703. } );
  1704. it( 'creates context based on an array of elements', () => {
  1705. const blockQuote = root.getChild( 0 );
  1706. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1707. const ctx = new SchemaContext( [ blockQuote, text ] );
  1708. expect( ctx.length ).to.equal( 2 );
  1709. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  1710. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  1711. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1712. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  1713. } );
  1714. it( 'creates context based on a mixed array of strings and elements', () => {
  1715. const blockQuote = root.getChild( 0 );
  1716. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1717. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  1718. expect( ctx.length ).to.equal( 3 );
  1719. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  1720. } );
  1721. it( 'creates context based on a root element', () => {
  1722. const ctx = new SchemaContext( root );
  1723. expect( ctx.length ).to.equal( 1 );
  1724. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  1725. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1726. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1727. } );
  1728. it( 'creates context based on a nested element', () => {
  1729. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  1730. expect( ctx.length ).to.equal( 3 );
  1731. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1732. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1733. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  1734. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  1735. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  1736. } );
  1737. it( 'creates context based on a text node', () => {
  1738. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1739. expect( ctx.length ).to.equal( 4 );
  1740. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1741. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1742. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1743. } );
  1744. it( 'creates context based on a text proxy', () => {
  1745. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  1746. const textProxy = new TextProxy( text, 0, 1 );
  1747. const ctx = new SchemaContext( textProxy );
  1748. expect( ctx.length ).to.equal( 4 );
  1749. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1750. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1751. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1752. } );
  1753. it( 'creates context based on a position', () => {
  1754. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  1755. const ctx = new SchemaContext( pos );
  1756. expect( ctx.length ).to.equal( 3 );
  1757. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1758. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  1759. } );
  1760. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  1761. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  1762. expect( ctx.length ).to.equal( 1 );
  1763. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1764. } );
  1765. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  1766. const p = new Element( 'paragraph' );
  1767. const docFrag = new DocumentFragment();
  1768. docFrag.appendChildren( p );
  1769. const ctx = new SchemaContext( p );
  1770. expect( ctx.length ).to.equal( 1 );
  1771. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1772. } );
  1773. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  1774. const p = new Element( 'paragraph' );
  1775. const docFrag = new DocumentFragment();
  1776. docFrag.appendChildren( p );
  1777. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  1778. expect( ctx.length ).to.equal( 1 );
  1779. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1780. } );
  1781. } );
  1782. describe( 'length', () => {
  1783. it( 'gets the number of items', () => {
  1784. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1785. expect( ctx.length ).to.equal( 3 );
  1786. } );
  1787. } );
  1788. describe( 'last', () => {
  1789. it( 'gets the last item', () => {
  1790. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1791. expect( ctx.last ).to.be.an( 'object' );
  1792. expect( ctx.last.name ).to.equal( 'c' );
  1793. } );
  1794. } );
  1795. describe( 'Symbol.iterator', () => {
  1796. it( 'exists', () => {
  1797. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1798. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  1799. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1800. } );
  1801. } );
  1802. describe( 'getItem()', () => {
  1803. it( 'returns item by index', () => {
  1804. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1805. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  1806. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  1807. } );
  1808. it( 'returns undefined if index exceeds the range', () => {
  1809. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1810. expect( ctx.getItem( 3 ) ).to.be.undefined;
  1811. } );
  1812. } );
  1813. describe( 'getNames()', () => {
  1814. it( 'returns an iterator', () => {
  1815. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1816. expect( ctx.getNames().next ).to.be.a( 'function' );
  1817. } );
  1818. it( 'returns an iterator which returns all item names', () => {
  1819. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1820. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1821. } );
  1822. } );
  1823. describe( 'endsWith()', () => {
  1824. it( 'returns true if the end of the context matches the query - 1 item', () => {
  1825. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1826. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  1827. } );
  1828. it( 'returns true if the end of the context matches the query - 2 items', () => {
  1829. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1830. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  1831. } );
  1832. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  1833. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  1834. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  1835. } );
  1836. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  1837. const ctx = new SchemaContext( [ 'foo' ] );
  1838. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  1839. } );
  1840. it( 'returns true if not only the end of the context matches the query', () => {
  1841. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  1842. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  1843. } );
  1844. it( 'returns false if query matches the middle of the context', () => {
  1845. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1846. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  1847. } );
  1848. it( 'returns false if query matches the start of the context', () => {
  1849. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1850. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  1851. } );
  1852. it( 'returns false if query does not match', () => {
  1853. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1854. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  1855. } );
  1856. it( 'returns false if query is longer than context', () => {
  1857. const ctx = new SchemaContext( [ 'foo' ] );
  1858. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  1859. } );
  1860. } );
  1861. } );