8
0

schema.js 100 KB

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