schema.js 103 KB

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