schema.js 62 KB

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