8
0

schema.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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 there is no element after the position', () => {
  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. listItem
  571. ] );
  572. const position = Position.createAfter( listItem );
  573. expect( () => {
  574. expect( schema.checkMerge( position ) );
  575. } ).to.throw( CKEditorError, /^schema-check-merge-no-element-after:/ );
  576. } );
  577. // This is an invalid case by definition – the baseElement should not contain disallowed elements
  578. // in the first place. However, the check is focused on the elementToMerge's children so let's make sure
  579. // that only them counts.
  580. it( 'returns true if element to merge contains a valid content but base element contains disallowed elements', () => {
  581. const listItem = new Element( 'listItem', null, [
  582. new Text( 'foo' ),
  583. new Element( 'paragraph', null, [
  584. new Text( 'bar' )
  585. ] )
  586. ] );
  587. const listItemToMerge = new Element( 'listItem', null, [
  588. new Text( 'xyz' )
  589. ] );
  590. expect( schema.checkMerge( listItem, listItemToMerge ) ).to.be.true;
  591. } );
  592. // The checkMerge() method should also check whether all ancestors of elementToMerge are allowed in their new
  593. // context (now, we check only immediate children), but for now we ignore these cases.
  594. } );
  595. describe( 'getLimitElement()', () => {
  596. let model, doc, root;
  597. beforeEach( () => {
  598. model = new Model();
  599. doc = model.document;
  600. schema = model.schema;
  601. root = doc.createRoot();
  602. schema.register( 'div', {
  603. inheritAllFrom: '$block'
  604. } );
  605. schema.register( 'article', {
  606. inheritAllFrom: '$block',
  607. allowIn: 'section'
  608. } );
  609. schema.register( 'section', {
  610. inheritAllFrom: '$block',
  611. allowIn: 'div'
  612. } );
  613. schema.register( 'paragraph', {
  614. inheritAllFrom: '$block',
  615. allowIn: 'article'
  616. } );
  617. schema.register( 'widget', {
  618. inheritAllFrom: '$block',
  619. allowIn: 'div'
  620. } );
  621. schema.register( 'image', {
  622. inheritAllFrom: '$block',
  623. allowIn: 'widget'
  624. } );
  625. schema.register( 'caption', {
  626. inheritAllFrom: '$block',
  627. allowIn: 'image'
  628. } );
  629. } );
  630. it( 'always returns $root element if any other limit was not defined', () => {
  631. schema.extend( '$root', {
  632. isLimit: false
  633. } );
  634. expect( schema.isLimit( '$root' ) ).to.be.false;
  635. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  636. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  637. } );
  638. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  639. schema.extend( 'article', { isLimit: true } );
  640. schema.extend( 'section', { isLimit: true } );
  641. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  642. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  643. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  644. } );
  645. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  646. schema.extend( 'article', { isLimit: true } );
  647. schema.extend( 'section', { isLimit: true } );
  648. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  649. const section = root.getNodeByPath( [ 0, 0 ] );
  650. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  651. } );
  652. it( 'works fine with multi-range selections', () => {
  653. schema.extend( 'article', { isLimit: true } );
  654. schema.extend( 'widget', { isLimit: true } );
  655. schema.extend( 'div', { isLimit: true } );
  656. setData(
  657. model,
  658. '<div>' +
  659. '<section>' +
  660. '<article>' +
  661. '<paragraph>[foo]</paragraph>' +
  662. '</article>' +
  663. '</section>' +
  664. '<widget>' +
  665. '<image>' +
  666. '<caption>b[a]r</caption>' +
  667. '</image>' +
  668. '</widget>' +
  669. '</div>'
  670. );
  671. const div = root.getNodeByPath( [ 0 ] );
  672. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  673. } );
  674. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  675. setData(
  676. model,
  677. '<div>' +
  678. '<section>' +
  679. '<article>' +
  680. '<paragraph>[foo]</paragraph>' +
  681. '</article>' +
  682. '</section>' +
  683. '</div>' +
  684. '<section>b[]ar</section>'
  685. );
  686. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  687. } );
  688. } );
  689. describe( 'checkAttributeInSelection()', () => {
  690. const attribute = 'bold';
  691. let model, doc, schema;
  692. beforeEach( () => {
  693. model = new Model();
  694. doc = model.document;
  695. doc.createRoot();
  696. schema = model.schema;
  697. schema.register( 'p', { inheritAllFrom: '$block' } );
  698. schema.register( 'h1', { inheritAllFrom: '$block' } );
  699. schema.register( 'img', { allowWhere: '$text' } );
  700. schema.register( 'figure', {
  701. allowIn: '$root',
  702. allowAttributes: [ 'name', 'title' ]
  703. } );
  704. schema.addAttributeCheck( ( ctx, attributeName ) => {
  705. // Allow 'bold' on p>$text.
  706. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  707. return true;
  708. }
  709. // Allow 'bold' on $root>p.
  710. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  711. return true;
  712. }
  713. } );
  714. } );
  715. describe( 'when selection is collapsed', () => {
  716. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  717. setData( model, '<p>f[]oo</p>' );
  718. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  719. } );
  720. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  721. setData( model, '<h1>[]</h1>' );
  722. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  723. setData( model, '[]' );
  724. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  725. } );
  726. } );
  727. describe( 'when selection is not collapsed', () => {
  728. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  729. // Simple selection on a few characters.
  730. setData( model, '<p>[foo]</p>' );
  731. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  732. // Selection spans over characters but also include nodes that can't have attribute.
  733. setData( model, '<p>fo[o<img />b]ar</p>' );
  734. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  735. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  736. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  737. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  738. // Selection on empty P. P can have the attribute.
  739. setData( model, '[<p></p>]' );
  740. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  741. } );
  742. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  743. // Selection on DIV which can't have bold text.
  744. setData( model, '[<h1></h1>]' );
  745. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  746. // Selection on two images which can't be bold.
  747. setData( model, '<p>foo[<img /><img />]bar</p>' );
  748. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  749. } );
  750. it( 'should return true when checking element with required attribute', () => {
  751. setData( model, '[<figure name="figure"></figure>]' );
  752. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  753. } );
  754. it( 'should return true when checking element when attribute is already present', () => {
  755. setData( model, '[<figure name="figure" title="title"></figure>]' );
  756. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  757. } );
  758. } );
  759. } );
  760. describe( 'getValidRanges()', () => {
  761. const attribute = 'bold';
  762. let model, doc, root, schema, ranges;
  763. beforeEach( () => {
  764. model = new Model();
  765. doc = model.document;
  766. schema = model.schema;
  767. root = doc.createRoot();
  768. schema.register( 'p', { inheritAllFrom: '$block' } );
  769. schema.register( 'h1', { inheritAllFrom: '$block' } );
  770. schema.register( 'img', {
  771. allowWhere: '$text'
  772. } );
  773. schema.addAttributeCheck( ( ctx, attributeName ) => {
  774. // Allow 'bold' on p>$text.
  775. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  776. return true;
  777. }
  778. // Allow 'bold' on $root>p.
  779. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  780. return true;
  781. }
  782. } );
  783. setData( model, '<p>foo<img />bar</p>' );
  784. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  785. } );
  786. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  787. schema.extend( 'img', { allowAttributes: 'bold' } );
  788. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  789. } );
  790. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  791. schema.extend( 'img', { allowAttributes: 'bold' } );
  792. schema.extend( '$text', { allowIn: 'img' } );
  793. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  794. } );
  795. it( 'should return two ranges when attribute is not allowed on one item', () => {
  796. schema.extend( 'img', { allowAttributes: 'bold' } );
  797. schema.extend( '$text', { allowIn: 'img' } );
  798. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  799. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  800. const sel = new Selection();
  801. sel.setRanges( validRanges );
  802. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  803. } );
  804. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  805. schema.extend( '$text', { allowIn: 'img' } );
  806. schema.addAttributeCheck( ( ctx, attributeName ) => {
  807. // Allow 'bold' on img>$text.
  808. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  809. return true;
  810. }
  811. } );
  812. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  813. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  814. const sel = new Selection();
  815. sel.setRanges( validRanges );
  816. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  817. } );
  818. it( 'should not leak beyond the given ranges', () => {
  819. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  820. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  821. const sel = new Selection();
  822. sel.setRanges( validRanges );
  823. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  824. } );
  825. it( 'should correctly handle a range which ends in a disallowed position', () => {
  826. schema.extend( '$text', { allowIn: 'img' } );
  827. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  828. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  829. const sel = new Selection();
  830. sel.setRanges( validRanges );
  831. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  832. } );
  833. it( 'should split range into two ranges and omit disallowed element', () => {
  834. schema.addAttributeCheck( ( ctx, attributeName ) => {
  835. // Disallow 'bold' on p>img.
  836. if ( ctx.endsWith( 'p img' ) && attributeName == 'bold' ) {
  837. return false;
  838. }
  839. } );
  840. const result = schema.getValidRanges( ranges, attribute );
  841. expect( result ).to.length( 2 );
  842. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  843. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  844. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  845. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  846. } );
  847. } );
  848. describe( 'removeDisallowedAttributes()', () => {
  849. let model, doc, root;
  850. beforeEach( () => {
  851. model = new Model();
  852. doc = model.document;
  853. root = doc.createRoot();
  854. schema = model.schema;
  855. schema.register( 'paragraph', {
  856. inheritAllFrom: '$block'
  857. } );
  858. schema.register( 'div', {
  859. inheritAllFrom: '$block'
  860. } );
  861. schema.register( 'image', {
  862. isObject: true
  863. } );
  864. schema.extend( '$block', {
  865. allowIn: 'div'
  866. } );
  867. } );
  868. it( 'should filter out disallowed attributes from given nodes', () => {
  869. schema.extend( '$text', { allowAttributes: 'a' } );
  870. schema.extend( 'image', { allowAttributes: 'b' } );
  871. const text = new Text( 'foo', { a: 1, b: 1 } );
  872. const image = new Element( 'image', { a: 1, b: 1 } );
  873. root.appendChildren( [ text, image ] );
  874. model.change( writer => {
  875. schema.removeDisallowedAttributes( root.getChildren(), writer );
  876. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  877. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  878. expect( writer.batch.deltas ).to.length( 2 );
  879. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  880. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  881. expect( getData( model, { withoutSelection: true } ) )
  882. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  883. } );
  884. } );
  885. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  886. schema.addAttributeCheck( ( ctx, attributeName ) => {
  887. // Allow 'a' on div>$text.
  888. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  889. return true;
  890. }
  891. // Allow 'b' on div>paragraph>$text.
  892. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  893. return true;
  894. }
  895. // Allow 'a' on div>image.
  896. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  897. return true;
  898. }
  899. // Allow 'b' on div>paragraph>image.
  900. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  901. return true;
  902. }
  903. } );
  904. const foo = new Text( 'foo', { a: 1, b: 1 } );
  905. const bar = new Text( 'bar', { a: 1, b: 1 } );
  906. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  907. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  908. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  909. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  910. root.appendChildren( [ div ] );
  911. model.change( writer => {
  912. schema.removeDisallowedAttributes( root.getChildren(), writer );
  913. expect( writer.batch.deltas ).to.length( 4 );
  914. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  915. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  916. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  917. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  918. expect( getData( model, { withoutSelection: true } ) )
  919. .to.equal(
  920. '<div>' +
  921. '<paragraph>' +
  922. '<$text b="1">foo</$text>' +
  923. '<image b="1"></image>' +
  924. '</paragraph>' +
  925. '<$text a="1">bar</$text>' +
  926. '<image a="1"></image>' +
  927. '</div>'
  928. );
  929. } );
  930. } );
  931. } );
  932. describe( 'definitions compilation', () => {
  933. describe( 'allowIn cases', () => {
  934. it( 'passes $root>paragraph', () => {
  935. schema.register( '$root' );
  936. schema.register( 'paragraph', {
  937. allowIn: '$root'
  938. } );
  939. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  940. } );
  941. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  942. schema.register( '$root' );
  943. schema.register( '$root2' );
  944. schema.register( 'paragraph', {
  945. allowIn: [ '$root', '$root2' ]
  946. } );
  947. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  948. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  949. } );
  950. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  951. schema.register( '$root' );
  952. schema.register( 'paragraph', {
  953. allowIn: '$root'
  954. } );
  955. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  956. } );
  957. it( 'passes $root>div>div – in case of circular refs', () => {
  958. schema.register( '$root' );
  959. schema.register( 'div', {
  960. allowIn: [ '$root', 'div' ]
  961. } );
  962. const div = new Element( 'div' );
  963. root1.appendChildren( div );
  964. const div2 = new Element( 'div' );
  965. expect( schema.checkChild( div, div2 ) ).to.be.true;
  966. } );
  967. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  968. schema.register( '$root' );
  969. schema.register( 'div', {
  970. allowIn: [ '$root', 'div' ]
  971. } );
  972. const div = new Element( 'div' );
  973. root1.appendChildren( div );
  974. expect( schema.checkChild( div, div ) ).to.be.true;
  975. } );
  976. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  977. schema.register( '$root' );
  978. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  979. } );
  980. it( 'rejects $root>paragraph – registered different item', () => {
  981. schema.register( '$root' );
  982. schema.register( 'paragraph' );
  983. schema.register( 'listItem', {
  984. allowIn: '$root'
  985. } );
  986. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  987. } );
  988. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  989. schema.register( '$root' );
  990. schema.register( '$fancyRoot' );
  991. schema.register( 'paragraph', {
  992. allowIn: '$fancyRoot'
  993. } );
  994. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  995. } );
  996. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  997. schema.register( '$root' );
  998. schema.register( 'paragraph', {
  999. allowIn: '$root'
  1000. } );
  1001. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1002. } );
  1003. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1004. schema.register( '$root' );
  1005. schema.register( 'blockQuote', {
  1006. allowIn: '$root'
  1007. } );
  1008. schema.register( 'paragraph', {
  1009. allowIn: '$root'
  1010. } );
  1011. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1012. } );
  1013. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1014. schema.register( '$root' );
  1015. schema.register( 'paragraph', {
  1016. allowIn: '$root'
  1017. } );
  1018. schema.register( '$text', {
  1019. allowIn: 'paragraph'
  1020. } );
  1021. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1022. } );
  1023. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1024. schema.register( '$root' );
  1025. schema.register( 'blockQuote' );
  1026. schema.register( 'paragraph', {
  1027. allowIn: [ 'blockQuote', '$root' ]
  1028. } );
  1029. schema.register( '$text', {
  1030. allowIn: 'paragraph'
  1031. } );
  1032. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1033. } );
  1034. } );
  1035. describe( 'allowWhere cases', () => {
  1036. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1037. schema.register( '$root' );
  1038. schema.register( '$block', {
  1039. allowIn: '$root'
  1040. } );
  1041. schema.register( 'paragraph', {
  1042. allowWhere: '$block'
  1043. } );
  1044. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1045. } );
  1046. it( 'supports the array syntax', () => {
  1047. schema.register( '$root' );
  1048. schema.register( '$root2' );
  1049. schema.register( '$block', {
  1050. allowIn: '$root'
  1051. } );
  1052. schema.register( '$block2', {
  1053. allowIn: '$root2'
  1054. } );
  1055. schema.register( 'paragraph', {
  1056. allowWhere: [ '$block', '$block2' ]
  1057. } );
  1058. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1059. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1060. } );
  1061. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1062. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1063. schema.register( '$root' );
  1064. schema.register( 'paragraph', {
  1065. allowWhere: '$block'
  1066. } );
  1067. schema.register( '$block', {
  1068. allowIn: '$root'
  1069. } );
  1070. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1071. } );
  1072. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1073. schema.register( '$root' );
  1074. schema.register( '$block', {
  1075. allowIn: '$root'
  1076. } );
  1077. schema.register( '$specialBlock', {
  1078. allowWhere: '$block'
  1079. } );
  1080. schema.register( 'paragraph', {
  1081. allowWhere: '$specialBlock'
  1082. } );
  1083. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1084. } );
  1085. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1086. schema.register( '$root' );
  1087. schema.register( '$block' );
  1088. schema.register( 'paragraph', {
  1089. allowWhere: '$block'
  1090. } );
  1091. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1092. } );
  1093. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1094. schema.register( '$root' );
  1095. schema.register( '$block' );
  1096. schema.register( 'paragraph', {
  1097. allowWhere: '$block'
  1098. } );
  1099. schema.register( '$text', {
  1100. allowIn: 'paragraph'
  1101. } );
  1102. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1103. } );
  1104. } );
  1105. describe( 'allowContentOf cases', () => {
  1106. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1107. schema.register( '$root' );
  1108. schema.register( 'paragraph', {
  1109. allowIn: '$root'
  1110. } );
  1111. schema.register( '$root2', {
  1112. allowContentOf: '$root'
  1113. } );
  1114. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1115. } );
  1116. it( 'supports the array syntax', () => {
  1117. schema.register( '$root' );
  1118. schema.register( '$root2' );
  1119. schema.register( 'paragraph', {
  1120. allowIn: '$root'
  1121. } );
  1122. schema.register( 'heading1', {
  1123. allowIn: '$root2'
  1124. } );
  1125. schema.register( '$root3', {
  1126. allowContentOf: [ '$root', '$root2' ]
  1127. } );
  1128. const root3 = new Element( '$root3' );
  1129. const heading1 = new Element( 'heading1' );
  1130. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1131. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1132. } );
  1133. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1134. schema.register( '$root' );
  1135. schema.register( '$root2', {
  1136. allowContentOf: '$root'
  1137. } );
  1138. schema.register( 'paragraph', {
  1139. allowIn: '$root'
  1140. } );
  1141. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1142. } );
  1143. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1144. schema.register( '$root' );
  1145. schema.register( '$block' );
  1146. schema.register( 'paragraph', {
  1147. allowIn: '$root',
  1148. allowContentOf: '$block'
  1149. } );
  1150. schema.register( '$text', {
  1151. allowIn: '$block'
  1152. } );
  1153. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1154. } );
  1155. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1156. schema.register( '$root' );
  1157. schema.register( 'blockQuote', {
  1158. allowIn: '$root',
  1159. allowContentOf: '$root'
  1160. } );
  1161. schema.register( 'paragraph', {
  1162. allowIn: '$root'
  1163. } );
  1164. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1165. } );
  1166. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1167. schema.register( '$root' );
  1168. schema.register( 'paragraph' );
  1169. schema.register( '$root2', {
  1170. allowContentOf: '$root'
  1171. } );
  1172. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1173. } );
  1174. } );
  1175. describe( 'mix of allowContentOf and allowWhere', () => {
  1176. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1177. schema.register( '$root' );
  1178. schema.register( '$block', {
  1179. allowIn: '$root'
  1180. } );
  1181. schema.register( 'paragraph', {
  1182. allowContentOf: '$block',
  1183. allowWhere: '$block'
  1184. } );
  1185. schema.register( '$text', {
  1186. allowIn: '$block'
  1187. } );
  1188. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1189. } );
  1190. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1191. 'and paragraph inherits allowWhere from $block', () => {
  1192. schema.register( '$root' );
  1193. schema.register( '$root2', {
  1194. allowContentOf: '$root'
  1195. } );
  1196. schema.register( '$block', {
  1197. allowIn: '$root'
  1198. } );
  1199. schema.register( 'paragraph', {
  1200. allowWhere: '$block'
  1201. } );
  1202. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1203. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1204. } );
  1205. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1206. schema.register( 'b' );
  1207. schema.register( 'a', { allowIn: 'b' } );
  1208. schema.register( 'c', { allowContentOf: 'b' } );
  1209. schema.register( 'd', { allowContentOf: 'c' } );
  1210. const d = new Element( 'd' );
  1211. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1212. } );
  1213. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1214. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1215. // as long as the previous iteration found something to compile.
  1216. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1217. // and since we've found something, we'd now try d<-c which would work.
  1218. //
  1219. // We ignore those situations for now as they are very unlikely to happen and would
  1220. // significantly raised the complexity of definition compilation.
  1221. //
  1222. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1223. // schema.register( 'b' );
  1224. // schema.register( 'a', { allowIn: 'b' } );
  1225. // schema.register( 'd', { allowContentOf: 'c' } );
  1226. // schema.register( 'c', { allowContentOf: 'b' } );
  1227. //
  1228. // const d = new Element( 'd' );
  1229. //
  1230. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1231. // } );
  1232. } );
  1233. describe( 'inheritTypesFrom', () => {
  1234. it( 'inherit properties of another item', () => {
  1235. schema.register( '$block', {
  1236. isBlock: true,
  1237. isLimit: true
  1238. } );
  1239. schema.register( 'paragraph', {
  1240. inheritTypesFrom: '$block'
  1241. } );
  1242. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1243. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1244. } );
  1245. it( 'inherit properties of other items – support for arrays', () => {
  1246. schema.register( '$block', {
  1247. isBlock: true
  1248. } );
  1249. schema.register( '$block2', {
  1250. isLimit: true
  1251. } );
  1252. schema.register( 'paragraph', {
  1253. inheritTypesFrom: [ '$block', '$block2' ]
  1254. } );
  1255. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1256. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1257. } );
  1258. it( 'does not override existing props', () => {
  1259. schema.register( '$block', {
  1260. isBlock: true,
  1261. isLimit: true
  1262. } );
  1263. schema.register( 'paragraph', {
  1264. inheritTypesFrom: '$block',
  1265. isLimit: false
  1266. } );
  1267. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1268. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1269. } );
  1270. } );
  1271. describe( 'inheritAllFrom', () => {
  1272. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1273. schema.register( '$root' );
  1274. schema.register( '$block', {
  1275. allowIn: '$root'
  1276. } );
  1277. schema.register( 'paragraph', {
  1278. inheritAllFrom: '$block'
  1279. } );
  1280. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1281. } );
  1282. it( 'paragraph inherit properties of $block', () => {
  1283. schema.register( '$block', {
  1284. isBlock: true
  1285. } );
  1286. schema.register( 'paragraph', {
  1287. inheritAllFrom: '$block'
  1288. } );
  1289. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1290. } );
  1291. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1292. schema.register( '$root' );
  1293. schema.register( '$block', {
  1294. allowIn: '$root'
  1295. } );
  1296. schema.register( '$text', {
  1297. allowIn: '$block'
  1298. } );
  1299. schema.register( 'paragraph', {
  1300. inheritAllFrom: '$block'
  1301. } );
  1302. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1303. } );
  1304. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1305. schema.register( '$root' );
  1306. schema.register( '$blockProto', {
  1307. allowIn: '$root'
  1308. } );
  1309. schema.register( '$block', {
  1310. allowWhere: '$blockProto'
  1311. } );
  1312. schema.register( 'paragraph', {
  1313. inheritAllFrom: '$block'
  1314. } );
  1315. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1316. } );
  1317. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1318. schema.register( '$root' );
  1319. schema.register( '$blockProto' );
  1320. schema.register( '$block', {
  1321. allowContentOf: '$blockProto',
  1322. allowIn: '$root'
  1323. } );
  1324. schema.register( '$text', {
  1325. allowIn: '$blockProto'
  1326. } );
  1327. schema.register( 'paragraph', {
  1328. inheritAllFrom: '$block'
  1329. } );
  1330. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1331. } );
  1332. } );
  1333. // We need to handle cases where some independent features registered definitions which might use
  1334. // optional elements (elements which might not have been registered).
  1335. describe( 'missing structure definitions', () => {
  1336. it( 'does not break when trying to check a child which is not registered', () => {
  1337. schema.register( '$root' );
  1338. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1339. } );
  1340. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1341. const foo404 = new Element( 'foo404' );
  1342. root1.appendChildren( foo404 );
  1343. schema.register( '$root' );
  1344. schema.register( '$text', {
  1345. allowIn: '$root'
  1346. } );
  1347. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1348. } );
  1349. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1350. schema.register( '$root' );
  1351. schema.register( '$text', {
  1352. allowIn: 'foo404'
  1353. } );
  1354. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1355. } );
  1356. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1357. schema.register( '$root' );
  1358. schema.register( '$text', {
  1359. allowWhere: 'foo404'
  1360. } );
  1361. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1362. } );
  1363. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1364. schema.register( '$root', {
  1365. allowContentOf: 'foo404'
  1366. } );
  1367. schema.register( '$text', {
  1368. allowIn: '$root'
  1369. } );
  1370. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1371. } );
  1372. it( 'checks whether allowIn uses a registered element', () => {
  1373. schema.register( 'paragraph', {
  1374. allowIn: '$root'
  1375. } );
  1376. // $root isn't registered!
  1377. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1378. } );
  1379. it( 'does not break when inheriting all from an non-registered element', () => {
  1380. schema.register( 'paragraph', {
  1381. inheritAllFrom: '$block'
  1382. } );
  1383. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1384. } );
  1385. } );
  1386. describe( 'allowAttributes', () => {
  1387. it( 'passes paragraph[align]', () => {
  1388. schema.register( 'paragraph', {
  1389. allowAttributes: 'align'
  1390. } );
  1391. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1392. } );
  1393. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1394. schema.register( 'paragraph', {
  1395. allowAttributes: [ 'align', 'dir' ]
  1396. } );
  1397. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1398. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1399. } );
  1400. it( 'passes paragraph>$text[bold]', () => {
  1401. schema.register( 'paragraph' );
  1402. schema.register( '$text', {
  1403. allowIn: 'paragraph',
  1404. allowAttributes: 'bold'
  1405. } );
  1406. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1407. } );
  1408. } );
  1409. describe( 'allowAttributesOf', () => {
  1410. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1411. schema.register( '$block', {
  1412. allowAttributes: 'align'
  1413. } );
  1414. schema.register( 'paragraph', {
  1415. allowAttributesOf: '$block'
  1416. } );
  1417. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1418. } );
  1419. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1420. schema.register( '$block', {
  1421. allowAttributes: 'align'
  1422. } );
  1423. schema.register( '$block2', {
  1424. allowAttributes: 'dir'
  1425. } );
  1426. schema.register( 'paragraph', {
  1427. allowAttributesOf: [ '$block', '$block2' ]
  1428. } );
  1429. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1430. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1431. } );
  1432. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1433. schema.register( '$block', {
  1434. allowAttributes: 'align'
  1435. } );
  1436. schema.register( 'paragraph', {
  1437. allowAttributes: 'dir',
  1438. allowAttributesOf: '$block'
  1439. } );
  1440. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1441. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1442. } );
  1443. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1444. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1445. } );
  1446. describe( 'inheritAllFrom', () => {
  1447. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1448. schema.register( '$block', {
  1449. allowAttributes: 'align'
  1450. } );
  1451. schema.register( 'paragraph', {
  1452. inheritAllFrom: '$block'
  1453. } );
  1454. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1455. } );
  1456. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1457. schema.register( '$blockProto', {
  1458. allowAttributes: 'align'
  1459. } );
  1460. schema.register( '$block', {
  1461. allowAttributesOf: '$blockProto'
  1462. } );
  1463. schema.register( 'paragraph', {
  1464. inheritAllFrom: '$block'
  1465. } );
  1466. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1467. } );
  1468. } );
  1469. describe( 'missing attribute definitions', () => {
  1470. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1471. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1472. } );
  1473. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1474. schema.register( 'paragraph', {
  1475. allowAttributesOf: '$block'
  1476. } );
  1477. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1478. } );
  1479. it( 'does not crash when inheriting all from a non-registered element', () => {
  1480. schema.register( 'paragraph', {
  1481. allowAttributesOf: '$block'
  1482. } );
  1483. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1484. } );
  1485. } );
  1486. describe( 'missing types definitions', () => {
  1487. it( 'does not crash when inheriting types of an non-registered element', () => {
  1488. schema.register( 'paragraph', {
  1489. inheritTypesFrom: '$block'
  1490. } );
  1491. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1492. } );
  1493. } );
  1494. } );
  1495. describe( 'real scenarios', () => {
  1496. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1497. const definitions = [
  1498. () => {
  1499. schema.register( 'paragraph', {
  1500. inheritAllFrom: '$block'
  1501. } );
  1502. },
  1503. () => {
  1504. schema.register( 'heading1', {
  1505. inheritAllFrom: '$block'
  1506. } );
  1507. },
  1508. () => {
  1509. schema.register( 'listItem', {
  1510. inheritAllFrom: '$block',
  1511. allowAttributes: [ 'indent', 'type' ]
  1512. } );
  1513. },
  1514. () => {
  1515. schema.register( 'blockQuote', {
  1516. allowWhere: '$block',
  1517. allowContentOf: '$root'
  1518. } );
  1519. // Disallow blockQuote in blockQuote.
  1520. schema.addChildCheck( ( ctx, childDef ) => {
  1521. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1522. return false;
  1523. }
  1524. } );
  1525. },
  1526. () => {
  1527. schema.register( 'image', {
  1528. allowWhere: '$block',
  1529. allowAttributes: [ 'src', 'alt' ],
  1530. isObject: true,
  1531. isBlock: true
  1532. } );
  1533. },
  1534. () => {
  1535. schema.register( 'caption', {
  1536. allowIn: 'image',
  1537. allowContentOf: '$block',
  1538. isLimit: true
  1539. } );
  1540. },
  1541. () => {
  1542. schema.extend( '$text', {
  1543. allowAttributes: [ 'bold', 'italic' ]
  1544. } );
  1545. // Disallow bold in heading1.
  1546. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1547. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1548. return false;
  1549. }
  1550. } );
  1551. },
  1552. () => {
  1553. schema.extend( '$block', {
  1554. allowAttributes: 'alignment'
  1555. } );
  1556. }
  1557. ];
  1558. beforeEach( () => {
  1559. schema.register( '$root', {
  1560. isLimit: true
  1561. } );
  1562. schema.register( '$block', {
  1563. allowIn: '$root',
  1564. isBlock: true
  1565. } );
  1566. schema.register( '$text', {
  1567. allowIn: '$block'
  1568. } );
  1569. for ( const definition of definitions ) {
  1570. definition();
  1571. }
  1572. // or...
  1573. //
  1574. // Use the below code to shuffle the definitions.
  1575. // Don't look here, Szymon!
  1576. //
  1577. // const definitionsCopy = definitions.slice();
  1578. //
  1579. // while ( definitionsCopy.length ) {
  1580. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1581. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1582. // }
  1583. root1 = new Element( '$root', null, [
  1584. new Element( 'paragraph', null, 'foo' ),
  1585. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1586. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1587. new Element( 'heading1', null, 'foo' ),
  1588. new Element( 'blockQuote', null, [
  1589. new Element( 'paragraph', null, 'foo' ),
  1590. new Element( 'listItem', { type: 'x', indent: 0 }, 'foo' ),
  1591. new Element( 'image', null, [
  1592. new Element( 'caption', null, 'foo' )
  1593. ] )
  1594. ] ),
  1595. new Element( 'image', null, [
  1596. new Element( 'caption', null, 'foo' )
  1597. ] )
  1598. ] );
  1599. r1p1 = root1.getChild( 0 );
  1600. r1p2 = root1.getChild( 1 );
  1601. r1lI = root1.getChild( 2 );
  1602. r1h = root1.getChild( 3 );
  1603. r1bQ = root1.getChild( 4 );
  1604. r1i = root1.getChild( 5 );
  1605. r1bQp = r1bQ.getChild( 0 );
  1606. r1bQlI = r1bQ.getChild( 1 );
  1607. r1bQi = r1bQ.getChild( 2 );
  1608. } );
  1609. it( 'passes $root>paragraph', () => {
  1610. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1611. } );
  1612. it( 'passes $root>paragraph>$text', () => {
  1613. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  1614. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  1615. } );
  1616. it( 'passes $root>listItem', () => {
  1617. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  1618. } );
  1619. it( 'passes $root>listItem>$text', () => {
  1620. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  1621. } );
  1622. it( 'passes $root>blockQuote>paragraph', () => {
  1623. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  1624. } );
  1625. it( 'passes $root>blockQuote>paragraph>$text', () => {
  1626. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  1627. } );
  1628. it( 'passes $root>blockQuote>listItem', () => {
  1629. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  1630. } );
  1631. it( 'passes $root>blockQuote>listItem>$text', () => {
  1632. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  1633. } );
  1634. it( 'passes $root>blockQuote>image', () => {
  1635. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  1636. } );
  1637. it( 'passes $root>blockQuote>image>caption', () => {
  1638. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  1639. } );
  1640. it( 'passes $root>blockQuote>image>caption>$text', () => {
  1641. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  1642. } );
  1643. it( 'passes $root>image', () => {
  1644. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  1645. } );
  1646. it( 'passes $root>image>caption', () => {
  1647. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  1648. } );
  1649. it( 'passes $root>image>caption>$text', () => {
  1650. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  1651. } );
  1652. it( 'rejects $root>$root', () => {
  1653. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  1654. } );
  1655. it( 'rejects $root>$text', () => {
  1656. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1657. } );
  1658. it( 'rejects $root>caption', () => {
  1659. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  1660. } );
  1661. it( 'rejects $root>paragraph>paragraph', () => {
  1662. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  1663. } );
  1664. it( 'rejects $root>paragraph>paragraph>$text', () => {
  1665. // Edge case because p>p should not exist in the first place.
  1666. // But it's good to know that it blocks also this.
  1667. const p = new Element( 'p' );
  1668. r1p1.appendChildren( p );
  1669. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  1670. } );
  1671. it( 'rejects $root>paragraph>$block', () => {
  1672. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  1673. } );
  1674. it( 'rejects $root>paragraph>blockQuote', () => {
  1675. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  1676. } );
  1677. it( 'rejects $root>paragraph>image', () => {
  1678. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1679. } );
  1680. it( 'rejects $root>paragraph>caption', () => {
  1681. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  1682. } );
  1683. it( 'rejects $root>blockQuote>blockQuote', () => {
  1684. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  1685. } );
  1686. it( 'rejects $root>blockQuote>caption', () => {
  1687. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  1688. } );
  1689. it( 'rejects $root>blockQuote>$text', () => {
  1690. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  1691. } );
  1692. it( 'rejects $root>image>$text', () => {
  1693. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  1694. } );
  1695. it( 'rejects $root>image>paragraph', () => {
  1696. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  1697. } );
  1698. it( 'rejects $root>image>caption>paragraph', () => {
  1699. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  1700. } );
  1701. it( 'rejects $root>image>caption>blockQuote', () => {
  1702. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  1703. } );
  1704. it( 'accepts attribute $root>paragraph[alignment]', () => {
  1705. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  1706. } );
  1707. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  1708. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1709. } );
  1710. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  1711. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  1712. } );
  1713. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  1714. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  1715. } );
  1716. it( 'accepts attribute $root>listItem[alignment]', () => {
  1717. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  1718. } );
  1719. it( 'accepts attribute $root>listItem[indent]', () => {
  1720. expect( schema.checkAttribute( r1lI, 'indent' ) ).to.be.true;
  1721. } );
  1722. it( 'accepts attribute $root>listItem[type]', () => {
  1723. expect( schema.checkAttribute( r1lI, 'type' ) ).to.be.true;
  1724. } );
  1725. it( 'accepts attribute $root>image[src]', () => {
  1726. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  1727. } );
  1728. it( 'accepts attribute $root>image[alt]', () => {
  1729. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  1730. } );
  1731. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  1732. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  1733. } );
  1734. it( 'rejects attribute $root[indent]', () => {
  1735. expect( schema.checkAttribute( root1, 'indent' ) ).to.be.false;
  1736. } );
  1737. it( 'rejects attribute $root>paragraph[indent]', () => {
  1738. expect( schema.checkAttribute( r1p1, 'indent' ) ).to.be.false;
  1739. } );
  1740. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  1741. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  1742. } );
  1743. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  1744. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  1745. } );
  1746. it( 'rejects attribute $root>blockQuote[indent]', () => {
  1747. expect( schema.checkAttribute( r1bQ, 'indent' ) ).to.be.false;
  1748. } );
  1749. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  1750. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  1751. } );
  1752. it( 'rejects attribute $root>image[indent]', () => {
  1753. expect( schema.checkAttribute( r1i, 'indent' ) ).to.be.false;
  1754. } );
  1755. it( 'rejects attribute $root>image[alignment]', () => {
  1756. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  1757. } );
  1758. it( '$root is limit', () => {
  1759. expect( schema.isLimit( '$root' ) ).to.be.true;
  1760. expect( schema.isBlock( '$root' ) ).to.be.false;
  1761. expect( schema.isObject( '$root' ) ).to.be.false;
  1762. } );
  1763. it( 'paragraph is block', () => {
  1764. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  1765. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  1766. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  1767. } );
  1768. it( 'heading1 is block', () => {
  1769. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  1770. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  1771. expect( schema.isObject( 'heading1' ) ).to.be.false;
  1772. } );
  1773. it( 'listItem is block', () => {
  1774. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  1775. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  1776. expect( schema.isObject( 'listItem' ) ).to.be.false;
  1777. } );
  1778. it( 'image is block object', () => {
  1779. expect( schema.isLimit( 'image' ) ).to.be.false;
  1780. expect( schema.isBlock( 'image' ) ).to.be.true;
  1781. expect( schema.isObject( 'image' ) ).to.be.true;
  1782. } );
  1783. it( 'caption is limit', () => {
  1784. expect( schema.isLimit( 'caption' ) ).to.be.true;
  1785. expect( schema.isBlock( 'caption' ) ).to.be.false;
  1786. expect( schema.isObject( 'caption' ) ).to.be.false;
  1787. } );
  1788. } );
  1789. } );
  1790. describe( 'SchemaContext', () => {
  1791. let root;
  1792. beforeEach( () => {
  1793. root = new Element( '$root', null, [
  1794. new Element( 'blockQuote', { foo: 1 }, [
  1795. new Element( 'paragraph', { align: 'left' }, [
  1796. new Text( 'foo', { bold: true, italic: true } )
  1797. ] )
  1798. ] )
  1799. ] );
  1800. } );
  1801. describe( 'constructor()', () => {
  1802. it( 'creates context based on an array of strings', () => {
  1803. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1804. expect( ctx.length ).to.equal( 3 );
  1805. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1806. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  1807. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1808. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1809. } );
  1810. it( 'creates context based on an array of elements', () => {
  1811. const blockQuote = root.getChild( 0 );
  1812. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1813. const ctx = new SchemaContext( [ blockQuote, text ] );
  1814. expect( ctx.length ).to.equal( 2 );
  1815. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  1816. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  1817. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1818. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  1819. } );
  1820. it( 'creates context based on a mixed array of strings and elements', () => {
  1821. const blockQuote = root.getChild( 0 );
  1822. const text = blockQuote.getChild( 0 ).getChild( 0 );
  1823. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  1824. expect( ctx.length ).to.equal( 3 );
  1825. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  1826. } );
  1827. it( 'creates context based on a root element', () => {
  1828. const ctx = new SchemaContext( root );
  1829. expect( ctx.length ).to.equal( 1 );
  1830. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  1831. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  1832. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  1833. } );
  1834. it( 'creates context based on a nested element', () => {
  1835. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  1836. expect( ctx.length ).to.equal( 3 );
  1837. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1838. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1839. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  1840. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  1841. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  1842. } );
  1843. it( 'creates context based on a text node', () => {
  1844. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  1845. expect( ctx.length ).to.equal( 4 );
  1846. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1847. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1848. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1849. } );
  1850. it( 'creates context based on a text proxy', () => {
  1851. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  1852. const textProxy = new TextProxy( text, 0, 1 );
  1853. const ctx = new SchemaContext( textProxy );
  1854. expect( ctx.length ).to.equal( 4 );
  1855. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  1856. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  1857. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  1858. } );
  1859. it( 'creates context based on a position', () => {
  1860. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  1861. const ctx = new SchemaContext( pos );
  1862. expect( ctx.length ).to.equal( 3 );
  1863. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  1864. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  1865. } );
  1866. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  1867. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  1868. expect( ctx.length ).to.equal( 1 );
  1869. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1870. } );
  1871. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  1872. const p = new Element( 'paragraph' );
  1873. const docFrag = new DocumentFragment();
  1874. docFrag.appendChildren( p );
  1875. const ctx = new SchemaContext( p );
  1876. expect( ctx.length ).to.equal( 1 );
  1877. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1878. } );
  1879. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  1880. const p = new Element( 'paragraph' );
  1881. const docFrag = new DocumentFragment();
  1882. docFrag.appendChildren( p );
  1883. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  1884. expect( ctx.length ).to.equal( 1 );
  1885. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  1886. } );
  1887. } );
  1888. describe( 'length', () => {
  1889. it( 'gets the number of items', () => {
  1890. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1891. expect( ctx.length ).to.equal( 3 );
  1892. } );
  1893. } );
  1894. describe( 'last', () => {
  1895. it( 'gets the last item', () => {
  1896. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1897. expect( ctx.last ).to.be.an( 'object' );
  1898. expect( ctx.last.name ).to.equal( 'c' );
  1899. } );
  1900. } );
  1901. describe( 'Symbol.iterator', () => {
  1902. it( 'exists', () => {
  1903. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1904. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  1905. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1906. } );
  1907. } );
  1908. describe( 'getItem()', () => {
  1909. it( 'returns item by index', () => {
  1910. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1911. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  1912. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  1913. } );
  1914. it( 'returns undefined if index exceeds the range', () => {
  1915. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1916. expect( ctx.getItem( 3 ) ).to.be.undefined;
  1917. } );
  1918. } );
  1919. describe( 'getNames()', () => {
  1920. it( 'returns an iterator', () => {
  1921. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1922. expect( ctx.getNames().next ).to.be.a( 'function' );
  1923. } );
  1924. it( 'returns an iterator which returns all item names', () => {
  1925. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  1926. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  1927. } );
  1928. } );
  1929. describe( 'endsWith()', () => {
  1930. it( 'returns true if the end of the context matches the query - 1 item', () => {
  1931. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1932. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  1933. } );
  1934. it( 'returns true if the end of the context matches the query - 2 items', () => {
  1935. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1936. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  1937. } );
  1938. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  1939. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  1940. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  1941. } );
  1942. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  1943. const ctx = new SchemaContext( [ 'foo' ] );
  1944. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  1945. } );
  1946. it( 'returns true if not only the end of the context matches the query', () => {
  1947. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  1948. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  1949. } );
  1950. it( 'returns false if query matches the middle of the context', () => {
  1951. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1952. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  1953. } );
  1954. it( 'returns false if query matches the start of the context', () => {
  1955. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1956. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  1957. } );
  1958. it( 'returns false if query does not match', () => {
  1959. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  1960. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  1961. } );
  1962. it( 'returns false if query is longer than context', () => {
  1963. const ctx = new SchemaContext( [ 'foo' ] );
  1964. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  1965. } );
  1966. } );
  1967. } );