8
0

schema.js 87 KB

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