schema.js 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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 position as a context', () => {
  291. const posInRoot = Position.createAt( root1 );
  292. const posInParagraph = Position.createAt( r1p1 );
  293. expect( schema.checkChild( posInRoot, 'paragraph' ) ).to.be.true;
  294. expect( schema.checkChild( posInRoot, '$text' ) ).to.be.false;
  295. expect( schema.checkChild( posInParagraph, '$text' ) ).to.be.true;
  296. expect( schema.checkChild( posInParagraph, 'paragraph' ) ).to.be.false;
  297. } );
  298. // This is a temporary feature which is needed to make the current V->M conversion works.
  299. // It should be removed once V->M conversion uses real positions.
  300. // Of course, real positions have this advantage that we know element attributes at this point.
  301. it( 'accepts an array of element names as a context', () => {
  302. const contextInRoot = [ '$root' ];
  303. const contextInParagraph = [ '$root', 'paragraph' ];
  304. expect( schema.checkChild( contextInRoot, 'paragraph' ) ).to.be.true;
  305. expect( schema.checkChild( contextInRoot, '$text' ) ).to.be.false;
  306. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  307. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  308. } );
  309. it( 'accepts an array of elements as a context', () => {
  310. const contextInRoot = [ root1 ];
  311. const contextInParagraph = [ root1, r1p1 ];
  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. // Again, this is needed temporarily to handle current V->M conversion
  318. it( 'accepts a mixed array of elements and strings as a context', () => {
  319. const contextInParagraph = [ '$root', r1p1 ];
  320. expect( schema.checkChild( contextInParagraph, '$text' ) ).to.be.true;
  321. expect( schema.checkChild( contextInParagraph, 'paragraph' ) ).to.be.false;
  322. } );
  323. it( 'accepts a node as a child', () => {
  324. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  325. expect( schema.checkChild( root1, new Text( 'foo' ) ) ).to.be.false;
  326. } );
  327. it( 'fires the checkChild event with already normalized params', done => {
  328. schema.on( 'checkChild', ( evt, [ ctx, child ] ) => {
  329. expect( ctx ).to.be.instanceof( SchemaContext );
  330. expect( child ).to.equal( schema.getDefinition( 'paragraph' ) );
  331. done();
  332. }, { priority: 'highest' } );
  333. schema.checkChild( root1, r1p1 );
  334. } );
  335. } );
  336. describe( 'checkAttribute()', () => {
  337. beforeEach( () => {
  338. schema.register( 'paragraph', {
  339. allowAttributes: 'align'
  340. } );
  341. schema.register( '$text', {
  342. allowAttributes: 'bold'
  343. } );
  344. } );
  345. it( 'accepts an element as a context', () => {
  346. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  347. expect( schema.checkAttribute( r1p1, 'bold' ) ).to.be.false;
  348. } );
  349. it( 'accepts a text as a context', () => {
  350. expect( schema.checkAttribute( new Text( 'foo' ), 'bold' ) ).to.be.true;
  351. expect( schema.checkAttribute( new Text( 'foo' ), 'align' ) ).to.be.false;
  352. } );
  353. it( 'accepts a position as a context', () => {
  354. const posInRoot = Position.createAt( root1 );
  355. const posInParagraph = Position.createAt( r1p1 );
  356. expect( schema.checkAttribute( posInRoot, 'align' ) ).to.be.false;
  357. expect( schema.checkAttribute( posInParagraph, 'align' ) ).to.be.true;
  358. } );
  359. it( 'accepts an array of node names as a context', () => {
  360. const contextInRoot = [ '$root' ];
  361. const contextInParagraph = [ '$root', 'paragraph' ];
  362. const contextInText = [ '$root', 'paragraph', '$text' ];
  363. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  364. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  365. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  366. } );
  367. it( 'accepts an array of nodes as a context', () => {
  368. const contextInRoot = [ root1 ];
  369. const contextInParagraph = [ root1, r1p1 ];
  370. const contextInText = [ root1, r1p1, r1p1.getChild( 0 ) ];
  371. expect( schema.checkAttribute( contextInRoot, 'align' ) ).to.be.false;
  372. expect( schema.checkAttribute( contextInParagraph, 'align' ) ).to.be.true;
  373. expect( schema.checkAttribute( contextInText, 'bold' ) ).to.be.true;
  374. } );
  375. it( 'fires the checkAttribute event with already normalized context', done => {
  376. schema.on( 'checkAttribute', ( evt, [ ctx, attributeName ] ) => {
  377. expect( ctx ).to.be.instanceof( SchemaContext );
  378. expect( attributeName ).to.equal( 'bold' );
  379. done();
  380. }, { priority: 'highest' } );
  381. schema.checkAttribute( r1p1, 'bold' );
  382. } );
  383. } );
  384. describe( 'addChildCheck()', () => {
  385. beforeEach( () => {
  386. schema.register( '$root' );
  387. schema.register( 'paragraph', {
  388. allowIn: '$root'
  389. } );
  390. } );
  391. it( 'adds a high-priority listener', () => {
  392. const order = [];
  393. schema.on( 'checkChild', () => {
  394. order.push( 'checkChild:high-before' );
  395. }, { priority: 'high' } );
  396. schema.addChildCheck( () => {
  397. order.push( 'addChildCheck' );
  398. } );
  399. schema.on( 'checkChild', () => {
  400. order.push( 'checkChild:high-after' );
  401. }, { priority: 'high' } );
  402. schema.checkChild( root1, r1p1 );
  403. expect( order.join() ).to.equal( 'checkChild:high-before,addChildCheck,checkChild:high-after' );
  404. } );
  405. it( 'stops the event and overrides the return value when callback returned true', () => {
  406. schema.register( '$text' );
  407. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  408. schema.addChildCheck( () => {
  409. return true;
  410. } );
  411. schema.on( 'checkChild', () => {
  412. throw new Error( 'the event should be stopped' );
  413. }, { priority: 'high' } );
  414. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  415. } );
  416. it( 'stops the event and overrides the return value when callback returned false', () => {
  417. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  418. schema.addChildCheck( () => {
  419. return false;
  420. } );
  421. schema.on( 'checkChild', () => {
  422. throw new Error( 'the event should be stopped' );
  423. }, { priority: 'high' } );
  424. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  425. } );
  426. it( 'receives context and child definition as params', () => {
  427. schema.addChildCheck( ( ctx, childDef ) => {
  428. expect( ctx ).to.be.instanceOf( SchemaContext );
  429. expect( childDef ).to.equal( schema.getDefinition( 'paragraph' ) );
  430. } );
  431. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  432. } );
  433. it( 'is not called when checking a non-registered element', () => {
  434. expect( schema.getDefinition( 'foo' ) ).to.be.undefined;
  435. schema.addChildCheck( () => {
  436. throw new Error( 'callback should not be called' );
  437. } );
  438. expect( schema.checkChild( root1, 'foo' ) ).to.be.false;
  439. } );
  440. } );
  441. describe( 'addAttributeCheck()', () => {
  442. beforeEach( () => {
  443. schema.register( 'paragraph', {
  444. allowAttributes: 'foo'
  445. } );
  446. } );
  447. it( 'adds a high-priority listener', () => {
  448. const order = [];
  449. schema.on( 'checkAttribute', () => {
  450. order.push( 'checkAttribute:high-before' );
  451. }, { priority: 'high' } );
  452. schema.addAttributeCheck( () => {
  453. order.push( 'addAttributeCheck' );
  454. } );
  455. schema.on( 'checkAttribute', () => {
  456. order.push( 'checkAttribute:high-after' );
  457. }, { priority: 'high' } );
  458. schema.checkAttribute( r1p1, 'foo' );
  459. expect( order.join() ).to.equal( 'checkAttribute:high-before,addAttributeCheck,checkAttribute:high-after' );
  460. } );
  461. it( 'stops the event and overrides the return value when callback returned true', () => {
  462. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.false;
  463. schema.addAttributeCheck( () => {
  464. return true;
  465. } );
  466. schema.on( 'checkAttribute', () => {
  467. throw new Error( 'the event should be stopped' );
  468. }, { priority: 'high' } );
  469. expect( schema.checkAttribute( r1p1, 'bar' ) ).to.be.true;
  470. } );
  471. it( 'stops the event and overrides the return value when callback returned false', () => {
  472. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  473. schema.addAttributeCheck( () => {
  474. return false;
  475. } );
  476. schema.on( 'checkAttribute', () => {
  477. throw new Error( 'the event should be stopped' );
  478. }, { priority: 'high' } );
  479. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.false;
  480. } );
  481. it( 'receives context and attribute name as params', () => {
  482. schema.addAttributeCheck( ( ctx, attributeName ) => {
  483. expect( ctx ).to.be.instanceOf( SchemaContext );
  484. expect( attributeName ).to.equal( 'foo' );
  485. } );
  486. expect( schema.checkAttribute( r1p1, 'foo' ) ).to.be.true;
  487. } );
  488. } );
  489. describe( 'checkMerge()', () => {
  490. beforeEach( () => {
  491. schema.register( '$root' );
  492. schema.register( '$block', {
  493. allowIn: '$root'
  494. } );
  495. schema.register( 'paragraph', {
  496. allowWhere: '$block'
  497. } );
  498. schema.register( '$text', {
  499. allowIn: 'paragraph'
  500. } );
  501. schema.register( 'blockQuote', {
  502. allowWhere: '$block',
  503. allowContentOf: '$root'
  504. } );
  505. schema.register( 'listItem', {
  506. inheritAllFrom: '$block'
  507. } );
  508. } );
  509. it( 'returns false if a block is not allowed in other block', () => {
  510. const paragraph = new Element( 'paragraph', null, [
  511. new Text( 'xyz' )
  512. ] );
  513. const blockQuote = new Element( 'blockQuote', null, [ paragraph ] );
  514. const listItem = new Element( 'listItem' );
  515. expect( schema.checkMerge( listItem, blockQuote ) ).to.equal( false );
  516. } );
  517. it( 'returns true if a block is allowed in other block', () => {
  518. const paragraph = new Element( 'paragraph', null, [
  519. new Text( 'xyz' )
  520. ] );
  521. const listItem = new Element( 'listItem' );
  522. expect( schema.checkMerge( listItem, paragraph ) ).to.equal( false );
  523. } );
  524. } );
  525. describe( 'getLimitElement()', () => {
  526. let model, doc, root;
  527. beforeEach( () => {
  528. model = new Model();
  529. doc = model.document;
  530. schema = model.schema;
  531. root = doc.createRoot();
  532. schema.register( 'div', {
  533. inheritAllFrom: '$block'
  534. } );
  535. schema.register( 'article', {
  536. inheritAllFrom: '$block',
  537. allowIn: 'section'
  538. } );
  539. schema.register( 'section', {
  540. inheritAllFrom: '$block',
  541. allowIn: 'div'
  542. } );
  543. schema.register( 'paragraph', {
  544. inheritAllFrom: '$block',
  545. allowIn: 'article'
  546. } );
  547. schema.register( 'widget', {
  548. inheritAllFrom: '$block',
  549. allowIn: 'div'
  550. } );
  551. schema.register( 'image', {
  552. inheritAllFrom: '$block',
  553. allowIn: 'widget'
  554. } );
  555. schema.register( 'caption', {
  556. inheritAllFrom: '$block',
  557. allowIn: 'image'
  558. } );
  559. } );
  560. it( 'always returns $root element if any other limit was not defined', () => {
  561. schema.extend( '$root', {
  562. isLimit: false
  563. } );
  564. expect( schema.isLimit( '$root' ) ).to.be.false;
  565. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  566. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  567. } );
  568. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  569. schema.extend( 'article', { isLimit: true } );
  570. schema.extend( 'section', { isLimit: true } );
  571. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  572. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  573. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  574. } );
  575. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  576. schema.extend( 'article', { isLimit: true } );
  577. schema.extend( 'section', { isLimit: true } );
  578. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  579. const section = root.getNodeByPath( [ 0, 0 ] );
  580. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  581. } );
  582. it( 'works fine with multi-range selections', () => {
  583. schema.extend( 'article', { isLimit: true } );
  584. schema.extend( 'widget', { isLimit: true } );
  585. schema.extend( 'div', { isLimit: true } );
  586. setData(
  587. model,
  588. '<div>' +
  589. '<section>' +
  590. '<article>' +
  591. '<paragraph>[foo]</paragraph>' +
  592. '</article>' +
  593. '</section>' +
  594. '<widget>' +
  595. '<image>' +
  596. '<caption>b[a]r</caption>' +
  597. '</image>' +
  598. '</widget>' +
  599. '</div>'
  600. );
  601. const div = root.getNodeByPath( [ 0 ] );
  602. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  603. } );
  604. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  605. setData(
  606. model,
  607. '<div>' +
  608. '<section>' +
  609. '<article>' +
  610. '<paragraph>[foo]</paragraph>' +
  611. '</article>' +
  612. '</section>' +
  613. '</div>' +
  614. '<section>b[]ar</section>'
  615. );
  616. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  617. } );
  618. } );
  619. describe( 'checkAttributeInSelection()', () => {
  620. const attribute = 'bold';
  621. let model, doc, schema;
  622. beforeEach( () => {
  623. model = new Model();
  624. doc = model.document;
  625. doc.createRoot();
  626. schema = model.schema;
  627. schema.register( 'p', { inheritAllFrom: '$block' } );
  628. schema.register( 'h1', { inheritAllFrom: '$block' } );
  629. schema.register( 'img', { allowWhere: '$text' } );
  630. schema.register( 'figure', {
  631. allowIn: '$root',
  632. allowAttributes: [ 'name', 'title' ]
  633. } );
  634. schema.addAttributeCheck( ( ctx, attributeName ) => {
  635. // Allow 'bold' on p>$text.
  636. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  637. return true;
  638. }
  639. // Allow 'bold' on $root>p.
  640. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  641. return true;
  642. }
  643. } );
  644. } );
  645. describe( 'when selection is collapsed', () => {
  646. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  647. setData( model, '<p>f[]oo</p>' );
  648. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  649. } );
  650. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  651. setData( model, '<h1>[]</h1>' );
  652. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  653. setData( model, '[]' );
  654. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  655. } );
  656. } );
  657. describe( 'when selection is not collapsed', () => {
  658. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  659. // Simple selection on a few characters.
  660. setData( model, '<p>[foo]</p>' );
  661. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  662. // Selection spans over characters but also include nodes that can't have attribute.
  663. setData( model, '<p>fo[o<img />b]ar</p>' );
  664. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  665. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  666. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  667. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  668. // Selection on empty P. P can have the attribute.
  669. setData( model, '[<p></p>]' );
  670. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  671. } );
  672. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  673. // Selection on DIV which can't have bold text.
  674. setData( model, '[<h1></h1>]' );
  675. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  676. // Selection on two images which can't be bold.
  677. setData( model, '<p>foo[<img /><img />]bar</p>' );
  678. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  679. } );
  680. it( 'should return true when checking element with required attribute', () => {
  681. setData( model, '[<figure name="figure"></figure>]' );
  682. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  683. } );
  684. it( 'should return true when checking element when attribute is already present', () => {
  685. setData( model, '[<figure name="figure" title="title"></figure>]' );
  686. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  687. } );
  688. } );
  689. } );
  690. describe( 'getValidRanges()', () => {
  691. const attribute = 'bold';
  692. let model, doc, root, schema, ranges;
  693. beforeEach( () => {
  694. model = new Model();
  695. doc = model.document;
  696. schema = model.schema;
  697. root = doc.createRoot();
  698. schema.register( 'p', { inheritAllFrom: '$block' } );
  699. schema.register( 'h1', { inheritAllFrom: '$block' } );
  700. schema.register( 'img', {
  701. allowWhere: '$text'
  702. } );
  703. schema.addAttributeCheck( ( ctx, attributeName ) => {
  704. // Allow 'bold' on p>$text.
  705. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  706. return true;
  707. }
  708. // Allow 'bold' on $root>p.
  709. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  710. return true;
  711. }
  712. } );
  713. setData( model, '<p>foo<img />bar</p>' );
  714. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  715. } );
  716. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  717. schema.extend( 'img', { allowAttributes: 'bold' } );
  718. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  719. } );
  720. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  721. schema.extend( 'img', { allowAttributes: 'bold' } );
  722. schema.extend( '$text', { allowIn: 'img' } );
  723. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  724. } );
  725. it( 'should return two ranges when attribute is not allowed on one item', () => {
  726. schema.extend( 'img', { allowAttributes: 'bold' } );
  727. schema.extend( '$text', { allowIn: 'img' } );
  728. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  729. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  730. const sel = new Selection();
  731. sel.setRanges( validRanges );
  732. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  733. } );
  734. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  735. schema.extend( '$text', { allowIn: 'img' } );
  736. schema.addAttributeCheck( ( ctx, attributeName ) => {
  737. // Allow 'bold' on img>$text.
  738. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  739. return true;
  740. }
  741. } );
  742. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  743. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  744. const sel = new Selection();
  745. sel.setRanges( validRanges );
  746. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  747. } );
  748. it( 'should not leak beyond the given ranges', () => {
  749. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  750. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  751. const sel = new Selection();
  752. sel.setRanges( validRanges );
  753. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  754. } );
  755. it( 'should correctly handle a range which ends in a disallowed position', () => {
  756. schema.extend( '$text', { allowIn: 'img' } );
  757. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  758. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  759. const sel = new Selection();
  760. sel.setRanges( validRanges );
  761. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  762. } );
  763. it( 'should split range into two ranges and omit disallowed element', () => {
  764. schema.addAttributeCheck( ( ctx, attributeName ) => {
  765. // Disallow 'bold' on p>img.
  766. if ( ctx.endsWith( 'p img' ) && attributeName == 'bold' ) {
  767. return false;
  768. }
  769. } );
  770. const result = schema.getValidRanges( ranges, attribute );
  771. expect( result ).to.length( 2 );
  772. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  773. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  774. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  775. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  776. } );
  777. } );
  778. describe( 'removeDisallowedAttributes()', () => {
  779. let model, doc, root;
  780. beforeEach( () => {
  781. model = new Model();
  782. doc = model.document;
  783. root = doc.createRoot();
  784. schema = model.schema;
  785. schema.register( 'paragraph', {
  786. inheritAllFrom: '$block'
  787. } );
  788. schema.register( 'div', {
  789. inheritAllFrom: '$block'
  790. } );
  791. schema.register( 'image', {
  792. isObject: true
  793. } );
  794. schema.extend( '$block', {
  795. allowIn: 'div'
  796. } );
  797. } );
  798. it( 'should filter out disallowed attributes from given nodes', () => {
  799. schema.extend( '$text', { allowAttributes: 'a' } );
  800. schema.extend( 'image', { allowAttributes: 'b' } );
  801. const text = new Text( 'foo', { a: 1, b: 1 } );
  802. const image = new Element( 'image', { a: 1, b: 1 } );
  803. root.appendChildren( [ text, image ] );
  804. model.change( writer => {
  805. schema.removeDisallowedAttributes( root.getChildren(), writer );
  806. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  807. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  808. expect( writer.batch.deltas ).to.length( 2 );
  809. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  810. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  811. expect( getData( model, { withoutSelection: true } ) )
  812. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  813. } );
  814. } );
  815. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  816. schema.addAttributeCheck( ( ctx, attributeName ) => {
  817. // Allow 'a' on div>$text.
  818. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  819. return true;
  820. }
  821. // Allow 'b' on div>paragraph>$text.
  822. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  823. return true;
  824. }
  825. // Allow 'a' on div>image.
  826. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  827. return true;
  828. }
  829. // Allow 'b' on div>paragraph>image.
  830. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  831. return true;
  832. }
  833. } );
  834. const foo = new Text( 'foo', { a: 1, b: 1 } );
  835. const bar = new Text( 'bar', { a: 1, b: 1 } );
  836. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  837. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  838. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  839. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  840. root.appendChildren( [ div ] );
  841. model.change( writer => {
  842. schema.removeDisallowedAttributes( root.getChildren(), writer );
  843. expect( writer.batch.deltas ).to.length( 4 );
  844. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  845. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  846. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  847. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  848. expect( getData( model, { withoutSelection: true } ) )
  849. .to.equal(
  850. '<div>' +
  851. '<paragraph>' +
  852. '<$text b="1">foo</$text>' +
  853. '<image b="1"></image>' +
  854. '</paragraph>' +
  855. '<$text a="1">bar</$text>' +
  856. '<image a="1"></image>' +
  857. '</div>'
  858. );
  859. } );
  860. } );
  861. } );
  862. describe( 'definitions compilation', () => {
  863. describe( 'allowIn cases', () => {
  864. it( 'passes $root>paragraph', () => {
  865. schema.register( '$root' );
  866. schema.register( 'paragraph', {
  867. allowIn: '$root'
  868. } );
  869. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  870. } );
  871. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  872. schema.register( '$root' );
  873. schema.register( '$root2' );
  874. schema.register( 'paragraph', {
  875. allowIn: [ '$root', '$root2' ]
  876. } );
  877. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  878. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  879. } );
  880. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  881. schema.register( '$root' );
  882. schema.register( 'paragraph', {
  883. allowIn: '$root'
  884. } );
  885. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  886. } );
  887. it( 'passes $root>div>div – in case of circular refs', () => {
  888. schema.register( '$root' );
  889. schema.register( 'div', {
  890. allowIn: [ '$root', 'div' ]
  891. } );
  892. const div = new Element( 'div' );
  893. root1.appendChildren( div );
  894. const div2 = new Element( 'div' );
  895. expect( schema.checkChild( div, div2 ) ).to.be.true;
  896. } );
  897. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  898. schema.register( '$root' );
  899. schema.register( 'div', {
  900. allowIn: [ '$root', 'div' ]
  901. } );
  902. const div = new Element( 'div' );
  903. root1.appendChildren( div );
  904. expect( schema.checkChild( div, div ) ).to.be.true;
  905. } );
  906. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  907. schema.register( '$root' );
  908. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  909. } );
  910. it( 'rejects $root>paragraph – registered different item', () => {
  911. schema.register( '$root' );
  912. schema.register( 'paragraph' );
  913. schema.register( 'listItem', {
  914. allowIn: '$root'
  915. } );
  916. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  917. } );
  918. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  919. schema.register( '$root' );
  920. schema.register( '$fancyRoot' );
  921. schema.register( 'paragraph', {
  922. allowIn: '$fancyRoot'
  923. } );
  924. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  925. } );
  926. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  927. schema.register( '$root' );
  928. schema.register( 'paragraph', {
  929. allowIn: '$root'
  930. } );
  931. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  932. } );
  933. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  934. schema.register( '$root' );
  935. schema.register( 'blockQuote', {
  936. allowIn: '$root'
  937. } );
  938. schema.register( 'paragraph', {
  939. allowIn: '$root'
  940. } );
  941. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  942. } );
  943. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  944. schema.register( '$root' );
  945. schema.register( 'paragraph', {
  946. allowIn: '$root'
  947. } );
  948. schema.register( '$text', {
  949. allowIn: 'paragraph'
  950. } );
  951. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  952. } );
  953. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  954. schema.register( '$root' );
  955. schema.register( 'blockQuote' );
  956. schema.register( 'paragraph', {
  957. allowIn: [ 'blockQuote', '$root' ]
  958. } );
  959. schema.register( '$text', {
  960. allowIn: 'paragraph'
  961. } );
  962. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  963. } );
  964. } );
  965. describe( 'allowWhere cases', () => {
  966. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  967. schema.register( '$root' );
  968. schema.register( '$block', {
  969. allowIn: '$root'
  970. } );
  971. schema.register( 'paragraph', {
  972. allowWhere: '$block'
  973. } );
  974. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  975. } );
  976. it( 'supports the array syntax', () => {
  977. schema.register( '$root' );
  978. schema.register( '$root2' );
  979. schema.register( '$block', {
  980. allowIn: '$root'
  981. } );
  982. schema.register( '$block2', {
  983. allowIn: '$root2'
  984. } );
  985. schema.register( 'paragraph', {
  986. allowWhere: [ '$block', '$block2' ]
  987. } );
  988. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  989. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  990. } );
  991. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  992. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  993. schema.register( '$root' );
  994. schema.register( 'paragraph', {
  995. allowWhere: '$block'
  996. } );
  997. schema.register( '$block', {
  998. allowIn: '$root'
  999. } );
  1000. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1001. } );
  1002. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1003. schema.register( '$root' );
  1004. schema.register( '$block', {
  1005. allowIn: '$root'
  1006. } );
  1007. schema.register( '$specialBlock', {
  1008. allowWhere: '$block'
  1009. } );
  1010. schema.register( 'paragraph', {
  1011. allowWhere: '$specialBlock'
  1012. } );
  1013. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1014. } );
  1015. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1016. schema.register( '$root' );
  1017. schema.register( '$block' );
  1018. schema.register( 'paragraph', {
  1019. allowWhere: '$block'
  1020. } );
  1021. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1022. } );
  1023. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1024. schema.register( '$root' );
  1025. schema.register( '$block' );
  1026. schema.register( 'paragraph', {
  1027. allowWhere: '$block'
  1028. } );
  1029. schema.register( '$text', {
  1030. allowIn: 'paragraph'
  1031. } );
  1032. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1033. } );
  1034. } );
  1035. describe( 'allowContentOf cases', () => {
  1036. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1037. schema.register( '$root' );
  1038. schema.register( 'paragraph', {
  1039. allowIn: '$root'
  1040. } );
  1041. schema.register( '$root2', {
  1042. allowContentOf: '$root'
  1043. } );
  1044. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1045. } );
  1046. it( 'supports the array syntax', () => {
  1047. schema.register( '$root' );
  1048. schema.register( '$root2' );
  1049. schema.register( 'paragraph', {
  1050. allowIn: '$root'
  1051. } );
  1052. schema.register( 'heading1', {
  1053. allowIn: '$root2'
  1054. } );
  1055. schema.register( '$root3', {
  1056. allowContentOf: [ '$root', '$root2' ]
  1057. } );
  1058. const root3 = new Element( '$root3' );
  1059. const heading1 = new Element( 'heading1' );
  1060. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1061. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1062. } );
  1063. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1064. schema.register( '$root' );
  1065. schema.register( '$root2', {
  1066. allowContentOf: '$root'
  1067. } );
  1068. schema.register( 'paragraph', {
  1069. allowIn: '$root'
  1070. } );
  1071. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1072. } );
  1073. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1074. schema.register( '$root' );
  1075. schema.register( '$block' );
  1076. schema.register( 'paragraph', {
  1077. allowIn: '$root',
  1078. allowContentOf: '$block'
  1079. } );
  1080. schema.register( '$text', {
  1081. allowIn: '$block'
  1082. } );
  1083. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1084. } );
  1085. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1086. schema.register( '$root' );
  1087. schema.register( 'blockQuote', {
  1088. allowIn: '$root',
  1089. allowContentOf: '$root'
  1090. } );
  1091. schema.register( 'paragraph', {
  1092. allowIn: '$root'
  1093. } );
  1094. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1095. } );
  1096. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1097. schema.register( '$root' );
  1098. schema.register( 'paragraph' );
  1099. schema.register( '$root2', {
  1100. allowContentOf: '$root'
  1101. } );
  1102. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1103. } );
  1104. } );
  1105. describe( 'mix of allowContentOf and allowWhere', () => {
  1106. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1107. schema.register( '$root' );
  1108. schema.register( '$block', {
  1109. allowIn: '$root'
  1110. } );
  1111. schema.register( 'paragraph', {
  1112. allowContentOf: '$block',
  1113. allowWhere: '$block'
  1114. } );
  1115. schema.register( '$text', {
  1116. allowIn: '$block'
  1117. } );
  1118. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1119. } );
  1120. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1121. 'and paragraph inherits allowWhere from $block', () => {
  1122. schema.register( '$root' );
  1123. schema.register( '$root2', {
  1124. allowContentOf: '$root'
  1125. } );
  1126. schema.register( '$block', {
  1127. allowIn: '$root'
  1128. } );
  1129. schema.register( 'paragraph', {
  1130. allowWhere: '$block'
  1131. } );
  1132. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1133. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1134. } );
  1135. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1136. schema.register( 'b' );
  1137. schema.register( 'a', { allowIn: 'b' } );
  1138. schema.register( 'c', { allowContentOf: 'b' } );
  1139. schema.register( 'd', { allowContentOf: 'c' } );
  1140. const d = new Element( 'd' );
  1141. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1142. } );
  1143. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1144. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1145. // as long as the previous iteration found something to compile.
  1146. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1147. // and since we've found something, we'd now try d<-c which would work.
  1148. //
  1149. // We ignore those situations for now as they are very unlikely to happen and would
  1150. // significantly raised the complexity of definition compilation.
  1151. //
  1152. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1153. // schema.register( 'b' );
  1154. // schema.register( 'a', { allowIn: 'b' } );
  1155. // schema.register( 'd', { allowContentOf: 'c' } );
  1156. // schema.register( 'c', { allowContentOf: 'b' } );
  1157. //
  1158. // const d = new Element( 'd' );
  1159. //
  1160. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1161. // } );
  1162. } );
  1163. describe( 'inheritTypesFrom', () => {
  1164. it( 'inherit properties of another item', () => {
  1165. schema.register( '$block', {
  1166. isBlock: true,
  1167. isLimit: true
  1168. } );
  1169. schema.register( 'paragraph', {
  1170. inheritTypesFrom: '$block'
  1171. } );
  1172. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1173. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1174. } );
  1175. it( 'inherit properties of other items – support for arrays', () => {
  1176. schema.register( '$block', {
  1177. isBlock: true
  1178. } );
  1179. schema.register( '$block2', {
  1180. isLimit: true
  1181. } );
  1182. schema.register( 'paragraph', {
  1183. inheritTypesFrom: [ '$block', '$block2' ]
  1184. } );
  1185. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1186. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1187. } );
  1188. it( 'does not override existing props', () => {
  1189. schema.register( '$block', {
  1190. isBlock: true,
  1191. isLimit: true
  1192. } );
  1193. schema.register( 'paragraph', {
  1194. inheritTypesFrom: '$block',
  1195. isLimit: false
  1196. } );
  1197. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1198. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1199. } );
  1200. } );
  1201. describe( 'inheritAllFrom', () => {
  1202. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1203. schema.register( '$root' );
  1204. schema.register( '$block', {
  1205. allowIn: '$root'
  1206. } );
  1207. schema.register( 'paragraph', {
  1208. inheritAllFrom: '$block'
  1209. } );
  1210. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1211. } );
  1212. it( 'paragraph inherit properties of $block', () => {
  1213. schema.register( '$block', {
  1214. isBlock: true
  1215. } );
  1216. schema.register( 'paragraph', {
  1217. inheritAllFrom: '$block'
  1218. } );
  1219. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1220. } );
  1221. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1222. schema.register( '$root' );
  1223. schema.register( '$block', {
  1224. allowIn: '$root'
  1225. } );
  1226. schema.register( '$text', {
  1227. allowIn: '$block'
  1228. } );
  1229. schema.register( 'paragraph', {
  1230. inheritAllFrom: '$block'
  1231. } );
  1232. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1233. } );
  1234. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1235. schema.register( '$root' );
  1236. schema.register( '$blockProto', {
  1237. allowIn: '$root'
  1238. } );
  1239. schema.register( '$block', {
  1240. allowWhere: '$blockProto'
  1241. } );
  1242. schema.register( 'paragraph', {
  1243. inheritAllFrom: '$block'
  1244. } );
  1245. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1246. } );
  1247. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1248. schema.register( '$root' );
  1249. schema.register( '$blockProto' );
  1250. schema.register( '$block', {
  1251. allowContentOf: '$blockProto',
  1252. allowIn: '$root'
  1253. } );
  1254. schema.register( '$text', {
  1255. allowIn: '$blockProto'
  1256. } );
  1257. schema.register( 'paragraph', {
  1258. inheritAllFrom: '$block'
  1259. } );
  1260. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1261. } );
  1262. } );
  1263. // We need to handle cases where some independent features registered definitions which might use
  1264. // optional elements (elements which might not have been registered).
  1265. describe( 'missing structure definitions', () => {
  1266. it( 'does not break when trying to check a child which is not registered', () => {
  1267. schema.register( '$root' );
  1268. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1269. } );
  1270. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1271. const foo404 = new Element( 'foo404' );
  1272. root1.appendChildren( foo404 );
  1273. schema.register( '$root' );
  1274. schema.register( '$text', {
  1275. allowIn: '$root'
  1276. } );
  1277. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1278. } );
  1279. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1280. schema.register( '$root' );
  1281. schema.register( '$text', {
  1282. allowIn: 'foo404'
  1283. } );
  1284. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1285. } );
  1286. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1287. schema.register( '$root' );
  1288. schema.register( '$text', {
  1289. allowWhere: 'foo404'
  1290. } );
  1291. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1292. } );
  1293. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1294. schema.register( '$root', {
  1295. allowContentOf: 'foo404'
  1296. } );
  1297. schema.register( '$text', {
  1298. allowIn: '$root'
  1299. } );
  1300. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1301. } );
  1302. it( 'checks whether allowIn uses a registered element', () => {
  1303. schema.register( 'paragraph', {
  1304. allowIn: '$root'
  1305. } );
  1306. // $root isn't registered!
  1307. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1308. } );
  1309. it( 'does not break when inheriting all from an non-registered element', () => {
  1310. schema.register( 'paragraph', {
  1311. inheritAllFrom: '$block'
  1312. } );
  1313. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1314. } );
  1315. } );
  1316. describe( 'allowAttributes', () => {
  1317. it( 'passes paragraph[align]', () => {
  1318. schema.register( 'paragraph', {
  1319. allowAttributes: 'align'
  1320. } );
  1321. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1322. } );
  1323. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1324. schema.register( 'paragraph', {
  1325. allowAttributes: [ 'align', 'dir' ]
  1326. } );
  1327. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1328. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1329. } );
  1330. it( 'passes paragraph>$text[bold]', () => {
  1331. schema.register( 'paragraph' );
  1332. schema.register( '$text', {
  1333. allowIn: 'paragraph',
  1334. allowAttributes: 'bold'
  1335. } );
  1336. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1337. } );
  1338. } );
  1339. describe( 'allowAttributesOf', () => {
  1340. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1341. schema.register( '$block', {
  1342. allowAttributes: 'align'
  1343. } );
  1344. schema.register( 'paragraph', {
  1345. allowAttributesOf: '$block'
  1346. } );
  1347. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1348. } );
  1349. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1350. schema.register( '$block', {
  1351. allowAttributes: 'align'
  1352. } );
  1353. schema.register( '$block2', {
  1354. allowAttributes: 'dir'
  1355. } );
  1356. schema.register( 'paragraph', {
  1357. allowAttributesOf: [ '$block', '$block2' ]
  1358. } );
  1359. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1360. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1361. } );
  1362. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1363. schema.register( '$block', {
  1364. allowAttributes: 'align'
  1365. } );
  1366. schema.register( 'paragraph', {
  1367. allowAttributes: 'dir',
  1368. allowAttributesOf: '$block'
  1369. } );
  1370. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1371. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1372. } );
  1373. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1374. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1375. } );
  1376. describe( 'inheritAllFrom', () => {
  1377. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1378. schema.register( '$block', {
  1379. allowAttributes: 'align'
  1380. } );
  1381. schema.register( 'paragraph', {
  1382. inheritAllFrom: '$block'
  1383. } );
  1384. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1385. } );
  1386. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1387. schema.register( '$blockProto', {
  1388. allowAttributes: 'align'
  1389. } );
  1390. schema.register( '$block', {
  1391. allowAttributesOf: '$blockProto'
  1392. } );
  1393. schema.register( 'paragraph', {
  1394. inheritAllFrom: '$block'
  1395. } );
  1396. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1397. } );
  1398. } );
  1399. describe( 'missing attribute definitions', () => {
  1400. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1401. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1402. } );
  1403. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1404. schema.register( 'paragraph', {
  1405. allowAttributesOf: '$block'
  1406. } );
  1407. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1408. } );
  1409. it( 'does not crash when inheriting all from a non-registered element', () => {
  1410. schema.register( 'paragraph', {
  1411. allowAttributesOf: '$block'
  1412. } );
  1413. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1414. } );
  1415. } );
  1416. describe( 'missing types definitions', () => {
  1417. it( 'does not crash when inheriting types of an non-registered element', () => {
  1418. schema.register( 'paragraph', {
  1419. inheritTypesFrom: '$block'
  1420. } );
  1421. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1422. } );
  1423. } );
  1424. } );
  1425. describe( 'real scenarios', () => {
  1426. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1427. const definitions = [
  1428. () => {
  1429. schema.register( 'paragraph', {
  1430. inheritAllFrom: '$block'
  1431. } );
  1432. },
  1433. () => {
  1434. schema.register( 'heading1', {
  1435. inheritAllFrom: '$block'
  1436. } );
  1437. },
  1438. () => {
  1439. schema.register( 'listItem', {
  1440. inheritAllFrom: '$block',
  1441. allowAttributes: [ 'indent', 'type' ]
  1442. } );
  1443. },
  1444. () => {
  1445. schema.register( 'blockQuote', {
  1446. allowWhere: '$block',
  1447. allowContentOf: '$root'
  1448. } );
  1449. // Disallow blockQuote in blockQuote.
  1450. schema.addChildCheck( ( ctx, childDef ) => {
  1451. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1452. return false;
  1453. }
  1454. } );
  1455. },
  1456. () => {
  1457. schema.register( 'image', {
  1458. allowWhere: '$block',
  1459. allowAttributes: [ 'src', 'alt' ],
  1460. isObject: true,
  1461. isBlock: true
  1462. } );
  1463. },
  1464. () => {
  1465. schema.register( 'caption', {
  1466. allowIn: 'image',
  1467. allowContentOf: '$block',
  1468. isLimit: true
  1469. } );
  1470. },
  1471. () => {
  1472. schema.extend( '$text', {
  1473. allowAttributes: [ 'bold', 'italic' ]
  1474. } );
  1475. // Disallow bold in heading1.
  1476. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1477. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1478. return false;
  1479. }
  1480. } );
  1481. },
  1482. () => {
  1483. schema.extend( '$block', {
  1484. allowAttributes: 'alignment'
  1485. } );
  1486. }
  1487. ];
  1488. beforeEach( () => {
  1489. schema.register( '$root', {
  1490. isLimit: true
  1491. } );
  1492. schema.register( '$block', {
  1493. allowIn: '$root',
  1494. isBlock: true
  1495. } );
  1496. schema.register( '$text', {
  1497. allowIn: '$block'
  1498. } );
  1499. for ( const definition of definitions ) {
  1500. definition();
  1501. }
  1502. // or...
  1503. //
  1504. // Use the below code to shuffle the definitions.
  1505. // Don't look here, Szymon!
  1506. //
  1507. // const definitionsCopy = definitions.slice();
  1508. //
  1509. // while ( definitionsCopy.length ) {
  1510. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1511. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1512. // }
  1513. root1 = new Element( '$root', null, [
  1514. new Element( 'paragraph', null, 'foo' ),
  1515. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1516. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1517. new Element( 'heading1', null, 'foo' ),
  1518. new Element( 'blockQuote', null, [
  1519. new Element( 'paragraph', null, 'foo' ),
  1520. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1521. new Element( 'image', null, [
  1522. new Element( 'caption', null, 'foo' )
  1523. ] )
  1524. ] ),
  1525. new Element( 'image', null, [
  1526. new Element( 'caption', null, 'foo' )
  1527. ] )
  1528. ] );
  1529. r1p1 = root1.getChild( 0 );
  1530. r1p2 = root1.getChild( 1 );
  1531. r1lI = root1.getChild( 2 );
  1532. r1h = root1.getChild( 3 );
  1533. r1bQ = root1.getChild( 4 );
  1534. r1i = root1.getChild( 5 );
  1535. r1bQp = r1bQ.getChild( 0 );
  1536. r1bQlI = r1bQ.getChild( 1 );
  1537. r1bQi = r1bQ.getChild( 2 );
  1538. } );
  1539. it( 'passes $root>paragraph', () => {
  1540. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1541. } );
  1542. it( 'passes $root>paragraph>$text', () => {
  1543. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1544. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1545. } );
  1546. it( 'passes $root>listItem', () => {
  1547. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1548. } );
  1549. it( 'passes $root>listItem>$text', () => {
  1550. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1551. } );
  1552. it( 'passes $root>blockQuote>paragraph', () => {
  1553. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1554. } );
  1555. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1556. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1557. } );
  1558. it( 'passes $root>blockQuote>listItem', () => {
  1559. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1560. } );
  1561. it( 'passes $root>blockQuote>listItem>$text', () => {
  1562. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1563. } );
  1564. it( 'passes $root>blockQuote>image', () => {
  1565. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1566. } );
  1567. it( 'passes $root>blockQuote>image>caption', () => {
  1568. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1569. } );
  1570. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1571. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1572. } );
  1573. it( 'passes $root>image', () => {
  1574. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1575. } );
  1576. it( 'passes $root>image>caption', () => {
  1577. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1578. } );
  1579. it( 'passes $root>image>caption>$text', () => {
  1580. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1581. } );
  1582. it( 'rejects $root>$root', () => {
  1583. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1584. } );
  1585. it( 'rejects $root>$text', () => {
  1586. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1587. } );
  1588. it( 'rejects $root>caption', () => {
  1589. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1590. } );
  1591. it( 'rejects $root>paragraph>paragraph', () => {
  1592. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1593. } );
  1594. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1595. // Edge case because p>p should not exist in the first place.
  1596. // But it's good to know that it blocks also this.
  1597. const p = new Element( 'p' );
  1598. r1p1.appendChildren( p );
  1599. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1600. } );
  1601. it( 'rejects $root>paragraph>$block', () => {
  1602. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1603. } );
  1604. it( 'rejects $root>paragraph>blockQuote', () => {
  1605. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1606. } );
  1607. it( 'rejects $root>paragraph>image', () => {
  1608. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1609. } );
  1610. it( 'rejects $root>paragraph>caption', () => {
  1611. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1612. } );
  1613. it( 'rejects $root>blockQuote>blockQuote', () => {
  1614. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1615. } );
  1616. it( 'rejects $root>blockQuote>caption', () => {
  1617. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1618. } );
  1619. it( 'rejects $root>blockQuote>$text', () => {
  1620. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1621. } );
  1622. it( 'rejects $root>image>$text', () => {
  1623. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1624. } );
  1625. it( 'rejects $root>image>paragraph', () => {
  1626. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1627. } );
  1628. it( 'rejects $root>image>caption>paragraph', () => {
  1629. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1630. } );
  1631. it( 'rejects $root>image>caption>blockQuote', () => {
  1632. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1633. } );
  1634. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1635. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1636. } );
  1637. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1638. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1639. } );
  1640. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1641. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1642. } );
  1643. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1644. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1645. } );
  1646. it( 'accepts attribute $root>listItem[alignment]', () => {
  1647. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1648. } );
  1649. it( 'accepts attribute $root>listItem[indent]', () => {
  1650. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1651. } );
  1652. it( 'accepts attribute $root>listItem[type]', () => {
  1653. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1654. } );
  1655. it( 'accepts attribute $root>image[src]', () => {
  1656. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1657. } );
  1658. it( 'accepts attribute $root>image[alt]', () => {
  1659. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1660. } );
  1661. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1662. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1663. } );
  1664. it( 'rejects attribute $root[indent]', () => {
  1665. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1666. } );
  1667. it( 'rejects attribute $root>paragraph[indent]', () => {
  1668. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1669. } );
  1670. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1671. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1672. } );
  1673. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1674. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1675. } );
  1676. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1677. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1678. } );
  1679. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1680. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1681. } );
  1682. it( 'rejects attribute $root>image[indent]', () => {
  1683. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1684. } );
  1685. it( 'rejects attribute $root>image[alignment]', () => {
  1686. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1687. } );
  1688. it( '$root is limit', () => {
  1689. expect( schema.isLimit( '$root' ) ).to.be.true;
  1690. expect( schema.isBlock( '$root' ) ).to.be.false;
  1691. expect( schema.isObject( '$root' ) ).to.be.false;
  1692. } );
  1693. it( 'paragraph is block', () => {
  1694. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  1695. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  1696. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  1697. } );
  1698. it( 'heading1 is block', () => {
  1699. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  1700. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  1701. expect( schema.isObject( 'heading1' ) ).to.be.false;
  1702. } );
  1703. it( 'listItem is block', () => {
  1704. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  1705. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  1706. expect( schema.isObject( 'listItem' ) ).to.be.false;
  1707. } );
  1708. it( 'image is block object', () => {
  1709. expect( schema.isLimit( 'image' ) ).to.be.false;
  1710. expect( schema.isBlock( 'image' ) ).to.be.true;
  1711. expect( schema.isObject( 'image' ) ).to.be.true;
  1712. } );
  1713. it( 'caption is limit', () => {
  1714. expect( schema.isLimit( 'caption' ) ).to.be.true;
  1715. expect( schema.isBlock( 'caption' ) ).to.be.false;
  1716. expect( schema.isObject( 'caption' ) ).to.be.false;
  1717. } );
  1718. } );
  1719. } );
  1720. describe( 'SchemaContext', () => {
  1721. let root;
  1722. beforeEach( () => {
  1723. root = new Element( '$root', null, [
  1724. new Element( 'blockQuote', { foo: 1 }, [
  1725. new Element( 'paragraph', { align: 'left' }, [
  1726. new Text( 'foo', { bold: true, italic: true } )
  1727. ] )
  1728. ] )
  1729. ] );
  1730. } );
  1731. describe( 'constructor()', () => {
  1732. it( 'creates context based on an array of strings', () => {
  1733. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1734. expect( ctx.length ).to.equal( 3 );
  1735. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1736. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  1737. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1738. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1739. } );
  1740. it( 'creates context based on an array of elements', () => {
  1741. const blockQuote = root.getChild( 0 );
  1742. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1743. const ctx = new SchemaContext( [ blockQuote, text ] );
  1744. expect( ctx.length ).to.equal( 2 );
  1745. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  1746. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  1747. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1748. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  1749. } );
  1750. it( 'creates context based on a mixed array of strings and elements', () => {
  1751. const blockQuote = root.getChild( 0 );
  1752. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1753. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  1754. expect( ctx.length ).to.equal( 3 );
  1755. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  1756. } );
  1757. it( 'creates context based on a root element', () => {
  1758. const ctx = new SchemaContext( root );
  1759. expect( ctx.length ).to.equal( 1 );
  1760. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  1761. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1762. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1763. } );
  1764. it( 'creates context based on a nested element', () => {
  1765. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  1766. expect( ctx.length ).to.equal( 3 );
  1767. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1768. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1769. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  1770. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  1771. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  1772. } );
  1773. it( 'creates context based on a text node', () => {
  1774. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1775. expect( ctx.length ).to.equal( 4 );
  1776. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1777. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1778. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1779. } );
  1780. it( 'creates context based on a text proxy', () => {
  1781. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  1782. const textProxy = new TextProxy( text, 0, 1 );
  1783. const ctx = new SchemaContext( textProxy );
  1784. expect( ctx.length ).to.equal( 4 );
  1785. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1786. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1787. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1788. } );
  1789. it( 'creates context based on a position', () => {
  1790. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  1791. const ctx = new SchemaContext( pos );
  1792. expect( ctx.length ).to.equal( 3 );
  1793. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1794. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  1795. } );
  1796. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  1797. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  1798. expect( ctx.length ).to.equal( 1 );
  1799. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1800. } );
  1801. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  1802. const p = new Element( 'paragraph' );
  1803. const docFrag = new DocumentFragment();
  1804. docFrag.appendChildren( p );
  1805. const ctx = new SchemaContext( p );
  1806. expect( ctx.length ).to.equal( 1 );
  1807. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1808. } );
  1809. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  1810. const p = new Element( 'paragraph' );
  1811. const docFrag = new DocumentFragment();
  1812. docFrag.appendChildren( p );
  1813. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  1814. expect( ctx.length ).to.equal( 1 );
  1815. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1816. } );
  1817. } );
  1818. describe( 'length', () => {
  1819. it( 'gets the number of items', () => {
  1820. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1821. expect( ctx.length ).to.equal( 3 );
  1822. } );
  1823. } );
  1824. describe( 'last', () => {
  1825. it( 'gets the last item', () => {
  1826. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1827. expect( ctx.last ).to.be.an( 'object' );
  1828. expect( ctx.last.name ).to.equal( 'c' );
  1829. } );
  1830. } );
  1831. describe( 'Symbol.iterator', () => {
  1832. it( 'exists', () => {
  1833. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1834. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  1835. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1836. } );
  1837. } );
  1838. describe( 'getItem()', () => {
  1839. it( 'returns item by index', () => {
  1840. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1841. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  1842. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  1843. } );
  1844. it( 'returns undefined if index exceeds the range', () => {
  1845. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1846. expect( ctx.getItem( 3 ) ).to.be.undefined;
  1847. } );
  1848. } );
  1849. describe( 'getNames()', () => {
  1850. it( 'returns an iterator', () => {
  1851. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1852. expect( ctx.getNames().next ).to.be.a( 'function' );
  1853. } );
  1854. it( 'returns an iterator which returns all item names', () => {
  1855. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1856. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1857. } );
  1858. } );
  1859. describe( 'endsWith()', () => {
  1860. it( 'returns true if the end of the context matches the query - 1 item', () => {
  1861. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1862. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  1863. } );
  1864. it( 'returns true if the end of the context matches the query - 2 items', () => {
  1865. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1866. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  1867. } );
  1868. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  1869. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  1870. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  1871. } );
  1872. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  1873. const ctx = new SchemaContext( [ 'foo' ] );
  1874. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  1875. } );
  1876. it( 'returns true if not only the end of the context matches the query', () => {
  1877. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  1878. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  1879. } );
  1880. it( 'returns false if query matches the middle of the context', () => {
  1881. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1882. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  1883. } );
  1884. it( 'returns false if query matches the start of the context', () => {
  1885. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1886. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  1887. } );
  1888. it( 'returns false if query does not match', () => {
  1889. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1890. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  1891. } );
  1892. it( 'returns false if query is longer than context', () => {
  1893. const ctx = new SchemaContext( [ 'foo' ] );
  1894. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  1895. } );
  1896. } );
  1897. } );