schema.js 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. // Similar to the <caption> case.
  1111. model.schema.register( 'blockWidget', {
  1112. allowIn: '$root',
  1113. allowContentOf: '$block',
  1114. isObject: true
  1115. } );
  1116. // Similar to the <tableCell> case.
  1117. model.schema.register( 'blockContainerWidget', {
  1118. allowIn: '$root',
  1119. allowContentOf: '$root',
  1120. isObject: true
  1121. } );
  1122. doc.createRoot();
  1123. selection = doc.selection;
  1124. } );
  1125. test(
  1126. 'should return collapsed range if text node can be placed at that position - both',
  1127. '<paragraph>[]</paragraph>',
  1128. 'both',
  1129. '<paragraph>[]</paragraph>'
  1130. );
  1131. test(
  1132. 'should return collapsed range if text node can be placed at that position - forward',
  1133. '<paragraph>[]</paragraph>',
  1134. 'forward',
  1135. '<paragraph>[]</paragraph>'
  1136. );
  1137. test(
  1138. 'should return collapsed range if text node can be placed at that position - backward',
  1139. '<paragraph>[]</paragraph>',
  1140. 'backward',
  1141. '<paragraph>[]</paragraph>'
  1142. );
  1143. test( 'should return null in empty document - both', '', 'both', null );
  1144. test( 'should return null in empty document - backward', '', 'backward', null );
  1145. test( 'should return null in empty document - forward', '', 'forward', null );
  1146. test(
  1147. 'should find range before when searching both ways',
  1148. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1149. 'both',
  1150. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1151. );
  1152. test(
  1153. 'should find range before when searching backward',
  1154. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1155. 'backward',
  1156. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1157. );
  1158. test(
  1159. 'should find range after when searching forward',
  1160. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1161. 'forward',
  1162. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1163. );
  1164. test(
  1165. 'should find range after when searching both ways when it is closer',
  1166. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  1167. 'both',
  1168. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  1169. );
  1170. test(
  1171. 'should find range before when searching both ways when it is closer',
  1172. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  1173. 'both',
  1174. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  1175. );
  1176. test(
  1177. 'should return null if there is no valid range',
  1178. '[]<emptyBlock></emptyBlock>',
  1179. 'both',
  1180. null
  1181. );
  1182. test(
  1183. 'should return null if there is no valid range in given direction - backward',
  1184. '[]<paragraph></paragraph>',
  1185. 'backward',
  1186. null
  1187. );
  1188. test(
  1189. 'should return null if there is no valid range in given direction - forward',
  1190. '<paragraph></paragraph>[]',
  1191. 'forward',
  1192. null
  1193. );
  1194. test(
  1195. 'should move forward when placed at root start',
  1196. '[]<paragraph></paragraph><paragraph></paragraph>',
  1197. 'both',
  1198. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1199. );
  1200. test(
  1201. 'should move backward when placed at root end',
  1202. '<paragraph></paragraph><paragraph></paragraph>[]',
  1203. 'both',
  1204. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1205. );
  1206. describe( 'in case of objects which do not allow text inside', () => {
  1207. test(
  1208. 'should select nearest object (o[]o) - both',
  1209. '<widget></widget>[]<widget></widget>',
  1210. 'both',
  1211. '[<widget></widget>]<widget></widget>'
  1212. );
  1213. test(
  1214. 'should select nearest object (o[]o) - forward',
  1215. '<widget></widget>[]<widget></widget>',
  1216. 'forward',
  1217. '<widget></widget>[<widget></widget>]'
  1218. );
  1219. test(
  1220. 'should select nearest object (o[]o) - backward',
  1221. '<widget></widget>[]<widget></widget>',
  1222. 'both',
  1223. '[<widget></widget>]<widget></widget>'
  1224. );
  1225. test(
  1226. 'should select nearest object (p[]o) - forward',
  1227. '<paragraph></paragraph>[]<widget></widget>',
  1228. 'forward',
  1229. '<paragraph></paragraph>[<widget></widget>]'
  1230. );
  1231. test(
  1232. 'should select nearest object (o[]p) - both',
  1233. '<widget></widget>[]<paragraph></paragraph>',
  1234. 'both',
  1235. '[<widget></widget>]<paragraph></paragraph>'
  1236. );
  1237. test(
  1238. 'should select nearest object (o[]p) - backward',
  1239. '<widget></widget>[]<paragraph></paragraph>',
  1240. 'backward',
  1241. '[<widget></widget>]<paragraph></paragraph>'
  1242. );
  1243. test(
  1244. 'should select nearest object ([]o) - both',
  1245. '[]<widget></widget><paragraph></paragraph>',
  1246. 'both',
  1247. '[<widget></widget>]<paragraph></paragraph>'
  1248. );
  1249. test(
  1250. 'should select nearest object ([]o) - forward',
  1251. '[]<widget></widget><paragraph></paragraph>',
  1252. 'forward',
  1253. '[<widget></widget>]<paragraph></paragraph>'
  1254. );
  1255. test(
  1256. 'should select nearest object (o[]) - both',
  1257. '<paragraph></paragraph><widget></widget>[]',
  1258. 'both',
  1259. '<paragraph></paragraph>[<widget></widget>]'
  1260. );
  1261. test(
  1262. 'should select nearest object (o[]) - backward',
  1263. '<paragraph></paragraph><widget></widget>[]',
  1264. 'both',
  1265. '<paragraph></paragraph>[<widget></widget>]'
  1266. );
  1267. } );
  1268. describe( 'in case of objects which allow text inside', () => {
  1269. test(
  1270. 'should select nearest object which allows text (o[]o) - both',
  1271. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  1272. 'both',
  1273. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  1274. );
  1275. test(
  1276. 'should select nearest object (o[]p) - both',
  1277. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  1278. 'both',
  1279. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1280. );
  1281. test(
  1282. 'should select nearest object which allows text ([]o) - both',
  1283. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  1284. 'both',
  1285. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1286. );
  1287. } );
  1288. describe( 'in case of other types of objects', () => {
  1289. test(
  1290. 'should return null when cannot leave a limit element',
  1291. '<blockContainerWidget>[]</blockContainerWidget>',
  1292. 'both',
  1293. null
  1294. );
  1295. test(
  1296. 'should return null when cannot leave a limit element (surrounded with paragraphs)',
  1297. '<paragraph>x</paragraph><blockContainerWidget>[]</blockContainerWidget><paragraph>x</paragraph>',
  1298. 'both',
  1299. null
  1300. );
  1301. test(
  1302. 'should return null when cannot leave a limit element (surrounded by other widgets)',
  1303. '<blockContainerWidget><paragraph>x</paragraph></blockContainerWidget>' +
  1304. '<blockContainerWidget>[]</blockContainerWidget>' +
  1305. '<blockContainerWidget><paragraph>x</paragraph></blockContainerWidget>',
  1306. 'both',
  1307. null
  1308. );
  1309. test(
  1310. 'should keep scanning even though encountering a limit in one direction (left)',
  1311. '<paragraph>x</paragraph><blockContainerWidget>[]<paragraph>x</paragraph></blockContainerWidget><paragraph>x</paragraph>',
  1312. 'both',
  1313. '<paragraph>x</paragraph><blockContainerWidget><paragraph>[]x</paragraph></blockContainerWidget><paragraph>x</paragraph>'
  1314. );
  1315. test(
  1316. 'should keep scanning even though encountering a limit in one direction (right)',
  1317. '<paragraph>x</paragraph><blockContainerWidget><paragraph>x</paragraph>[]</blockContainerWidget><paragraph>x</paragraph>',
  1318. 'both',
  1319. '<paragraph>x</paragraph><blockContainerWidget><paragraph>x[]</paragraph></blockContainerWidget><paragraph>x</paragraph>'
  1320. );
  1321. } );
  1322. function test( testName, data, direction, expected ) {
  1323. it( testName, () => {
  1324. let range;
  1325. model.enqueueChange( 'transparent', () => {
  1326. setData( model, data );
  1327. range = schema.getNearestSelectionRange( selection.anchor, direction );
  1328. } );
  1329. if ( expected === null ) {
  1330. expect( range ).to.be.null;
  1331. } else {
  1332. model.change( writer => {
  1333. writer.setSelection( range );
  1334. } );
  1335. expect( getData( model ) ).to.equal( expected );
  1336. }
  1337. } );
  1338. }
  1339. } );
  1340. describe( 'findAllowedParent()', () => {
  1341. beforeEach( () => {
  1342. schema.register( '$root' );
  1343. schema.register( 'blockQuote', {
  1344. allowIn: '$root'
  1345. } );
  1346. schema.register( 'paragraph', {
  1347. allowIn: 'blockQuote'
  1348. } );
  1349. schema.register( '$text', {
  1350. allowIn: 'paragraph'
  1351. } );
  1352. } );
  1353. it( 'should return position ancestor that allows to insert given node to it', () => {
  1354. const node = new Element( 'paragraph' );
  1355. const allowedParent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), node );
  1356. expect( allowedParent ).to.equal( r1bQ );
  1357. } );
  1358. it( 'should return position ancestor that allows to insert given node to it - works with a string too', () => {
  1359. const allowedParent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), 'paragraph' );
  1360. expect( allowedParent ).to.equal( r1bQ );
  1361. } );
  1362. it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
  1363. const node = new Text( 'text' );
  1364. const parent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), node );
  1365. expect( parent ).to.equal( r1bQp );
  1366. } );
  1367. it( 'should return null when limit element is reached before allowed parent', () => {
  1368. schema.extend( 'blockQuote', {
  1369. isLimit: true
  1370. } );
  1371. schema.register( 'div', {
  1372. allowIn: '$root'
  1373. } );
  1374. const node = new Element( 'div' );
  1375. const parent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), node );
  1376. expect( parent ).to.null;
  1377. } );
  1378. it( 'should return null when object element is reached before allowed parent', () => {
  1379. schema.extend( 'blockQuote', {
  1380. isObject: true
  1381. } );
  1382. schema.register( 'div', {
  1383. allowIn: '$root'
  1384. } );
  1385. const node = new Element( 'div' );
  1386. const parent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), node );
  1387. expect( parent ).to.null;
  1388. } );
  1389. it( 'should return null when there is no allowed ancestor for given position', () => {
  1390. const node = new Element( 'section' );
  1391. const parent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), node );
  1392. expect( parent ).to.null;
  1393. } );
  1394. it( 'should return null when there is no allowed ancestor for given position – works with a string too', () => {
  1395. const parent = schema.findAllowedParent( Position._createAt( r1bQp, 0 ), 'section' );
  1396. expect( parent ).to.null;
  1397. } );
  1398. } );
  1399. describe( 'removeDisallowedAttributes()', () => {
  1400. let model, doc, root;
  1401. beforeEach( () => {
  1402. model = new Model();
  1403. doc = model.document;
  1404. root = doc.createRoot();
  1405. schema = model.schema;
  1406. schema.register( 'paragraph', {
  1407. inheritAllFrom: '$block'
  1408. } );
  1409. schema.register( 'div', {
  1410. inheritAllFrom: '$block'
  1411. } );
  1412. schema.register( 'image', {
  1413. isObject: true
  1414. } );
  1415. schema.extend( '$block', {
  1416. allowIn: 'div'
  1417. } );
  1418. } );
  1419. it( 'should filter out disallowed attributes from given nodes', () => {
  1420. schema.extend( '$text', { allowAttributes: 'a' } );
  1421. schema.extend( 'image', { allowAttributes: 'b' } );
  1422. const text = new Text( 'foo', { a: 1, b: 1 } );
  1423. const image = new Element( 'image', { a: 1, b: 1 } );
  1424. root._appendChild( [ text, image ] );
  1425. model.change( writer => {
  1426. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1427. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  1428. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  1429. expect( writer.batch.operations ).to.length( 2 );
  1430. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1431. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1432. expect( getData( model, { withoutSelection: true } ) )
  1433. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  1434. } );
  1435. } );
  1436. it( 'should filter out disallowed attributes from empty element', () => {
  1437. schema.extend( 'div', { allowAttributes: 'a' } );
  1438. const div = new Element( 'div', { a: 1, b: 1 } );
  1439. root._appendChild( [ div ] );
  1440. model.change( writer => {
  1441. schema.removeDisallowedAttributes( [ div ], writer );
  1442. expect( getData( model, { withoutSelection: true } ) )
  1443. .to.equal( '<div a="1"></div>' );
  1444. } );
  1445. } );
  1446. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  1447. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1448. // Allow 'a' on div>$text.
  1449. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  1450. return true;
  1451. }
  1452. // Allow 'b' on div>paragraph>$text.
  1453. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  1454. return true;
  1455. }
  1456. // Allow 'a' on div>image.
  1457. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  1458. return true;
  1459. }
  1460. // Allow 'b' on div>paragraph>image.
  1461. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  1462. return true;
  1463. }
  1464. // Allow 'a' on div>paragraph.
  1465. if ( ctx.endsWith( 'div paragraph' ) && attributeName == 'a' ) {
  1466. return true;
  1467. }
  1468. } );
  1469. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1470. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1471. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  1472. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  1473. const paragraph = new Element( 'paragraph', { a: 1, b: 1 }, [ foo, imageInParagraph ] );
  1474. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  1475. root._appendChild( [ div ] );
  1476. model.change( writer => {
  1477. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1478. expect( getData( model, { withoutSelection: true } ) )
  1479. .to.equal(
  1480. '<div>' +
  1481. '<paragraph a="1">' +
  1482. '<$text b="1">foo</$text>' +
  1483. '<image b="1"></image>' +
  1484. '</paragraph>' +
  1485. '<$text a="1">bar</$text>' +
  1486. '<image a="1"></image>' +
  1487. '</div>'
  1488. );
  1489. } );
  1490. } );
  1491. it( 'should filter out disallowed attributes from parent node and all descendants nodes', () => {
  1492. schema.extend( 'div', { allowAttributes: 'a' } );
  1493. schema.extend( '$text', { allowAttributes: 'b' } );
  1494. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1495. const div = new Element( 'div', { a: 1, b: 1 }, [ foo ] );
  1496. root._appendChild( [ div ] );
  1497. model.change( writer => {
  1498. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1499. expect( getData( model, { withoutSelection: true } ) )
  1500. .to.equal( '<div a="1"><$text b="1">foo</$text></div>' );
  1501. } );
  1502. } );
  1503. it( 'should filter out all attributes from nodes that are merged while clearing', () => {
  1504. const a = new Text( 'a', { a: 1, b: 1 } );
  1505. const b = new Text( 'b', { b: 1 } );
  1506. const c = new Text( 'c', { a: 1, b: 1 } );
  1507. const div = new Element( 'div', [], [ a, b, c ] );
  1508. root._appendChild( [ div ] );
  1509. model.change( writer => {
  1510. schema.removeDisallowedAttributes( [ div ], writer );
  1511. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<div>abc</div>' );
  1512. } );
  1513. } );
  1514. it( 'should do not filter out sibling nodes', () => {
  1515. const foo = new Text( 'foo', { a: 1 } );
  1516. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1517. const biz = new Text( 'biz', { a: 1 } );
  1518. const div = new Element( 'div', [], [ foo, bar, biz ] );
  1519. root._appendChild( [ div ] );
  1520. model.change( writer => {
  1521. schema.removeDisallowedAttributes( [ bar ], writer );
  1522. expect( getData( model, { withoutSelection: true } ) )
  1523. .to.equal( '<div><$text a="1">foo</$text>bar<$text a="1">biz</$text></div>' );
  1524. } );
  1525. } );
  1526. } );
  1527. describe( 'definitions compilation', () => {
  1528. describe( 'allowIn cases', () => {
  1529. it( 'passes $root>paragraph', () => {
  1530. schema.register( '$root' );
  1531. schema.register( 'paragraph', {
  1532. allowIn: '$root'
  1533. } );
  1534. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1535. } );
  1536. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1537. schema.register( '$root' );
  1538. schema.register( '$root2' );
  1539. schema.register( 'paragraph', {
  1540. allowIn: [ '$root', '$root2' ]
  1541. } );
  1542. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1543. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1544. } );
  1545. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1546. schema.register( '$root' );
  1547. schema.register( 'paragraph', {
  1548. allowIn: '$root'
  1549. } );
  1550. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1551. } );
  1552. it( 'passes $root>div>div – in case of circular refs', () => {
  1553. schema.register( '$root' );
  1554. schema.register( 'div', {
  1555. allowIn: [ '$root', 'div' ]
  1556. } );
  1557. const div = new Element( 'div' );
  1558. root1._appendChild( div );
  1559. const div2 = new Element( 'div' );
  1560. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1561. } );
  1562. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1563. schema.register( '$root' );
  1564. schema.register( 'div', {
  1565. allowIn: [ '$root', 'div' ]
  1566. } );
  1567. const div = new Element( 'div' );
  1568. root1._appendChild( div );
  1569. expect( schema.checkChild( div, div ) ).to.be.true;
  1570. } );
  1571. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1572. schema.register( '$root' );
  1573. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1574. } );
  1575. it( 'rejects $root>paragraph – registered different item', () => {
  1576. schema.register( '$root' );
  1577. schema.register( 'paragraph' );
  1578. schema.register( 'listItem', {
  1579. allowIn: '$root'
  1580. } );
  1581. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1582. } );
  1583. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1584. schema.register( '$root' );
  1585. schema.register( '$fancyRoot' );
  1586. schema.register( 'paragraph', {
  1587. allowIn: '$fancyRoot'
  1588. } );
  1589. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1590. } );
  1591. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1592. schema.register( '$root' );
  1593. schema.register( 'paragraph', {
  1594. allowIn: '$root'
  1595. } );
  1596. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1597. } );
  1598. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1599. schema.register( '$root' );
  1600. schema.register( 'blockQuote', {
  1601. allowIn: '$root'
  1602. } );
  1603. schema.register( 'paragraph', {
  1604. allowIn: '$root'
  1605. } );
  1606. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1607. } );
  1608. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1609. schema.register( '$root' );
  1610. schema.register( 'paragraph', {
  1611. allowIn: '$root'
  1612. } );
  1613. schema.register( '$text', {
  1614. allowIn: 'paragraph'
  1615. } );
  1616. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1617. } );
  1618. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1619. schema.register( '$root' );
  1620. schema.register( 'blockQuote' );
  1621. schema.register( 'paragraph', {
  1622. allowIn: [ 'blockQuote', '$root' ]
  1623. } );
  1624. schema.register( '$text', {
  1625. allowIn: 'paragraph'
  1626. } );
  1627. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1628. } );
  1629. } );
  1630. describe( 'allowWhere cases', () => {
  1631. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1632. schema.register( '$root' );
  1633. schema.register( '$block', {
  1634. allowIn: '$root'
  1635. } );
  1636. schema.register( 'paragraph', {
  1637. allowWhere: '$block'
  1638. } );
  1639. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1640. } );
  1641. it( 'supports the array syntax', () => {
  1642. schema.register( '$root' );
  1643. schema.register( '$root2' );
  1644. schema.register( '$block', {
  1645. allowIn: '$root'
  1646. } );
  1647. schema.register( '$block2', {
  1648. allowIn: '$root2'
  1649. } );
  1650. schema.register( 'paragraph', {
  1651. allowWhere: [ '$block', '$block2' ]
  1652. } );
  1653. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1654. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1655. } );
  1656. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1657. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1658. schema.register( '$root' );
  1659. schema.register( 'paragraph', {
  1660. allowWhere: '$block'
  1661. } );
  1662. schema.register( '$block', {
  1663. allowIn: '$root'
  1664. } );
  1665. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1666. } );
  1667. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1668. schema.register( '$root' );
  1669. schema.register( '$block', {
  1670. allowIn: '$root'
  1671. } );
  1672. schema.register( '$specialBlock', {
  1673. allowWhere: '$block'
  1674. } );
  1675. schema.register( 'paragraph', {
  1676. allowWhere: '$specialBlock'
  1677. } );
  1678. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1679. } );
  1680. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1681. schema.register( '$root' );
  1682. schema.register( '$block' );
  1683. schema.register( 'paragraph', {
  1684. allowWhere: '$block'
  1685. } );
  1686. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1687. } );
  1688. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1689. schema.register( '$root' );
  1690. schema.register( '$block' );
  1691. schema.register( 'paragraph', {
  1692. allowWhere: '$block'
  1693. } );
  1694. schema.register( '$text', {
  1695. allowIn: 'paragraph'
  1696. } );
  1697. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1698. } );
  1699. } );
  1700. describe( 'allowContentOf cases', () => {
  1701. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1702. schema.register( '$root' );
  1703. schema.register( 'paragraph', {
  1704. allowIn: '$root'
  1705. } );
  1706. schema.register( '$root2', {
  1707. allowContentOf: '$root'
  1708. } );
  1709. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1710. } );
  1711. it( 'supports the array syntax', () => {
  1712. schema.register( '$root' );
  1713. schema.register( '$root2' );
  1714. schema.register( 'paragraph', {
  1715. allowIn: '$root'
  1716. } );
  1717. schema.register( 'heading1', {
  1718. allowIn: '$root2'
  1719. } );
  1720. schema.register( '$root3', {
  1721. allowContentOf: [ '$root', '$root2' ]
  1722. } );
  1723. const root3 = new Element( '$root3' );
  1724. const heading1 = new Element( 'heading1' );
  1725. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1726. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1727. } );
  1728. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1729. schema.register( '$root' );
  1730. schema.register( '$root2', {
  1731. allowContentOf: '$root'
  1732. } );
  1733. schema.register( 'paragraph', {
  1734. allowIn: '$root'
  1735. } );
  1736. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1737. } );
  1738. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1739. schema.register( '$root' );
  1740. schema.register( '$block' );
  1741. schema.register( 'paragraph', {
  1742. allowIn: '$root',
  1743. allowContentOf: '$block'
  1744. } );
  1745. schema.register( '$text', {
  1746. allowIn: '$block'
  1747. } );
  1748. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1749. } );
  1750. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1751. schema.register( '$root' );
  1752. schema.register( 'blockQuote', {
  1753. allowIn: '$root',
  1754. allowContentOf: '$root'
  1755. } );
  1756. schema.register( 'paragraph', {
  1757. allowIn: '$root'
  1758. } );
  1759. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1760. } );
  1761. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1762. schema.register( '$root' );
  1763. schema.register( 'paragraph' );
  1764. schema.register( '$root2', {
  1765. allowContentOf: '$root'
  1766. } );
  1767. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1768. } );
  1769. } );
  1770. describe( 'mix of allowContentOf and allowWhere', () => {
  1771. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1772. schema.register( '$root' );
  1773. schema.register( '$block', {
  1774. allowIn: '$root'
  1775. } );
  1776. schema.register( 'paragraph', {
  1777. allowContentOf: '$block',
  1778. allowWhere: '$block'
  1779. } );
  1780. schema.register( '$text', {
  1781. allowIn: '$block'
  1782. } );
  1783. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1784. } );
  1785. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1786. 'and paragraph inherits allowWhere from $block', () => {
  1787. schema.register( '$root' );
  1788. schema.register( '$root2', {
  1789. allowContentOf: '$root'
  1790. } );
  1791. schema.register( '$block', {
  1792. allowIn: '$root'
  1793. } );
  1794. schema.register( 'paragraph', {
  1795. allowWhere: '$block'
  1796. } );
  1797. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1798. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1799. } );
  1800. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1801. schema.register( 'b' );
  1802. schema.register( 'a', { allowIn: 'b' } );
  1803. schema.register( 'c', { allowContentOf: 'b' } );
  1804. schema.register( 'd', { allowContentOf: 'c' } );
  1805. const d = new Element( 'd' );
  1806. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1807. } );
  1808. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1809. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1810. // as long as the previous iteration found something to compile.
  1811. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1812. // and since we've found something, we'd now try d<-c which would work.
  1813. //
  1814. // We ignore those situations for now as they are very unlikely to happen and would
  1815. // significantly raised the complexity of definition compilation.
  1816. //
  1817. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1818. // schema.register( 'b' );
  1819. // schema.register( 'a', { allowIn: 'b' } );
  1820. // schema.register( 'd', { allowContentOf: 'c' } );
  1821. // schema.register( 'c', { allowContentOf: 'b' } );
  1822. //
  1823. // const d = new Element( 'd' );
  1824. //
  1825. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1826. // } );
  1827. } );
  1828. describe( 'inheritTypesFrom', () => {
  1829. it( 'inherit properties of another item', () => {
  1830. schema.register( '$block', {
  1831. isBlock: true,
  1832. isLimit: true
  1833. } );
  1834. schema.register( 'paragraph', {
  1835. inheritTypesFrom: '$block'
  1836. } );
  1837. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1838. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1839. } );
  1840. it( 'inherit properties of other items – support for arrays', () => {
  1841. schema.register( '$block', {
  1842. isBlock: true
  1843. } );
  1844. schema.register( '$block2', {
  1845. isLimit: true
  1846. } );
  1847. schema.register( 'paragraph', {
  1848. inheritTypesFrom: [ '$block', '$block2' ]
  1849. } );
  1850. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1851. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1852. } );
  1853. it( 'does not override existing props', () => {
  1854. schema.register( '$block', {
  1855. isBlock: true,
  1856. isLimit: true
  1857. } );
  1858. schema.register( 'paragraph', {
  1859. inheritTypesFrom: '$block',
  1860. isLimit: false
  1861. } );
  1862. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1863. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1864. } );
  1865. } );
  1866. describe( 'inheritAllFrom', () => {
  1867. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1868. schema.register( '$root' );
  1869. schema.register( '$block', {
  1870. allowIn: '$root'
  1871. } );
  1872. schema.register( 'paragraph', {
  1873. inheritAllFrom: '$block'
  1874. } );
  1875. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1876. } );
  1877. it( 'paragraph inherit properties of $block', () => {
  1878. schema.register( '$block', {
  1879. isBlock: true
  1880. } );
  1881. schema.register( 'paragraph', {
  1882. inheritAllFrom: '$block'
  1883. } );
  1884. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1885. } );
  1886. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1887. schema.register( '$root' );
  1888. schema.register( '$block', {
  1889. allowIn: '$root'
  1890. } );
  1891. schema.register( '$text', {
  1892. allowIn: '$block'
  1893. } );
  1894. schema.register( 'paragraph', {
  1895. inheritAllFrom: '$block'
  1896. } );
  1897. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1898. } );
  1899. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1900. schema.register( '$root' );
  1901. schema.register( '$blockProto', {
  1902. allowIn: '$root'
  1903. } );
  1904. schema.register( '$block', {
  1905. allowWhere: '$blockProto'
  1906. } );
  1907. schema.register( 'paragraph', {
  1908. inheritAllFrom: '$block'
  1909. } );
  1910. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1911. } );
  1912. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1913. schema.register( '$root' );
  1914. schema.register( '$blockProto' );
  1915. schema.register( '$block', {
  1916. allowContentOf: '$blockProto',
  1917. allowIn: '$root'
  1918. } );
  1919. schema.register( '$text', {
  1920. allowIn: '$blockProto'
  1921. } );
  1922. schema.register( 'paragraph', {
  1923. inheritAllFrom: '$block'
  1924. } );
  1925. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1926. } );
  1927. } );
  1928. // We need to handle cases where some independent features registered definitions which might use
  1929. // optional elements (elements which might not have been registered).
  1930. describe( 'missing structure definitions', () => {
  1931. it( 'does not break when trying to check a child which is not registered', () => {
  1932. schema.register( '$root' );
  1933. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1934. } );
  1935. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1936. const foo404 = new Element( 'foo404' );
  1937. root1._appendChild( foo404 );
  1938. schema.register( '$root' );
  1939. schema.register( '$text', {
  1940. allowIn: '$root'
  1941. } );
  1942. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1943. } );
  1944. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1945. schema.register( '$root' );
  1946. schema.register( '$text', {
  1947. allowIn: 'foo404'
  1948. } );
  1949. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1950. } );
  1951. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1952. schema.register( '$root' );
  1953. schema.register( '$text', {
  1954. allowWhere: 'foo404'
  1955. } );
  1956. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1957. } );
  1958. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1959. schema.register( '$root', {
  1960. allowContentOf: 'foo404'
  1961. } );
  1962. schema.register( '$text', {
  1963. allowIn: '$root'
  1964. } );
  1965. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1966. } );
  1967. it( 'checks whether allowIn uses a registered element', () => {
  1968. schema.register( 'paragraph', {
  1969. allowIn: '$root'
  1970. } );
  1971. // $root isn't registered!
  1972. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1973. } );
  1974. it( 'does not break when inheriting all from an non-registered element', () => {
  1975. schema.register( 'paragraph', {
  1976. inheritAllFrom: '$block'
  1977. } );
  1978. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1979. } );
  1980. } );
  1981. describe( 'allowAttributes', () => {
  1982. it( 'passes paragraph[align]', () => {
  1983. schema.register( 'paragraph', {
  1984. allowAttributes: 'align'
  1985. } );
  1986. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1987. } );
  1988. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1989. schema.register( 'paragraph', {
  1990. allowAttributes: [ 'align', 'dir' ]
  1991. } );
  1992. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1993. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1994. } );
  1995. it( 'passes paragraph>$text[bold]', () => {
  1996. schema.register( 'paragraph' );
  1997. schema.register( '$text', {
  1998. allowIn: 'paragraph',
  1999. allowAttributes: 'bold'
  2000. } );
  2001. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2002. } );
  2003. } );
  2004. describe( 'allowAttributesOf', () => {
  2005. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  2006. schema.register( '$block', {
  2007. allowAttributes: 'align'
  2008. } );
  2009. schema.register( 'paragraph', {
  2010. allowAttributesOf: '$block'
  2011. } );
  2012. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  2013. } );
  2014. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  2015. schema.register( '$block', {
  2016. allowAttributes: 'align'
  2017. } );
  2018. schema.register( '$block2', {
  2019. allowAttributes: 'dir'
  2020. } );
  2021. schema.register( 'paragraph', {
  2022. allowAttributesOf: [ '$block', '$block2' ]
  2023. } );
  2024. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  2025. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  2026. } );
  2027. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  2028. schema.register( '$block', {
  2029. allowAttributes: 'align'
  2030. } );
  2031. schema.register( 'paragraph', {
  2032. allowAttributes: 'dir',
  2033. allowAttributesOf: '$block'
  2034. } );
  2035. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  2036. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  2037. } );
  2038. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  2039. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  2040. } );
  2041. describe( 'inheritAllFrom', () => {
  2042. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  2043. schema.register( '$block', {
  2044. allowAttributes: 'align'
  2045. } );
  2046. schema.register( 'paragraph', {
  2047. inheritAllFrom: '$block'
  2048. } );
  2049. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  2050. } );
  2051. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  2052. schema.register( '$blockProto', {
  2053. allowAttributes: 'align'
  2054. } );
  2055. schema.register( '$block', {
  2056. allowAttributesOf: '$blockProto'
  2057. } );
  2058. schema.register( 'paragraph', {
  2059. inheritAllFrom: '$block'
  2060. } );
  2061. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  2062. } );
  2063. } );
  2064. describe( 'missing attribute definitions', () => {
  2065. it( 'does not crash when checking an attribute of a non-registered element', () => {
  2066. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  2067. } );
  2068. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  2069. schema.register( 'paragraph', {
  2070. allowAttributesOf: '$block'
  2071. } );
  2072. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  2073. } );
  2074. it( 'does not crash when inheriting all from a non-registered element', () => {
  2075. schema.register( 'paragraph', {
  2076. allowAttributesOf: '$block'
  2077. } );
  2078. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  2079. } );
  2080. } );
  2081. describe( 'missing types definitions', () => {
  2082. it( 'does not crash when inheriting types of an non-registered element', () => {
  2083. schema.register( 'paragraph', {
  2084. inheritTypesFrom: '$block'
  2085. } );
  2086. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  2087. } );
  2088. } );
  2089. } );
  2090. describe( 'real scenarios', () => {
  2091. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  2092. const definitions = [
  2093. () => {
  2094. schema.register( 'paragraph', {
  2095. inheritAllFrom: '$block'
  2096. } );
  2097. },
  2098. () => {
  2099. schema.register( 'heading1', {
  2100. inheritAllFrom: '$block'
  2101. } );
  2102. },
  2103. () => {
  2104. schema.register( 'listItem', {
  2105. inheritAllFrom: '$block',
  2106. allowAttributes: [ 'listIndent', 'listType' ]
  2107. } );
  2108. },
  2109. () => {
  2110. schema.register( 'blockQuote', {
  2111. allowWhere: '$block',
  2112. allowContentOf: '$root'
  2113. } );
  2114. // Disallow blockQuote in blockQuote.
  2115. schema.addChildCheck( ( ctx, childDef ) => {
  2116. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  2117. return false;
  2118. }
  2119. } );
  2120. },
  2121. () => {
  2122. schema.register( 'image', {
  2123. allowWhere: '$block',
  2124. allowAttributes: [ 'src', 'alt' ],
  2125. isObject: true,
  2126. isBlock: true
  2127. } );
  2128. },
  2129. () => {
  2130. schema.register( 'caption', {
  2131. allowIn: 'image',
  2132. allowContentOf: '$block',
  2133. isLimit: true
  2134. } );
  2135. },
  2136. () => {
  2137. schema.extend( '$text', {
  2138. allowAttributes: [ 'bold', 'italic' ],
  2139. isInline: true
  2140. } );
  2141. // Disallow bold in heading1.
  2142. schema.addAttributeCheck( ( ctx, attributeName ) => {
  2143. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  2144. return false;
  2145. }
  2146. } );
  2147. },
  2148. () => {
  2149. schema.extend( '$block', {
  2150. allowAttributes: 'alignment'
  2151. } );
  2152. }
  2153. ];
  2154. beforeEach( () => {
  2155. schema.register( '$root', {
  2156. isLimit: true
  2157. } );
  2158. schema.register( '$block', {
  2159. allowIn: '$root',
  2160. isBlock: true
  2161. } );
  2162. schema.register( '$text', {
  2163. allowIn: '$block',
  2164. isInline: true
  2165. } );
  2166. for ( const definition of definitions ) {
  2167. definition();
  2168. }
  2169. // or...
  2170. //
  2171. // Use the below code to shuffle the definitions.
  2172. // Don't look here, Szymon!
  2173. //
  2174. // const definitionsCopy = definitions.slice();
  2175. //
  2176. // while ( definitionsCopy.length ) {
  2177. // const r = Math.floor( Math.random() * definitionsCopy.length );
  2178. // definitionsCopy.splice( r, 1 )[ 0 ]();
  2179. // }
  2180. root1 = new Element( '$root', null, [
  2181. new Element( 'paragraph', null, 'foo' ),
  2182. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  2183. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2184. new Element( 'heading1', null, 'foo' ),
  2185. new Element( 'blockQuote', null, [
  2186. new Element( 'paragraph', null, 'foo' ),
  2187. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  2188. new Element( 'image', null, [
  2189. new Element( 'caption', null, 'foo' )
  2190. ] )
  2191. ] ),
  2192. new Element( 'image', null, [
  2193. new Element( 'caption', null, 'foo' )
  2194. ] )
  2195. ] );
  2196. r1p1 = root1.getChild( 0 );
  2197. r1p2 = root1.getChild( 1 );
  2198. r1lI = root1.getChild( 2 );
  2199. r1h = root1.getChild( 3 );
  2200. r1bQ = root1.getChild( 4 );
  2201. r1i = root1.getChild( 5 );
  2202. r1bQp = r1bQ.getChild( 0 );
  2203. r1bQlI = r1bQ.getChild( 1 );
  2204. r1bQi = r1bQ.getChild( 2 );
  2205. } );
  2206. it( 'passes $root>paragraph', () => {
  2207. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  2208. } );
  2209. it( 'passes $root>paragraph>$text', () => {
  2210. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  2211. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  2212. } );
  2213. it( 'passes $root>listItem', () => {
  2214. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  2215. } );
  2216. it( 'passes $root>listItem>$text', () => {
  2217. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  2218. } );
  2219. it( 'passes $root>blockQuote>paragraph', () => {
  2220. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  2221. } );
  2222. it( 'passes $root>blockQuote>paragraph>$text', () => {
  2223. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  2224. } );
  2225. it( 'passes $root>blockQuote>listItem', () => {
  2226. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  2227. } );
  2228. it( 'passes $root>blockQuote>listItem>$text', () => {
  2229. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  2230. } );
  2231. it( 'passes $root>blockQuote>image', () => {
  2232. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  2233. } );
  2234. it( 'passes $root>blockQuote>image>caption', () => {
  2235. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  2236. } );
  2237. it( 'passes $root>blockQuote>image>caption>$text', () => {
  2238. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  2239. } );
  2240. it( 'passes $root>image', () => {
  2241. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  2242. } );
  2243. it( 'passes $root>image>caption', () => {
  2244. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  2245. } );
  2246. it( 'passes $root>image>caption>$text', () => {
  2247. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  2248. } );
  2249. it( 'rejects $root>$root', () => {
  2250. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  2251. } );
  2252. it( 'rejects $root>$text', () => {
  2253. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  2254. } );
  2255. it( 'rejects $root>caption', () => {
  2256. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  2257. } );
  2258. it( 'rejects $root>paragraph>paragraph', () => {
  2259. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  2260. } );
  2261. it( 'rejects $root>paragraph>paragraph>$text', () => {
  2262. // Edge case because p>p should not exist in the first place.
  2263. // But it's good to know that it blocks also this.
  2264. const p = new Element( 'p' );
  2265. r1p1._appendChild( p );
  2266. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  2267. } );
  2268. it( 'rejects $root>paragraph>$block', () => {
  2269. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  2270. } );
  2271. it( 'rejects $root>paragraph>blockQuote', () => {
  2272. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  2273. } );
  2274. it( 'rejects $root>paragraph>image', () => {
  2275. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2276. } );
  2277. it( 'rejects $root>paragraph>caption', () => {
  2278. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  2279. } );
  2280. it( 'rejects $root>blockQuote>blockQuote', () => {
  2281. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  2282. } );
  2283. it( 'rejects $root>blockQuote>caption', () => {
  2284. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2285. } );
  2286. it( 'rejects $root>blockQuote>$text', () => {
  2287. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  2288. } );
  2289. it( 'rejects $root>image>$text', () => {
  2290. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  2291. } );
  2292. it( 'rejects $root>image>paragraph', () => {
  2293. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  2294. } );
  2295. it( 'rejects $root>image>caption>paragraph', () => {
  2296. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  2297. } );
  2298. it( 'rejects $root>image>caption>blockQuote', () => {
  2299. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  2300. } );
  2301. it( 'accepts attribute $root>paragraph[alignment]', () => {
  2302. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  2303. } );
  2304. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  2305. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2306. } );
  2307. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  2308. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  2309. } );
  2310. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  2311. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  2312. } );
  2313. it( 'accepts attribute $root>listItem[alignment]', () => {
  2314. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  2315. } );
  2316. it( 'accepts attribute $root>listItem[indent]', () => {
  2317. expect( schema.checkAttribute( r1lI, 'listIndent' ) ).to.be.true;
  2318. } );
  2319. it( 'accepts attribute $root>listItem[type]', () => {
  2320. expect( schema.checkAttribute( r1lI, 'listType' ) ).to.be.true;
  2321. } );
  2322. it( 'accepts attribute $root>image[src]', () => {
  2323. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  2324. } );
  2325. it( 'accepts attribute $root>image[alt]', () => {
  2326. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  2327. } );
  2328. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  2329. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  2330. } );
  2331. it( 'rejects attribute $root[indent]', () => {
  2332. expect( schema.checkAttribute( root1, 'listIndent' ) ).to.be.false;
  2333. } );
  2334. it( 'rejects attribute $root>paragraph[indent]', () => {
  2335. expect( schema.checkAttribute( r1p1, 'listIndent' ) ).to.be.false;
  2336. } );
  2337. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  2338. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  2339. } );
  2340. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  2341. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  2342. } );
  2343. it( 'rejects attribute $root>blockQuote[indent]', () => {
  2344. expect( schema.checkAttribute( r1bQ, 'listIndent' ) ).to.be.false;
  2345. } );
  2346. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  2347. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  2348. } );
  2349. it( 'rejects attribute $root>image[indent]', () => {
  2350. expect( schema.checkAttribute( r1i, 'listIndent' ) ).to.be.false;
  2351. } );
  2352. it( 'rejects attribute $root>image[alignment]', () => {
  2353. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  2354. } );
  2355. it( '$text is inline', () => {
  2356. expect( schema.isLimit( '$text' ) ).to.be.false;
  2357. expect( schema.isBlock( '$text' ) ).to.be.false;
  2358. expect( schema.isObject( '$text' ) ).to.be.false;
  2359. expect( schema.isInline( '$text' ) ).to.be.true;
  2360. } );
  2361. it( '$root is limit', () => {
  2362. expect( schema.isLimit( '$root' ) ).to.be.true;
  2363. expect( schema.isBlock( '$root' ) ).to.be.false;
  2364. expect( schema.isObject( '$root' ) ).to.be.false;
  2365. expect( schema.isInline( '$root' ) ).to.be.false;
  2366. } );
  2367. it( 'paragraph is block', () => {
  2368. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  2369. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  2370. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  2371. expect( schema.isInline( 'paragraph' ) ).to.be.false;
  2372. } );
  2373. it( 'heading1 is block', () => {
  2374. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  2375. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  2376. expect( schema.isObject( 'heading1' ) ).to.be.false;
  2377. expect( schema.isInline( 'heading1' ) ).to.be.false;
  2378. } );
  2379. it( 'listItem is block', () => {
  2380. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  2381. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  2382. expect( schema.isObject( 'listItem' ) ).to.be.false;
  2383. expect( schema.isInline( 'lisItem' ) ).to.be.false;
  2384. } );
  2385. it( 'image is block object', () => {
  2386. expect( schema.isLimit( 'image' ) ).to.be.true;
  2387. expect( schema.isBlock( 'image' ) ).to.be.true;
  2388. expect( schema.isObject( 'image' ) ).to.be.true;
  2389. expect( schema.isInline( 'image' ) ).to.be.false;
  2390. } );
  2391. it( 'caption is limit', () => {
  2392. expect( schema.isLimit( 'caption' ) ).to.be.true;
  2393. expect( schema.isBlock( 'caption' ) ).to.be.false;
  2394. expect( schema.isObject( 'caption' ) ).to.be.false;
  2395. expect( schema.isInline( 'caption' ) ).to.be.false;
  2396. } );
  2397. } );
  2398. describe( 'createContext()', () => {
  2399. it( 'should return SchemaContext instance', () => {
  2400. const ctx = schema.createContext( [ 'a', 'b', 'c' ] );
  2401. expect( ctx ).to.be.instanceof( SchemaContext );
  2402. } );
  2403. } );
  2404. } );
  2405. describe( 'SchemaContext', () => {
  2406. let root;
  2407. beforeEach( () => {
  2408. root = new Element( '$root', null, [
  2409. new Element( 'blockQuote', { foo: 1 }, [
  2410. new Element( 'paragraph', { align: 'left' }, [
  2411. new Text( 'foo', { bold: true, italic: true } )
  2412. ] )
  2413. ] )
  2414. ] );
  2415. } );
  2416. describe( 'constructor()', () => {
  2417. it( 'creates context based on an array of strings', () => {
  2418. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2419. expect( ctx.length ).to.equal( 3 );
  2420. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2421. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  2422. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2423. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2424. } );
  2425. it( 'creates context based on an array of elements', () => {
  2426. const blockQuote = root.getChild( 0 );
  2427. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2428. const ctx = new SchemaContext( [ blockQuote, text ] );
  2429. expect( ctx.length ).to.equal( 2 );
  2430. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  2431. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  2432. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2433. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  2434. } );
  2435. it( 'creates context based on a mixed array of strings and elements', () => {
  2436. const blockQuote = root.getChild( 0 );
  2437. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2438. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  2439. expect( ctx.length ).to.equal( 3 );
  2440. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  2441. } );
  2442. it( 'creates context based on a root element', () => {
  2443. const ctx = new SchemaContext( root );
  2444. expect( ctx.length ).to.equal( 1 );
  2445. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  2446. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2447. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2448. } );
  2449. it( 'creates context based on a nested element', () => {
  2450. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  2451. expect( ctx.length ).to.equal( 3 );
  2452. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2453. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2454. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  2455. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  2456. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  2457. } );
  2458. it( 'creates context based on a text node', () => {
  2459. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  2460. expect( ctx.length ).to.equal( 4 );
  2461. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2462. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2463. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2464. } );
  2465. it( 'creates context based on a text proxy', () => {
  2466. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  2467. const textProxy = new TextProxy( text, 0, 1 );
  2468. const ctx = new SchemaContext( textProxy );
  2469. expect( ctx.length ).to.equal( 4 );
  2470. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2471. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2472. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2473. } );
  2474. it( 'creates context based on a position', () => {
  2475. const pos = Position._createAt( root.getChild( 0 ).getChild( 0 ), 0 );
  2476. const ctx = new SchemaContext( pos );
  2477. expect( ctx.length ).to.equal( 3 );
  2478. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2479. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  2480. } );
  2481. it( 'creates context based on a string', () => {
  2482. const ctx = new SchemaContext( 'paragraph' );
  2483. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2484. } );
  2485. it( 'creates context based on a SchemaContext instance', () => {
  2486. const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2487. const ctx = new SchemaContext( previousCtx );
  2488. expect( ctx ).to.equal( previousCtx );
  2489. } );
  2490. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  2491. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  2492. expect( ctx.length ).to.equal( 1 );
  2493. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2494. } );
  2495. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  2496. const p = new Element( 'paragraph' );
  2497. const docFrag = new DocumentFragment();
  2498. docFrag._appendChild( p );
  2499. const ctx = new SchemaContext( p );
  2500. expect( ctx.length ).to.equal( 1 );
  2501. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2502. } );
  2503. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  2504. const p = new Element( 'paragraph' );
  2505. const docFrag = new DocumentFragment();
  2506. docFrag._appendChild( p );
  2507. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  2508. expect( ctx.length ).to.equal( 1 );
  2509. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2510. } );
  2511. } );
  2512. describe( 'length', () => {
  2513. it( 'gets the number of items', () => {
  2514. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2515. expect( ctx.length ).to.equal( 3 );
  2516. } );
  2517. } );
  2518. describe( 'last', () => {
  2519. it( 'gets the last item', () => {
  2520. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2521. expect( ctx.last ).to.be.an( 'object' );
  2522. expect( ctx.last.name ).to.equal( 'c' );
  2523. } );
  2524. } );
  2525. describe( 'Symbol.iterator', () => {
  2526. it( 'exists', () => {
  2527. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2528. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  2529. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2530. } );
  2531. } );
  2532. describe( 'getItem()', () => {
  2533. it( 'returns item by index', () => {
  2534. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2535. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2536. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2537. } );
  2538. it( 'returns undefined if index exceeds the range', () => {
  2539. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2540. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2541. } );
  2542. } );
  2543. describe( 'push()', () => {
  2544. it( 'creates new SchemaContext instance with new item - #string', () => {
  2545. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2546. const newCtx = ctx.push( 'd' );
  2547. expect( newCtx ).to.instanceof( SchemaContext );
  2548. expect( newCtx ).to.not.equal( ctx );
  2549. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2550. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2551. } );
  2552. it( 'creates new SchemaContext instance with new item - #text', () => {
  2553. const node = new Text( 'd' );
  2554. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2555. const newCtx = ctx.push( node );
  2556. expect( newCtx ).to.instanceof( SchemaContext );
  2557. expect( newCtx ).to.not.equal( ctx );
  2558. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2559. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2560. } );
  2561. it( 'creates new SchemaContext instance with new item - #element', () => {
  2562. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2563. const parent = new Element( 'parent', null, new Element( 'd' ) );
  2564. const newCtx = ctx.push( parent.getChild( 0 ) );
  2565. expect( newCtx ).to.instanceof( SchemaContext );
  2566. expect( newCtx ).to.not.equal( ctx );
  2567. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2568. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2569. } );
  2570. } );
  2571. describe( 'getNames()', () => {
  2572. it( 'returns an iterator', () => {
  2573. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2574. expect( ctx.getNames().next ).to.be.a( 'function' );
  2575. } );
  2576. it( 'returns an iterator which returns all item names', () => {
  2577. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2578. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2579. } );
  2580. } );
  2581. describe( 'endsWith()', () => {
  2582. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2583. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2584. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2585. } );
  2586. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2587. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2588. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2589. } );
  2590. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2591. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2592. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2593. } );
  2594. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2595. const ctx = new SchemaContext( [ 'foo' ] );
  2596. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2597. } );
  2598. it( 'returns true if not only the end of the context matches the query', () => {
  2599. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2600. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2601. } );
  2602. it( 'returns false if query matches the middle of the context', () => {
  2603. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2604. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2605. } );
  2606. it( 'returns false if query matches the start of the context', () => {
  2607. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2608. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2609. } );
  2610. it( 'returns false if query does not match', () => {
  2611. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2612. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2613. } );
  2614. it( 'returns false if query is longer than context', () => {
  2615. const ctx = new SchemaContext( [ 'foo' ] );
  2616. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2617. } );
  2618. } );
  2619. describe( 'startsWith()', () => {
  2620. it( 'returns true if the start of the context matches the query - 1 item', () => {
  2621. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2622. expect( ctx.startsWith( 'foo' ) ).to.be.true;
  2623. } );
  2624. it( 'returns true if the start of the context matches the query - 2 items', () => {
  2625. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2626. expect( ctx.startsWith( 'foo bar' ) ).to.be.true;
  2627. } );
  2628. it( 'returns true if the start of the context matches the query - full match of 3 items', () => {
  2629. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2630. expect( ctx.startsWith( 'foo bar bom' ) ).to.be.true;
  2631. } );
  2632. it( 'returns true if the start of the context matches the query - full match of 1 items', () => {
  2633. const ctx = new SchemaContext( [ 'foo' ] );
  2634. expect( ctx.startsWith( 'foo' ) ).to.be.true;
  2635. } );
  2636. it( 'returns true if not only the start of the context matches the query', () => {
  2637. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2638. expect( ctx.startsWith( 'foo foo' ) ).to.be.true;
  2639. } );
  2640. it( 'returns false if query matches the middle of the context', () => {
  2641. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2642. expect( ctx.startsWith( 'bom' ) ).to.be.false;
  2643. } );
  2644. it( 'returns false if query matches the end of the context', () => {
  2645. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2646. expect( ctx.startsWith( 'dom' ) ).to.be.false;
  2647. } );
  2648. it( 'returns false if query does not match', () => {
  2649. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2650. expect( ctx.startsWith( 'dom bar' ) ).to.be.false;
  2651. } );
  2652. it( 'returns false if query is longer than context', () => {
  2653. const ctx = new SchemaContext( [ 'foo' ] );
  2654. expect( ctx.startsWith( 'bar', 'foo' ) ).to.be.false;
  2655. } );
  2656. } );
  2657. } );