schema.js 65 KB

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