schema.js 75 KB

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