8
0

schema.js 94 KB

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