schema.js 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070
  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, 0 ) );
  304. const paragraphContext = new SchemaContext( Position.createAt( r1p1, 0 ) );
  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, 0 );
  312. const posInParagraph = Position.createAt( r1p1, 0 );
  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, 0 );
  375. const posInParagraph = Position.createAt( r1p1, 0 );
  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, 0 ) );
  381. const paragraphContext = new SchemaContext( Position.createAt( r1p1, 0 ) );
  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.extend( '$text', {
  811. allowAttributes: [ 'italic' ]
  812. } );
  813. schema.addAttributeCheck( ( ctx, attributeName ) => {
  814. // Allow 'bold' on p>$text.
  815. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  816. return true;
  817. }
  818. // Allow 'bold' on $root>p.
  819. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  820. return true;
  821. }
  822. // Disallow 'italic' on $text that has 'bold' already.
  823. if ( inTextWithBold( ctx ) && attributeName == 'italic' ) {
  824. return false;
  825. }
  826. function inTextWithBold( context ) {
  827. return context.endsWith( '$text' ) && context.last.getAttribute( 'bold' );
  828. }
  829. } );
  830. } );
  831. describe( 'when selection is collapsed', () => {
  832. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  833. setData( model, '<p>f[]oo</p>' );
  834. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  835. } );
  836. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  837. setData( model, '<h1>[]</h1>' );
  838. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  839. setData( model, '[]' );
  840. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  841. } );
  842. it( 'should check attributes of the selection (selection inside the $text[bold])', () => {
  843. setData( model, '<p><$text bold="true">f[]oo</$text></p>' );
  844. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  845. model.change( writer => {
  846. writer.removeSelectionAttribute( 'bold' );
  847. } );
  848. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.true;
  849. } );
  850. it( 'should check attributes of the selection (attribute set manually on selection)', () => {
  851. setData( model, '<p>foo[]bar</p>' );
  852. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.true;
  853. model.change( writer => {
  854. writer.setSelectionAttribute( 'bold', true );
  855. } );
  856. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  857. } );
  858. it( 'should pass all selection\'s attributes to checkAttribute()', done => {
  859. schema.on( 'checkAttribute', ( evt, args ) => {
  860. const context = args[ 0 ];
  861. const attributeName = args[ 1 ];
  862. expect( attributeName ).to.equal( 'italic' );
  863. expect( Array.from( context.last.getAttributeKeys() ) ).to.deep.equal( [ 'bold', 'underline' ] );
  864. done();
  865. }, { priority: 'highest' } );
  866. setData( model, '<p>foo[]bar</p>' );
  867. model.change( writer => {
  868. writer.setSelectionAttribute( 'bold', true );
  869. writer.setSelectionAttribute( 'underline', true );
  870. } );
  871. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  872. } );
  873. } );
  874. describe( 'when selection is not collapsed', () => {
  875. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  876. // Simple selection on a few characters.
  877. setData( model, '<p>[foo]</p>' );
  878. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  879. // Selection spans over characters but also include nodes that can't have attribute.
  880. setData( model, '<p>fo[o<img />b]ar</p>' );
  881. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  882. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  883. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  884. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  885. // Selection on empty P. P can have the attribute.
  886. setData( model, '[<p></p>]' );
  887. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  888. } );
  889. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  890. // Selection on DIV which can't have bold text.
  891. setData( model, '[<h1></h1>]' );
  892. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  893. // Selection on two images which can't be bold.
  894. setData( model, '<p>foo[<img /><img />]bar</p>' );
  895. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  896. } );
  897. it( 'should return true when checking element with required attribute', () => {
  898. setData( model, '[<figure name="figure"></figure>]' );
  899. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  900. } );
  901. it( 'should return true when checking element when attribute is already present', () => {
  902. setData( model, '[<figure name="figure" title="title"></figure>]' );
  903. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  904. } );
  905. it( 'should check attributes of text', () => {
  906. setData( model, '<p><$text bold="true">f[o]o</$text></p>' );
  907. expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
  908. } );
  909. } );
  910. } );
  911. describe( 'getValidRanges()', () => {
  912. let model, doc, root, schema;
  913. beforeEach( () => {
  914. model = new Model();
  915. doc = model.document;
  916. schema = model.schema;
  917. root = doc.createRoot();
  918. schema.register( 'p', { inheritAllFrom: '$block' } );
  919. schema.register( 'img', { allowWhere: '$text' } );
  920. // This is a "hack" to allow setting any ranges in `setData` util.
  921. schema.extend( '$text', { allowIn: '$root' } );
  922. } );
  923. function testValidRangesForAttribute( input, attribute, output ) {
  924. setData( model, input );
  925. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  926. const sel = new Selection( validRanges );
  927. expect( stringify( root, sel ) ).to.equal( output );
  928. }
  929. it( 'should return a range with p for an attribute allowed only on p', () => {
  930. schema.extend( 'p', { allowAttributes: 'foo' } );
  931. testValidRangesForAttribute(
  932. '[<p>foo<img></img>bar</p>]',
  933. 'foo',
  934. '[<p>foo<img></img>bar</p>]'
  935. );
  936. } );
  937. it( 'should return ranges on text nodes for an attribute allowed only on text', () => {
  938. schema.extend( '$text', { allowAttributes: 'bold' } );
  939. testValidRangesForAttribute(
  940. '[<p>foo<img></img>bar</p>]',
  941. 'bold',
  942. '<p>[foo]<img></img>[bar]</p>'
  943. );
  944. } );
  945. it( 'should return a range on img for an attribute allowed only on img', () => {
  946. schema.extend( 'img', { allowAttributes: 'src' } );
  947. testValidRangesForAttribute(
  948. '[<p>foo<img></img>bar</p>]',
  949. 'src',
  950. '<p>foo[<img></img>]bar</p>'
  951. );
  952. } );
  953. it( 'should return a range containing all children for an attribute allowed on all children', () => {
  954. schema.extend( '$text', { allowAttributes: 'bold' } );
  955. schema.extend( 'img', { allowAttributes: 'bold' } );
  956. testValidRangesForAttribute(
  957. '[<p>foo<img></img>bar</p>]',
  958. 'bold',
  959. '<p>[foo<img></img>bar]</p>'
  960. );
  961. } );
  962. it( 'should return a range with p and a range on all children for an attribute allowed on p and its children', () => {
  963. schema.extend( 'p', { allowAttributes: 'foo' } );
  964. schema.extend( '$text', { allowAttributes: 'foo' } );
  965. schema.extend( 'img', { allowAttributes: 'foo' } );
  966. setData( model, '[<p>foo<img></img>bar</p>]' );
  967. const validRanges = Array.from( schema.getValidRanges( doc.selection.getRanges(), 'foo' ) );
  968. expect( validRanges.length ).to.equal( 2 );
  969. expect( validRanges[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  970. expect( validRanges[ 0 ].end.path ).to.deep.equal( [ 0, 7 ] );
  971. expect( validRanges[ 1 ].start.path ).to.deep.equal( [ 0 ] );
  972. expect( validRanges[ 1 ].end.path ).to.deep.equal( [ 1 ] );
  973. } );
  974. it( 'should not break a range if children are not allowed to have the attribute', () => {
  975. schema.extend( 'p', { allowAttributes: 'foo' } );
  976. testValidRangesForAttribute(
  977. '[<p>foo</p><p>bar</p>]',
  978. 'foo',
  979. '[<p>foo</p><p>bar</p>]'
  980. );
  981. } );
  982. it( 'should search deeply', () => {
  983. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  984. testValidRangesForAttribute(
  985. '[<p>foo<img>xxx</img>bar</p>]',
  986. 'bold',
  987. '<p>[foo]<img>[xxx]</img>[bar]</p>'
  988. );
  989. } );
  990. it( 'should work with multiple ranges', () => {
  991. schema.extend( '$text', { allowAttributes: 'bold' } );
  992. testValidRangesForAttribute(
  993. '[<p>a</p><p>b</p>]<p>c</p><p>[d]</p>',
  994. 'bold',
  995. '<p>[a]</p><p>[b]</p><p>c</p><p>[d]</p>'
  996. );
  997. } );
  998. it( 'should work with non-flat ranges', () => {
  999. schema.extend( '$text', { allowAttributes: 'bold' } );
  1000. testValidRangesForAttribute(
  1001. '[<p>a</p><p>b</p><p>c]</p><p>d</p>',
  1002. 'bold',
  1003. '<p>[a]</p><p>[b]</p><p>[c]</p><p>d</p>'
  1004. );
  1005. } );
  1006. it( 'should not leak beyond the given ranges', () => {
  1007. schema.extend( '$text', { allowAttributes: 'bold' } );
  1008. testValidRangesForAttribute(
  1009. '[<p>foo</p><p>b]a[r</p><p>x]yz</p>',
  1010. 'bold',
  1011. '<p>[foo]</p><p>[b]a[r]</p><p>[x]yz</p>'
  1012. );
  1013. } );
  1014. it( 'should correctly handle a range which ends in a disallowed position', () => {
  1015. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  1016. // Disallow bold on text inside image.
  1017. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1018. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  1019. return false;
  1020. }
  1021. } );
  1022. testValidRangesForAttribute(
  1023. '[<p>foo<img>xx]x</img>bar</p>',
  1024. 'bold',
  1025. '<p>[foo]<img>xxx</img>bar</p>'
  1026. );
  1027. } );
  1028. } );
  1029. describe( 'getNearestSelectionRange()', () => {
  1030. let selection, model, doc;
  1031. beforeEach( () => {
  1032. model = new Model();
  1033. doc = model.document;
  1034. schema = model.schema;
  1035. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  1036. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  1037. model.schema.register( 'widget', {
  1038. allowIn: '$root',
  1039. isObject: true
  1040. } );
  1041. model.schema.register( 'blockWidget', {
  1042. allowIn: '$root',
  1043. allowContentOf: '$block',
  1044. isObject: true
  1045. } );
  1046. doc.createRoot();
  1047. selection = doc.selection;
  1048. } );
  1049. test(
  1050. 'should return collapsed range if text node can be placed at that position - both',
  1051. '<paragraph>[]</paragraph>',
  1052. 'both',
  1053. '<paragraph>[]</paragraph>'
  1054. );
  1055. test(
  1056. 'should return collapsed range if text node can be placed at that position - forward',
  1057. '<paragraph>[]</paragraph>',
  1058. 'forward',
  1059. '<paragraph>[]</paragraph>'
  1060. );
  1061. test(
  1062. 'should return collapsed range if text node can be placed at that position - backward',
  1063. '<paragraph>[]</paragraph>',
  1064. 'backward',
  1065. '<paragraph>[]</paragraph>'
  1066. );
  1067. test( 'should return null in empty document - both', '', 'both', null );
  1068. test( 'should return null in empty document - backward', '', 'backward', null );
  1069. test( 'should return null in empty document - forward', '', 'forward', null );
  1070. test(
  1071. 'should find range before when searching both ways',
  1072. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1073. 'both',
  1074. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1075. );
  1076. test(
  1077. 'should find range before when searching backward',
  1078. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1079. 'backward',
  1080. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1081. );
  1082. test(
  1083. 'should find range after when searching forward',
  1084. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1085. 'forward',
  1086. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1087. );
  1088. test(
  1089. 'should find range after when searching both ways when it is closer',
  1090. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  1091. 'both',
  1092. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  1093. );
  1094. test(
  1095. 'should find range before when searching both ways when it is closer',
  1096. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  1097. 'both',
  1098. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  1099. );
  1100. test(
  1101. 'should return null if there is no valid range',
  1102. '[]<emptyBlock></emptyBlock>',
  1103. 'both',
  1104. null
  1105. );
  1106. test(
  1107. 'should return null if there is no valid range in given direction - backward',
  1108. '[]<paragraph></paragraph>',
  1109. 'backward',
  1110. null
  1111. );
  1112. test(
  1113. 'should return null if there is no valid range in given direction - forward',
  1114. '<paragraph></paragraph>[]',
  1115. 'forward',
  1116. null
  1117. );
  1118. test(
  1119. 'should move forward when placed at root start',
  1120. '[]<paragraph></paragraph><paragraph></paragraph>',
  1121. 'both',
  1122. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1123. );
  1124. test(
  1125. 'should move backward when placed at root end',
  1126. '<paragraph></paragraph><paragraph></paragraph>[]',
  1127. 'both',
  1128. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1129. );
  1130. describe( 'in case of objects which do not allow text inside', () => {
  1131. test(
  1132. 'should select nearest object (o[]o) - both',
  1133. '<widget></widget>[]<widget></widget>',
  1134. 'both',
  1135. '[<widget></widget>]<widget></widget>'
  1136. );
  1137. test(
  1138. 'should select nearest object (o[]o) - forward',
  1139. '<widget></widget>[]<widget></widget>',
  1140. 'forward',
  1141. '<widget></widget>[<widget></widget>]'
  1142. );
  1143. test(
  1144. 'should select nearest object (o[]o) - backward',
  1145. '<widget></widget>[]<widget></widget>',
  1146. 'both',
  1147. '[<widget></widget>]<widget></widget>'
  1148. );
  1149. test(
  1150. 'should select nearest object (p[]o) - forward',
  1151. '<paragraph></paragraph>[]<widget></widget>',
  1152. 'forward',
  1153. '<paragraph></paragraph>[<widget></widget>]'
  1154. );
  1155. test(
  1156. 'should select nearest object (o[]p) - both',
  1157. '<widget></widget>[]<paragraph></paragraph>',
  1158. 'both',
  1159. '[<widget></widget>]<paragraph></paragraph>'
  1160. );
  1161. test(
  1162. 'should select nearest object (o[]p) - backward',
  1163. '<widget></widget>[]<paragraph></paragraph>',
  1164. 'backward',
  1165. '[<widget></widget>]<paragraph></paragraph>'
  1166. );
  1167. test(
  1168. 'should select nearest object ([]o) - both',
  1169. '[]<widget></widget><paragraph></paragraph>',
  1170. 'both',
  1171. '[<widget></widget>]<paragraph></paragraph>'
  1172. );
  1173. test(
  1174. 'should select nearest object ([]o) - forward',
  1175. '[]<widget></widget><paragraph></paragraph>',
  1176. 'forward',
  1177. '[<widget></widget>]<paragraph></paragraph>'
  1178. );
  1179. test(
  1180. 'should select nearest object (o[]) - both',
  1181. '<paragraph></paragraph><widget></widget>[]',
  1182. 'both',
  1183. '<paragraph></paragraph>[<widget></widget>]'
  1184. );
  1185. test(
  1186. 'should select nearest object (o[]) - backward',
  1187. '<paragraph></paragraph><widget></widget>[]',
  1188. 'both',
  1189. '<paragraph></paragraph>[<widget></widget>]'
  1190. );
  1191. } );
  1192. describe( 'in case of objects which allow text inside', () => {
  1193. test(
  1194. 'should select nearest object which allows text (o[]o) - both',
  1195. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  1196. 'both',
  1197. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  1198. );
  1199. test(
  1200. 'should select nearest object (o[]p) - both',
  1201. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  1202. 'both',
  1203. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1204. );
  1205. test(
  1206. 'should select nearest object which allows text ([]o) - both',
  1207. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  1208. 'both',
  1209. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1210. );
  1211. } );
  1212. function test( testName, data, direction, expected ) {
  1213. it( testName, () => {
  1214. let range;
  1215. model.enqueueChange( 'transparent', () => {
  1216. setData( model, data );
  1217. range = schema.getNearestSelectionRange( selection.anchor, direction );
  1218. } );
  1219. if ( expected === null ) {
  1220. expect( range ).to.be.null;
  1221. } else {
  1222. model.change( writer => {
  1223. writer.setSelection( range );
  1224. } );
  1225. expect( getData( model ) ).to.equal( expected );
  1226. }
  1227. } );
  1228. }
  1229. } );
  1230. describe( 'findAllowedParent()', () => {
  1231. beforeEach( () => {
  1232. schema.register( '$root' );
  1233. schema.register( 'blockQuote', {
  1234. allowIn: '$root'
  1235. } );
  1236. schema.register( 'paragraph', {
  1237. allowIn: 'blockQuote'
  1238. } );
  1239. schema.register( '$text', {
  1240. allowIn: 'paragraph'
  1241. } );
  1242. } );
  1243. it( 'should return position ancestor that allows to insert given node to it', () => {
  1244. const node = new Element( 'paragraph' );
  1245. const allowedParent = schema.findAllowedParent( node, Position.createAt( r1bQp, 0 ) );
  1246. expect( allowedParent ).to.equal( r1bQ );
  1247. } );
  1248. it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
  1249. const node = new Text( 'text' );
  1250. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp, 0 ) );
  1251. expect( parent ).to.equal( r1bQp );
  1252. } );
  1253. it( 'should return null when limit element is reached before allowed parent', () => {
  1254. schema.extend( 'blockQuote', {
  1255. isLimit: true
  1256. } );
  1257. schema.register( 'div', {
  1258. allowIn: '$root'
  1259. } );
  1260. const node = new Element( 'div' );
  1261. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp, 0 ) );
  1262. expect( parent ).to.null;
  1263. } );
  1264. it( 'should return null when object element is reached before allowed parent', () => {
  1265. schema.extend( 'blockQuote', {
  1266. isObject: true
  1267. } );
  1268. schema.register( 'div', {
  1269. allowIn: '$root'
  1270. } );
  1271. const node = new Element( 'div' );
  1272. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp, 0 ) );
  1273. expect( parent ).to.null;
  1274. } );
  1275. it( 'should return null when there is no allowed ancestor for given position', () => {
  1276. const node = new Element( 'section' );
  1277. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp, 0 ) );
  1278. expect( parent ).to.null;
  1279. } );
  1280. } );
  1281. describe( 'removeDisallowedAttributes()', () => {
  1282. let model, doc, root;
  1283. beforeEach( () => {
  1284. model = new Model();
  1285. doc = model.document;
  1286. root = doc.createRoot();
  1287. schema = model.schema;
  1288. schema.register( 'paragraph', {
  1289. inheritAllFrom: '$block'
  1290. } );
  1291. schema.register( 'div', {
  1292. inheritAllFrom: '$block'
  1293. } );
  1294. schema.register( 'image', {
  1295. isObject: true
  1296. } );
  1297. schema.extend( '$block', {
  1298. allowIn: 'div'
  1299. } );
  1300. } );
  1301. it( 'should filter out disallowed attributes from given nodes', () => {
  1302. schema.extend( '$text', { allowAttributes: 'a' } );
  1303. schema.extend( 'image', { allowAttributes: 'b' } );
  1304. const text = new Text( 'foo', { a: 1, b: 1 } );
  1305. const image = new Element( 'image', { a: 1, b: 1 } );
  1306. root._appendChild( [ text, image ] );
  1307. model.change( writer => {
  1308. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1309. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  1310. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  1311. expect( writer.batch.operations ).to.length( 2 );
  1312. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1313. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1314. expect( getData( model, { withoutSelection: true } ) )
  1315. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  1316. } );
  1317. } );
  1318. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  1319. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1320. // Allow 'a' on div>$text.
  1321. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  1322. return true;
  1323. }
  1324. // Allow 'b' on div>paragraph>$text.
  1325. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  1326. return true;
  1327. }
  1328. // Allow 'a' on div>image.
  1329. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  1330. return true;
  1331. }
  1332. // Allow 'b' on div>paragraph>image.
  1333. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  1334. return true;
  1335. }
  1336. } );
  1337. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1338. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1339. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  1340. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  1341. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  1342. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  1343. root._appendChild( [ div ] );
  1344. model.change( writer => {
  1345. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1346. expect( writer.batch.operations ).to.length( 4 );
  1347. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1348. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1349. expect( writer.batch.operations[ 2 ] ).to.instanceof( AttributeOperation );
  1350. expect( writer.batch.operations[ 3 ] ).to.instanceof( AttributeOperation );
  1351. expect( getData( model, { withoutSelection: true } ) )
  1352. .to.equal(
  1353. '<div>' +
  1354. '<paragraph>' +
  1355. '<$text b="1">foo</$text>' +
  1356. '<image b="1"></image>' +
  1357. '</paragraph>' +
  1358. '<$text a="1">bar</$text>' +
  1359. '<image a="1"></image>' +
  1360. '</div>'
  1361. );
  1362. } );
  1363. } );
  1364. } );
  1365. describe( 'definitions compilation', () => {
  1366. describe( 'allowIn cases', () => {
  1367. it( 'passes $root>paragraph', () => {
  1368. schema.register( '$root' );
  1369. schema.register( 'paragraph', {
  1370. allowIn: '$root'
  1371. } );
  1372. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1373. } );
  1374. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1375. schema.register( '$root' );
  1376. schema.register( '$root2' );
  1377. schema.register( 'paragraph', {
  1378. allowIn: [ '$root', '$root2' ]
  1379. } );
  1380. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1381. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1382. } );
  1383. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1384. schema.register( '$root' );
  1385. schema.register( 'paragraph', {
  1386. allowIn: '$root'
  1387. } );
  1388. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1389. } );
  1390. it( 'passes $root>div>div – in case of circular refs', () => {
  1391. schema.register( '$root' );
  1392. schema.register( 'div', {
  1393. allowIn: [ '$root', 'div' ]
  1394. } );
  1395. const div = new Element( 'div' );
  1396. root1._appendChild( div );
  1397. const div2 = new Element( 'div' );
  1398. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1399. } );
  1400. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1401. schema.register( '$root' );
  1402. schema.register( 'div', {
  1403. allowIn: [ '$root', 'div' ]
  1404. } );
  1405. const div = new Element( 'div' );
  1406. root1._appendChild( div );
  1407. expect( schema.checkChild( div, div ) ).to.be.true;
  1408. } );
  1409. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1410. schema.register( '$root' );
  1411. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1412. } );
  1413. it( 'rejects $root>paragraph – registered different item', () => {
  1414. schema.register( '$root' );
  1415. schema.register( 'paragraph' );
  1416. schema.register( 'listItem', {
  1417. allowIn: '$root'
  1418. } );
  1419. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1420. } );
  1421. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1422. schema.register( '$root' );
  1423. schema.register( '$fancyRoot' );
  1424. schema.register( 'paragraph', {
  1425. allowIn: '$fancyRoot'
  1426. } );
  1427. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1428. } );
  1429. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1430. schema.register( '$root' );
  1431. schema.register( 'paragraph', {
  1432. allowIn: '$root'
  1433. } );
  1434. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1435. } );
  1436. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1437. schema.register( '$root' );
  1438. schema.register( 'blockQuote', {
  1439. allowIn: '$root'
  1440. } );
  1441. schema.register( 'paragraph', {
  1442. allowIn: '$root'
  1443. } );
  1444. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1445. } );
  1446. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1447. schema.register( '$root' );
  1448. schema.register( 'paragraph', {
  1449. allowIn: '$root'
  1450. } );
  1451. schema.register( '$text', {
  1452. allowIn: 'paragraph'
  1453. } );
  1454. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1455. } );
  1456. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1457. schema.register( '$root' );
  1458. schema.register( 'blockQuote' );
  1459. schema.register( 'paragraph', {
  1460. allowIn: [ 'blockQuote', '$root' ]
  1461. } );
  1462. schema.register( '$text', {
  1463. allowIn: 'paragraph'
  1464. } );
  1465. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1466. } );
  1467. } );
  1468. describe( 'allowWhere cases', () => {
  1469. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1470. schema.register( '$root' );
  1471. schema.register( '$block', {
  1472. allowIn: '$root'
  1473. } );
  1474. schema.register( 'paragraph', {
  1475. allowWhere: '$block'
  1476. } );
  1477. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1478. } );
  1479. it( 'supports the array syntax', () => {
  1480. schema.register( '$root' );
  1481. schema.register( '$root2' );
  1482. schema.register( '$block', {
  1483. allowIn: '$root'
  1484. } );
  1485. schema.register( '$block2', {
  1486. allowIn: '$root2'
  1487. } );
  1488. schema.register( 'paragraph', {
  1489. allowWhere: [ '$block', '$block2' ]
  1490. } );
  1491. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1492. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1493. } );
  1494. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1495. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1496. schema.register( '$root' );
  1497. schema.register( 'paragraph', {
  1498. allowWhere: '$block'
  1499. } );
  1500. schema.register( '$block', {
  1501. allowIn: '$root'
  1502. } );
  1503. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1504. } );
  1505. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1506. schema.register( '$root' );
  1507. schema.register( '$block', {
  1508. allowIn: '$root'
  1509. } );
  1510. schema.register( '$specialBlock', {
  1511. allowWhere: '$block'
  1512. } );
  1513. schema.register( 'paragraph', {
  1514. allowWhere: '$specialBlock'
  1515. } );
  1516. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1517. } );
  1518. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1519. schema.register( '$root' );
  1520. schema.register( '$block' );
  1521. schema.register( 'paragraph', {
  1522. allowWhere: '$block'
  1523. } );
  1524. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1525. } );
  1526. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1527. schema.register( '$root' );
  1528. schema.register( '$block' );
  1529. schema.register( 'paragraph', {
  1530. allowWhere: '$block'
  1531. } );
  1532. schema.register( '$text', {
  1533. allowIn: 'paragraph'
  1534. } );
  1535. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1536. } );
  1537. } );
  1538. describe( 'allowContentOf cases', () => {
  1539. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1540. schema.register( '$root' );
  1541. schema.register( 'paragraph', {
  1542. allowIn: '$root'
  1543. } );
  1544. schema.register( '$root2', {
  1545. allowContentOf: '$root'
  1546. } );
  1547. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1548. } );
  1549. it( 'supports the array syntax', () => {
  1550. schema.register( '$root' );
  1551. schema.register( '$root2' );
  1552. schema.register( 'paragraph', {
  1553. allowIn: '$root'
  1554. } );
  1555. schema.register( 'heading1', {
  1556. allowIn: '$root2'
  1557. } );
  1558. schema.register( '$root3', {
  1559. allowContentOf: [ '$root', '$root2' ]
  1560. } );
  1561. const root3 = new Element( '$root3' );
  1562. const heading1 = new Element( 'heading1' );
  1563. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1564. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1565. } );
  1566. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1567. schema.register( '$root' );
  1568. schema.register( '$root2', {
  1569. allowContentOf: '$root'
  1570. } );
  1571. schema.register( 'paragraph', {
  1572. allowIn: '$root'
  1573. } );
  1574. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1575. } );
  1576. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1577. schema.register( '$root' );
  1578. schema.register( '$block' );
  1579. schema.register( 'paragraph', {
  1580. allowIn: '$root',
  1581. allowContentOf: '$block'
  1582. } );
  1583. schema.register( '$text', {
  1584. allowIn: '$block'
  1585. } );
  1586. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1587. } );
  1588. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1589. schema.register( '$root' );
  1590. schema.register( 'blockQuote', {
  1591. allowIn: '$root',
  1592. allowContentOf: '$root'
  1593. } );
  1594. schema.register( 'paragraph', {
  1595. allowIn: '$root'
  1596. } );
  1597. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1598. } );
  1599. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1600. schema.register( '$root' );
  1601. schema.register( 'paragraph' );
  1602. schema.register( '$root2', {
  1603. allowContentOf: '$root'
  1604. } );
  1605. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1606. } );
  1607. } );
  1608. describe( 'mix of allowContentOf and allowWhere', () => {
  1609. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1610. schema.register( '$root' );
  1611. schema.register( '$block', {
  1612. allowIn: '$root'
  1613. } );
  1614. schema.register( 'paragraph', {
  1615. allowContentOf: '$block',
  1616. allowWhere: '$block'
  1617. } );
  1618. schema.register( '$text', {
  1619. allowIn: '$block'
  1620. } );
  1621. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1622. } );
  1623. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1624. 'and paragraph inherits allowWhere from $block', () => {
  1625. schema.register( '$root' );
  1626. schema.register( '$root2', {
  1627. allowContentOf: '$root'
  1628. } );
  1629. schema.register( '$block', {
  1630. allowIn: '$root'
  1631. } );
  1632. schema.register( 'paragraph', {
  1633. allowWhere: '$block'
  1634. } );
  1635. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1636. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1637. } );
  1638. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1639. schema.register( 'b' );
  1640. schema.register( 'a', { allowIn: 'b' } );
  1641. schema.register( 'c', { allowContentOf: 'b' } );
  1642. schema.register( 'd', { allowContentOf: 'c' } );
  1643. const d = new Element( 'd' );
  1644. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1645. } );
  1646. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1647. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1648. // as long as the previous iteration found something to compile.
  1649. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1650. // and since we've found something, we'd now try d<-c which would work.
  1651. //
  1652. // We ignore those situations for now as they are very unlikely to happen and would
  1653. // significantly raised the complexity of definition compilation.
  1654. //
  1655. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1656. // schema.register( 'b' );
  1657. // schema.register( 'a', { allowIn: 'b' } );
  1658. // schema.register( 'd', { allowContentOf: 'c' } );
  1659. // schema.register( 'c', { allowContentOf: 'b' } );
  1660. //
  1661. // const d = new Element( 'd' );
  1662. //
  1663. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1664. // } );
  1665. } );
  1666. describe( 'inheritTypesFrom', () => {
  1667. it( 'inherit properties of another item', () => {
  1668. schema.register( '$block', {
  1669. isBlock: true,
  1670. isLimit: true
  1671. } );
  1672. schema.register( 'paragraph', {
  1673. inheritTypesFrom: '$block'
  1674. } );
  1675. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1676. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1677. } );
  1678. it( 'inherit properties of other items – support for arrays', () => {
  1679. schema.register( '$block', {
  1680. isBlock: true
  1681. } );
  1682. schema.register( '$block2', {
  1683. isLimit: true
  1684. } );
  1685. schema.register( 'paragraph', {
  1686. inheritTypesFrom: [ '$block', '$block2' ]
  1687. } );
  1688. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1689. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1690. } );
  1691. it( 'does not override existing props', () => {
  1692. schema.register( '$block', {
  1693. isBlock: true,
  1694. isLimit: true
  1695. } );
  1696. schema.register( 'paragraph', {
  1697. inheritTypesFrom: '$block',
  1698. isLimit: false
  1699. } );
  1700. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1701. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1702. } );
  1703. } );
  1704. describe( 'inheritAllFrom', () => {
  1705. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1706. schema.register( '$root' );
  1707. schema.register( '$block', {
  1708. allowIn: '$root'
  1709. } );
  1710. schema.register( 'paragraph', {
  1711. inheritAllFrom: '$block'
  1712. } );
  1713. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1714. } );
  1715. it( 'paragraph inherit properties of $block', () => {
  1716. schema.register( '$block', {
  1717. isBlock: true
  1718. } );
  1719. schema.register( 'paragraph', {
  1720. inheritAllFrom: '$block'
  1721. } );
  1722. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1723. } );
  1724. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1725. schema.register( '$root' );
  1726. schema.register( '$block', {
  1727. allowIn: '$root'
  1728. } );
  1729. schema.register( '$text', {
  1730. allowIn: '$block'
  1731. } );
  1732. schema.register( 'paragraph', {
  1733. inheritAllFrom: '$block'
  1734. } );
  1735. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1736. } );
  1737. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1738. schema.register( '$root' );
  1739. schema.register( '$blockProto', {
  1740. allowIn: '$root'
  1741. } );
  1742. schema.register( '$block', {
  1743. allowWhere: '$blockProto'
  1744. } );
  1745. schema.register( 'paragraph', {
  1746. inheritAllFrom: '$block'
  1747. } );
  1748. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1749. } );
  1750. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1751. schema.register( '$root' );
  1752. schema.register( '$blockProto' );
  1753. schema.register( '$block', {
  1754. allowContentOf: '$blockProto',
  1755. allowIn: '$root'
  1756. } );
  1757. schema.register( '$text', {
  1758. allowIn: '$blockProto'
  1759. } );
  1760. schema.register( 'paragraph', {
  1761. inheritAllFrom: '$block'
  1762. } );
  1763. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1764. } );
  1765. } );
  1766. // We need to handle cases where some independent features registered definitions which might use
  1767. // optional elements (elements which might not have been registered).
  1768. describe( 'missing structure definitions', () => {
  1769. it( 'does not break when trying to check a child which is not registered', () => {
  1770. schema.register( '$root' );
  1771. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1772. } );
  1773. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1774. const foo404 = new Element( 'foo404' );
  1775. root1._appendChild( foo404 );
  1776. schema.register( '$root' );
  1777. schema.register( '$text', {
  1778. allowIn: '$root'
  1779. } );
  1780. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1781. } );
  1782. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1783. schema.register( '$root' );
  1784. schema.register( '$text', {
  1785. allowIn: 'foo404'
  1786. } );
  1787. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1788. } );
  1789. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1790. schema.register( '$root' );
  1791. schema.register( '$text', {
  1792. allowWhere: 'foo404'
  1793. } );
  1794. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1795. } );
  1796. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1797. schema.register( '$root', {
  1798. allowContentOf: 'foo404'
  1799. } );
  1800. schema.register( '$text', {
  1801. allowIn: '$root'
  1802. } );
  1803. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1804. } );
  1805. it( 'checks whether allowIn uses a registered element', () => {
  1806. schema.register( 'paragraph', {
  1807. allowIn: '$root'
  1808. } );
  1809. // $root isn't registered!
  1810. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1811. } );
  1812. it( 'does not break when inheriting all from an non-registered element', () => {
  1813. schema.register( 'paragraph', {
  1814. inheritAllFrom: '$block'
  1815. } );
  1816. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1817. } );
  1818. } );
  1819. describe( 'allowAttributes', () => {
  1820. it( 'passes paragraph[align]', () => {
  1821. schema.register( 'paragraph', {
  1822. allowAttributes: 'align'
  1823. } );
  1824. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1825. } );
  1826. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1827. schema.register( 'paragraph', {
  1828. allowAttributes: [ 'align', 'dir' ]
  1829. } );
  1830. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1831. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1832. } );
  1833. it( 'passes paragraph>$text[bold]', () => {
  1834. schema.register( 'paragraph' );
  1835. schema.register( '$text', {
  1836. allowIn: 'paragraph',
  1837. allowAttributes: 'bold'
  1838. } );
  1839. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1840. } );
  1841. } );
  1842. describe( 'allowAttributesOf', () => {
  1843. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1844. schema.register( '$block', {
  1845. allowAttributes: 'align'
  1846. } );
  1847. schema.register( 'paragraph', {
  1848. allowAttributesOf: '$block'
  1849. } );
  1850. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1851. } );
  1852. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1853. schema.register( '$block', {
  1854. allowAttributes: 'align'
  1855. } );
  1856. schema.register( '$block2', {
  1857. allowAttributes: 'dir'
  1858. } );
  1859. schema.register( 'paragraph', {
  1860. allowAttributesOf: [ '$block', '$block2' ]
  1861. } );
  1862. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1863. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1864. } );
  1865. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1866. schema.register( '$block', {
  1867. allowAttributes: 'align'
  1868. } );
  1869. schema.register( 'paragraph', {
  1870. allowAttributes: 'dir',
  1871. allowAttributesOf: '$block'
  1872. } );
  1873. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1874. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1875. } );
  1876. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1877. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1878. } );
  1879. describe( 'inheritAllFrom', () => {
  1880. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1881. schema.register( '$block', {
  1882. allowAttributes: 'align'
  1883. } );
  1884. schema.register( 'paragraph', {
  1885. inheritAllFrom: '$block'
  1886. } );
  1887. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1888. } );
  1889. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1890. schema.register( '$blockProto', {
  1891. allowAttributes: 'align'
  1892. } );
  1893. schema.register( '$block', {
  1894. allowAttributesOf: '$blockProto'
  1895. } );
  1896. schema.register( 'paragraph', {
  1897. inheritAllFrom: '$block'
  1898. } );
  1899. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1900. } );
  1901. } );
  1902. describe( 'missing attribute definitions', () => {
  1903. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1904. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1905. } );
  1906. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1907. schema.register( 'paragraph', {
  1908. allowAttributesOf: '$block'
  1909. } );
  1910. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1911. } );
  1912. it( 'does not crash when inheriting all from a non-registered element', () => {
  1913. schema.register( 'paragraph', {
  1914. allowAttributesOf: '$block'
  1915. } );
  1916. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1917. } );
  1918. } );
  1919. describe( 'missing types definitions', () => {
  1920. it( 'does not crash when inheriting types of an non-registered element', () => {
  1921. schema.register( 'paragraph', {
  1922. inheritTypesFrom: '$block'
  1923. } );
  1924. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1925. } );
  1926. } );
  1927. } );
  1928. describe( 'real scenarios', () => {
  1929. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1930. const definitions = [
  1931. () => {
  1932. schema.register( 'paragraph', {
  1933. inheritAllFrom: '$block'
  1934. } );
  1935. },
  1936. () => {
  1937. schema.register( 'heading1', {
  1938. inheritAllFrom: '$block'
  1939. } );
  1940. },
  1941. () => {
  1942. schema.register( 'listItem', {
  1943. inheritAllFrom: '$block',
  1944. allowAttributes: [ 'listIndent', 'listType' ]
  1945. } );
  1946. },
  1947. () => {
  1948. schema.register( 'blockQuote', {
  1949. allowWhere: '$block',
  1950. allowContentOf: '$root'
  1951. } );
  1952. // Disallow blockQuote in blockQuote.
  1953. schema.addChildCheck( ( ctx, childDef ) => {
  1954. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1955. return false;
  1956. }
  1957. } );
  1958. },
  1959. () => {
  1960. schema.register( 'image', {
  1961. allowWhere: '$block',
  1962. allowAttributes: [ 'src', 'alt' ],
  1963. isObject: true,
  1964. isBlock: true
  1965. } );
  1966. },
  1967. () => {
  1968. schema.register( 'caption', {
  1969. allowIn: 'image',
  1970. allowContentOf: '$block',
  1971. isLimit: true
  1972. } );
  1973. },
  1974. () => {
  1975. schema.extend( '$text', {
  1976. allowAttributes: [ 'bold', 'italic' ]
  1977. } );
  1978. // Disallow bold in heading1.
  1979. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1980. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1981. return false;
  1982. }
  1983. } );
  1984. },
  1985. () => {
  1986. schema.extend( '$block', {
  1987. allowAttributes: 'alignment'
  1988. } );
  1989. }
  1990. ];
  1991. beforeEach( () => {
  1992. schema.register( '$root', {
  1993. isLimit: true
  1994. } );
  1995. schema.register( '$block', {
  1996. allowIn: '$root',
  1997. isBlock: true
  1998. } );
  1999. schema.register( '$text', {
  2000. allowIn: '$block'
  2001. } );
  2002. for ( const definition of definitions ) {
  2003. definition();
  2004. }
  2005. // or...
  2006. //
  2007. // Use the below code to shuffle the definitions.
  2008. // Don't look here, Szymon!
  2009. //
  2010. // const definitionsCopy = definitions.slice();
  2011. //
  2012. // while ( definitionsCopy.length ) {
  2013. // const r = Math.floor( Math.random() * definitionsCopy.length );
  2014. // definitionsCopy.splice( r, 1 )[ 0 ]();
  2015. // }
  2016. root1 = new Element( '$root', null, [
  2017. new Element( 'paragraph', null, 'foo' ),
  2018. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  2019. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2020. new Element( 'heading1', null, 'foo' ),
  2021. new Element( 'blockQuote', null, [
  2022. new Element( 'paragraph', null, 'foo' ),
  2023. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2024. new Element( 'image', null, [
  2025. new Element( 'caption', null, 'foo' )
  2026. ] )
  2027. ] ),
  2028. new Element( 'image', null, [
  2029. new Element( 'caption', null, 'foo' )
  2030. ] )
  2031. ] );
  2032. r1p1 = root1.getChild( 0 );
  2033. r1p2 = root1.getChild( 1 );
  2034. r1lI = root1.getChild( 2 );
  2035. r1h = root1.getChild( 3 );
  2036. r1bQ = root1.getChild( 4 );
  2037. r1i = root1.getChild( 5 );
  2038. r1bQp = r1bQ.getChild( 0 );
  2039. r1bQlI = r1bQ.getChild( 1 );
  2040. r1bQi = r1bQ.getChild( 2 );
  2041. } );
  2042. it( 'passes $root>paragraph', () => {
  2043. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  2044. } );
  2045. it( 'passes $root>paragraph>$text', () => {
  2046. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  2047. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  2048. } );
  2049. it( 'passes $root>listItem', () => {
  2050. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  2051. } );
  2052. it( 'passes $root>listItem>$text', () => {
  2053. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  2054. } );
  2055. it( 'passes $root>blockQuote>paragraph', () => {
  2056. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  2057. } );
  2058. it( 'passes $root>blockQuote>paragraph>$text', () => {
  2059. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  2060. } );
  2061. it( 'passes $root>blockQuote>listItem', () => {
  2062. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  2063. } );
  2064. it( 'passes $root>blockQuote>listItem>$text', () => {
  2065. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  2066. } );
  2067. it( 'passes $root>blockQuote>image', () => {
  2068. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  2069. } );
  2070. it( 'passes $root>blockQuote>image>caption', () => {
  2071. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  2072. } );
  2073. it( 'passes $root>blockQuote>image>caption>$text', () => {
  2074. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  2075. } );
  2076. it( 'passes $root>image', () => {
  2077. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  2078. } );
  2079. it( 'passes $root>image>caption', () => {
  2080. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  2081. } );
  2082. it( 'passes $root>image>caption>$text', () => {
  2083. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  2084. } );
  2085. it( 'rejects $root>$root', () => {
  2086. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  2087. } );
  2088. it( 'rejects $root>$text', () => {
  2089. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  2090. } );
  2091. it( 'rejects $root>caption', () => {
  2092. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  2093. } );
  2094. it( 'rejects $root>paragraph>paragraph', () => {
  2095. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  2096. } );
  2097. it( 'rejects $root>paragraph>paragraph>$text', () => {
  2098. // Edge case because p>p should not exist in the first place.
  2099. // But it's good to know that it blocks also this.
  2100. const p = new Element( 'p' );
  2101. r1p1._appendChild( p );
  2102. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  2103. } );
  2104. it( 'rejects $root>paragraph>$block', () => {
  2105. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  2106. } );
  2107. it( 'rejects $root>paragraph>blockQuote', () => {
  2108. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  2109. } );
  2110. it( 'rejects $root>paragraph>image', () => {
  2111. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2112. } );
  2113. it( 'rejects $root>paragraph>caption', () => {
  2114. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  2115. } );
  2116. it( 'rejects $root>blockQuote>blockQuote', () => {
  2117. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  2118. } );
  2119. it( 'rejects $root>blockQuote>caption', () => {
  2120. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2121. } );
  2122. it( 'rejects $root>blockQuote>$text', () => {
  2123. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  2124. } );
  2125. it( 'rejects $root>image>$text', () => {
  2126. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  2127. } );
  2128. it( 'rejects $root>image>paragraph', () => {
  2129. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  2130. } );
  2131. it( 'rejects $root>image>caption>paragraph', () => {
  2132. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  2133. } );
  2134. it( 'rejects $root>image>caption>blockQuote', () => {
  2135. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  2136. } );
  2137. it( 'accepts attribute $root>paragraph[alignment]', () => {
  2138. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  2139. } );
  2140. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  2141. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2142. } );
  2143. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  2144. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  2145. } );
  2146. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  2147. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  2148. } );
  2149. it( 'accepts attribute $root>listItem[alignment]', () => {
  2150. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  2151. } );
  2152. it( 'accepts attribute $root>listItem[indent]', () => {
  2153. expect( schema.checkAttribute( r1lI, 'listIndent' ) ).to.be.true;
  2154. } );
  2155. it( 'accepts attribute $root>listItem[type]', () => {
  2156. expect( schema.checkAttribute( r1lI, 'listType' ) ).to.be.true;
  2157. } );
  2158. it( 'accepts attribute $root>image[src]', () => {
  2159. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  2160. } );
  2161. it( 'accepts attribute $root>image[alt]', () => {
  2162. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  2163. } );
  2164. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  2165. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  2166. } );
  2167. it( 'rejects attribute $root[indent]', () => {
  2168. expect( schema.checkAttribute( root1, 'listIndent' ) ).to.be.false;
  2169. } );
  2170. it( 'rejects attribute $root>paragraph[indent]', () => {
  2171. expect( schema.checkAttribute( r1p1, 'listIndent' ) ).to.be.false;
  2172. } );
  2173. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  2174. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  2175. } );
  2176. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  2177. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  2178. } );
  2179. it( 'rejects attribute $root>blockQuote[indent]', () => {
  2180. expect( schema.checkAttribute( r1bQ, 'listIndent' ) ).to.be.false;
  2181. } );
  2182. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  2183. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  2184. } );
  2185. it( 'rejects attribute $root>image[indent]', () => {
  2186. expect( schema.checkAttribute( r1i, 'listIndent' ) ).to.be.false;
  2187. } );
  2188. it( 'rejects attribute $root>image[alignment]', () => {
  2189. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  2190. } );
  2191. it( '$root is limit', () => {
  2192. expect( schema.isLimit( '$root' ) ).to.be.true;
  2193. expect( schema.isBlock( '$root' ) ).to.be.false;
  2194. expect( schema.isObject( '$root' ) ).to.be.false;
  2195. } );
  2196. it( 'paragraph is block', () => {
  2197. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  2198. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  2199. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  2200. } );
  2201. it( 'heading1 is block', () => {
  2202. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  2203. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  2204. expect( schema.isObject( 'heading1' ) ).to.be.false;
  2205. } );
  2206. it( 'listItem is block', () => {
  2207. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  2208. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  2209. expect( schema.isObject( 'listItem' ) ).to.be.false;
  2210. } );
  2211. it( 'image is block object', () => {
  2212. expect( schema.isLimit( 'image' ) ).to.be.true;
  2213. expect( schema.isBlock( 'image' ) ).to.be.true;
  2214. expect( schema.isObject( 'image' ) ).to.be.true;
  2215. } );
  2216. it( 'caption is limit', () => {
  2217. expect( schema.isLimit( 'caption' ) ).to.be.true;
  2218. expect( schema.isBlock( 'caption' ) ).to.be.false;
  2219. expect( schema.isObject( 'caption' ) ).to.be.false;
  2220. } );
  2221. } );
  2222. } );
  2223. describe( 'SchemaContext', () => {
  2224. let root;
  2225. beforeEach( () => {
  2226. root = new Element( '$root', null, [
  2227. new Element( 'blockQuote', { foo: 1 }, [
  2228. new Element( 'paragraph', { align: 'left' }, [
  2229. new Text( 'foo', { bold: true, italic: true } )
  2230. ] )
  2231. ] )
  2232. ] );
  2233. } );
  2234. describe( 'constructor()', () => {
  2235. it( 'creates context based on an array of strings', () => {
  2236. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2237. expect( ctx.length ).to.equal( 3 );
  2238. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2239. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  2240. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2241. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2242. } );
  2243. it( 'creates context based on an array of elements', () => {
  2244. const blockQuote = root.getChild( 0 );
  2245. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2246. const ctx = new SchemaContext( [ blockQuote, text ] );
  2247. expect( ctx.length ).to.equal( 2 );
  2248. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  2249. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  2250. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2251. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  2252. } );
  2253. it( 'creates context based on a mixed array of strings and elements', () => {
  2254. const blockQuote = root.getChild( 0 );
  2255. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2256. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  2257. expect( ctx.length ).to.equal( 3 );
  2258. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  2259. } );
  2260. it( 'creates context based on a root element', () => {
  2261. const ctx = new SchemaContext( root );
  2262. expect( ctx.length ).to.equal( 1 );
  2263. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  2264. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2265. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2266. } );
  2267. it( 'creates context based on a nested element', () => {
  2268. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  2269. expect( ctx.length ).to.equal( 3 );
  2270. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2271. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2272. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  2273. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  2274. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  2275. } );
  2276. it( 'creates context based on a text node', () => {
  2277. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  2278. expect( ctx.length ).to.equal( 4 );
  2279. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2280. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2281. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2282. } );
  2283. it( 'creates context based on a text proxy', () => {
  2284. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  2285. const textProxy = new TextProxy( text, 0, 1 );
  2286. const ctx = new SchemaContext( textProxy );
  2287. expect( ctx.length ).to.equal( 4 );
  2288. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2289. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2290. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2291. } );
  2292. it( 'creates context based on a position', () => {
  2293. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ), 0 );
  2294. const ctx = new SchemaContext( pos );
  2295. expect( ctx.length ).to.equal( 3 );
  2296. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2297. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  2298. } );
  2299. it( 'creates context based on a string', () => {
  2300. const ctx = new SchemaContext( 'paragraph' );
  2301. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2302. } );
  2303. it( 'creates context based on a SchemaContext instance', () => {
  2304. const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2305. const ctx = new SchemaContext( previousCtx );
  2306. expect( ctx ).to.equal( previousCtx );
  2307. } );
  2308. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  2309. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  2310. expect( ctx.length ).to.equal( 1 );
  2311. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2312. } );
  2313. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  2314. const p = new Element( 'paragraph' );
  2315. const docFrag = new DocumentFragment();
  2316. docFrag._appendChild( p );
  2317. const ctx = new SchemaContext( p );
  2318. expect( ctx.length ).to.equal( 1 );
  2319. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2320. } );
  2321. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  2322. const p = new Element( 'paragraph' );
  2323. const docFrag = new DocumentFragment();
  2324. docFrag._appendChild( p );
  2325. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  2326. expect( ctx.length ).to.equal( 1 );
  2327. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2328. } );
  2329. } );
  2330. describe( 'length', () => {
  2331. it( 'gets the number of items', () => {
  2332. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2333. expect( ctx.length ).to.equal( 3 );
  2334. } );
  2335. } );
  2336. describe( 'last', () => {
  2337. it( 'gets the last item', () => {
  2338. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2339. expect( ctx.last ).to.be.an( 'object' );
  2340. expect( ctx.last.name ).to.equal( 'c' );
  2341. } );
  2342. } );
  2343. describe( 'Symbol.iterator', () => {
  2344. it( 'exists', () => {
  2345. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2346. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  2347. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2348. } );
  2349. } );
  2350. describe( 'getItem()', () => {
  2351. it( 'returns item by index', () => {
  2352. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2353. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2354. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2355. } );
  2356. it( 'returns undefined if index exceeds the range', () => {
  2357. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2358. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2359. } );
  2360. } );
  2361. describe( 'push()', () => {
  2362. it( 'creates new SchemaContext instance with new item - #string', () => {
  2363. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2364. const newCtx = ctx.push( 'd' );
  2365. expect( newCtx ).to.instanceof( SchemaContext );
  2366. expect( newCtx ).to.not.equal( ctx );
  2367. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2368. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2369. } );
  2370. it( 'creates new SchemaContext instance with new item - #text', () => {
  2371. const node = new Text( 'd' );
  2372. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2373. const newCtx = ctx.push( node );
  2374. expect( newCtx ).to.instanceof( SchemaContext );
  2375. expect( newCtx ).to.not.equal( ctx );
  2376. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2377. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2378. } );
  2379. it( 'creates new SchemaContext instance with new item - #element', () => {
  2380. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2381. const parent = new Element( 'parent', null, new Element( 'd' ) );
  2382. const newCtx = ctx.push( parent.getChild( 0 ) );
  2383. expect( newCtx ).to.instanceof( SchemaContext );
  2384. expect( newCtx ).to.not.equal( ctx );
  2385. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2386. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2387. } );
  2388. } );
  2389. describe( 'getNames()', () => {
  2390. it( 'returns an iterator', () => {
  2391. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2392. expect( ctx.getNames().next ).to.be.a( 'function' );
  2393. } );
  2394. it( 'returns an iterator which returns all item names', () => {
  2395. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2396. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2397. } );
  2398. } );
  2399. describe( 'endsWith()', () => {
  2400. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2401. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2402. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2403. } );
  2404. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2405. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2406. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2407. } );
  2408. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2409. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2410. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2411. } );
  2412. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2413. const ctx = new SchemaContext( [ 'foo' ] );
  2414. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2415. } );
  2416. it( 'returns true if not only the end of the context matches the query', () => {
  2417. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2418. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2419. } );
  2420. it( 'returns false if query matches the middle of the context', () => {
  2421. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2422. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2423. } );
  2424. it( 'returns false if query matches the start of the context', () => {
  2425. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2426. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2427. } );
  2428. it( 'returns false if query does not match', () => {
  2429. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2430. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2431. } );
  2432. it( 'returns false if query is longer than context', () => {
  2433. const ctx = new SchemaContext( [ 'foo' ] );
  2434. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2435. } );
  2436. } );
  2437. } );