8
0

schema.js 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061
  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 { getData, setData, stringify, parse } from '../../src/dev-utils/model';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  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 true if an item was registered as an object element (because all objects are limits too)', () => {
  244. schema.register( 'foo', {
  245. isObject: true
  246. } );
  247. expect( schema.isLimit( 'foo' ) ).to.be.true;
  248. } );
  249. it( 'returns false if an item was not registered as a limit element', () => {
  250. schema.register( 'foo' );
  251. expect( schema.isLimit( 'foo' ) ).to.be.false;
  252. } );
  253. it( 'returns false if an item was not registered at all', () => {
  254. expect( schema.isLimit( 'foo' ) ).to.be.false;
  255. } );
  256. it( 'uses getDefinition()\'s item to definition normalization', () => {
  257. const stub = sinon.stub( schema, 'getDefinition' ).returns( { isLimit: true } );
  258. expect( schema.isLimit( 'foo' ) ).to.be.true;
  259. expect( stub.calledOnce ).to.be.true;
  260. } );
  261. } );
  262. describe( 'isObject()', () => {
  263. it( 'returns true if an item was registered as an object', () => {
  264. schema.register( 'foo', {
  265. isObject: true
  266. } );
  267. expect( schema.isObject( 'foo' ) ).to.be.true;
  268. } );
  269. it( 'returns false if an item was registered as a limit (because not all limits are objects)', () => {
  270. schema.register( 'foo', {
  271. isLimit: true
  272. } );
  273. expect( schema.isObject( 'foo' ) ).to.be.false;
  274. } );
  275. it( 'returns false if an item was not registered as an object', () => {
  276. schema.register( 'foo' );
  277. expect( schema.isObject( 'foo' ) ).to.be.false;
  278. } );
  279. it( 'returns false if an item was not registered at all', () => {
  280. expect( schema.isObject( 'foo' ) ).to.be.false;
  281. } );
  282. it( 'uses getDefinition()\'s item to definition normalization', () => {
  283. const stub = sinon.stub( schema, 'getDefinition' ).returns( { isObject: true } );
  284. expect( schema.isObject( 'foo' ) ).to.be.true;
  285. expect( stub.calledOnce ).to.be.true;
  286. } );
  287. } );
  288. describe( 'checkChild()', () => {
  289. beforeEach( () => {
  290. schema.register( '$root' );
  291. schema.register( 'paragraph', {
  292. allowIn: '$root'
  293. } );
  294. schema.register( '$text', {
  295. allowIn: 'paragraph'
  296. } );
  297. } );
  298. it( 'accepts an element as a context and a node name as a child', () => {
  299. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  300. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  301. } );
  302. it( 'accepts a schemaContext instance as a context', () => {
  303. const rootContext = new SchemaContext( Position.createAt( root1 ) );
  304. const paragraphContext = new SchemaContext( Position.createAt( r1p1 ) );
  305. expect( schema.checkChild( rootContext, 'paragraph' ) ).to.be.true;
  306. expect( schema.checkChild( rootContext, '$text' ) ).to.be.false;
  307. expect( schema.checkChild( paragraphContext, '$text' ) ).to.be.true;
  308. expect( schema.checkChild( paragraphContext, 'paragraph' ) ).to.be.false;
  309. } );
  310. it( 'accepts a position as a context', () => {
  311. const posInRoot = Position.createAt( root1 );
  312. const posInParagraph = Position.createAt( r1p1 );
  313. expect( schema.checkChild( posInRoot, 'paragraph' ) ).to.be.true;
  314. expect( schema.checkChild( posInRoot, '$text' ) ).to.be.false;
  315. expect( schema.checkChild( posInParagraph, '$text' ) ).to.be.true;
  316. expect( schema.checkChild( posInParagraph, 'paragraph' ) ).to.be.false;
  317. } );
  318. // This is a temporary feature which is needed to make the current V->M conversion works.
  319. // It should be removed once V->M conversion uses real positions.
  320. // Of course, real positions have this advantage that we know element attributes at this point.
  321. it( 'accepts an array of element names as a context', () => {
  322. const contextInRoot = [ '$root' ];
  323. const contextInParagraph = [ '$root', 'paragraph' ];
  324. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  325. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  326. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  327. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  328. } );
  329. it( 'accepts an array of elements as a context', () => {
  330. const contextInRoot = [ root1 ];
  331. const contextInParagraph = [ root1, r1p1 ];
  332. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  333. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  334. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  335. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  336. } );
  337. // Again, this is needed temporarily to handle current V->M conversion
  338. it( 'accepts a mixed array of elements and strings as a context', () => {
  339. const contextInParagraph = [ '$root', r1p1 ];
  340. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  341. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  342. } );
  343. it( 'accepts a node as a child', () => {
  344. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  345. expect( schema.checkChild( root1, new Text( 'foo' ) ) ).to.be.false;
  346. } );
  347. it( 'fires the checkChild event with already normalized params', done => {
  348. schema.on( 'checkChild', ( evt, [ ctx, child ] ) => {
  349. expect( ctx ).to.be.instanceof( SchemaContext );
  350. expect( child ).to.equal( schema.getDefinition( 'paragraph' ) );
  351. done();
  352. }, { priority: 'highest' } );
  353. schema.checkChild( root1, r1p1 );
  354. } );
  355. } );
  356. describe( 'checkAttribute()', () => {
  357. beforeEach( () => {
  358. schema.register( 'paragraph', {
  359. allowAttributes: 'align'
  360. } );
  361. schema.register( '$text', {
  362. allowAttributes: 'bold'
  363. } );
  364. } );
  365. it( 'accepts an element as a context', () => {
  366. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  367. expect( schema.checkAttribute( r1p1, 'bold' ) ).to.be.false;
  368. } );
  369. it( 'accepts a text as a context', () => {
  370. expect( schema.checkAttribute( new Text( 'foo' ), 'bold' ) ).to.be.true;
  371. expect( schema.checkAttribute( new Text( 'foo' ), 'align' ) ).to.be.false;
  372. } );
  373. it( 'accepts a position as a context', () => {
  374. const posInRoot = Position.createAt( root1 );
  375. const posInParagraph = Position.createAt( r1p1 );
  376. expect( schema.checkAttribute( posInRoot, 'align' ) ).to.be.false;
  377. expect( schema.checkAttribute( posInParagraph, 'align' ) ).to.be.true;
  378. } );
  379. it( 'accepts a schemaContext instance as a context', () => {
  380. const rootContext = new SchemaContext( Position.createAt( root1 ) );
  381. const paragraphContext = new SchemaContext( Position.createAt( r1p1 ) );
  382. expect( schema.checkAttribute( rootContext, 'align' ) ).to.be.false;
  383. expect( schema.checkAttribute( paragraphContext, 'align' ) ).to.be.true;
  384. } );
  385. it( 'accepts an array of node names as a context', () => {
  386. const contextInRoot = [ '$root' ];
  387. const contextInParagraph = [ '$root', 'paragraph' ];
  388. const contextInText = [ '$root', 'paragraph', '$text' ];
  389. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  390. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  391. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  392. } );
  393. it( 'accepts an array of nodes as a context', () => {
  394. const contextInRoot = [ root1 ];
  395. const contextInParagraph = [ root1, r1p1 ];
  396. const contextInText = [ root1, r1p1, r1p1.getChild( 0 ) ];
  397. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  398. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  399. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  400. } );
  401. it( 'fires the checkAttribute event with already normalized context', done => {
  402. schema.on( 'checkAttribute', ( evt, [ ctx, attributeName ] ) => {
  403. expect( ctx ).to.be.instanceof( SchemaContext );
  404. expect( attributeName ).to.equal( 'bold' );
  405. done();
  406. }, { priority: 'highest' } );
  407. schema.checkAttribute( r1p1, 'bold' );
  408. } );
  409. } );
  410. describe( 'addChildCheck()', () => {
  411. beforeEach( () => {
  412. schema.register( '$root' );
  413. schema.register( 'paragraph', {
  414. allowIn: '$root'
  415. } );
  416. } );
  417. it( 'adds a high-priority listener', () => {
  418. const order = [];
  419. schema.on( 'checkChild', () => {
  420. order.push( 'checkChild:high-before' );
  421. }, { priority: 'high' } );
  422. schema.addChildCheck( () => {
  423. order.push( 'addChildCheck' );
  424. } );
  425. schema.on( 'checkChild', () => {
  426. order.push( 'checkChild:high-after' );
  427. }, { priority: 'high' } );
  428. schema.checkChild( root1, r1p1 );
  429. expect( order.join() ).to.equal( 'checkChild:high-before,addChildCheck,checkChild:high-after' );
  430. } );
  431. it( 'stops the event and overrides the return value when callback returned true', () => {
  432. schema.register( '$text' );
  433. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  434. schema.addChildCheck( () => {
  435. return true;
  436. } );
  437. schema.on( 'checkChild', () => {
  438. throw new Error( 'the event should be stopped' );
  439. }, { priority: 'high' } );
  440. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  441. } );
  442. it( 'stops the event and overrides the return value when callback returned false', () => {
  443. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  444. schema.addChildCheck( () => {
  445. return false;
  446. } );
  447. schema.on( 'checkChild', () => {
  448. throw new Error( 'the event should be stopped' );
  449. }, { priority: 'high' } );
  450. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  451. } );
  452. it( 'receives context and child definition as params', () => {
  453. schema.addChildCheck( ( ctx, childDef ) => {
  454. expect( ctx ).to.be.instanceOf( SchemaContext );
  455. expect( childDef ).to.equal( schema.getDefinition( 'paragraph' ) );
  456. } );
  457. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  458. } );
  459. it( 'is not called when checking a non-registered element', () => {
  460. expect( schema.getDefinition( 'foo' ) ).to.be.undefined;
  461. schema.addChildCheck( () => {
  462. throw new Error( 'callback should not be called' );
  463. } );
  464. expect( schema.checkChild( root1, 'foo' ) ).to.be.false;
  465. } );
  466. } );
  467. describe( 'addAttributeCheck()', () => {
  468. beforeEach( () => {
  469. schema.register( 'paragraph', {
  470. allowAttributes: 'foo'
  471. } );
  472. } );
  473. it( 'adds a high-priority listener', () => {
  474. const order = [];
  475. schema.on( 'checkAttribute', () => {
  476. order.push( 'checkAttribute:high-before' );
  477. }, { priority: 'high' } );
  478. schema.addAttributeCheck( () => {
  479. order.push( 'addAttributeCheck' );
  480. } );
  481. schema.on( 'checkAttribute', () => {
  482. order.push( 'checkAttribute:high-after' );
  483. }, { priority: 'high' } );
  484. schema.checkAttribute( r1p1, 'foo' );
  485. expect( order.join() ).to.equal( 'checkAttribute:high-before,addAttributeCheck,checkAttribute:high-after' );
  486. } );
  487. it( 'stops the event and overrides the return value when callback returned true', () => {
  488. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.false;
  489. schema.addAttributeCheck( () => {
  490. return true;
  491. } );
  492. schema.on( 'checkAttribute', () => {
  493. throw new Error( 'the event should be stopped' );
  494. }, { priority: 'high' } );
  495. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.true;
  496. } );
  497. it( 'stops the event and overrides the return value when callback returned false', () => {
  498. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  499. schema.addAttributeCheck( () => {
  500. return false;
  501. } );
  502. schema.on( 'checkAttribute', () => {
  503. throw new Error( 'the event should be stopped' );
  504. }, { priority: 'high' } );
  505. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.false;
  506. } );
  507. it( 'receives context and attribute name as params', () => {
  508. schema.addAttributeCheck( ( ctx, attributeName ) => {
  509. expect( ctx ).to.be.instanceOf( SchemaContext );
  510. expect( attributeName ).to.equal( 'foo' );
  511. } );
  512. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  513. } );
  514. } );
  515. describe( 'checkMerge()', () => {
  516. beforeEach( () => {
  517. schema.register( '$root' );
  518. schema.register( '$block', {
  519. allowIn: '$root',
  520. isBlock: true
  521. } );
  522. schema.register( '$text', {
  523. allowIn: '$block'
  524. } );
  525. schema.register( 'paragraph', {
  526. inheritAllFrom: '$block'
  527. } );
  528. schema.register( 'listItem', {
  529. inheritAllFrom: '$block'
  530. } );
  531. schema.register( 'blockQuote', {
  532. allowWhere: '$block',
  533. allowContentOf: '$root'
  534. } );
  535. } );
  536. it( 'returns false if a block cannot be merged with other block (disallowed element is the first child)', () => {
  537. const paragraph = new Element( 'paragraph', null, [
  538. new Text( 'xyz' )
  539. ] );
  540. const blockQuote = new Element( 'blockQuote', null, [ paragraph ] );
  541. const listItem = new Element( 'listItem' );
  542. expect( schema.checkMerge( listItem, blockQuote ) ).to.be.false;
  543. } );
  544. it( 'returns false if a block cannot be merged with other block (disallowed element is not the first child)', () => {
  545. const paragraph = new Element( 'paragraph', null, [
  546. new Text( 'foo' )
  547. ] );
  548. const blockQuote = new Element( 'blockQuote', null, [
  549. new Text( 'bar', { bold: true } ),
  550. new Text( 'xyz' ),
  551. paragraph
  552. ] );
  553. const listItem = new Element( 'listItem' );
  554. expect( schema.checkMerge( listItem, blockQuote ) ).to.be.false;
  555. } );
  556. it( 'returns true if a block can be merged with other block', () => {
  557. const listItem = new Element( 'listItem' );
  558. const listItemToMerge = new Element( 'listItem', null, [
  559. new Text( 'xyz' )
  560. ] );
  561. expect( schema.checkMerge( listItem, listItemToMerge ) ).to.be.true;
  562. } );
  563. it( 'return true if two elements between the position can be merged', () => {
  564. const listItem = new Element( 'listItem', null, [
  565. new Text( 'foo' )
  566. ] );
  567. const listItemToMerge = new Element( 'listItem', null, [
  568. new Text( 'bar' )
  569. ] );
  570. // eslint-disable-next-line no-new
  571. new Element( '$root', null, [
  572. listItem, listItemToMerge
  573. ] );
  574. const position = Position.createAfter( listItem );
  575. expect( schema.checkMerge( position ) ).to.be.true;
  576. } );
  577. it( 'throws an error if there is no element before the position', () => {
  578. const listItem = new Element( 'listItem', null, [
  579. new Text( 'foo' )
  580. ] );
  581. // eslint-disable-next-line no-new
  582. new Element( '$root', null, [
  583. listItem
  584. ] );
  585. const position = Position.createBefore( listItem );
  586. expect( () => {
  587. expect( schema.checkMerge( position ) );
  588. } ).to.throw( CKEditorError, /^schema-check-merge-no-element-before:/ );
  589. } );
  590. it( 'throws an error if the node before the position is not the element', () => {
  591. const listItem = new Element( 'listItem', null, [
  592. new Text( 'foo' )
  593. ] );
  594. // eslint-disable-next-line no-new
  595. new Element( '$root', null, [
  596. new Text( 'bar' ),
  597. listItem
  598. ] );
  599. const position = Position.createBefore( listItem );
  600. expect( () => {
  601. expect( schema.checkMerge( position ) );
  602. } ).to.throw( CKEditorError, /^schema-check-merge-no-element-before:/ );
  603. } );
  604. it( 'throws an error if there is no element after the position', () => {
  605. const listItem = new Element( 'listItem', null, [
  606. new Text( 'foo' )
  607. ] );
  608. // eslint-disable-next-line no-new
  609. new Element( '$root', null, [
  610. listItem
  611. ] );
  612. const position = Position.createAfter( listItem );
  613. expect( () => {
  614. expect( schema.checkMerge( position ) );
  615. } ).to.throw( CKEditorError, /^schema-check-merge-no-element-after:/ );
  616. } );
  617. it( 'throws an error if the node after the position is not the element', () => {
  618. const listItem = new Element( 'listItem', null, [
  619. new Text( 'foo' )
  620. ] );
  621. // eslint-disable-next-line no-new
  622. new Element( '$root', null, [
  623. listItem,
  624. new Text( 'bar' )
  625. ] );
  626. const position = Position.createBefore( listItem );
  627. expect( () => {
  628. expect( schema.checkMerge( position ) );
  629. } ).to.throw( CKEditorError, /^schema-check-merge-no-element-before:/ );
  630. } );
  631. // This is an invalid case by definition – the baseElement should not contain disallowed elements
  632. // in the first place. However, the check is focused on the elementToMerge's children so let's make sure
  633. // that only them counts.
  634. it( 'returns true if element to merge contains a valid content but base element contains disallowed elements', () => {
  635. const listItem = new Element( 'listItem', null, [
  636. new Text( 'foo' ),
  637. new Element( 'paragraph', null, [
  638. new Text( 'bar' )
  639. ] )
  640. ] );
  641. const listItemToMerge = new Element( 'listItem', null, [
  642. new Text( 'xyz' )
  643. ] );
  644. expect( schema.checkMerge( listItem, listItemToMerge ) ).to.be.true;
  645. } );
  646. // The checkMerge() method should also check whether all ancestors of elementToMerge are allowed in their new
  647. // context (now, we check only immediate children), but for now we ignore these cases.
  648. } );
  649. describe( 'getLimitElement()', () => {
  650. let model, doc, root;
  651. beforeEach( () => {
  652. model = new Model();
  653. doc = model.document;
  654. schema = model.schema;
  655. root = doc.createRoot();
  656. schema.register( 'div', {
  657. inheritAllFrom: '$block'
  658. } );
  659. schema.register( 'article', {
  660. inheritAllFrom: '$block',
  661. allowIn: 'section'
  662. } );
  663. schema.register( 'section', {
  664. inheritAllFrom: '$block',
  665. allowIn: 'div'
  666. } );
  667. schema.register( 'paragraph', {
  668. inheritAllFrom: '$block',
  669. allowIn: 'article'
  670. } );
  671. schema.register( 'widget', {
  672. inheritAllFrom: '$block',
  673. allowIn: 'div'
  674. } );
  675. schema.register( 'image', {
  676. inheritAllFrom: '$block',
  677. allowIn: 'widget'
  678. } );
  679. schema.register( 'caption', {
  680. inheritAllFrom: '$block',
  681. allowIn: 'image'
  682. } );
  683. } );
  684. it( 'always returns $root element if any other limit was not defined', () => {
  685. schema.extend( '$root', {
  686. isLimit: false
  687. } );
  688. expect( schema.isLimit( '$root' ) ).to.be.false;
  689. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  690. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  691. } );
  692. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  693. schema.extend( 'article', { isLimit: true } );
  694. schema.extend( 'section', { isLimit: true } );
  695. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  696. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  697. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  698. } );
  699. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  700. schema.extend( 'article', { isLimit: true } );
  701. schema.extend( 'section', { isLimit: true } );
  702. model.enqueueChange( 'transparent', () => {
  703. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  704. const section = root.getNodeByPath( [ 0, 0 ] );
  705. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  706. } );
  707. } );
  708. it( 'works fine with multi-range selections', () => {
  709. schema.extend( 'article', { isLimit: true } );
  710. schema.extend( 'widget', { isLimit: true } );
  711. schema.extend( 'div', { isLimit: true } );
  712. setData(
  713. model,
  714. '<div>' +
  715. '<section>' +
  716. '<article>' +
  717. '<paragraph>[foo]</paragraph>' +
  718. '</article>' +
  719. '</section>' +
  720. '<widget>' +
  721. '<image>' +
  722. '<caption>b[a]r</caption>' +
  723. '</image>' +
  724. '</widget>' +
  725. '</div>'
  726. );
  727. const div = root.getNodeByPath( [ 0 ] );
  728. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  729. } );
  730. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  731. setData(
  732. model,
  733. '<div>' +
  734. '<section>' +
  735. '<article>' +
  736. '<paragraph>[foo]</paragraph>' +
  737. '</article>' +
  738. '</section>' +
  739. '</div>' +
  740. '<section>b[]ar</section>'
  741. );
  742. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  743. } );
  744. it( 'works fine with multi-range selections if the first range has the root element as a limit element', () => {
  745. setData(
  746. model,
  747. '<image>' +
  748. '<caption>[Foo</caption>' +
  749. '</image>' +
  750. '<article>' +
  751. '<paragraph>Paragraph in article]</paragraph>' +
  752. '</article>' +
  753. '<paragraph>Paragraph item 1</paragraph>' +
  754. '<paragraph>Paragraph [item 2]</paragraph>'
  755. );
  756. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  757. } );
  758. it( 'works fine with multi-range selections if the last range has the root element as a limit element', () => {
  759. setData(
  760. model,
  761. '<paragraph>Paragraph item 1</paragraph>' +
  762. '<paragraph>Paragraph [item 2]</paragraph>' +
  763. '<image>' +
  764. '<caption>[Foo</caption>' +
  765. '</image>' +
  766. '<article>' +
  767. '<paragraph>Paragraph in article]</paragraph>' +
  768. '</article>'
  769. );
  770. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  771. } );
  772. it( 'accepts range as an argument', () => {
  773. schema.extend( 'article', { isLimit: true } );
  774. schema.extend( 'section', { isLimit: true } );
  775. const data = '<div><section><article><paragraph>foobar</paragraph></article></section></div>';
  776. const parsedModel = parse( data, model.schema, { context: [ root.name ] } );
  777. model.change( writer => {
  778. writer.insert( parsedModel, root );
  779. } );
  780. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  781. expect( schema.getLimitElement( new Range( new Position( root, [ 0, 0, 0, 0, 2 ] ) ) ) ).to.equal( article );
  782. } );
  783. it( 'accepts position as an argument', () => {
  784. schema.extend( 'article', { isLimit: true } );
  785. schema.extend( 'section', { isLimit: true } );
  786. const data = '<div><section><article><paragraph>foobar</paragraph></article></section></div>';
  787. const parsedModel = parse( data, model.schema, { context: [ root.name ] } );
  788. model.change( writer => {
  789. writer.insert( parsedModel, root );
  790. } );
  791. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  792. expect( schema.getLimitElement( new Position( root, [ 0, 0, 0, 0, 2 ] ) ) ).to.equal( article );
  793. } );
  794. } );
  795. describe( 'checkAttributeInSelection()', () => {
  796. const attribute = 'bold';
  797. let model, doc, schema;
  798. beforeEach( () => {
  799. model = new Model();
  800. doc = model.document;
  801. doc.createRoot();
  802. schema = model.schema;
  803. schema.register( 'p', { inheritAllFrom: '$block' } );
  804. schema.register( 'h1', { inheritAllFrom: '$block' } );
  805. schema.register( 'img', { allowWhere: '$text' } );
  806. schema.register( 'figure', {
  807. allowIn: '$root',
  808. allowAttributes: [ 'name', 'title' ]
  809. } );
  810. schema.addAttributeCheck( ( ctx, attributeName ) => {
  811. // Allow 'bold' on p>$text.
  812. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  813. return true;
  814. }
  815. // Allow 'bold' on $root>p.
  816. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  817. return true;
  818. }
  819. } );
  820. schema.addAttributeCheck( ( ctx, attributeName ) => {
  821. // Disallow 'italic' on $text that has attribute 'bold'.
  822. if ( inTextWithBold( ctx ) && attributeName == 'italic' ) {
  823. return false;
  824. }
  825. // Allow 'italic' on p>$text.
  826. if ( ctx.endsWith( 'p $text' ) && attributeName == 'italic' ) {
  827. return true;
  828. }
  829. // Allow 'italic' on $root>p.
  830. if ( ctx.endsWith( '$root p' ) && attributeName == 'italic' ) {
  831. return true;
  832. }
  833. function inTextWithBold( context ) {
  834. return context.endsWith( '$text' ) && context.last.getAttribute( 'bold' );
  835. }
  836. } );
  837. } );
  838. describe( 'when selection is collapsed', () => {
  839. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  840. setData( model, '<p>f[]oo</p>' );
  841. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  842. } );
  843. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  844. setData( model, '<h1>[]</h1>' );
  845. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  846. setData( model, '[]' );
  847. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  848. } );
  849. it( 'should check attributes of the selection (selection at the beginning of the text)', () => {
  850. setData( model, '<p><$text bold="true">[]foo</$text></p>' );
  851. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  852. } );
  853. it( 'should check attributes of the selection (selection inside the text)', () => {
  854. setData( model, '<p><$text bold="true">f[]oo</$text></p>' );
  855. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  856. } );
  857. it( 'should check attributes of the selection (selection at the end of the text)', () => {
  858. setData( model, '<p><$text bold="true">foo[]</$text></p>' );
  859. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  860. } );
  861. it( 'should check attributes of the selection (an attribute sets manually)', () => {
  862. setData( model, '<p>foo[]bar</p>' );
  863. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.true;
  864. model.change( writer => {
  865. writer.setSelectionAttribute( 'bold', true );
  866. } );
  867. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  868. } );
  869. } );
  870. describe( 'when selection is not collapsed', () => {
  871. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  872. // Simple selection on a few characters.
  873. setData( model, '<p>[foo]</p>' );
  874. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  875. // Selection spans over characters but also include nodes that can't have attribute.
  876. setData( model, '<p>fo[o<img />b]ar</p>' );
  877. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  878. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  879. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  880. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  881. // Selection on empty P. P can have the attribute.
  882. setData( model, '[<p></p>]' );
  883. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  884. } );
  885. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  886. // Selection on DIV which can't have bold text.
  887. setData( model, '[<h1></h1>]' );
  888. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  889. // Selection on two images which can't be bold.
  890. setData( model, '<p>foo[<img /><img />]bar</p>' );
  891. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  892. } );
  893. it( 'should return true when checking element with required attribute', () => {
  894. setData( model, '[<figure name="figure"></figure>]' );
  895. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  896. } );
  897. it( 'should return true when checking element when attribute is already present', () => {
  898. setData( model, '[<figure name="figure" title="title"></figure>]' );
  899. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  900. } );
  901. it( 'should check attributes of text', () => {
  902. setData( model, '<p><$text bold="true">f[o]o</$text></p>' );
  903. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  904. } );
  905. } );
  906. } );
  907. describe( 'getValidRanges()', () => {
  908. let model, doc, root, schema;
  909. beforeEach( () => {
  910. model = new Model();
  911. doc = model.document;
  912. schema = model.schema;
  913. root = doc.createRoot();
  914. schema.register( 'p', { inheritAllFrom: '$block' } );
  915. schema.register( 'img', { allowWhere: '$text' } );
  916. // This is a "hack" to allow setting any ranges in `setData` util.
  917. schema.extend( '$text', { allowIn: '$root' } );
  918. } );
  919. function testValidRangesForAttribute( input, attribute, output ) {
  920. setData( model, input );
  921. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  922. const sel = new Selection( validRanges );
  923. expect( stringify( root, sel ) ).to.equal( output );
  924. }
  925. it( 'should return a range with p for an attribute allowed only on p', () => {
  926. schema.extend( 'p', { allowAttributes: 'foo' } );
  927. testValidRangesForAttribute(
  928. '[<p>foo<img></img>bar</p>]',
  929. 'foo',
  930. '[<p>foo<img></img>bar</p>]'
  931. );
  932. } );
  933. it( 'should return ranges on text nodes for an attribute allowed only on text', () => {
  934. schema.extend( '$text', { allowAttributes: 'bold' } );
  935. testValidRangesForAttribute(
  936. '[<p>foo<img></img>bar</p>]',
  937. 'bold',
  938. '<p>[foo]<img></img>[bar]</p>'
  939. );
  940. } );
  941. it( 'should return a range on img for an attribute allowed only on img', () => {
  942. schema.extend( 'img', { allowAttributes: 'src' } );
  943. testValidRangesForAttribute(
  944. '[<p>foo<img></img>bar</p>]',
  945. 'src',
  946. '<p>foo[<img></img>]bar</p>'
  947. );
  948. } );
  949. it( 'should return a range containing all children for an attribute allowed on all children', () => {
  950. schema.extend( '$text', { allowAttributes: 'bold' } );
  951. schema.extend( 'img', { allowAttributes: 'bold' } );
  952. testValidRangesForAttribute(
  953. '[<p>foo<img></img>bar</p>]',
  954. 'bold',
  955. '<p>[foo<img></img>bar]</p>'
  956. );
  957. } );
  958. it( 'should return a range with p and a range on all children for an attribute allowed on p and its children', () => {
  959. schema.extend( 'p', { allowAttributes: 'foo' } );
  960. schema.extend( '$text', { allowAttributes: 'foo' } );
  961. schema.extend( 'img', { allowAttributes: 'foo' } );
  962. setData( model, '[<p>foo<img></img>bar</p>]' );
  963. const validRanges = Array.from( schema.getValidRanges( doc.selection.getRanges(), 'foo' ) );
  964. expect( validRanges.length ).to.equal( 2 );
  965. expect( validRanges[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  966. expect( validRanges[ 0 ].end.path ).to.deep.equal( [ 0, 7 ] );
  967. expect( validRanges[ 1 ].start.path ).to.deep.equal( [ 0 ] );
  968. expect( validRanges[ 1 ].end.path ).to.deep.equal( [ 1 ] );
  969. } );
  970. it( 'should not break a range if children are not allowed to have the attribute', () => {
  971. schema.extend( 'p', { allowAttributes: 'foo' } );
  972. testValidRangesForAttribute(
  973. '[<p>foo</p><p>bar</p>]',
  974. 'foo',
  975. '[<p>foo</p><p>bar</p>]'
  976. );
  977. } );
  978. it( 'should search deeply', () => {
  979. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  980. testValidRangesForAttribute(
  981. '[<p>foo<img>xxx</img>bar</p>]',
  982. 'bold',
  983. '<p>[foo]<img>[xxx]</img>[bar]</p>'
  984. );
  985. } );
  986. it( 'should work with multiple ranges', () => {
  987. schema.extend( '$text', { allowAttributes: 'bold' } );
  988. testValidRangesForAttribute(
  989. '[<p>a</p><p>b</p>]<p>c</p><p>[d]</p>',
  990. 'bold',
  991. '<p>[a]</p><p>[b]</p><p>c</p><p>[d]</p>'
  992. );
  993. } );
  994. it( 'should work with non-flat ranges', () => {
  995. schema.extend( '$text', { allowAttributes: 'bold' } );
  996. testValidRangesForAttribute(
  997. '[<p>a</p><p>b</p><p>c]</p><p>d</p>',
  998. 'bold',
  999. '<p>[a]</p><p>[b]</p><p>[c]</p><p>d</p>'
  1000. );
  1001. } );
  1002. it( 'should not leak beyond the given ranges', () => {
  1003. schema.extend( '$text', { allowAttributes: 'bold' } );
  1004. testValidRangesForAttribute(
  1005. '[<p>foo</p><p>b]a[r</p><p>x]yz</p>',
  1006. 'bold',
  1007. '<p>[foo]</p><p>[b]a[r]</p><p>[x]yz</p>'
  1008. );
  1009. } );
  1010. it( 'should correctly handle a range which ends in a disallowed position', () => {
  1011. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  1012. // Disallow bold on text inside image.
  1013. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1014. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  1015. return false;
  1016. }
  1017. } );
  1018. testValidRangesForAttribute(
  1019. '[<p>foo<img>xx]x</img>bar</p>',
  1020. 'bold',
  1021. '<p>[foo]<img>xxx</img>bar</p>'
  1022. );
  1023. } );
  1024. } );
  1025. describe( 'getNearestSelectionRange()', () => {
  1026. let selection, model, doc;
  1027. beforeEach( () => {
  1028. model = new Model();
  1029. doc = model.document;
  1030. schema = model.schema;
  1031. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  1032. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  1033. model.schema.register( 'widget', {
  1034. allowIn: '$root',
  1035. isObject: true
  1036. } );
  1037. model.schema.register( 'blockWidget', {
  1038. allowIn: '$root',
  1039. allowContentOf: '$block',
  1040. isObject: true
  1041. } );
  1042. doc.createRoot();
  1043. selection = doc.selection;
  1044. } );
  1045. test(
  1046. 'should return collapsed range if text node can be placed at that position - both',
  1047. '<paragraph>[]</paragraph>',
  1048. 'both',
  1049. '<paragraph>[]</paragraph>'
  1050. );
  1051. test(
  1052. 'should return collapsed range if text node can be placed at that position - forward',
  1053. '<paragraph>[]</paragraph>',
  1054. 'forward',
  1055. '<paragraph>[]</paragraph>'
  1056. );
  1057. test(
  1058. 'should return collapsed range if text node can be placed at that position - backward',
  1059. '<paragraph>[]</paragraph>',
  1060. 'backward',
  1061. '<paragraph>[]</paragraph>'
  1062. );
  1063. test( 'should return null in empty document - both', '', 'both', null );
  1064. test( 'should return null in empty document - backward', '', 'backward', null );
  1065. test( 'should return null in empty document - forward', '', 'forward', null );
  1066. test(
  1067. 'should find range before when searching both ways',
  1068. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1069. 'both',
  1070. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1071. );
  1072. test(
  1073. 'should find range before when searching backward',
  1074. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1075. 'backward',
  1076. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1077. );
  1078. test(
  1079. 'should find range after when searching forward',
  1080. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1081. 'forward',
  1082. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1083. );
  1084. test(
  1085. 'should find range after when searching both ways when it is closer',
  1086. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  1087. 'both',
  1088. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  1089. );
  1090. test(
  1091. 'should find range before when searching both ways when it is closer',
  1092. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  1093. 'both',
  1094. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  1095. );
  1096. test(
  1097. 'should return null if there is no valid range',
  1098. '[]<emptyBlock></emptyBlock>',
  1099. 'both',
  1100. null
  1101. );
  1102. test(
  1103. 'should return null if there is no valid range in given direction - backward',
  1104. '[]<paragraph></paragraph>',
  1105. 'backward',
  1106. null
  1107. );
  1108. test(
  1109. 'should return null if there is no valid range in given direction - forward',
  1110. '<paragraph></paragraph>[]',
  1111. 'forward',
  1112. null
  1113. );
  1114. test(
  1115. 'should move forward when placed at root start',
  1116. '[]<paragraph></paragraph><paragraph></paragraph>',
  1117. 'both',
  1118. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1119. );
  1120. test(
  1121. 'should move backward when placed at root end',
  1122. '<paragraph></paragraph><paragraph></paragraph>[]',
  1123. 'both',
  1124. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1125. );
  1126. describe( 'in case of objects which do not allow text inside', () => {
  1127. test(
  1128. 'should select nearest object (o[]o) - both',
  1129. '<widget></widget>[]<widget></widget>',
  1130. 'both',
  1131. '[<widget></widget>]<widget></widget>'
  1132. );
  1133. test(
  1134. 'should select nearest object (o[]o) - forward',
  1135. '<widget></widget>[]<widget></widget>',
  1136. 'forward',
  1137. '<widget></widget>[<widget></widget>]'
  1138. );
  1139. test(
  1140. 'should select nearest object (o[]o) - backward',
  1141. '<widget></widget>[]<widget></widget>',
  1142. 'both',
  1143. '[<widget></widget>]<widget></widget>'
  1144. );
  1145. test(
  1146. 'should select nearest object (p[]o) - forward',
  1147. '<paragraph></paragraph>[]<widget></widget>',
  1148. 'forward',
  1149. '<paragraph></paragraph>[<widget></widget>]'
  1150. );
  1151. test(
  1152. 'should select nearest object (o[]p) - both',
  1153. '<widget></widget>[]<paragraph></paragraph>',
  1154. 'both',
  1155. '[<widget></widget>]<paragraph></paragraph>'
  1156. );
  1157. test(
  1158. 'should select nearest object (o[]p) - backward',
  1159. '<widget></widget>[]<paragraph></paragraph>',
  1160. 'backward',
  1161. '[<widget></widget>]<paragraph></paragraph>'
  1162. );
  1163. test(
  1164. 'should select nearest object ([]o) - both',
  1165. '[]<widget></widget><paragraph></paragraph>',
  1166. 'both',
  1167. '[<widget></widget>]<paragraph></paragraph>'
  1168. );
  1169. test(
  1170. 'should select nearest object ([]o) - forward',
  1171. '[]<widget></widget><paragraph></paragraph>',
  1172. 'forward',
  1173. '[<widget></widget>]<paragraph></paragraph>'
  1174. );
  1175. test(
  1176. 'should select nearest object (o[]) - both',
  1177. '<paragraph></paragraph><widget></widget>[]',
  1178. 'both',
  1179. '<paragraph></paragraph>[<widget></widget>]'
  1180. );
  1181. test(
  1182. 'should select nearest object (o[]) - backward',
  1183. '<paragraph></paragraph><widget></widget>[]',
  1184. 'both',
  1185. '<paragraph></paragraph>[<widget></widget>]'
  1186. );
  1187. } );
  1188. describe( 'in case of objects which allow text inside', () => {
  1189. test(
  1190. 'should select nearest object which allows text (o[]o) - both',
  1191. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  1192. 'both',
  1193. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  1194. );
  1195. test(
  1196. 'should select nearest object (o[]p) - both',
  1197. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  1198. 'both',
  1199. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1200. );
  1201. test(
  1202. 'should select nearest object which allows text ([]o) - both',
  1203. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  1204. 'both',
  1205. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1206. );
  1207. } );
  1208. function test( testName, data, direction, expected ) {
  1209. it( testName, () => {
  1210. let range;
  1211. model.enqueueChange( 'transparent', () => {
  1212. setData( model, data );
  1213. range = schema.getNearestSelectionRange( selection.anchor, direction );
  1214. } );
  1215. if ( expected === null ) {
  1216. expect( range ).to.be.null;
  1217. } else {
  1218. model.change( writer => {
  1219. writer.setSelection( range );
  1220. } );
  1221. expect( getData( model ) ).to.equal( expected );
  1222. }
  1223. } );
  1224. }
  1225. } );
  1226. describe( 'findAllowedParent()', () => {
  1227. beforeEach( () => {
  1228. schema.register( '$root' );
  1229. schema.register( 'blockQuote', {
  1230. allowIn: '$root'
  1231. } );
  1232. schema.register( 'paragraph', {
  1233. allowIn: 'blockQuote'
  1234. } );
  1235. schema.register( '$text', {
  1236. allowIn: 'paragraph'
  1237. } );
  1238. } );
  1239. it( 'should return position ancestor that allows to insert given node to it', () => {
  1240. const node = new Element( 'paragraph' );
  1241. const allowedParent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1242. expect( allowedParent ).to.equal( r1bQ );
  1243. } );
  1244. it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
  1245. const node = new Text( 'text' );
  1246. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1247. expect( parent ).to.equal( r1bQp );
  1248. } );
  1249. it( 'should return null when limit element is reached before allowed parent', () => {
  1250. schema.extend( 'blockQuote', {
  1251. isLimit: true
  1252. } );
  1253. schema.register( 'div', {
  1254. allowIn: '$root'
  1255. } );
  1256. const node = new Element( 'div' );
  1257. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1258. expect( parent ).to.null;
  1259. } );
  1260. it( 'should return null when object element is reached before allowed parent', () => {
  1261. schema.extend( 'blockQuote', {
  1262. isObject: true
  1263. } );
  1264. schema.register( 'div', {
  1265. allowIn: '$root'
  1266. } );
  1267. const node = new Element( 'div' );
  1268. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1269. expect( parent ).to.null;
  1270. } );
  1271. it( 'should return null when there is no allowed ancestor for given position', () => {
  1272. const node = new Element( 'section' );
  1273. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1274. expect( parent ).to.null;
  1275. } );
  1276. } );
  1277. describe( 'removeDisallowedAttributes()', () => {
  1278. let model, doc, root;
  1279. beforeEach( () => {
  1280. model = new Model();
  1281. doc = model.document;
  1282. root = doc.createRoot();
  1283. schema = model.schema;
  1284. schema.register( 'paragraph', {
  1285. inheritAllFrom: '$block'
  1286. } );
  1287. schema.register( 'div', {
  1288. inheritAllFrom: '$block'
  1289. } );
  1290. schema.register( 'image', {
  1291. isObject: true
  1292. } );
  1293. schema.extend( '$block', {
  1294. allowIn: 'div'
  1295. } );
  1296. } );
  1297. it( 'should filter out disallowed attributes from given nodes', () => {
  1298. schema.extend( '$text', { allowAttributes: 'a' } );
  1299. schema.extend( 'image', { allowAttributes: 'b' } );
  1300. const text = new Text( 'foo', { a: 1, b: 1 } );
  1301. const image = new Element( 'image', { a: 1, b: 1 } );
  1302. root._appendChild( [ text, image ] );
  1303. model.change( writer => {
  1304. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1305. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  1306. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  1307. expect( writer.batch.operations ).to.length( 2 );
  1308. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1309. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1310. expect( getData( model, { withoutSelection: true } ) )
  1311. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  1312. } );
  1313. } );
  1314. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  1315. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1316. // Allow 'a' on div>$text.
  1317. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  1318. return true;
  1319. }
  1320. // Allow 'b' on div>paragraph>$text.
  1321. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  1322. return true;
  1323. }
  1324. // Allow 'a' on div>image.
  1325. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  1326. return true;
  1327. }
  1328. // Allow 'b' on div>paragraph>image.
  1329. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  1330. return true;
  1331. }
  1332. } );
  1333. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1334. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1335. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  1336. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  1337. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  1338. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  1339. root._appendChild( [ div ] );
  1340. model.change( writer => {
  1341. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1342. expect( writer.batch.operations ).to.length( 4 );
  1343. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1344. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1345. expect( writer.batch.operations[ 2 ] ).to.instanceof( AttributeOperation );
  1346. expect( writer.batch.operations[ 3 ] ).to.instanceof( AttributeOperation );
  1347. expect( getData( model, { withoutSelection: true } ) )
  1348. .to.equal(
  1349. '<div>' +
  1350. '<paragraph>' +
  1351. '<$text b="1">foo</$text>' +
  1352. '<image b="1"></image>' +
  1353. '</paragraph>' +
  1354. '<$text a="1">bar</$text>' +
  1355. '<image a="1"></image>' +
  1356. '</div>'
  1357. );
  1358. } );
  1359. } );
  1360. } );
  1361. describe( 'definitions compilation', () => {
  1362. describe( 'allowIn cases', () => {
  1363. it( 'passes $root>paragraph', () => {
  1364. schema.register( '$root' );
  1365. schema.register( 'paragraph', {
  1366. allowIn: '$root'
  1367. } );
  1368. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1369. } );
  1370. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1371. schema.register( '$root' );
  1372. schema.register( '$root2' );
  1373. schema.register( 'paragraph', {
  1374. allowIn: [ '$root', '$root2' ]
  1375. } );
  1376. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1377. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1378. } );
  1379. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1380. schema.register( '$root' );
  1381. schema.register( 'paragraph', {
  1382. allowIn: '$root'
  1383. } );
  1384. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1385. } );
  1386. it( 'passes $root>div>div – in case of circular refs', () => {
  1387. schema.register( '$root' );
  1388. schema.register( 'div', {
  1389. allowIn: [ '$root', 'div' ]
  1390. } );
  1391. const div = new Element( 'div' );
  1392. root1._appendChild( div );
  1393. const div2 = new Element( 'div' );
  1394. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1395. } );
  1396. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1397. schema.register( '$root' );
  1398. schema.register( 'div', {
  1399. allowIn: [ '$root', 'div' ]
  1400. } );
  1401. const div = new Element( 'div' );
  1402. root1._appendChild( div );
  1403. expect( schema.checkChild( div, div ) ).to.be.true;
  1404. } );
  1405. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1406. schema.register( '$root' );
  1407. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1408. } );
  1409. it( 'rejects $root>paragraph – registered different item', () => {
  1410. schema.register( '$root' );
  1411. schema.register( 'paragraph' );
  1412. schema.register( 'listItem', {
  1413. allowIn: '$root'
  1414. } );
  1415. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1416. } );
  1417. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1418. schema.register( '$root' );
  1419. schema.register( '$fancyRoot' );
  1420. schema.register( 'paragraph', {
  1421. allowIn: '$fancyRoot'
  1422. } );
  1423. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1424. } );
  1425. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1426. schema.register( '$root' );
  1427. schema.register( 'paragraph', {
  1428. allowIn: '$root'
  1429. } );
  1430. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1431. } );
  1432. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1433. schema.register( '$root' );
  1434. schema.register( 'blockQuote', {
  1435. allowIn: '$root'
  1436. } );
  1437. schema.register( 'paragraph', {
  1438. allowIn: '$root'
  1439. } );
  1440. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1441. } );
  1442. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1443. schema.register( '$root' );
  1444. schema.register( 'paragraph', {
  1445. allowIn: '$root'
  1446. } );
  1447. schema.register( '$text', {
  1448. allowIn: 'paragraph'
  1449. } );
  1450. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1451. } );
  1452. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1453. schema.register( '$root' );
  1454. schema.register( 'blockQuote' );
  1455. schema.register( 'paragraph', {
  1456. allowIn: [ 'blockQuote', '$root' ]
  1457. } );
  1458. schema.register( '$text', {
  1459. allowIn: 'paragraph'
  1460. } );
  1461. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1462. } );
  1463. } );
  1464. describe( 'allowWhere cases', () => {
  1465. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1466. schema.register( '$root' );
  1467. schema.register( '$block', {
  1468. allowIn: '$root'
  1469. } );
  1470. schema.register( 'paragraph', {
  1471. allowWhere: '$block'
  1472. } );
  1473. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1474. } );
  1475. it( 'supports the array syntax', () => {
  1476. schema.register( '$root' );
  1477. schema.register( '$root2' );
  1478. schema.register( '$block', {
  1479. allowIn: '$root'
  1480. } );
  1481. schema.register( '$block2', {
  1482. allowIn: '$root2'
  1483. } );
  1484. schema.register( 'paragraph', {
  1485. allowWhere: [ '$block', '$block2' ]
  1486. } );
  1487. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1488. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1489. } );
  1490. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1491. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1492. schema.register( '$root' );
  1493. schema.register( 'paragraph', {
  1494. allowWhere: '$block'
  1495. } );
  1496. schema.register( '$block', {
  1497. allowIn: '$root'
  1498. } );
  1499. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1500. } );
  1501. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1502. schema.register( '$root' );
  1503. schema.register( '$block', {
  1504. allowIn: '$root'
  1505. } );
  1506. schema.register( '$specialBlock', {
  1507. allowWhere: '$block'
  1508. } );
  1509. schema.register( 'paragraph', {
  1510. allowWhere: '$specialBlock'
  1511. } );
  1512. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1513. } );
  1514. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1515. schema.register( '$root' );
  1516. schema.register( '$block' );
  1517. schema.register( 'paragraph', {
  1518. allowWhere: '$block'
  1519. } );
  1520. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1521. } );
  1522. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1523. schema.register( '$root' );
  1524. schema.register( '$block' );
  1525. schema.register( 'paragraph', {
  1526. allowWhere: '$block'
  1527. } );
  1528. schema.register( '$text', {
  1529. allowIn: 'paragraph'
  1530. } );
  1531. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1532. } );
  1533. } );
  1534. describe( 'allowContentOf cases', () => {
  1535. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1536. schema.register( '$root' );
  1537. schema.register( 'paragraph', {
  1538. allowIn: '$root'
  1539. } );
  1540. schema.register( '$root2', {
  1541. allowContentOf: '$root'
  1542. } );
  1543. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1544. } );
  1545. it( 'supports the array syntax', () => {
  1546. schema.register( '$root' );
  1547. schema.register( '$root2' );
  1548. schema.register( 'paragraph', {
  1549. allowIn: '$root'
  1550. } );
  1551. schema.register( 'heading1', {
  1552. allowIn: '$root2'
  1553. } );
  1554. schema.register( '$root3', {
  1555. allowContentOf: [ '$root', '$root2' ]
  1556. } );
  1557. const root3 = new Element( '$root3' );
  1558. const heading1 = new Element( 'heading1' );
  1559. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1560. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1561. } );
  1562. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1563. schema.register( '$root' );
  1564. schema.register( '$root2', {
  1565. allowContentOf: '$root'
  1566. } );
  1567. schema.register( 'paragraph', {
  1568. allowIn: '$root'
  1569. } );
  1570. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1571. } );
  1572. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1573. schema.register( '$root' );
  1574. schema.register( '$block' );
  1575. schema.register( 'paragraph', {
  1576. allowIn: '$root',
  1577. allowContentOf: '$block'
  1578. } );
  1579. schema.register( '$text', {
  1580. allowIn: '$block'
  1581. } );
  1582. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1583. } );
  1584. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1585. schema.register( '$root' );
  1586. schema.register( 'blockQuote', {
  1587. allowIn: '$root',
  1588. allowContentOf: '$root'
  1589. } );
  1590. schema.register( 'paragraph', {
  1591. allowIn: '$root'
  1592. } );
  1593. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1594. } );
  1595. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1596. schema.register( '$root' );
  1597. schema.register( 'paragraph' );
  1598. schema.register( '$root2', {
  1599. allowContentOf: '$root'
  1600. } );
  1601. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1602. } );
  1603. } );
  1604. describe( 'mix of allowContentOf and allowWhere', () => {
  1605. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1606. schema.register( '$root' );
  1607. schema.register( '$block', {
  1608. allowIn: '$root'
  1609. } );
  1610. schema.register( 'paragraph', {
  1611. allowContentOf: '$block',
  1612. allowWhere: '$block'
  1613. } );
  1614. schema.register( '$text', {
  1615. allowIn: '$block'
  1616. } );
  1617. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1618. } );
  1619. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1620. 'and paragraph inherits allowWhere from $block', () => {
  1621. schema.register( '$root' );
  1622. schema.register( '$root2', {
  1623. allowContentOf: '$root'
  1624. } );
  1625. schema.register( '$block', {
  1626. allowIn: '$root'
  1627. } );
  1628. schema.register( 'paragraph', {
  1629. allowWhere: '$block'
  1630. } );
  1631. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1632. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1633. } );
  1634. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1635. schema.register( 'b' );
  1636. schema.register( 'a', { allowIn: 'b' } );
  1637. schema.register( 'c', { allowContentOf: 'b' } );
  1638. schema.register( 'd', { allowContentOf: 'c' } );
  1639. const d = new Element( 'd' );
  1640. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1641. } );
  1642. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1643. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1644. // as long as the previous iteration found something to compile.
  1645. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1646. // and since we've found something, we'd now try d<-c which would work.
  1647. //
  1648. // We ignore those situations for now as they are very unlikely to happen and would
  1649. // significantly raised the complexity of definition compilation.
  1650. //
  1651. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1652. // schema.register( 'b' );
  1653. // schema.register( 'a', { allowIn: 'b' } );
  1654. // schema.register( 'd', { allowContentOf: 'c' } );
  1655. // schema.register( 'c', { allowContentOf: 'b' } );
  1656. //
  1657. // const d = new Element( 'd' );
  1658. //
  1659. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1660. // } );
  1661. } );
  1662. describe( 'inheritTypesFrom', () => {
  1663. it( 'inherit properties of another item', () => {
  1664. schema.register( '$block', {
  1665. isBlock: true,
  1666. isLimit: true
  1667. } );
  1668. schema.register( 'paragraph', {
  1669. inheritTypesFrom: '$block'
  1670. } );
  1671. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1672. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1673. } );
  1674. it( 'inherit properties of other items – support for arrays', () => {
  1675. schema.register( '$block', {
  1676. isBlock: true
  1677. } );
  1678. schema.register( '$block2', {
  1679. isLimit: true
  1680. } );
  1681. schema.register( 'paragraph', {
  1682. inheritTypesFrom: [ '$block', '$block2' ]
  1683. } );
  1684. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1685. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1686. } );
  1687. it( 'does not override existing props', () => {
  1688. schema.register( '$block', {
  1689. isBlock: true,
  1690. isLimit: true
  1691. } );
  1692. schema.register( 'paragraph', {
  1693. inheritTypesFrom: '$block',
  1694. isLimit: false
  1695. } );
  1696. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1697. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1698. } );
  1699. } );
  1700. describe( 'inheritAllFrom', () => {
  1701. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1702. schema.register( '$root' );
  1703. schema.register( '$block', {
  1704. allowIn: '$root'
  1705. } );
  1706. schema.register( 'paragraph', {
  1707. inheritAllFrom: '$block'
  1708. } );
  1709. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1710. } );
  1711. it( 'paragraph inherit properties of $block', () => {
  1712. schema.register( '$block', {
  1713. isBlock: true
  1714. } );
  1715. schema.register( 'paragraph', {
  1716. inheritAllFrom: '$block'
  1717. } );
  1718. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1719. } );
  1720. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1721. schema.register( '$root' );
  1722. schema.register( '$block', {
  1723. allowIn: '$root'
  1724. } );
  1725. schema.register( '$text', {
  1726. allowIn: '$block'
  1727. } );
  1728. schema.register( 'paragraph', {
  1729. inheritAllFrom: '$block'
  1730. } );
  1731. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1732. } );
  1733. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1734. schema.register( '$root' );
  1735. schema.register( '$blockProto', {
  1736. allowIn: '$root'
  1737. } );
  1738. schema.register( '$block', {
  1739. allowWhere: '$blockProto'
  1740. } );
  1741. schema.register( 'paragraph', {
  1742. inheritAllFrom: '$block'
  1743. } );
  1744. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1745. } );
  1746. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1747. schema.register( '$root' );
  1748. schema.register( '$blockProto' );
  1749. schema.register( '$block', {
  1750. allowContentOf: '$blockProto',
  1751. allowIn: '$root'
  1752. } );
  1753. schema.register( '$text', {
  1754. allowIn: '$blockProto'
  1755. } );
  1756. schema.register( 'paragraph', {
  1757. inheritAllFrom: '$block'
  1758. } );
  1759. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1760. } );
  1761. } );
  1762. // We need to handle cases where some independent features registered definitions which might use
  1763. // optional elements (elements which might not have been registered).
  1764. describe( 'missing structure definitions', () => {
  1765. it( 'does not break when trying to check a child which is not registered', () => {
  1766. schema.register( '$root' );
  1767. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1768. } );
  1769. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1770. const foo404 = new Element( 'foo404' );
  1771. root1._appendChild( foo404 );
  1772. schema.register( '$root' );
  1773. schema.register( '$text', {
  1774. allowIn: '$root'
  1775. } );
  1776. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1777. } );
  1778. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1779. schema.register( '$root' );
  1780. schema.register( '$text', {
  1781. allowIn: 'foo404'
  1782. } );
  1783. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1784. } );
  1785. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1786. schema.register( '$root' );
  1787. schema.register( '$text', {
  1788. allowWhere: 'foo404'
  1789. } );
  1790. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1791. } );
  1792. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1793. schema.register( '$root', {
  1794. allowContentOf: 'foo404'
  1795. } );
  1796. schema.register( '$text', {
  1797. allowIn: '$root'
  1798. } );
  1799. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1800. } );
  1801. it( 'checks whether allowIn uses a registered element', () => {
  1802. schema.register( 'paragraph', {
  1803. allowIn: '$root'
  1804. } );
  1805. // $root isn't registered!
  1806. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1807. } );
  1808. it( 'does not break when inheriting all from an non-registered element', () => {
  1809. schema.register( 'paragraph', {
  1810. inheritAllFrom: '$block'
  1811. } );
  1812. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1813. } );
  1814. } );
  1815. describe( 'allowAttributes', () => {
  1816. it( 'passes paragraph[align]', () => {
  1817. schema.register( 'paragraph', {
  1818. allowAttributes: 'align'
  1819. } );
  1820. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1821. } );
  1822. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1823. schema.register( 'paragraph', {
  1824. allowAttributes: [ 'align', 'dir' ]
  1825. } );
  1826. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1827. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1828. } );
  1829. it( 'passes paragraph>$text[bold]', () => {
  1830. schema.register( 'paragraph' );
  1831. schema.register( '$text', {
  1832. allowIn: 'paragraph',
  1833. allowAttributes: 'bold'
  1834. } );
  1835. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1836. } );
  1837. } );
  1838. describe( 'allowAttributesOf', () => {
  1839. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1840. schema.register( '$block', {
  1841. allowAttributes: 'align'
  1842. } );
  1843. schema.register( 'paragraph', {
  1844. allowAttributesOf: '$block'
  1845. } );
  1846. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1847. } );
  1848. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1849. schema.register( '$block', {
  1850. allowAttributes: 'align'
  1851. } );
  1852. schema.register( '$block2', {
  1853. allowAttributes: 'dir'
  1854. } );
  1855. schema.register( 'paragraph', {
  1856. allowAttributesOf: [ '$block', '$block2' ]
  1857. } );
  1858. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1859. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1860. } );
  1861. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1862. schema.register( '$block', {
  1863. allowAttributes: 'align'
  1864. } );
  1865. schema.register( 'paragraph', {
  1866. allowAttributes: 'dir',
  1867. allowAttributesOf: '$block'
  1868. } );
  1869. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1870. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1871. } );
  1872. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1873. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1874. } );
  1875. describe( 'inheritAllFrom', () => {
  1876. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1877. schema.register( '$block', {
  1878. allowAttributes: 'align'
  1879. } );
  1880. schema.register( 'paragraph', {
  1881. inheritAllFrom: '$block'
  1882. } );
  1883. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1884. } );
  1885. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1886. schema.register( '$blockProto', {
  1887. allowAttributes: 'align'
  1888. } );
  1889. schema.register( '$block', {
  1890. allowAttributesOf: '$blockProto'
  1891. } );
  1892. schema.register( 'paragraph', {
  1893. inheritAllFrom: '$block'
  1894. } );
  1895. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1896. } );
  1897. } );
  1898. describe( 'missing attribute definitions', () => {
  1899. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1900. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1901. } );
  1902. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1903. schema.register( 'paragraph', {
  1904. allowAttributesOf: '$block'
  1905. } );
  1906. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1907. } );
  1908. it( 'does not crash when inheriting all from a non-registered element', () => {
  1909. schema.register( 'paragraph', {
  1910. allowAttributesOf: '$block'
  1911. } );
  1912. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1913. } );
  1914. } );
  1915. describe( 'missing types definitions', () => {
  1916. it( 'does not crash when inheriting types of an non-registered element', () => {
  1917. schema.register( 'paragraph', {
  1918. inheritTypesFrom: '$block'
  1919. } );
  1920. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1921. } );
  1922. } );
  1923. } );
  1924. describe( 'real scenarios', () => {
  1925. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1926. const definitions = [
  1927. () => {
  1928. schema.register( 'paragraph', {
  1929. inheritAllFrom: '$block'
  1930. } );
  1931. },
  1932. () => {
  1933. schema.register( 'heading1', {
  1934. inheritAllFrom: '$block'
  1935. } );
  1936. },
  1937. () => {
  1938. schema.register( 'listItem', {
  1939. inheritAllFrom: '$block',
  1940. allowAttributes: [ 'listIndent', 'listType' ]
  1941. } );
  1942. },
  1943. () => {
  1944. schema.register( 'blockQuote', {
  1945. allowWhere: '$block',
  1946. allowContentOf: '$root'
  1947. } );
  1948. // Disallow blockQuote in blockQuote.
  1949. schema.addChildCheck( ( ctx, childDef ) => {
  1950. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1951. return false;
  1952. }
  1953. } );
  1954. },
  1955. () => {
  1956. schema.register( 'image', {
  1957. allowWhere: '$block',
  1958. allowAttributes: [ 'src', 'alt' ],
  1959. isObject: true,
  1960. isBlock: true
  1961. } );
  1962. },
  1963. () => {
  1964. schema.register( 'caption', {
  1965. allowIn: 'image',
  1966. allowContentOf: '$block',
  1967. isLimit: true
  1968. } );
  1969. },
  1970. () => {
  1971. schema.extend( '$text', {
  1972. allowAttributes: [ 'bold', 'italic' ]
  1973. } );
  1974. // Disallow bold in heading1.
  1975. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1976. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1977. return false;
  1978. }
  1979. } );
  1980. },
  1981. () => {
  1982. schema.extend( '$block', {
  1983. allowAttributes: 'alignment'
  1984. } );
  1985. }
  1986. ];
  1987. beforeEach( () => {
  1988. schema.register( '$root', {
  1989. isLimit: true
  1990. } );
  1991. schema.register( '$block', {
  1992. allowIn: '$root',
  1993. isBlock: true
  1994. } );
  1995. schema.register( '$text', {
  1996. allowIn: '$block'
  1997. } );
  1998. for ( const definition of definitions ) {
  1999. definition();
  2000. }
  2001. // or...
  2002. //
  2003. // Use the below code to shuffle the definitions.
  2004. // Don't look here, Szymon!
  2005. //
  2006. // const definitionsCopy = definitions.slice();
  2007. //
  2008. // while ( definitionsCopy.length ) {
  2009. // const r = Math.floor( Math.random() * definitionsCopy.length );
  2010. // definitionsCopy.splice( r, 1 )[ 0 ]();
  2011. // }
  2012. root1 = new Element( '$root', null, [
  2013. new Element( 'paragraph', null, 'foo' ),
  2014. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  2015. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2016. new Element( 'heading1', null, 'foo' ),
  2017. new Element( 'blockQuote', null, [
  2018. new Element( 'paragraph', null, 'foo' ),
  2019. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2020. new Element( 'image', null, [
  2021. new Element( 'caption', null, 'foo' )
  2022. ] )
  2023. ] ),
  2024. new Element( 'image', null, [
  2025. new Element( 'caption', null, 'foo' )
  2026. ] )
  2027. ] );
  2028. r1p1 = root1.getChild( 0 );
  2029. r1p2 = root1.getChild( 1 );
  2030. r1lI = root1.getChild( 2 );
  2031. r1h = root1.getChild( 3 );
  2032. r1bQ = root1.getChild( 4 );
  2033. r1i = root1.getChild( 5 );
  2034. r1bQp = r1bQ.getChild( 0 );
  2035. r1bQlI = r1bQ.getChild( 1 );
  2036. r1bQi = r1bQ.getChild( 2 );
  2037. } );
  2038. it( 'passes $root>paragraph', () => {
  2039. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  2040. } );
  2041. it( 'passes $root>paragraph>$text', () => {
  2042. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  2043. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  2044. } );
  2045. it( 'passes $root>listItem', () => {
  2046. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  2047. } );
  2048. it( 'passes $root>listItem>$text', () => {
  2049. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  2050. } );
  2051. it( 'passes $root>blockQuote>paragraph', () => {
  2052. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  2053. } );
  2054. it( 'passes $root>blockQuote>paragraph>$text', () => {
  2055. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  2056. } );
  2057. it( 'passes $root>blockQuote>listItem', () => {
  2058. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  2059. } );
  2060. it( 'passes $root>blockQuote>listItem>$text', () => {
  2061. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  2062. } );
  2063. it( 'passes $root>blockQuote>image', () => {
  2064. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  2065. } );
  2066. it( 'passes $root>blockQuote>image>caption', () => {
  2067. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  2068. } );
  2069. it( 'passes $root>blockQuote>image>caption>$text', () => {
  2070. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  2071. } );
  2072. it( 'passes $root>image', () => {
  2073. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  2074. } );
  2075. it( 'passes $root>image>caption', () => {
  2076. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  2077. } );
  2078. it( 'passes $root>image>caption>$text', () => {
  2079. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  2080. } );
  2081. it( 'rejects $root>$root', () => {
  2082. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  2083. } );
  2084. it( 'rejects $root>$text', () => {
  2085. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  2086. } );
  2087. it( 'rejects $root>caption', () => {
  2088. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  2089. } );
  2090. it( 'rejects $root>paragraph>paragraph', () => {
  2091. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  2092. } );
  2093. it( 'rejects $root>paragraph>paragraph>$text', () => {
  2094. // Edge case because p>p should not exist in the first place.
  2095. // But it's good to know that it blocks also this.
  2096. const p = new Element( 'p' );
  2097. r1p1._appendChild( p );
  2098. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  2099. } );
  2100. it( 'rejects $root>paragraph>$block', () => {
  2101. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  2102. } );
  2103. it( 'rejects $root>paragraph>blockQuote', () => {
  2104. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  2105. } );
  2106. it( 'rejects $root>paragraph>image', () => {
  2107. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2108. } );
  2109. it( 'rejects $root>paragraph>caption', () => {
  2110. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  2111. } );
  2112. it( 'rejects $root>blockQuote>blockQuote', () => {
  2113. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  2114. } );
  2115. it( 'rejects $root>blockQuote>caption', () => {
  2116. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2117. } );
  2118. it( 'rejects $root>blockQuote>$text', () => {
  2119. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  2120. } );
  2121. it( 'rejects $root>image>$text', () => {
  2122. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  2123. } );
  2124. it( 'rejects $root>image>paragraph', () => {
  2125. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  2126. } );
  2127. it( 'rejects $root>image>caption>paragraph', () => {
  2128. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  2129. } );
  2130. it( 'rejects $root>image>caption>blockQuote', () => {
  2131. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  2132. } );
  2133. it( 'accepts attribute $root>paragraph[alignment]', () => {
  2134. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  2135. } );
  2136. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  2137. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2138. } );
  2139. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  2140. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  2141. } );
  2142. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  2143. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  2144. } );
  2145. it( 'accepts attribute $root>listItem[alignment]', () => {
  2146. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  2147. } );
  2148. it( 'accepts attribute $root>listItem[indent]', () => {
  2149. expect( schema.checkAttribute( r1lI, 'listIndent' ) ).to.be.true;
  2150. } );
  2151. it( 'accepts attribute $root>listItem[type]', () => {
  2152. expect( schema.checkAttribute( r1lI, 'listType' ) ).to.be.true;
  2153. } );
  2154. it( 'accepts attribute $root>image[src]', () => {
  2155. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  2156. } );
  2157. it( 'accepts attribute $root>image[alt]', () => {
  2158. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  2159. } );
  2160. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  2161. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  2162. } );
  2163. it( 'rejects attribute $root[indent]', () => {
  2164. expect( schema.checkAttribute( root1, 'listIndent' ) ).to.be.false;
  2165. } );
  2166. it( 'rejects attribute $root>paragraph[indent]', () => {
  2167. expect( schema.checkAttribute( r1p1, 'listIndent' ) ).to.be.false;
  2168. } );
  2169. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  2170. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  2171. } );
  2172. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  2173. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  2174. } );
  2175. it( 'rejects attribute $root>blockQuote[indent]', () => {
  2176. expect( schema.checkAttribute( r1bQ, 'listIndent' ) ).to.be.false;
  2177. } );
  2178. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  2179. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  2180. } );
  2181. it( 'rejects attribute $root>image[indent]', () => {
  2182. expect( schema.checkAttribute( r1i, 'listIndent' ) ).to.be.false;
  2183. } );
  2184. it( 'rejects attribute $root>image[alignment]', () => {
  2185. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  2186. } );
  2187. it( '$root is limit', () => {
  2188. expect( schema.isLimit( '$root' ) ).to.be.true;
  2189. expect( schema.isBlock( '$root' ) ).to.be.false;
  2190. expect( schema.isObject( '$root' ) ).to.be.false;
  2191. } );
  2192. it( 'paragraph is block', () => {
  2193. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  2194. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  2195. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  2196. } );
  2197. it( 'heading1 is block', () => {
  2198. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  2199. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  2200. expect( schema.isObject( 'heading1' ) ).to.be.false;
  2201. } );
  2202. it( 'listItem is block', () => {
  2203. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  2204. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  2205. expect( schema.isObject( 'listItem' ) ).to.be.false;
  2206. } );
  2207. it( 'image is block object', () => {
  2208. expect( schema.isLimit( 'image' ) ).to.be.true;
  2209. expect( schema.isBlock( 'image' ) ).to.be.true;
  2210. expect( schema.isObject( 'image' ) ).to.be.true;
  2211. } );
  2212. it( 'caption is limit', () => {
  2213. expect( schema.isLimit( 'caption' ) ).to.be.true;
  2214. expect( schema.isBlock( 'caption' ) ).to.be.false;
  2215. expect( schema.isObject( 'caption' ) ).to.be.false;
  2216. } );
  2217. } );
  2218. } );
  2219. describe( 'SchemaContext', () => {
  2220. let root;
  2221. beforeEach( () => {
  2222. root = new Element( '$root', null, [
  2223. new Element( 'blockQuote', { foo: 1 }, [
  2224. new Element( 'paragraph', { align: 'left' }, [
  2225. new Text( 'foo', { bold: true, italic: true } )
  2226. ] )
  2227. ] )
  2228. ] );
  2229. } );
  2230. describe( 'constructor()', () => {
  2231. it( 'creates context based on an array of strings', () => {
  2232. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2233. expect( ctx.length ).to.equal( 3 );
  2234. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2235. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  2236. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2237. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2238. } );
  2239. it( 'creates context based on an array of elements', () => {
  2240. const blockQuote = root.getChild( 0 );
  2241. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2242. const ctx = new SchemaContext( [ blockQuote, text ] );
  2243. expect( ctx.length ).to.equal( 2 );
  2244. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  2245. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  2246. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2247. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  2248. } );
  2249. it( 'creates context based on a mixed array of strings and elements', () => {
  2250. const blockQuote = root.getChild( 0 );
  2251. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2252. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  2253. expect( ctx.length ).to.equal( 3 );
  2254. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  2255. } );
  2256. it( 'creates context based on a root element', () => {
  2257. const ctx = new SchemaContext( root );
  2258. expect( ctx.length ).to.equal( 1 );
  2259. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  2260. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2261. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2262. } );
  2263. it( 'creates context based on a nested element', () => {
  2264. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  2265. expect( ctx.length ).to.equal( 3 );
  2266. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2267. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2268. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  2269. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  2270. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  2271. } );
  2272. it( 'creates context based on a text node', () => {
  2273. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  2274. expect( ctx.length ).to.equal( 4 );
  2275. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2276. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2277. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2278. } );
  2279. it( 'creates context based on a text proxy', () => {
  2280. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  2281. const textProxy = new TextProxy( text, 0, 1 );
  2282. const ctx = new SchemaContext( textProxy );
  2283. expect( ctx.length ).to.equal( 4 );
  2284. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2285. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2286. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2287. } );
  2288. it( 'creates context based on a position', () => {
  2289. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  2290. const ctx = new SchemaContext( pos );
  2291. expect( ctx.length ).to.equal( 3 );
  2292. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2293. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  2294. } );
  2295. it( 'creates context based on a string', () => {
  2296. const ctx = new SchemaContext( 'paragraph' );
  2297. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2298. } );
  2299. it( 'creates context based on a SchemaContext instance', () => {
  2300. const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2301. const ctx = new SchemaContext( previousCtx );
  2302. expect( ctx ).to.equal( previousCtx );
  2303. } );
  2304. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  2305. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  2306. expect( ctx.length ).to.equal( 1 );
  2307. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2308. } );
  2309. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  2310. const p = new Element( 'paragraph' );
  2311. const docFrag = new DocumentFragment();
  2312. docFrag._appendChild( p );
  2313. const ctx = new SchemaContext( p );
  2314. expect( ctx.length ).to.equal( 1 );
  2315. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2316. } );
  2317. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  2318. const p = new Element( 'paragraph' );
  2319. const docFrag = new DocumentFragment();
  2320. docFrag._appendChild( p );
  2321. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  2322. expect( ctx.length ).to.equal( 1 );
  2323. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2324. } );
  2325. } );
  2326. describe( 'length', () => {
  2327. it( 'gets the number of items', () => {
  2328. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2329. expect( ctx.length ).to.equal( 3 );
  2330. } );
  2331. } );
  2332. describe( 'last', () => {
  2333. it( 'gets the last item', () => {
  2334. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2335. expect( ctx.last ).to.be.an( 'object' );
  2336. expect( ctx.last.name ).to.equal( 'c' );
  2337. } );
  2338. } );
  2339. describe( 'Symbol.iterator', () => {
  2340. it( 'exists', () => {
  2341. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2342. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  2343. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2344. } );
  2345. } );
  2346. describe( 'getItem()', () => {
  2347. it( 'returns item by index', () => {
  2348. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2349. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2350. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2351. } );
  2352. it( 'returns undefined if index exceeds the range', () => {
  2353. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2354. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2355. } );
  2356. } );
  2357. describe( 'push()', () => {
  2358. it( 'creates new SchemaContext instance with new item - #string', () => {
  2359. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2360. const newCtx = ctx.push( 'd' );
  2361. expect( newCtx ).to.instanceof( SchemaContext );
  2362. expect( newCtx ).to.not.equal( ctx );
  2363. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2364. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2365. } );
  2366. it( 'creates new SchemaContext instance with new item - #text', () => {
  2367. const node = new Text( 'd' );
  2368. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2369. const newCtx = ctx.push( node );
  2370. expect( newCtx ).to.instanceof( SchemaContext );
  2371. expect( newCtx ).to.not.equal( ctx );
  2372. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2373. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2374. } );
  2375. it( 'creates new SchemaContext instance with new item - #element', () => {
  2376. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2377. const parent = new Element( 'parent', null, new Element( 'd' ) );
  2378. const newCtx = ctx.push( parent.getChild( 0 ) );
  2379. expect( newCtx ).to.instanceof( SchemaContext );
  2380. expect( newCtx ).to.not.equal( ctx );
  2381. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2382. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2383. } );
  2384. } );
  2385. describe( 'getNames()', () => {
  2386. it( 'returns an iterator', () => {
  2387. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2388. expect( ctx.getNames().next ).to.be.a( 'function' );
  2389. } );
  2390. it( 'returns an iterator which returns all item names', () => {
  2391. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2392. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2393. } );
  2394. } );
  2395. describe( 'endsWith()', () => {
  2396. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2397. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2398. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2399. } );
  2400. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2401. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2402. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2403. } );
  2404. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2405. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2406. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2407. } );
  2408. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2409. const ctx = new SchemaContext( [ 'foo' ] );
  2410. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2411. } );
  2412. it( 'returns true if not only the end of the context matches the query', () => {
  2413. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2414. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2415. } );
  2416. it( 'returns false if query matches the middle of the context', () => {
  2417. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2418. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2419. } );
  2420. it( 'returns false if query matches the start of the context', () => {
  2421. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2422. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2423. } );
  2424. it( 'returns false if query does not match', () => {
  2425. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2426. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2427. } );
  2428. it( 'returns false if query is longer than context', () => {
  2429. const ctx = new SchemaContext( [ 'foo' ] );
  2430. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2431. } );
  2432. } );
  2433. } );