schema.js 73 KB

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