schema.js 81 KB

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