schema.js 66 KB

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