schema.js 86 KB

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