schema.js 95 KB

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