schema.js 70 KB

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