8
0

schema.js 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922
  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. model.enqueueChange( 'transparent', () => {
  691. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  692. const section = root.getNodeByPath( [ 0, 0 ] );
  693. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  694. } );
  695. } );
  696. it( 'works fine with multi-range selections', () => {
  697. schema.extend( 'article', { isLimit: true } );
  698. schema.extend( 'widget', { isLimit: true } );
  699. schema.extend( 'div', { isLimit: true } );
  700. setData(
  701. model,
  702. '<div>' +
  703. '<section>' +
  704. '<article>' +
  705. '<paragraph>[foo]</paragraph>' +
  706. '</article>' +
  707. '</section>' +
  708. '<widget>' +
  709. '<image>' +
  710. '<caption>b[a]r</caption>' +
  711. '</image>' +
  712. '</widget>' +
  713. '</div>'
  714. );
  715. const div = root.getNodeByPath( [ 0 ] );
  716. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  717. } );
  718. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  719. setData(
  720. model,
  721. '<div>' +
  722. '<section>' +
  723. '<article>' +
  724. '<paragraph>[foo]</paragraph>' +
  725. '</article>' +
  726. '</section>' +
  727. '</div>' +
  728. '<section>b[]ar</section>'
  729. );
  730. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  731. } );
  732. it( 'works fine with multi-range selections if the first range has the root element as a limit element', () => {
  733. setData(
  734. model,
  735. '<image>' +
  736. '<caption>[Foo</caption>' +
  737. '</image>' +
  738. '<article>' +
  739. '<paragraph>Paragraph in article]</paragraph>' +
  740. '</article>' +
  741. '<paragraph>Paragraph item 1</paragraph>' +
  742. '<paragraph>Paragraph [item 2]</paragraph>'
  743. );
  744. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  745. } );
  746. it( 'works fine with multi-range selections if the last range has the root element as a limit element', () => {
  747. setData(
  748. model,
  749. '<paragraph>Paragraph item 1</paragraph>' +
  750. '<paragraph>Paragraph [item 2]</paragraph>' +
  751. '<image>' +
  752. '<caption>[Foo</caption>' +
  753. '</image>' +
  754. '<article>' +
  755. '<paragraph>Paragraph in article]</paragraph>' +
  756. '</article>'
  757. );
  758. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  759. } );
  760. } );
  761. describe( 'checkAttributeInSelection()', () => {
  762. const attribute = 'bold';
  763. let model, doc, schema;
  764. beforeEach( () => {
  765. model = new Model();
  766. doc = model.document;
  767. doc.createRoot();
  768. schema = model.schema;
  769. schema.register( 'p', { inheritAllFrom: '$block' } );
  770. schema.register( 'h1', { inheritAllFrom: '$block' } );
  771. schema.register( 'img', { allowWhere: '$text' } );
  772. schema.register( 'figure', {
  773. allowIn: '$root',
  774. allowAttributes: [ 'name', 'title' ]
  775. } );
  776. schema.addAttributeCheck( ( ctx, attributeName ) => {
  777. // Allow 'bold' on p>$text.
  778. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  779. return true;
  780. }
  781. // Allow 'bold' on $root>p.
  782. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  783. return true;
  784. }
  785. } );
  786. } );
  787. describe( 'when selection is collapsed', () => {
  788. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  789. setData( model, '<p>f[]oo</p>' );
  790. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  791. } );
  792. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  793. setData( model, '<h1>[]</h1>' );
  794. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  795. setData( model, '[]' );
  796. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  797. } );
  798. } );
  799. describe( 'when selection is not collapsed', () => {
  800. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  801. // Simple selection on a few characters.
  802. setData( model, '<p>[foo]</p>' );
  803. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  804. // Selection spans over characters but also include nodes that can't have attribute.
  805. setData( model, '<p>fo[o<img />b]ar</p>' );
  806. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  807. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  808. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  809. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  810. // Selection on empty P. P can have the attribute.
  811. setData( model, '[<p></p>]' );
  812. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  813. } );
  814. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  815. // Selection on DIV which can't have bold text.
  816. setData( model, '[<h1></h1>]' );
  817. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  818. // Selection on two images which can't be bold.
  819. setData( model, '<p>foo[<img /><img />]bar</p>' );
  820. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  821. } );
  822. it( 'should return true when checking element with required attribute', () => {
  823. setData( model, '[<figure name="figure"></figure>]' );
  824. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  825. } );
  826. it( 'should return true when checking element when attribute is already present', () => {
  827. setData( model, '[<figure name="figure" title="title"></figure>]' );
  828. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  829. } );
  830. } );
  831. } );
  832. describe( 'getValidRanges()', () => {
  833. const attribute = 'bold';
  834. let model, doc, root, schema, ranges;
  835. beforeEach( () => {
  836. model = new Model();
  837. doc = model.document;
  838. schema = model.schema;
  839. root = doc.createRoot();
  840. schema.register( 'p', { inheritAllFrom: '$block' } );
  841. schema.register( 'h1', { inheritAllFrom: '$block' } );
  842. schema.register( 'img', {
  843. allowWhere: '$text'
  844. } );
  845. schema.addAttributeCheck( ( ctx, attributeName ) => {
  846. // Allow 'bold' on p>$text.
  847. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  848. return true;
  849. }
  850. // Allow 'bold' on $root>p.
  851. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  852. return true;
  853. }
  854. } );
  855. setData( model, '<p>foo<img />bar</p>' );
  856. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  857. } );
  858. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  859. schema.extend( 'img', { allowAttributes: 'bold' } );
  860. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  861. } );
  862. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  863. schema.extend( 'img', { allowAttributes: 'bold' } );
  864. schema.extend( '$text', { allowIn: 'img' } );
  865. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  866. } );
  867. it( 'should return two ranges when attribute is not allowed on one item', () => {
  868. schema.extend( 'img', { allowAttributes: 'bold' } );
  869. schema.extend( '$text', { allowIn: 'img' } );
  870. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  871. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  872. const sel = new Selection( validRanges );
  873. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  874. } );
  875. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  876. schema.extend( '$text', { allowIn: 'img' } );
  877. schema.addAttributeCheck( ( ctx, attributeName ) => {
  878. // Allow 'bold' on img>$text.
  879. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  880. return true;
  881. }
  882. } );
  883. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  884. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  885. const sel = new Selection( validRanges );
  886. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  887. } );
  888. it( 'should not leak beyond the given ranges', () => {
  889. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  890. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  891. const sel = new Selection( validRanges );
  892. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  893. } );
  894. it( 'should correctly handle a range which ends in a disallowed position', () => {
  895. schema.extend( '$text', { allowIn: 'img' } );
  896. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  897. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  898. const sel = new Selection( validRanges );
  899. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  900. } );
  901. it( 'should split range into two ranges and omit disallowed element', () => {
  902. schema.addAttributeCheck( ( ctx, attributeName ) => {
  903. // Disallow 'bold' on p>img.
  904. if ( ctx.endsWith( 'p img' ) && attributeName == 'bold' ) {
  905. return false;
  906. }
  907. } );
  908. const result = schema.getValidRanges( ranges, attribute );
  909. expect( result ).to.length( 2 );
  910. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  911. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  912. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  913. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  914. } );
  915. } );
  916. describe( 'getNearestSelectionRange()', () => {
  917. let selection, model, doc;
  918. beforeEach( () => {
  919. model = new Model();
  920. doc = model.document;
  921. schema = model.schema;
  922. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  923. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  924. model.schema.register( 'widget', {
  925. allowIn: '$root',
  926. isObject: true
  927. } );
  928. model.schema.register( 'blockWidget', {
  929. allowIn: '$root',
  930. allowContentOf: '$block',
  931. isObject: true
  932. } );
  933. doc.createRoot();
  934. selection = doc.selection;
  935. } );
  936. test(
  937. 'should return collapsed range if text node can be placed at that position - both',
  938. '<paragraph>[]</paragraph>',
  939. 'both',
  940. '<paragraph>[]</paragraph>'
  941. );
  942. test(
  943. 'should return collapsed range if text node can be placed at that position - forward',
  944. '<paragraph>[]</paragraph>',
  945. 'forward',
  946. '<paragraph>[]</paragraph>'
  947. );
  948. test(
  949. 'should return collapsed range if text node can be placed at that position - backward',
  950. '<paragraph>[]</paragraph>',
  951. 'backward',
  952. '<paragraph>[]</paragraph>'
  953. );
  954. test( 'should return null in empty document - both', '', 'both', null );
  955. test( 'should return null in empty document - backward', '', 'backward', null );
  956. test( 'should return null in empty document - forward', '', 'forward', null );
  957. test(
  958. 'should find range before when searching both ways',
  959. '<paragraph></paragraph>[]<paragraph></paragraph>',
  960. 'both',
  961. '<paragraph>[]</paragraph><paragraph></paragraph>'
  962. );
  963. test(
  964. 'should find range before when searching backward',
  965. '<paragraph></paragraph>[]<paragraph></paragraph>',
  966. 'backward',
  967. '<paragraph>[]</paragraph><paragraph></paragraph>'
  968. );
  969. test(
  970. 'should find range after when searching forward',
  971. '<paragraph></paragraph>[]<paragraph></paragraph>',
  972. 'forward',
  973. '<paragraph></paragraph><paragraph>[]</paragraph>'
  974. );
  975. test(
  976. 'should find range after when searching both ways when it is closer',
  977. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  978. 'both',
  979. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  980. );
  981. test(
  982. 'should find range before when searching both ways when it is closer',
  983. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  984. 'both',
  985. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  986. );
  987. test(
  988. 'should return null if there is no valid range',
  989. '[]<emptyBlock></emptyBlock>',
  990. 'both',
  991. null
  992. );
  993. test(
  994. 'should return null if there is no valid range in given direction - backward',
  995. '[]<paragraph></paragraph>',
  996. 'backward',
  997. null
  998. );
  999. test(
  1000. 'should return null if there is no valid range in given direction - forward',
  1001. '<paragraph></paragraph>[]',
  1002. 'forward',
  1003. null
  1004. );
  1005. test(
  1006. 'should move forward when placed at root start',
  1007. '[]<paragraph></paragraph><paragraph></paragraph>',
  1008. 'both',
  1009. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1010. );
  1011. test(
  1012. 'should move backward when placed at root end',
  1013. '<paragraph></paragraph><paragraph></paragraph>[]',
  1014. 'both',
  1015. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1016. );
  1017. describe( 'in case of objects which do not allow text inside', () => {
  1018. test(
  1019. 'should select nearest object (o[]o) - both',
  1020. '<widget></widget>[]<widget></widget>',
  1021. 'both',
  1022. '[<widget></widget>]<widget></widget>'
  1023. );
  1024. test(
  1025. 'should select nearest object (o[]o) - forward',
  1026. '<widget></widget>[]<widget></widget>',
  1027. 'forward',
  1028. '<widget></widget>[<widget></widget>]'
  1029. );
  1030. test(
  1031. 'should select nearest object (o[]o) - backward',
  1032. '<widget></widget>[]<widget></widget>',
  1033. 'both',
  1034. '[<widget></widget>]<widget></widget>'
  1035. );
  1036. test(
  1037. 'should select nearest object (p[]o) - forward',
  1038. '<paragraph></paragraph>[]<widget></widget>',
  1039. 'forward',
  1040. '<paragraph></paragraph>[<widget></widget>]'
  1041. );
  1042. test(
  1043. 'should select nearest object (o[]p) - both',
  1044. '<widget></widget>[]<paragraph></paragraph>',
  1045. 'both',
  1046. '[<widget></widget>]<paragraph></paragraph>'
  1047. );
  1048. test(
  1049. 'should select nearest object (o[]p) - backward',
  1050. '<widget></widget>[]<paragraph></paragraph>',
  1051. 'backward',
  1052. '[<widget></widget>]<paragraph></paragraph>'
  1053. );
  1054. test(
  1055. 'should select nearest object ([]o) - both',
  1056. '[]<widget></widget><paragraph></paragraph>',
  1057. 'both',
  1058. '[<widget></widget>]<paragraph></paragraph>'
  1059. );
  1060. test(
  1061. 'should select nearest object ([]o) - forward',
  1062. '[]<widget></widget><paragraph></paragraph>',
  1063. 'forward',
  1064. '[<widget></widget>]<paragraph></paragraph>'
  1065. );
  1066. test(
  1067. 'should select nearest object (o[]) - both',
  1068. '<paragraph></paragraph><widget></widget>[]',
  1069. 'both',
  1070. '<paragraph></paragraph>[<widget></widget>]'
  1071. );
  1072. test(
  1073. 'should select nearest object (o[]) - backward',
  1074. '<paragraph></paragraph><widget></widget>[]',
  1075. 'both',
  1076. '<paragraph></paragraph>[<widget></widget>]'
  1077. );
  1078. } );
  1079. describe( 'in case of objects which allow text inside', () => {
  1080. test(
  1081. 'should select nearest object which allows text (o[]o) - both',
  1082. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  1083. 'both',
  1084. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  1085. );
  1086. test(
  1087. 'should select nearest object (o[]p) - both',
  1088. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  1089. 'both',
  1090. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1091. );
  1092. test(
  1093. 'should select nearest object which allows text ([]o) - both',
  1094. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  1095. 'both',
  1096. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1097. );
  1098. } );
  1099. function test( testName, data, direction, expected ) {
  1100. it( testName, () => {
  1101. let range;
  1102. model.enqueueChange( 'transparent', () => {
  1103. setData( model, data );
  1104. range = schema.getNearestSelectionRange( selection.anchor, direction );
  1105. } );
  1106. if ( expected === null ) {
  1107. expect( range ).to.be.null;
  1108. } else {
  1109. model.change( writer => {
  1110. writer.setSelection( range );
  1111. } );
  1112. expect( getData( model ) ).to.equal( expected );
  1113. }
  1114. } );
  1115. }
  1116. } );
  1117. describe( 'findAllowedParent()', () => {
  1118. beforeEach( () => {
  1119. schema.register( '$root' );
  1120. schema.register( 'blockQuote', {
  1121. allowIn: '$root'
  1122. } );
  1123. schema.register( 'paragraph', {
  1124. allowIn: 'blockQuote'
  1125. } );
  1126. schema.register( '$text', {
  1127. allowIn: 'paragraph'
  1128. } );
  1129. } );
  1130. it( 'should return position ancestor that allows to insert given node to it', () => {
  1131. const node = new Element( 'paragraph' );
  1132. const allowedParent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1133. expect( allowedParent ).to.equal( r1bQ );
  1134. } );
  1135. it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
  1136. const node = new Text( 'text' );
  1137. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1138. expect( parent ).to.equal( r1bQp );
  1139. } );
  1140. it( 'should return null when limit element is reached before allowed parent', () => {
  1141. schema.extend( 'blockQuote', {
  1142. isLimit: true
  1143. } );
  1144. schema.register( 'div', {
  1145. allowIn: '$root'
  1146. } );
  1147. const node = new Element( 'div' );
  1148. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1149. expect( parent ).to.null;
  1150. } );
  1151. it( 'should return null when object element is reached before allowed parent', () => {
  1152. schema.extend( 'blockQuote', {
  1153. isObject: true
  1154. } );
  1155. schema.register( 'div', {
  1156. allowIn: '$root'
  1157. } );
  1158. const node = new Element( 'div' );
  1159. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1160. expect( parent ).to.null;
  1161. } );
  1162. it( 'should return null when there is no allowed ancestor for given position', () => {
  1163. const node = new Element( 'section' );
  1164. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1165. expect( parent ).to.null;
  1166. } );
  1167. } );
  1168. describe( 'removeDisallowedAttributes()', () => {
  1169. let model, doc, root;
  1170. beforeEach( () => {
  1171. model = new Model();
  1172. doc = model.document;
  1173. root = doc.createRoot();
  1174. schema = model.schema;
  1175. schema.register( 'paragraph', {
  1176. inheritAllFrom: '$block'
  1177. } );
  1178. schema.register( 'div', {
  1179. inheritAllFrom: '$block'
  1180. } );
  1181. schema.register( 'image', {
  1182. isObject: true
  1183. } );
  1184. schema.extend( '$block', {
  1185. allowIn: 'div'
  1186. } );
  1187. } );
  1188. it( 'should filter out disallowed attributes from given nodes', () => {
  1189. schema.extend( '$text', { allowAttributes: 'a' } );
  1190. schema.extend( 'image', { allowAttributes: 'b' } );
  1191. const text = new Text( 'foo', { a: 1, b: 1 } );
  1192. const image = new Element( 'image', { a: 1, b: 1 } );
  1193. root._appendChild( [ text, image ] );
  1194. model.change( writer => {
  1195. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1196. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  1197. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  1198. expect( writer.batch.deltas ).to.length( 2 );
  1199. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  1200. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  1201. expect( getData( model, { withoutSelection: true } ) )
  1202. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  1203. } );
  1204. } );
  1205. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  1206. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1207. // Allow 'a' on div>$text.
  1208. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  1209. return true;
  1210. }
  1211. // Allow 'b' on div>paragraph>$text.
  1212. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  1213. return true;
  1214. }
  1215. // Allow 'a' on div>image.
  1216. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  1217. return true;
  1218. }
  1219. // Allow 'b' on div>paragraph>image.
  1220. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  1221. return true;
  1222. }
  1223. } );
  1224. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1225. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1226. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  1227. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  1228. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  1229. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  1230. root._appendChild( [ div ] );
  1231. model.change( writer => {
  1232. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1233. expect( writer.batch.deltas ).to.length( 4 );
  1234. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  1235. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  1236. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  1237. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  1238. expect( getData( model, { withoutSelection: true } ) )
  1239. .to.equal(
  1240. '<div>' +
  1241. '<paragraph>' +
  1242. '<$text b="1">foo</$text>' +
  1243. '<image b="1"></image>' +
  1244. '</paragraph>' +
  1245. '<$text a="1">bar</$text>' +
  1246. '<image a="1"></image>' +
  1247. '</div>'
  1248. );
  1249. } );
  1250. } );
  1251. } );
  1252. describe( 'definitions compilation', () => {
  1253. describe( 'allowIn cases', () => {
  1254. it( 'passes $root>paragraph', () => {
  1255. schema.register( '$root' );
  1256. schema.register( 'paragraph', {
  1257. allowIn: '$root'
  1258. } );
  1259. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1260. } );
  1261. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1262. schema.register( '$root' );
  1263. schema.register( '$root2' );
  1264. schema.register( 'paragraph', {
  1265. allowIn: [ '$root', '$root2' ]
  1266. } );
  1267. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1268. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1269. } );
  1270. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1271. schema.register( '$root' );
  1272. schema.register( 'paragraph', {
  1273. allowIn: '$root'
  1274. } );
  1275. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1276. } );
  1277. it( 'passes $root>div>div – in case of circular refs', () => {
  1278. schema.register( '$root' );
  1279. schema.register( 'div', {
  1280. allowIn: [ '$root', 'div' ]
  1281. } );
  1282. const div = new Element( 'div' );
  1283. root1._appendChild( div );
  1284. const div2 = new Element( 'div' );
  1285. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1286. } );
  1287. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1288. schema.register( '$root' );
  1289. schema.register( 'div', {
  1290. allowIn: [ '$root', 'div' ]
  1291. } );
  1292. const div = new Element( 'div' );
  1293. root1._appendChild( div );
  1294. expect( schema.checkChild( div, div ) ).to.be.true;
  1295. } );
  1296. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1297. schema.register( '$root' );
  1298. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1299. } );
  1300. it( 'rejects $root>paragraph – registered different item', () => {
  1301. schema.register( '$root' );
  1302. schema.register( 'paragraph' );
  1303. schema.register( 'listItem', {
  1304. allowIn: '$root'
  1305. } );
  1306. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1307. } );
  1308. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1309. schema.register( '$root' );
  1310. schema.register( '$fancyRoot' );
  1311. schema.register( 'paragraph', {
  1312. allowIn: '$fancyRoot'
  1313. } );
  1314. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1315. } );
  1316. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1317. schema.register( '$root' );
  1318. schema.register( 'paragraph', {
  1319. allowIn: '$root'
  1320. } );
  1321. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1322. } );
  1323. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1324. schema.register( '$root' );
  1325. schema.register( 'blockQuote', {
  1326. allowIn: '$root'
  1327. } );
  1328. schema.register( 'paragraph', {
  1329. allowIn: '$root'
  1330. } );
  1331. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1332. } );
  1333. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1334. schema.register( '$root' );
  1335. schema.register( 'paragraph', {
  1336. allowIn: '$root'
  1337. } );
  1338. schema.register( '$text', {
  1339. allowIn: 'paragraph'
  1340. } );
  1341. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1342. } );
  1343. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1344. schema.register( '$root' );
  1345. schema.register( 'blockQuote' );
  1346. schema.register( 'paragraph', {
  1347. allowIn: [ 'blockQuote', '$root' ]
  1348. } );
  1349. schema.register( '$text', {
  1350. allowIn: 'paragraph'
  1351. } );
  1352. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1353. } );
  1354. } );
  1355. describe( 'allowWhere cases', () => {
  1356. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1357. schema.register( '$root' );
  1358. schema.register( '$block', {
  1359. allowIn: '$root'
  1360. } );
  1361. schema.register( 'paragraph', {
  1362. allowWhere: '$block'
  1363. } );
  1364. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1365. } );
  1366. it( 'supports the array syntax', () => {
  1367. schema.register( '$root' );
  1368. schema.register( '$root2' );
  1369. schema.register( '$block', {
  1370. allowIn: '$root'
  1371. } );
  1372. schema.register( '$block2', {
  1373. allowIn: '$root2'
  1374. } );
  1375. schema.register( 'paragraph', {
  1376. allowWhere: [ '$block', '$block2' ]
  1377. } );
  1378. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1379. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1380. } );
  1381. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1382. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1383. schema.register( '$root' );
  1384. schema.register( 'paragraph', {
  1385. allowWhere: '$block'
  1386. } );
  1387. schema.register( '$block', {
  1388. allowIn: '$root'
  1389. } );
  1390. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1391. } );
  1392. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1393. schema.register( '$root' );
  1394. schema.register( '$block', {
  1395. allowIn: '$root'
  1396. } );
  1397. schema.register( '$specialBlock', {
  1398. allowWhere: '$block'
  1399. } );
  1400. schema.register( 'paragraph', {
  1401. allowWhere: '$specialBlock'
  1402. } );
  1403. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1404. } );
  1405. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1406. schema.register( '$root' );
  1407. schema.register( '$block' );
  1408. schema.register( 'paragraph', {
  1409. allowWhere: '$block'
  1410. } );
  1411. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1412. } );
  1413. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1414. schema.register( '$root' );
  1415. schema.register( '$block' );
  1416. schema.register( 'paragraph', {
  1417. allowWhere: '$block'
  1418. } );
  1419. schema.register( '$text', {
  1420. allowIn: 'paragraph'
  1421. } );
  1422. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1423. } );
  1424. } );
  1425. describe( 'allowContentOf cases', () => {
  1426. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1427. schema.register( '$root' );
  1428. schema.register( 'paragraph', {
  1429. allowIn: '$root'
  1430. } );
  1431. schema.register( '$root2', {
  1432. allowContentOf: '$root'
  1433. } );
  1434. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1435. } );
  1436. it( 'supports the array syntax', () => {
  1437. schema.register( '$root' );
  1438. schema.register( '$root2' );
  1439. schema.register( 'paragraph', {
  1440. allowIn: '$root'
  1441. } );
  1442. schema.register( 'heading1', {
  1443. allowIn: '$root2'
  1444. } );
  1445. schema.register( '$root3', {
  1446. allowContentOf: [ '$root', '$root2' ]
  1447. } );
  1448. const root3 = new Element( '$root3' );
  1449. const heading1 = new Element( 'heading1' );
  1450. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1451. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1452. } );
  1453. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1454. schema.register( '$root' );
  1455. schema.register( '$root2', {
  1456. allowContentOf: '$root'
  1457. } );
  1458. schema.register( 'paragraph', {
  1459. allowIn: '$root'
  1460. } );
  1461. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1462. } );
  1463. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1464. schema.register( '$root' );
  1465. schema.register( '$block' );
  1466. schema.register( 'paragraph', {
  1467. allowIn: '$root',
  1468. allowContentOf: '$block'
  1469. } );
  1470. schema.register( '$text', {
  1471. allowIn: '$block'
  1472. } );
  1473. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1474. } );
  1475. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1476. schema.register( '$root' );
  1477. schema.register( 'blockQuote', {
  1478. allowIn: '$root',
  1479. allowContentOf: '$root'
  1480. } );
  1481. schema.register( 'paragraph', {
  1482. allowIn: '$root'
  1483. } );
  1484. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1485. } );
  1486. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1487. schema.register( '$root' );
  1488. schema.register( 'paragraph' );
  1489. schema.register( '$root2', {
  1490. allowContentOf: '$root'
  1491. } );
  1492. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1493. } );
  1494. } );
  1495. describe( 'mix of allowContentOf and allowWhere', () => {
  1496. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1497. schema.register( '$root' );
  1498. schema.register( '$block', {
  1499. allowIn: '$root'
  1500. } );
  1501. schema.register( 'paragraph', {
  1502. allowContentOf: '$block',
  1503. allowWhere: '$block'
  1504. } );
  1505. schema.register( '$text', {
  1506. allowIn: '$block'
  1507. } );
  1508. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1509. } );
  1510. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1511. 'and paragraph inherits allowWhere from $block', () => {
  1512. schema.register( '$root' );
  1513. schema.register( '$root2', {
  1514. allowContentOf: '$root'
  1515. } );
  1516. schema.register( '$block', {
  1517. allowIn: '$root'
  1518. } );
  1519. schema.register( 'paragraph', {
  1520. allowWhere: '$block'
  1521. } );
  1522. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1523. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1524. } );
  1525. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1526. schema.register( 'b' );
  1527. schema.register( 'a', { allowIn: 'b' } );
  1528. schema.register( 'c', { allowContentOf: 'b' } );
  1529. schema.register( 'd', { allowContentOf: 'c' } );
  1530. const d = new Element( 'd' );
  1531. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1532. } );
  1533. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1534. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1535. // as long as the previous iteration found something to compile.
  1536. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1537. // and since we've found something, we'd now try d<-c which would work.
  1538. //
  1539. // We ignore those situations for now as they are very unlikely to happen and would
  1540. // significantly raised the complexity of definition compilation.
  1541. //
  1542. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1543. // schema.register( 'b' );
  1544. // schema.register( 'a', { allowIn: 'b' } );
  1545. // schema.register( 'd', { allowContentOf: 'c' } );
  1546. // schema.register( 'c', { allowContentOf: 'b' } );
  1547. //
  1548. // const d = new Element( 'd' );
  1549. //
  1550. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1551. // } );
  1552. } );
  1553. describe( 'inheritTypesFrom', () => {
  1554. it( 'inherit properties of another item', () => {
  1555. schema.register( '$block', {
  1556. isBlock: true,
  1557. isLimit: true
  1558. } );
  1559. schema.register( 'paragraph', {
  1560. inheritTypesFrom: '$block'
  1561. } );
  1562. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1563. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1564. } );
  1565. it( 'inherit properties of other items – support for arrays', () => {
  1566. schema.register( '$block', {
  1567. isBlock: true
  1568. } );
  1569. schema.register( '$block2', {
  1570. isLimit: true
  1571. } );
  1572. schema.register( 'paragraph', {
  1573. inheritTypesFrom: [ '$block', '$block2' ]
  1574. } );
  1575. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1576. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1577. } );
  1578. it( 'does not override existing props', () => {
  1579. schema.register( '$block', {
  1580. isBlock: true,
  1581. isLimit: true
  1582. } );
  1583. schema.register( 'paragraph', {
  1584. inheritTypesFrom: '$block',
  1585. isLimit: false
  1586. } );
  1587. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1588. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1589. } );
  1590. } );
  1591. describe( 'inheritAllFrom', () => {
  1592. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1593. schema.register( '$root' );
  1594. schema.register( '$block', {
  1595. allowIn: '$root'
  1596. } );
  1597. schema.register( 'paragraph', {
  1598. inheritAllFrom: '$block'
  1599. } );
  1600. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1601. } );
  1602. it( 'paragraph inherit properties of $block', () => {
  1603. schema.register( '$block', {
  1604. isBlock: true
  1605. } );
  1606. schema.register( 'paragraph', {
  1607. inheritAllFrom: '$block'
  1608. } );
  1609. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1610. } );
  1611. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1612. schema.register( '$root' );
  1613. schema.register( '$block', {
  1614. allowIn: '$root'
  1615. } );
  1616. schema.register( '$text', {
  1617. allowIn: '$block'
  1618. } );
  1619. schema.register( 'paragraph', {
  1620. inheritAllFrom: '$block'
  1621. } );
  1622. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1623. } );
  1624. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1625. schema.register( '$root' );
  1626. schema.register( '$blockProto', {
  1627. allowIn: '$root'
  1628. } );
  1629. schema.register( '$block', {
  1630. allowWhere: '$blockProto'
  1631. } );
  1632. schema.register( 'paragraph', {
  1633. inheritAllFrom: '$block'
  1634. } );
  1635. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1636. } );
  1637. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1638. schema.register( '$root' );
  1639. schema.register( '$blockProto' );
  1640. schema.register( '$block', {
  1641. allowContentOf: '$blockProto',
  1642. allowIn: '$root'
  1643. } );
  1644. schema.register( '$text', {
  1645. allowIn: '$blockProto'
  1646. } );
  1647. schema.register( 'paragraph', {
  1648. inheritAllFrom: '$block'
  1649. } );
  1650. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1651. } );
  1652. } );
  1653. // We need to handle cases where some independent features registered definitions which might use
  1654. // optional elements (elements which might not have been registered).
  1655. describe( 'missing structure definitions', () => {
  1656. it( 'does not break when trying to check a child which is not registered', () => {
  1657. schema.register( '$root' );
  1658. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1659. } );
  1660. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1661. const foo404 = new Element( 'foo404' );
  1662. root1._appendChild( foo404 );
  1663. schema.register( '$root' );
  1664. schema.register( '$text', {
  1665. allowIn: '$root'
  1666. } );
  1667. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1668. } );
  1669. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1670. schema.register( '$root' );
  1671. schema.register( '$text', {
  1672. allowIn: 'foo404'
  1673. } );
  1674. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1675. } );
  1676. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1677. schema.register( '$root' );
  1678. schema.register( '$text', {
  1679. allowWhere: 'foo404'
  1680. } );
  1681. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1682. } );
  1683. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1684. schema.register( '$root', {
  1685. allowContentOf: 'foo404'
  1686. } );
  1687. schema.register( '$text', {
  1688. allowIn: '$root'
  1689. } );
  1690. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1691. } );
  1692. it( 'checks whether allowIn uses a registered element', () => {
  1693. schema.register( 'paragraph', {
  1694. allowIn: '$root'
  1695. } );
  1696. // $root isn't registered!
  1697. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1698. } );
  1699. it( 'does not break when inheriting all from an non-registered element', () => {
  1700. schema.register( 'paragraph', {
  1701. inheritAllFrom: '$block'
  1702. } );
  1703. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1704. } );
  1705. } );
  1706. describe( 'allowAttributes', () => {
  1707. it( 'passes paragraph[align]', () => {
  1708. schema.register( 'paragraph', {
  1709. allowAttributes: 'align'
  1710. } );
  1711. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1712. } );
  1713. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1714. schema.register( 'paragraph', {
  1715. allowAttributes: [ 'align', 'dir' ]
  1716. } );
  1717. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1718. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1719. } );
  1720. it( 'passes paragraph>$text[bold]', () => {
  1721. schema.register( 'paragraph' );
  1722. schema.register( '$text', {
  1723. allowIn: 'paragraph',
  1724. allowAttributes: 'bold'
  1725. } );
  1726. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1727. } );
  1728. } );
  1729. describe( 'allowAttributesOf', () => {
  1730. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1731. schema.register( '$block', {
  1732. allowAttributes: 'align'
  1733. } );
  1734. schema.register( 'paragraph', {
  1735. allowAttributesOf: '$block'
  1736. } );
  1737. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1738. } );
  1739. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1740. schema.register( '$block', {
  1741. allowAttributes: 'align'
  1742. } );
  1743. schema.register( '$block2', {
  1744. allowAttributes: 'dir'
  1745. } );
  1746. schema.register( 'paragraph', {
  1747. allowAttributesOf: [ '$block', '$block2' ]
  1748. } );
  1749. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1750. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1751. } );
  1752. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1753. schema.register( '$block', {
  1754. allowAttributes: 'align'
  1755. } );
  1756. schema.register( 'paragraph', {
  1757. allowAttributes: 'dir',
  1758. allowAttributesOf: '$block'
  1759. } );
  1760. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1761. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1762. } );
  1763. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1764. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1765. } );
  1766. describe( 'inheritAllFrom', () => {
  1767. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1768. schema.register( '$block', {
  1769. allowAttributes: 'align'
  1770. } );
  1771. schema.register( 'paragraph', {
  1772. inheritAllFrom: '$block'
  1773. } );
  1774. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1775. } );
  1776. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1777. schema.register( '$blockProto', {
  1778. allowAttributes: 'align'
  1779. } );
  1780. schema.register( '$block', {
  1781. allowAttributesOf: '$blockProto'
  1782. } );
  1783. schema.register( 'paragraph', {
  1784. inheritAllFrom: '$block'
  1785. } );
  1786. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1787. } );
  1788. } );
  1789. describe( 'missing attribute definitions', () => {
  1790. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1791. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1792. } );
  1793. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1794. schema.register( 'paragraph', {
  1795. allowAttributesOf: '$block'
  1796. } );
  1797. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1798. } );
  1799. it( 'does not crash when inheriting all from a non-registered element', () => {
  1800. schema.register( 'paragraph', {
  1801. allowAttributesOf: '$block'
  1802. } );
  1803. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1804. } );
  1805. } );
  1806. describe( 'missing types definitions', () => {
  1807. it( 'does not crash when inheriting types of an non-registered element', () => {
  1808. schema.register( 'paragraph', {
  1809. inheritTypesFrom: '$block'
  1810. } );
  1811. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1812. } );
  1813. } );
  1814. } );
  1815. describe( 'real scenarios', () => {
  1816. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1817. const definitions = [
  1818. () => {
  1819. schema.register( 'paragraph', {
  1820. inheritAllFrom: '$block'
  1821. } );
  1822. },
  1823. () => {
  1824. schema.register( 'heading1', {
  1825. inheritAllFrom: '$block'
  1826. } );
  1827. },
  1828. () => {
  1829. schema.register( 'listItem', {
  1830. inheritAllFrom: '$block',
  1831. allowAttributes: [ 'indent', 'type' ]
  1832. } );
  1833. },
  1834. () => {
  1835. schema.register( 'blockQuote', {
  1836. allowWhere: '$block',
  1837. allowContentOf: '$root'
  1838. } );
  1839. // Disallow blockQuote in blockQuote.
  1840. schema.addChildCheck( ( ctx, childDef ) => {
  1841. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1842. return false;
  1843. }
  1844. } );
  1845. },
  1846. () => {
  1847. schema.register( 'image', {
  1848. allowWhere: '$block',
  1849. allowAttributes: [ 'src', 'alt' ],
  1850. isObject: true,
  1851. isBlock: true
  1852. } );
  1853. },
  1854. () => {
  1855. schema.register( 'caption', {
  1856. allowIn: 'image',
  1857. allowContentOf: '$block',
  1858. isLimit: true
  1859. } );
  1860. },
  1861. () => {
  1862. schema.extend( '$text', {
  1863. allowAttributes: [ 'bold', 'italic' ]
  1864. } );
  1865. // Disallow bold in heading1.
  1866. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1867. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1868. return false;
  1869. }
  1870. } );
  1871. },
  1872. () => {
  1873. schema.extend( '$block', {
  1874. allowAttributes: 'alignment'
  1875. } );
  1876. }
  1877. ];
  1878. beforeEach( () => {
  1879. schema.register( '$root', {
  1880. isLimit: true
  1881. } );
  1882. schema.register( '$block', {
  1883. allowIn: '$root',
  1884. isBlock: true
  1885. } );
  1886. schema.register( '$text', {
  1887. allowIn: '$block'
  1888. } );
  1889. for ( const definition of definitions ) {
  1890. definition();
  1891. }
  1892. // or...
  1893. //
  1894. // Use the below code to shuffle the definitions.
  1895. // Don't look here, Szymon!
  1896. //
  1897. // const definitionsCopy = definitions.slice();
  1898. //
  1899. // while ( definitionsCopy.length ) {
  1900. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1901. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1902. // }
  1903. root1 = new Element( '$root', null, [
  1904. new Element( 'paragraph', null, 'foo' ),
  1905. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1906. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1907. new Element( 'heading1', null, 'foo' ),
  1908. new Element( 'blockQuote', null, [
  1909. new Element( 'paragraph', null, 'foo' ),
  1910. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1911. new Element( 'image', null, [
  1912. new Element( 'caption', null, 'foo' )
  1913. ] )
  1914. ] ),
  1915. new Element( 'image', null, [
  1916. new Element( 'caption', null, 'foo' )
  1917. ] )
  1918. ] );
  1919. r1p1 = root1.getChild( 0 );
  1920. r1p2 = root1.getChild( 1 );
  1921. r1lI = root1.getChild( 2 );
  1922. r1h = root1.getChild( 3 );
  1923. r1bQ = root1.getChild( 4 );
  1924. r1i = root1.getChild( 5 );
  1925. r1bQp = r1bQ.getChild( 0 );
  1926. r1bQlI = r1bQ.getChild( 1 );
  1927. r1bQi = r1bQ.getChild( 2 );
  1928. } );
  1929. it( 'passes $root>paragraph', () => {
  1930. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1931. } );
  1932. it( 'passes $root>paragraph>$text', () => {
  1933. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1934. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1935. } );
  1936. it( 'passes $root>listItem', () => {
  1937. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1938. } );
  1939. it( 'passes $root>listItem>$text', () => {
  1940. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1941. } );
  1942. it( 'passes $root>blockQuote>paragraph', () => {
  1943. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1944. } );
  1945. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1946. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1947. } );
  1948. it( 'passes $root>blockQuote>listItem', () => {
  1949. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1950. } );
  1951. it( 'passes $root>blockQuote>listItem>$text', () => {
  1952. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1953. } );
  1954. it( 'passes $root>blockQuote>image', () => {
  1955. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1956. } );
  1957. it( 'passes $root>blockQuote>image>caption', () => {
  1958. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1959. } );
  1960. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1961. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1962. } );
  1963. it( 'passes $root>image', () => {
  1964. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1965. } );
  1966. it( 'passes $root>image>caption', () => {
  1967. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1968. } );
  1969. it( 'passes $root>image>caption>$text', () => {
  1970. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1971. } );
  1972. it( 'rejects $root>$root', () => {
  1973. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1974. } );
  1975. it( 'rejects $root>$text', () => {
  1976. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1977. } );
  1978. it( 'rejects $root>caption', () => {
  1979. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1980. } );
  1981. it( 'rejects $root>paragraph>paragraph', () => {
  1982. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1983. } );
  1984. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1985. // Edge case because p>p should not exist in the first place.
  1986. // But it's good to know that it blocks also this.
  1987. const p = new Element( 'p' );
  1988. r1p1._appendChild( p );
  1989. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1990. } );
  1991. it( 'rejects $root>paragraph>$block', () => {
  1992. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1993. } );
  1994. it( 'rejects $root>paragraph>blockQuote', () => {
  1995. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1996. } );
  1997. it( 'rejects $root>paragraph>image', () => {
  1998. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1999. } );
  2000. it( 'rejects $root>paragraph>caption', () => {
  2001. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  2002. } );
  2003. it( 'rejects $root>blockQuote>blockQuote', () => {
  2004. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  2005. } );
  2006. it( 'rejects $root>blockQuote>caption', () => {
  2007. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2008. } );
  2009. it( 'rejects $root>blockQuote>$text', () => {
  2010. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  2011. } );
  2012. it( 'rejects $root>image>$text', () => {
  2013. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  2014. } );
  2015. it( 'rejects $root>image>paragraph', () => {
  2016. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  2017. } );
  2018. it( 'rejects $root>image>caption>paragraph', () => {
  2019. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  2020. } );
  2021. it( 'rejects $root>image>caption>blockQuote', () => {
  2022. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  2023. } );
  2024. it( 'accepts attribute $root>paragraph[alignment]', () => {
  2025. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  2026. } );
  2027. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  2028. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2029. } );
  2030. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  2031. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  2032. } );
  2033. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  2034. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  2035. } );
  2036. it( 'accepts attribute $root>listItem[alignment]', () => {
  2037. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  2038. } );
  2039. it( 'accepts attribute $root>listItem[indent]', () => {
  2040. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  2041. } );
  2042. it( 'accepts attribute $root>listItem[type]', () => {
  2043. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  2044. } );
  2045. it( 'accepts attribute $root>image[src]', () => {
  2046. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  2047. } );
  2048. it( 'accepts attribute $root>image[alt]', () => {
  2049. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  2050. } );
  2051. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  2052. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  2053. } );
  2054. it( 'rejects attribute $root[indent]', () => {
  2055. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  2056. } );
  2057. it( 'rejects attribute $root>paragraph[indent]', () => {
  2058. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  2059. } );
  2060. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  2061. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  2062. } );
  2063. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  2064. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  2065. } );
  2066. it( 'rejects attribute $root>blockQuote[indent]', () => {
  2067. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  2068. } );
  2069. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  2070. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  2071. } );
  2072. it( 'rejects attribute $root>image[indent]', () => {
  2073. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  2074. } );
  2075. it( 'rejects attribute $root>image[alignment]', () => {
  2076. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  2077. } );
  2078. it( '$root is limit', () => {
  2079. expect( schema.isLimit( '$root' ) ).to.be.true;
  2080. expect( schema.isBlock( '$root' ) ).to.be.false;
  2081. expect( schema.isObject( '$root' ) ).to.be.false;
  2082. } );
  2083. it( 'paragraph is block', () => {
  2084. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  2085. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  2086. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  2087. } );
  2088. it( 'heading1 is block', () => {
  2089. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  2090. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  2091. expect( schema.isObject( 'heading1' ) ).to.be.false;
  2092. } );
  2093. it( 'listItem is block', () => {
  2094. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  2095. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  2096. expect( schema.isObject( 'listItem' ) ).to.be.false;
  2097. } );
  2098. it( 'image is block object', () => {
  2099. expect( schema.isLimit( 'image' ) ).to.be.false;
  2100. expect( schema.isBlock( 'image' ) ).to.be.true;
  2101. expect( schema.isObject( 'image' ) ).to.be.true;
  2102. } );
  2103. it( 'caption is limit', () => {
  2104. expect( schema.isLimit( 'caption' ) ).to.be.true;
  2105. expect( schema.isBlock( 'caption' ) ).to.be.false;
  2106. expect( schema.isObject( 'caption' ) ).to.be.false;
  2107. } );
  2108. } );
  2109. } );
  2110. describe( 'SchemaContext', () => {
  2111. let root;
  2112. beforeEach( () => {
  2113. root = new Element( '$root', null, [
  2114. new Element( 'blockQuote', { foo: 1 }, [
  2115. new Element( 'paragraph', { align: 'left' }, [
  2116. new Text( 'foo', { bold: true, italic: true } )
  2117. ] )
  2118. ] )
  2119. ] );
  2120. } );
  2121. describe( 'constructor()', () => {
  2122. it( 'creates context based on an array of strings', () => {
  2123. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2124. expect( ctx.length ).to.equal( 3 );
  2125. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2126. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  2127. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2128. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2129. } );
  2130. it( 'creates context based on an array of elements', () => {
  2131. const blockQuote = root.getChild( 0 );
  2132. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2133. const ctx = new SchemaContext( [ blockQuote, text ] );
  2134. expect( ctx.length ).to.equal( 2 );
  2135. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  2136. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  2137. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2138. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  2139. } );
  2140. it( 'creates context based on a mixed array of strings and elements', () => {
  2141. const blockQuote = root.getChild( 0 );
  2142. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2143. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  2144. expect( ctx.length ).to.equal( 3 );
  2145. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  2146. } );
  2147. it( 'creates context based on a root element', () => {
  2148. const ctx = new SchemaContext( root );
  2149. expect( ctx.length ).to.equal( 1 );
  2150. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  2151. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2152. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2153. } );
  2154. it( 'creates context based on a nested element', () => {
  2155. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  2156. expect( ctx.length ).to.equal( 3 );
  2157. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2158. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2159. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  2160. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  2161. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  2162. } );
  2163. it( 'creates context based on a text node', () => {
  2164. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  2165. expect( ctx.length ).to.equal( 4 );
  2166. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2167. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2168. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2169. } );
  2170. it( 'creates context based on a text proxy', () => {
  2171. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  2172. const textProxy = new TextProxy( text, 0, 1 );
  2173. const ctx = new SchemaContext( textProxy );
  2174. expect( ctx.length ).to.equal( 4 );
  2175. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2176. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2177. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2178. } );
  2179. it( 'creates context based on a position', () => {
  2180. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  2181. const ctx = new SchemaContext( pos );
  2182. expect( ctx.length ).to.equal( 3 );
  2183. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2184. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  2185. } );
  2186. it( 'creates context based on a string', () => {
  2187. const ctx = new SchemaContext( 'paragraph' );
  2188. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2189. } );
  2190. it( 'creates context based on a SchemaContext instance', () => {
  2191. const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2192. const ctx = new SchemaContext( previousCtx );
  2193. expect( ctx ).to.equal( previousCtx );
  2194. } );
  2195. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  2196. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  2197. expect( ctx.length ).to.equal( 1 );
  2198. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2199. } );
  2200. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  2201. const p = new Element( 'paragraph' );
  2202. const docFrag = new DocumentFragment();
  2203. docFrag._appendChild( p );
  2204. const ctx = new SchemaContext( p );
  2205. expect( ctx.length ).to.equal( 1 );
  2206. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2207. } );
  2208. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  2209. const p = new Element( 'paragraph' );
  2210. const docFrag = new DocumentFragment();
  2211. docFrag._appendChild( p );
  2212. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  2213. expect( ctx.length ).to.equal( 1 );
  2214. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2215. } );
  2216. } );
  2217. describe( 'length', () => {
  2218. it( 'gets the number of items', () => {
  2219. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2220. expect( ctx.length ).to.equal( 3 );
  2221. } );
  2222. } );
  2223. describe( 'last', () => {
  2224. it( 'gets the last item', () => {
  2225. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2226. expect( ctx.last ).to.be.an( 'object' );
  2227. expect( ctx.last.name ).to.equal( 'c' );
  2228. } );
  2229. } );
  2230. describe( 'Symbol.iterator', () => {
  2231. it( 'exists', () => {
  2232. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2233. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  2234. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2235. } );
  2236. } );
  2237. describe( 'getItem()', () => {
  2238. it( 'returns item by index', () => {
  2239. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2240. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2241. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2242. } );
  2243. it( 'returns undefined if index exceeds the range', () => {
  2244. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2245. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2246. } );
  2247. } );
  2248. describe( 'push()', () => {
  2249. it( 'creates new SchemaContext instance with new item - #string', () => {
  2250. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2251. const newCtx = ctx.push( 'd' );
  2252. expect( newCtx ).to.instanceof( SchemaContext );
  2253. expect( newCtx ).to.not.equal( ctx );
  2254. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2255. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2256. } );
  2257. it( 'creates new SchemaContext instance with new item - #text', () => {
  2258. const node = new Text( 'd' );
  2259. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2260. const newCtx = ctx.push( node );
  2261. expect( newCtx ).to.instanceof( SchemaContext );
  2262. expect( newCtx ).to.not.equal( ctx );
  2263. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2264. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2265. } );
  2266. it( 'creates new SchemaContext instance with new item - #element', () => {
  2267. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2268. const parent = new Element( 'parent', null, new Element( 'd' ) );
  2269. const newCtx = ctx.push( parent.getChild( 0 ) );
  2270. expect( newCtx ).to.instanceof( SchemaContext );
  2271. expect( newCtx ).to.not.equal( ctx );
  2272. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2273. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2274. } );
  2275. } );
  2276. describe( 'getNames()', () => {
  2277. it( 'returns an iterator', () => {
  2278. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2279. expect( ctx.getNames().next ).to.be.a( 'function' );
  2280. } );
  2281. it( 'returns an iterator which returns all item names', () => {
  2282. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2283. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2284. } );
  2285. } );
  2286. describe( 'endsWith()', () => {
  2287. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2288. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2289. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2290. } );
  2291. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2292. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2293. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2294. } );
  2295. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2296. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2297. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2298. } );
  2299. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2300. const ctx = new SchemaContext( [ 'foo' ] );
  2301. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2302. } );
  2303. it( 'returns true if not only the end of the context matches the query', () => {
  2304. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2305. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2306. } );
  2307. it( 'returns false if query matches the middle of the context', () => {
  2308. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2309. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2310. } );
  2311. it( 'returns false if query matches the start of the context', () => {
  2312. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2313. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2314. } );
  2315. it( 'returns false if query does not match', () => {
  2316. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2317. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2318. } );
  2319. it( 'returns false if query is longer than context', () => {
  2320. const ctx = new SchemaContext( [ 'foo' ] );
  2321. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2322. } );
  2323. } );
  2324. } );