schema.js 69 KB

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