schema.js 62 KB

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