8
0

schema.js 95 KB

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