schema.js 88 KB

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