schema.js 93 KB

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