schema.js 91 KB

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