schema.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  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. it( 'should use custom limit checker nad return null if returns true', () => {
  926. // $root is allowed ancestor for blockQuote.
  927. const node = new Element( 'blockQuote' );
  928. const parent = schema.findAllowedParent(
  929. node,
  930. Position.createAt( r1bQp ),
  931. // However lest stop searching when blockQuote is reached.
  932. element => element.name == 'blockQuote'
  933. );
  934. expect( parent ).to.null;
  935. } );
  936. } );
  937. describe( 'removeDisallowedAttributes()', () => {
  938. let model, doc, root;
  939. beforeEach( () => {
  940. model = new Model();
  941. doc = model.document;
  942. root = doc.createRoot();
  943. schema = model.schema;
  944. schema.register( 'paragraph', {
  945. inheritAllFrom: '$block'
  946. } );
  947. schema.register( 'div', {
  948. inheritAllFrom: '$block'
  949. } );
  950. schema.register( 'image', {
  951. isObject: true
  952. } );
  953. schema.extend( '$block', {
  954. allowIn: 'div'
  955. } );
  956. } );
  957. it( 'should filter out disallowed attributes from given nodes', () => {
  958. schema.extend( '$text', { allowAttributes: 'a' } );
  959. schema.extend( 'image', { allowAttributes: 'b' } );
  960. const text = new Text( 'foo', { a: 1, b: 1 } );
  961. const image = new Element( 'image', { a: 1, b: 1 } );
  962. root.appendChildren( [ text, image ] );
  963. model.change( writer => {
  964. schema.removeDisallowedAttributes( root.getChildren(), writer );
  965. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  966. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  967. expect( writer.batch.deltas ).to.length( 2 );
  968. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  969. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  970. expect( getData( model, { withoutSelection: true } ) )
  971. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  972. } );
  973. } );
  974. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  975. schema.addAttributeCheck( ( ctx, attributeName ) => {
  976. // Allow 'a' on div>$text.
  977. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  978. return true;
  979. }
  980. // Allow 'b' on div>paragraph>$text.
  981. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  982. return true;
  983. }
  984. // Allow 'a' on div>image.
  985. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  986. return true;
  987. }
  988. // Allow 'b' on div>paragraph>image.
  989. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  990. return true;
  991. }
  992. } );
  993. const foo = new Text( 'foo', { a: 1, b: 1 } );
  994. const bar = new Text( 'bar', { a: 1, b: 1 } );
  995. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  996. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  997. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  998. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  999. root.appendChildren( [ div ] );
  1000. model.change( writer => {
  1001. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1002. expect( writer.batch.deltas ).to.length( 4 );
  1003. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  1004. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  1005. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  1006. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  1007. expect( getData( model, { withoutSelection: true } ) )
  1008. .to.equal(
  1009. '<div>' +
  1010. '<paragraph>' +
  1011. '<$text b="1">foo</$text>' +
  1012. '<image b="1"></image>' +
  1013. '</paragraph>' +
  1014. '<$text a="1">bar</$text>' +
  1015. '<image a="1"></image>' +
  1016. '</div>'
  1017. );
  1018. } );
  1019. } );
  1020. } );
  1021. describe( 'definitions compilation', () => {
  1022. describe( 'allowIn cases', () => {
  1023. it( 'passes $root>paragraph', () => {
  1024. schema.register( '$root' );
  1025. schema.register( 'paragraph', {
  1026. allowIn: '$root'
  1027. } );
  1028. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1029. } );
  1030. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1031. schema.register( '$root' );
  1032. schema.register( '$root2' );
  1033. schema.register( 'paragraph', {
  1034. allowIn: [ '$root', '$root2' ]
  1035. } );
  1036. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1037. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1038. } );
  1039. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1040. schema.register( '$root' );
  1041. schema.register( 'paragraph', {
  1042. allowIn: '$root'
  1043. } );
  1044. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1045. } );
  1046. it( 'passes $root>div>div – in case of circular refs', () => {
  1047. schema.register( '$root' );
  1048. schema.register( 'div', {
  1049. allowIn: [ '$root', 'div' ]
  1050. } );
  1051. const div = new Element( 'div' );
  1052. root1.appendChildren( div );
  1053. const div2 = new Element( 'div' );
  1054. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1055. } );
  1056. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1057. schema.register( '$root' );
  1058. schema.register( 'div', {
  1059. allowIn: [ '$root', 'div' ]
  1060. } );
  1061. const div = new Element( 'div' );
  1062. root1.appendChildren( div );
  1063. expect( schema.checkChild( div, div ) ).to.be.true;
  1064. } );
  1065. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1066. schema.register( '$root' );
  1067. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1068. } );
  1069. it( 'rejects $root>paragraph – registered different item', () => {
  1070. schema.register( '$root' );
  1071. schema.register( 'paragraph' );
  1072. schema.register( 'listItem', {
  1073. allowIn: '$root'
  1074. } );
  1075. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1076. } );
  1077. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1078. schema.register( '$root' );
  1079. schema.register( '$fancyRoot' );
  1080. schema.register( 'paragraph', {
  1081. allowIn: '$fancyRoot'
  1082. } );
  1083. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1084. } );
  1085. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1086. schema.register( '$root' );
  1087. schema.register( 'paragraph', {
  1088. allowIn: '$root'
  1089. } );
  1090. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1091. } );
  1092. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1093. schema.register( '$root' );
  1094. schema.register( 'blockQuote', {
  1095. allowIn: '$root'
  1096. } );
  1097. schema.register( 'paragraph', {
  1098. allowIn: '$root'
  1099. } );
  1100. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1101. } );
  1102. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1103. schema.register( '$root' );
  1104. schema.register( 'paragraph', {
  1105. allowIn: '$root'
  1106. } );
  1107. schema.register( '$text', {
  1108. allowIn: 'paragraph'
  1109. } );
  1110. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1111. } );
  1112. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1113. schema.register( '$root' );
  1114. schema.register( 'blockQuote' );
  1115. schema.register( 'paragraph', {
  1116. allowIn: [ 'blockQuote', '$root' ]
  1117. } );
  1118. schema.register( '$text', {
  1119. allowIn: 'paragraph'
  1120. } );
  1121. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1122. } );
  1123. } );
  1124. describe( 'allowWhere cases', () => {
  1125. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1126. schema.register( '$root' );
  1127. schema.register( '$block', {
  1128. allowIn: '$root'
  1129. } );
  1130. schema.register( 'paragraph', {
  1131. allowWhere: '$block'
  1132. } );
  1133. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1134. } );
  1135. it( 'supports the array syntax', () => {
  1136. schema.register( '$root' );
  1137. schema.register( '$root2' );
  1138. schema.register( '$block', {
  1139. allowIn: '$root'
  1140. } );
  1141. schema.register( '$block2', {
  1142. allowIn: '$root2'
  1143. } );
  1144. schema.register( 'paragraph', {
  1145. allowWhere: [ '$block', '$block2' ]
  1146. } );
  1147. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1148. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1149. } );
  1150. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1151. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1152. schema.register( '$root' );
  1153. schema.register( 'paragraph', {
  1154. allowWhere: '$block'
  1155. } );
  1156. schema.register( '$block', {
  1157. allowIn: '$root'
  1158. } );
  1159. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1160. } );
  1161. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1162. schema.register( '$root' );
  1163. schema.register( '$block', {
  1164. allowIn: '$root'
  1165. } );
  1166. schema.register( '$specialBlock', {
  1167. allowWhere: '$block'
  1168. } );
  1169. schema.register( 'paragraph', {
  1170. allowWhere: '$specialBlock'
  1171. } );
  1172. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1173. } );
  1174. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1175. schema.register( '$root' );
  1176. schema.register( '$block' );
  1177. schema.register( 'paragraph', {
  1178. allowWhere: '$block'
  1179. } );
  1180. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1181. } );
  1182. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1183. schema.register( '$root' );
  1184. schema.register( '$block' );
  1185. schema.register( 'paragraph', {
  1186. allowWhere: '$block'
  1187. } );
  1188. schema.register( '$text', {
  1189. allowIn: 'paragraph'
  1190. } );
  1191. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1192. } );
  1193. } );
  1194. describe( 'allowContentOf cases', () => {
  1195. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1196. schema.register( '$root' );
  1197. schema.register( 'paragraph', {
  1198. allowIn: '$root'
  1199. } );
  1200. schema.register( '$root2', {
  1201. allowContentOf: '$root'
  1202. } );
  1203. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1204. } );
  1205. it( 'supports the array syntax', () => {
  1206. schema.register( '$root' );
  1207. schema.register( '$root2' );
  1208. schema.register( 'paragraph', {
  1209. allowIn: '$root'
  1210. } );
  1211. schema.register( 'heading1', {
  1212. allowIn: '$root2'
  1213. } );
  1214. schema.register( '$root3', {
  1215. allowContentOf: [ '$root', '$root2' ]
  1216. } );
  1217. const root3 = new Element( '$root3' );
  1218. const heading1 = new Element( 'heading1' );
  1219. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1220. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1221. } );
  1222. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1223. schema.register( '$root' );
  1224. schema.register( '$root2', {
  1225. allowContentOf: '$root'
  1226. } );
  1227. schema.register( 'paragraph', {
  1228. allowIn: '$root'
  1229. } );
  1230. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1231. } );
  1232. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1233. schema.register( '$root' );
  1234. schema.register( '$block' );
  1235. schema.register( 'paragraph', {
  1236. allowIn: '$root',
  1237. allowContentOf: '$block'
  1238. } );
  1239. schema.register( '$text', {
  1240. allowIn: '$block'
  1241. } );
  1242. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1243. } );
  1244. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1245. schema.register( '$root' );
  1246. schema.register( 'blockQuote', {
  1247. allowIn: '$root',
  1248. allowContentOf: '$root'
  1249. } );
  1250. schema.register( 'paragraph', {
  1251. allowIn: '$root'
  1252. } );
  1253. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1254. } );
  1255. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1256. schema.register( '$root' );
  1257. schema.register( 'paragraph' );
  1258. schema.register( '$root2', {
  1259. allowContentOf: '$root'
  1260. } );
  1261. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1262. } );
  1263. } );
  1264. describe( 'mix of allowContentOf and allowWhere', () => {
  1265. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1266. schema.register( '$root' );
  1267. schema.register( '$block', {
  1268. allowIn: '$root'
  1269. } );
  1270. schema.register( 'paragraph', {
  1271. allowContentOf: '$block',
  1272. allowWhere: '$block'
  1273. } );
  1274. schema.register( '$text', {
  1275. allowIn: '$block'
  1276. } );
  1277. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1278. } );
  1279. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1280. 'and paragraph inherits allowWhere from $block', () => {
  1281. schema.register( '$root' );
  1282. schema.register( '$root2', {
  1283. allowContentOf: '$root'
  1284. } );
  1285. schema.register( '$block', {
  1286. allowIn: '$root'
  1287. } );
  1288. schema.register( 'paragraph', {
  1289. allowWhere: '$block'
  1290. } );
  1291. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1292. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1293. } );
  1294. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1295. schema.register( 'b' );
  1296. schema.register( 'a', { allowIn: 'b' } );
  1297. schema.register( 'c', { allowContentOf: 'b' } );
  1298. schema.register( 'd', { allowContentOf: 'c' } );
  1299. const d = new Element( 'd' );
  1300. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1301. } );
  1302. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1303. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1304. // as long as the previous iteration found something to compile.
  1305. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1306. // and since we've found something, we'd now try d<-c which would work.
  1307. //
  1308. // We ignore those situations for now as they are very unlikely to happen and would
  1309. // significantly raised the complexity of definition compilation.
  1310. //
  1311. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1312. // schema.register( 'b' );
  1313. // schema.register( 'a', { allowIn: 'b' } );
  1314. // schema.register( 'd', { allowContentOf: 'c' } );
  1315. // schema.register( 'c', { allowContentOf: 'b' } );
  1316. //
  1317. // const d = new Element( 'd' );
  1318. //
  1319. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1320. // } );
  1321. } );
  1322. describe( 'inheritTypesFrom', () => {
  1323. it( 'inherit properties of another item', () => {
  1324. schema.register( '$block', {
  1325. isBlock: true,
  1326. isLimit: true
  1327. } );
  1328. schema.register( 'paragraph', {
  1329. inheritTypesFrom: '$block'
  1330. } );
  1331. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1332. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1333. } );
  1334. it( 'inherit properties of other items – support for arrays', () => {
  1335. schema.register( '$block', {
  1336. isBlock: true
  1337. } );
  1338. schema.register( '$block2', {
  1339. isLimit: true
  1340. } );
  1341. schema.register( 'paragraph', {
  1342. inheritTypesFrom: [ '$block', '$block2' ]
  1343. } );
  1344. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1345. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1346. } );
  1347. it( 'does not override existing props', () => {
  1348. schema.register( '$block', {
  1349. isBlock: true,
  1350. isLimit: true
  1351. } );
  1352. schema.register( 'paragraph', {
  1353. inheritTypesFrom: '$block',
  1354. isLimit: false
  1355. } );
  1356. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1357. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1358. } );
  1359. } );
  1360. describe( 'inheritAllFrom', () => {
  1361. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1362. schema.register( '$root' );
  1363. schema.register( '$block', {
  1364. allowIn: '$root'
  1365. } );
  1366. schema.register( 'paragraph', {
  1367. inheritAllFrom: '$block'
  1368. } );
  1369. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1370. } );
  1371. it( 'paragraph inherit properties of $block', () => {
  1372. schema.register( '$block', {
  1373. isBlock: true
  1374. } );
  1375. schema.register( 'paragraph', {
  1376. inheritAllFrom: '$block'
  1377. } );
  1378. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1379. } );
  1380. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1381. schema.register( '$root' );
  1382. schema.register( '$block', {
  1383. allowIn: '$root'
  1384. } );
  1385. schema.register( '$text', {
  1386. allowIn: '$block'
  1387. } );
  1388. schema.register( 'paragraph', {
  1389. inheritAllFrom: '$block'
  1390. } );
  1391. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1392. } );
  1393. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1394. schema.register( '$root' );
  1395. schema.register( '$blockProto', {
  1396. allowIn: '$root'
  1397. } );
  1398. schema.register( '$block', {
  1399. allowWhere: '$blockProto'
  1400. } );
  1401. schema.register( 'paragraph', {
  1402. inheritAllFrom: '$block'
  1403. } );
  1404. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1405. } );
  1406. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1407. schema.register( '$root' );
  1408. schema.register( '$blockProto' );
  1409. schema.register( '$block', {
  1410. allowContentOf: '$blockProto',
  1411. allowIn: '$root'
  1412. } );
  1413. schema.register( '$text', {
  1414. allowIn: '$blockProto'
  1415. } );
  1416. schema.register( 'paragraph', {
  1417. inheritAllFrom: '$block'
  1418. } );
  1419. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1420. } );
  1421. } );
  1422. // We need to handle cases where some independent features registered definitions which might use
  1423. // optional elements (elements which might not have been registered).
  1424. describe( 'missing structure definitions', () => {
  1425. it( 'does not break when trying to check a child which is not registered', () => {
  1426. schema.register( '$root' );
  1427. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1428. } );
  1429. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1430. const foo404 = new Element( 'foo404' );
  1431. root1.appendChildren( foo404 );
  1432. schema.register( '$root' );
  1433. schema.register( '$text', {
  1434. allowIn: '$root'
  1435. } );
  1436. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1437. } );
  1438. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1439. schema.register( '$root' );
  1440. schema.register( '$text', {
  1441. allowIn: 'foo404'
  1442. } );
  1443. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1444. } );
  1445. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1446. schema.register( '$root' );
  1447. schema.register( '$text', {
  1448. allowWhere: 'foo404'
  1449. } );
  1450. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1451. } );
  1452. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1453. schema.register( '$root', {
  1454. allowContentOf: 'foo404'
  1455. } );
  1456. schema.register( '$text', {
  1457. allowIn: '$root'
  1458. } );
  1459. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1460. } );
  1461. it( 'checks whether allowIn uses a registered element', () => {
  1462. schema.register( 'paragraph', {
  1463. allowIn: '$root'
  1464. } );
  1465. // $root isn't registered!
  1466. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1467. } );
  1468. it( 'does not break when inheriting all from an non-registered element', () => {
  1469. schema.register( 'paragraph', {
  1470. inheritAllFrom: '$block'
  1471. } );
  1472. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1473. } );
  1474. } );
  1475. describe( 'allowAttributes', () => {
  1476. it( 'passes paragraph[align]', () => {
  1477. schema.register( 'paragraph', {
  1478. allowAttributes: 'align'
  1479. } );
  1480. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1481. } );
  1482. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1483. schema.register( 'paragraph', {
  1484. allowAttributes: [ 'align', 'dir' ]
  1485. } );
  1486. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1487. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1488. } );
  1489. it( 'passes paragraph>$text[bold]', () => {
  1490. schema.register( 'paragraph' );
  1491. schema.register( '$text', {
  1492. allowIn: 'paragraph',
  1493. allowAttributes: 'bold'
  1494. } );
  1495. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1496. } );
  1497. } );
  1498. describe( 'allowAttributesOf', () => {
  1499. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1500. schema.register( '$block', {
  1501. allowAttributes: 'align'
  1502. } );
  1503. schema.register( 'paragraph', {
  1504. allowAttributesOf: '$block'
  1505. } );
  1506. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1507. } );
  1508. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1509. schema.register( '$block', {
  1510. allowAttributes: 'align'
  1511. } );
  1512. schema.register( '$block2', {
  1513. allowAttributes: 'dir'
  1514. } );
  1515. schema.register( 'paragraph', {
  1516. allowAttributesOf: [ '$block', '$block2' ]
  1517. } );
  1518. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1519. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1520. } );
  1521. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1522. schema.register( '$block', {
  1523. allowAttributes: 'align'
  1524. } );
  1525. schema.register( 'paragraph', {
  1526. allowAttributes: 'dir',
  1527. allowAttributesOf: '$block'
  1528. } );
  1529. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1530. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1531. } );
  1532. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1533. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1534. } );
  1535. describe( 'inheritAllFrom', () => {
  1536. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1537. schema.register( '$block', {
  1538. allowAttributes: 'align'
  1539. } );
  1540. schema.register( 'paragraph', {
  1541. inheritAllFrom: '$block'
  1542. } );
  1543. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1544. } );
  1545. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1546. schema.register( '$blockProto', {
  1547. allowAttributes: 'align'
  1548. } );
  1549. schema.register( '$block', {
  1550. allowAttributesOf: '$blockProto'
  1551. } );
  1552. schema.register( 'paragraph', {
  1553. inheritAllFrom: '$block'
  1554. } );
  1555. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1556. } );
  1557. } );
  1558. describe( 'missing attribute definitions', () => {
  1559. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1560. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1561. } );
  1562. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1563. schema.register( 'paragraph', {
  1564. allowAttributesOf: '$block'
  1565. } );
  1566. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1567. } );
  1568. it( 'does not crash when inheriting all from a non-registered element', () => {
  1569. schema.register( 'paragraph', {
  1570. allowAttributesOf: '$block'
  1571. } );
  1572. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1573. } );
  1574. } );
  1575. describe( 'missing types definitions', () => {
  1576. it( 'does not crash when inheriting types of an non-registered element', () => {
  1577. schema.register( 'paragraph', {
  1578. inheritTypesFrom: '$block'
  1579. } );
  1580. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1581. } );
  1582. } );
  1583. } );
  1584. describe( 'real scenarios', () => {
  1585. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1586. const definitions = [
  1587. () => {
  1588. schema.register( 'paragraph', {
  1589. inheritAllFrom: '$block'
  1590. } );
  1591. },
  1592. () => {
  1593. schema.register( 'heading1', {
  1594. inheritAllFrom: '$block'
  1595. } );
  1596. },
  1597. () => {
  1598. schema.register( 'listItem', {
  1599. inheritAllFrom: '$block',
  1600. allowAttributes: [ 'indent', 'type' ]
  1601. } );
  1602. },
  1603. () => {
  1604. schema.register( 'blockQuote', {
  1605. allowWhere: '$block',
  1606. allowContentOf: '$root'
  1607. } );
  1608. // Disallow blockQuote in blockQuote.
  1609. schema.addChildCheck( ( ctx, childDef ) => {
  1610. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1611. return false;
  1612. }
  1613. } );
  1614. },
  1615. () => {
  1616. schema.register( 'image', {
  1617. allowWhere: '$block',
  1618. allowAttributes: [ 'src', 'alt' ],
  1619. isObject: true,
  1620. isBlock: true
  1621. } );
  1622. },
  1623. () => {
  1624. schema.register( 'caption', {
  1625. allowIn: 'image',
  1626. allowContentOf: '$block',
  1627. isLimit: true
  1628. } );
  1629. },
  1630. () => {
  1631. schema.extend( '$text', {
  1632. allowAttributes: [ 'bold', 'italic' ]
  1633. } );
  1634. // Disallow bold in heading1.
  1635. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1636. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1637. return false;
  1638. }
  1639. } );
  1640. },
  1641. () => {
  1642. schema.extend( '$block', {
  1643. allowAttributes: 'alignment'
  1644. } );
  1645. }
  1646. ];
  1647. beforeEach( () => {
  1648. schema.register( '$root', {
  1649. isLimit: true
  1650. } );
  1651. schema.register( '$block', {
  1652. allowIn: '$root',
  1653. isBlock: true
  1654. } );
  1655. schema.register( '$text', {
  1656. allowIn: '$block'
  1657. } );
  1658. for ( const definition of definitions ) {
  1659. definition();
  1660. }
  1661. // or...
  1662. //
  1663. // Use the below code to shuffle the definitions.
  1664. // Don't look here, Szymon!
  1665. //
  1666. // const definitionsCopy = definitions.slice();
  1667. //
  1668. // while ( definitionsCopy.length ) {
  1669. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1670. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1671. // }
  1672. root1 = new Element( '$root', null, [
  1673. new Element( 'paragraph', null, 'foo' ),
  1674. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1675. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1676. new Element( 'heading1', null, 'foo' ),
  1677. new Element( 'blockQuote', null, [
  1678. new Element( 'paragraph', null, 'foo' ),
  1679. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1680. new Element( 'image', null, [
  1681. new Element( 'caption', null, 'foo' )
  1682. ] )
  1683. ] ),
  1684. new Element( 'image', null, [
  1685. new Element( 'caption', null, 'foo' )
  1686. ] )
  1687. ] );
  1688. r1p1 = root1.getChild( 0 );
  1689. r1p2 = root1.getChild( 1 );
  1690. r1lI = root1.getChild( 2 );
  1691. r1h = root1.getChild( 3 );
  1692. r1bQ = root1.getChild( 4 );
  1693. r1i = root1.getChild( 5 );
  1694. r1bQp = r1bQ.getChild( 0 );
  1695. r1bQlI = r1bQ.getChild( 1 );
  1696. r1bQi = r1bQ.getChild( 2 );
  1697. } );
  1698. it( 'passes $root>paragraph', () => {
  1699. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1700. } );
  1701. it( 'passes $root>paragraph>$text', () => {
  1702. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1703. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1704. } );
  1705. it( 'passes $root>listItem', () => {
  1706. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1707. } );
  1708. it( 'passes $root>listItem>$text', () => {
  1709. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1710. } );
  1711. it( 'passes $root>blockQuote>paragraph', () => {
  1712. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1713. } );
  1714. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1715. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1716. } );
  1717. it( 'passes $root>blockQuote>listItem', () => {
  1718. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1719. } );
  1720. it( 'passes $root>blockQuote>listItem>$text', () => {
  1721. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1722. } );
  1723. it( 'passes $root>blockQuote>image', () => {
  1724. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1725. } );
  1726. it( 'passes $root>blockQuote>image>caption', () => {
  1727. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1728. } );
  1729. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1730. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1731. } );
  1732. it( 'passes $root>image', () => {
  1733. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1734. } );
  1735. it( 'passes $root>image>caption', () => {
  1736. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1737. } );
  1738. it( 'passes $root>image>caption>$text', () => {
  1739. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1740. } );
  1741. it( 'rejects $root>$root', () => {
  1742. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1743. } );
  1744. it( 'rejects $root>$text', () => {
  1745. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1746. } );
  1747. it( 'rejects $root>caption', () => {
  1748. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1749. } );
  1750. it( 'rejects $root>paragraph>paragraph', () => {
  1751. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1752. } );
  1753. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1754. // Edge case because p>p should not exist in the first place.
  1755. // But it's good to know that it blocks also this.
  1756. const p = new Element( 'p' );
  1757. r1p1.appendChildren( p );
  1758. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1759. } );
  1760. it( 'rejects $root>paragraph>$block', () => {
  1761. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1762. } );
  1763. it( 'rejects $root>paragraph>blockQuote', () => {
  1764. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1765. } );
  1766. it( 'rejects $root>paragraph>image', () => {
  1767. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1768. } );
  1769. it( 'rejects $root>paragraph>caption', () => {
  1770. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1771. } );
  1772. it( 'rejects $root>blockQuote>blockQuote', () => {
  1773. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1774. } );
  1775. it( 'rejects $root>blockQuote>caption', () => {
  1776. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1777. } );
  1778. it( 'rejects $root>blockQuote>$text', () => {
  1779. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1780. } );
  1781. it( 'rejects $root>image>$text', () => {
  1782. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1783. } );
  1784. it( 'rejects $root>image>paragraph', () => {
  1785. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1786. } );
  1787. it( 'rejects $root>image>caption>paragraph', () => {
  1788. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1789. } );
  1790. it( 'rejects $root>image>caption>blockQuote', () => {
  1791. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1792. } );
  1793. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1794. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1795. } );
  1796. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1797. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1798. } );
  1799. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1800. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1801. } );
  1802. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1803. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1804. } );
  1805. it( 'accepts attribute $root>listItem[alignment]', () => {
  1806. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1807. } );
  1808. it( 'accepts attribute $root>listItem[indent]', () => {
  1809. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1810. } );
  1811. it( 'accepts attribute $root>listItem[type]', () => {
  1812. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1813. } );
  1814. it( 'accepts attribute $root>image[src]', () => {
  1815. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1816. } );
  1817. it( 'accepts attribute $root>image[alt]', () => {
  1818. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1819. } );
  1820. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1821. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1822. } );
  1823. it( 'rejects attribute $root[indent]', () => {
  1824. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1825. } );
  1826. it( 'rejects attribute $root>paragraph[indent]', () => {
  1827. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1828. } );
  1829. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1830. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1831. } );
  1832. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1833. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1834. } );
  1835. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1836. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1837. } );
  1838. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1839. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1840. } );
  1841. it( 'rejects attribute $root>image[indent]', () => {
  1842. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1843. } );
  1844. it( 'rejects attribute $root>image[alignment]', () => {
  1845. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1846. } );
  1847. it( '$root is limit', () => {
  1848. expect( schema.isLimit( '$root' ) ).to.be.true;
  1849. expect( schema.isBlock( '$root' ) ).to.be.false;
  1850. expect( schema.isObject( '$root' ) ).to.be.false;
  1851. } );
  1852. it( 'paragraph is block', () => {
  1853. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  1854. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  1855. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  1856. } );
  1857. it( 'heading1 is block', () => {
  1858. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  1859. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  1860. expect( schema.isObject( 'heading1' ) ).to.be.false;
  1861. } );
  1862. it( 'listItem is block', () => {
  1863. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  1864. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  1865. expect( schema.isObject( 'listItem' ) ).to.be.false;
  1866. } );
  1867. it( 'image is block object', () => {
  1868. expect( schema.isLimit( 'image' ) ).to.be.false;
  1869. expect( schema.isBlock( 'image' ) ).to.be.true;
  1870. expect( schema.isObject( 'image' ) ).to.be.true;
  1871. } );
  1872. it( 'caption is limit', () => {
  1873. expect( schema.isLimit( 'caption' ) ).to.be.true;
  1874. expect( schema.isBlock( 'caption' ) ).to.be.false;
  1875. expect( schema.isObject( 'caption' ) ).to.be.false;
  1876. } );
  1877. } );
  1878. } );
  1879. describe( 'SchemaContext', () => {
  1880. let root;
  1881. beforeEach( () => {
  1882. root = new Element( '$root', null, [
  1883. new Element( 'blockQuote', { foo: 1 }, [
  1884. new Element( 'paragraph', { align: 'left' }, [
  1885. new Text( 'foo', { bold: true, italic: true } )
  1886. ] )
  1887. ] )
  1888. ] );
  1889. } );
  1890. describe( 'constructor()', () => {
  1891. it( 'creates context based on an array of strings', () => {
  1892. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1893. expect( ctx.length ).to.equal( 3 );
  1894. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1895. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  1896. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1897. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1898. } );
  1899. it( 'creates context based on an array of elements', () => {
  1900. const blockQuote = root.getChild( 0 );
  1901. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1902. const ctx = new SchemaContext( [ blockQuote, text ] );
  1903. expect( ctx.length ).to.equal( 2 );
  1904. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  1905. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  1906. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1907. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  1908. } );
  1909. it( 'creates context based on a mixed array of strings and elements', () => {
  1910. const blockQuote = root.getChild( 0 );
  1911. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1912. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  1913. expect( ctx.length ).to.equal( 3 );
  1914. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  1915. } );
  1916. it( 'creates context based on a root element', () => {
  1917. const ctx = new SchemaContext( root );
  1918. expect( ctx.length ).to.equal( 1 );
  1919. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  1920. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1921. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1922. } );
  1923. it( 'creates context based on a nested element', () => {
  1924. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  1925. expect( ctx.length ).to.equal( 3 );
  1926. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1927. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1928. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  1929. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  1930. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  1931. } );
  1932. it( 'creates context based on a text node', () => {
  1933. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1934. expect( ctx.length ).to.equal( 4 );
  1935. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1936. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1937. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1938. } );
  1939. it( 'creates context based on a text proxy', () => {
  1940. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  1941. const textProxy = new TextProxy( text, 0, 1 );
  1942. const ctx = new SchemaContext( textProxy );
  1943. expect( ctx.length ).to.equal( 4 );
  1944. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1945. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1946. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1947. } );
  1948. it( 'creates context based on a position', () => {
  1949. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  1950. const ctx = new SchemaContext( pos );
  1951. expect( ctx.length ).to.equal( 3 );
  1952. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1953. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  1954. } );
  1955. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  1956. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  1957. expect( ctx.length ).to.equal( 1 );
  1958. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1959. } );
  1960. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  1961. const p = new Element( 'paragraph' );
  1962. const docFrag = new DocumentFragment();
  1963. docFrag.appendChildren( p );
  1964. const ctx = new SchemaContext( p );
  1965. expect( ctx.length ).to.equal( 1 );
  1966. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1967. } );
  1968. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  1969. const p = new Element( 'paragraph' );
  1970. const docFrag = new DocumentFragment();
  1971. docFrag.appendChildren( p );
  1972. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  1973. expect( ctx.length ).to.equal( 1 );
  1974. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1975. } );
  1976. } );
  1977. describe( 'length', () => {
  1978. it( 'gets the number of items', () => {
  1979. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1980. expect( ctx.length ).to.equal( 3 );
  1981. } );
  1982. } );
  1983. describe( 'last', () => {
  1984. it( 'gets the last item', () => {
  1985. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1986. expect( ctx.last ).to.be.an( 'object' );
  1987. expect( ctx.last.name ).to.equal( 'c' );
  1988. } );
  1989. } );
  1990. describe( 'Symbol.iterator', () => {
  1991. it( 'exists', () => {
  1992. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1993. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  1994. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1995. } );
  1996. } );
  1997. describe( 'getItem()', () => {
  1998. it( 'returns item by index', () => {
  1999. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2000. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2001. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2002. } );
  2003. it( 'returns undefined if index exceeds the range', () => {
  2004. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2005. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2006. } );
  2007. } );
  2008. describe( 'addItem()', () => {
  2009. it( 'adds new item at the top of the context #text', () => {
  2010. const node = new Text( 'd' );
  2011. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2012. ctx.addItem( node );
  2013. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2014. } );
  2015. it( 'adds new item at the top of the context #string', () => {
  2016. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2017. ctx.addItem( 'd' );
  2018. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2019. } );
  2020. it( 'adds new item at the top of the context #node', () => {
  2021. const node = new Element( 'd' );
  2022. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2023. ctx.addItem( node );
  2024. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2025. } );
  2026. } );
  2027. describe( 'getNames()', () => {
  2028. it( 'returns an iterator', () => {
  2029. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2030. expect( ctx.getNames().next ).to.be.a( 'function' );
  2031. } );
  2032. it( 'returns an iterator which returns all item names', () => {
  2033. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2034. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2035. } );
  2036. } );
  2037. describe( 'endsWith()', () => {
  2038. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2039. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2040. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2041. } );
  2042. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2043. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2044. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2045. } );
  2046. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2047. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2048. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2049. } );
  2050. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2051. const ctx = new SchemaContext( [ 'foo' ] );
  2052. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2053. } );
  2054. it( 'returns true if not only the end of the context matches the query', () => {
  2055. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2056. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2057. } );
  2058. it( 'returns false if query matches the middle of the context', () => {
  2059. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2060. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2061. } );
  2062. it( 'returns false if query matches the start of the context', () => {
  2063. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2064. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2065. } );
  2066. it( 'returns false if query does not match', () => {
  2067. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2068. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2069. } );
  2070. it( 'returns false if query is longer than context', () => {
  2071. const ctx = new SchemaContext( [ 'foo' ] );
  2072. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2073. } );
  2074. } );
  2075. } );