8
0

schema.js 94 KB

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