schema.js 53 KB

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