8
0

schema.js 79 KB

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