8
0

schema.js 78 KB

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