schema.js 76 KB

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