schema.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  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 { getData, setData, stringify, parse } from '../../src/dev-utils/model';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  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. const data = '<div><section><article><paragraph>foobar</paragraph></article></section></div>';
  776. const parsedModel = parse( data, model.schema, { context: [ root.name ] } );
  777. model.change( writer => {
  778. writer.insert( parsedModel, root );
  779. } );
  780. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  781. expect( schema.getLimitElement( new Range( new Position( root, [ 0, 0, 0, 0, 2 ] ) ) ) ).to.equal( article );
  782. } );
  783. it( 'accepts position as an argument', () => {
  784. schema.extend( 'article', { isLimit: true } );
  785. schema.extend( 'section', { isLimit: true } );
  786. const data = '<div><section><article><paragraph>foobar</paragraph></article></section></div>';
  787. const parsedModel = parse( data, model.schema, { context: [ root.name ] } );
  788. model.change( writer => {
  789. writer.insert( parsedModel, root );
  790. } );
  791. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  792. expect( schema.getLimitElement( new Position( root, [ 0, 0, 0, 0, 2 ] ) ) ).to.equal( article );
  793. } );
  794. } );
  795. describe( 'checkAttributeInSelection()', () => {
  796. const attribute = 'bold';
  797. let model, doc, schema;
  798. beforeEach( () => {
  799. model = new Model();
  800. doc = model.document;
  801. doc.createRoot();
  802. schema = model.schema;
  803. schema.register( 'p', { inheritAllFrom: '$block' } );
  804. schema.register( 'h1', { inheritAllFrom: '$block' } );
  805. schema.register( 'img', { allowWhere: '$text' } );
  806. schema.register( 'figure', {
  807. allowIn: '$root',
  808. allowAttributes: [ 'name', 'title' ]
  809. } );
  810. schema.addAttributeCheck( ( ctx, attributeName ) => {
  811. // Allow 'bold' on p>$text.
  812. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  813. return true;
  814. }
  815. // Allow 'bold' on $root>p.
  816. if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) {
  817. return true;
  818. }
  819. } );
  820. } );
  821. describe( 'when selection is collapsed', () => {
  822. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  823. setData( model, '<p>f[]oo</p>' );
  824. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  825. } );
  826. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  827. setData( model, '<h1>[]</h1>' );
  828. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  829. setData( model, '[]' );
  830. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  831. } );
  832. } );
  833. describe( 'when selection is not collapsed', () => {
  834. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  835. // Simple selection on a few characters.
  836. setData( model, '<p>[foo]</p>' );
  837. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  838. // Selection spans over characters but also include nodes that can't have attribute.
  839. setData( model, '<p>fo[o<img />b]ar</p>' );
  840. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  841. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  842. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  843. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  844. // Selection on empty P. P can have the attribute.
  845. setData( model, '[<p></p>]' );
  846. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  847. } );
  848. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  849. // Selection on DIV which can't have bold text.
  850. setData( model, '[<h1></h1>]' );
  851. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  852. // Selection on two images which can't be bold.
  853. setData( model, '<p>foo[<img /><img />]bar</p>' );
  854. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  855. } );
  856. it( 'should return true when checking element with required attribute', () => {
  857. setData( model, '[<figure name="figure"></figure>]' );
  858. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  859. } );
  860. it( 'should return true when checking element when attribute is already present', () => {
  861. setData( model, '[<figure name="figure" title="title"></figure>]' );
  862. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  863. } );
  864. } );
  865. } );
  866. describe( 'getValidRanges()', () => {
  867. let model, doc, root, schema;
  868. beforeEach( () => {
  869. model = new Model();
  870. doc = model.document;
  871. schema = model.schema;
  872. root = doc.createRoot();
  873. schema.register( 'p', { inheritAllFrom: '$block' } );
  874. schema.register( 'img', { allowWhere: '$text' } );
  875. // This is a "hack" to allow setting any ranges in `setData` util.
  876. schema.extend( '$text', { allowIn: '$root' } );
  877. } );
  878. function test( input, attribute, output ) {
  879. setData( model, input );
  880. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  881. const sel = new Selection( validRanges );
  882. expect( stringify( root, sel ) ).to.equal( output );
  883. }
  884. it( 'should return a range with p for an attribute allowed only on p', () => {
  885. schema.extend( 'p', { allowAttributes: 'foo' } );
  886. test(
  887. '[<p>foo<img></img>bar</p>]',
  888. 'foo',
  889. '[<p>foo<img></img>bar</p>]'
  890. );
  891. } );
  892. it( 'should return ranges on text nodes for an attribute allowed only on text', () => {
  893. schema.extend( '$text', { allowAttributes: 'bold' } );
  894. test(
  895. '[<p>foo<img></img>bar</p>]',
  896. 'bold',
  897. '<p>[foo]<img></img>[bar]</p>'
  898. );
  899. } );
  900. it( 'should return a range on img for an attribute allowed only on img', () => {
  901. schema.extend( 'img', { allowAttributes: 'src' } );
  902. test(
  903. '[<p>foo<img></img>bar</p>]',
  904. 'src',
  905. '<p>foo[<img></img>]bar</p>'
  906. );
  907. } );
  908. it( 'should return a range containing all children for an attribute allowed on all children', () => {
  909. schema.extend( '$text', { allowAttributes: 'bold' } );
  910. schema.extend( 'img', { allowAttributes: 'bold' } );
  911. test(
  912. '[<p>foo<img></img>bar</p>]',
  913. 'bold',
  914. '<p>[foo<img></img>bar]</p>'
  915. );
  916. } );
  917. it( 'should return a range with p and a range on all children for an attribute allowed on p and its children', () => {
  918. schema.extend( 'p', { allowAttributes: 'foo' } );
  919. schema.extend( '$text', { allowAttributes: 'foo' } );
  920. schema.extend( 'img', { allowAttributes: 'foo' } );
  921. setData( model, '[<p>foo<img></img>bar</p>]' );
  922. const validRanges = Array.from( schema.getValidRanges( doc.selection.getRanges(), 'foo' ) );
  923. expect( validRanges.length ).to.equal( 2 );
  924. expect( validRanges[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  925. expect( validRanges[ 0 ].end.path ).to.deep.equal( [ 0, 7 ] );
  926. expect( validRanges[ 1 ].start.path ).to.deep.equal( [ 0 ] );
  927. expect( validRanges[ 1 ].end.path ).to.deep.equal( [ 1 ] );
  928. } );
  929. it( 'should not break a range if children are not allowed to have the attribute', () => {
  930. schema.extend( 'p', { allowAttributes: 'foo' } );
  931. test(
  932. '[<p>foo</p><p>bar</p>]',
  933. 'foo',
  934. '[<p>foo</p><p>bar</p>]'
  935. );
  936. } );
  937. it( 'should search deeply', () => {
  938. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  939. test(
  940. '[<p>foo<img>xxx</img>bar</p>]',
  941. 'bold',
  942. '<p>[foo]<img>[xxx]</img>[bar]</p>'
  943. );
  944. } );
  945. it( 'should work with multiple ranges', () => {
  946. schema.extend( '$text', { allowAttributes: 'bold' } );
  947. test(
  948. '[<p>a</p><p>b</p>]<p>c</p><p>[d]</p>',
  949. 'bold',
  950. '<p>[a]</p><p>[b]</p><p>c</p><p>[d]</p>'
  951. );
  952. } );
  953. it( 'should work with non-flat ranges', () => {
  954. schema.extend( '$text', { allowAttributes: 'bold' } );
  955. test(
  956. '[<p>a</p><p>b</p><p>c]</p><p>d</p>',
  957. 'bold',
  958. '<p>[a]</p><p>[b]</p><p>[c]</p><p>d</p>'
  959. );
  960. } );
  961. it( 'should not leak beyond the given ranges', () => {
  962. schema.extend( '$text', { allowAttributes: 'bold' } );
  963. test(
  964. '[<p>foo</p><p>b]a[r</p><p>x]yz</p>',
  965. 'bold',
  966. '<p>[foo]</p><p>[b]a[r]</p><p>[x]yz</p>'
  967. );
  968. } );
  969. it( 'should correctly handle a range which ends in a disallowed position', () => {
  970. schema.extend( '$text', { allowAttributes: 'bold', allowIn: 'img' } );
  971. // Disallow bold on text inside image.
  972. schema.addAttributeCheck( ( ctx, attributeName ) => {
  973. if ( ctx.endsWith( 'img $text' ) && attributeName == 'bold' ) {
  974. return false;
  975. }
  976. } );
  977. test(
  978. '[<p>foo<img>xx]x</img>bar</p>',
  979. 'bold',
  980. '<p>[foo]<img>xxx</img>bar</p>'
  981. );
  982. } );
  983. } );
  984. describe( 'getNearestSelectionRange()', () => {
  985. let selection, model, doc;
  986. beforeEach( () => {
  987. model = new Model();
  988. doc = model.document;
  989. schema = model.schema;
  990. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  991. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  992. model.schema.register( 'widget', {
  993. allowIn: '$root',
  994. isObject: true
  995. } );
  996. model.schema.register( 'blockWidget', {
  997. allowIn: '$root',
  998. allowContentOf: '$block',
  999. isObject: true
  1000. } );
  1001. doc.createRoot();
  1002. selection = doc.selection;
  1003. } );
  1004. test(
  1005. 'should return collapsed range if text node can be placed at that position - both',
  1006. '<paragraph>[]</paragraph>',
  1007. 'both',
  1008. '<paragraph>[]</paragraph>'
  1009. );
  1010. test(
  1011. 'should return collapsed range if text node can be placed at that position - forward',
  1012. '<paragraph>[]</paragraph>',
  1013. 'forward',
  1014. '<paragraph>[]</paragraph>'
  1015. );
  1016. test(
  1017. 'should return collapsed range if text node can be placed at that position - backward',
  1018. '<paragraph>[]</paragraph>',
  1019. 'backward',
  1020. '<paragraph>[]</paragraph>'
  1021. );
  1022. test( 'should return null in empty document - both', '', 'both', null );
  1023. test( 'should return null in empty document - backward', '', 'backward', null );
  1024. test( 'should return null in empty document - forward', '', 'forward', null );
  1025. test(
  1026. 'should find range before when searching both ways',
  1027. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1028. 'both',
  1029. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1030. );
  1031. test(
  1032. 'should find range before when searching backward',
  1033. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1034. 'backward',
  1035. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1036. );
  1037. test(
  1038. 'should find range after when searching forward',
  1039. '<paragraph></paragraph>[]<paragraph></paragraph>',
  1040. 'forward',
  1041. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1042. );
  1043. test(
  1044. 'should find range after when searching both ways when it is closer',
  1045. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  1046. 'both',
  1047. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  1048. );
  1049. test(
  1050. 'should find range before when searching both ways when it is closer',
  1051. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  1052. 'both',
  1053. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  1054. );
  1055. test(
  1056. 'should return null if there is no valid range',
  1057. '[]<emptyBlock></emptyBlock>',
  1058. 'both',
  1059. null
  1060. );
  1061. test(
  1062. 'should return null if there is no valid range in given direction - backward',
  1063. '[]<paragraph></paragraph>',
  1064. 'backward',
  1065. null
  1066. );
  1067. test(
  1068. 'should return null if there is no valid range in given direction - forward',
  1069. '<paragraph></paragraph>[]',
  1070. 'forward',
  1071. null
  1072. );
  1073. test(
  1074. 'should move forward when placed at root start',
  1075. '[]<paragraph></paragraph><paragraph></paragraph>',
  1076. 'both',
  1077. '<paragraph>[]</paragraph><paragraph></paragraph>'
  1078. );
  1079. test(
  1080. 'should move backward when placed at root end',
  1081. '<paragraph></paragraph><paragraph></paragraph>[]',
  1082. 'both',
  1083. '<paragraph></paragraph><paragraph>[]</paragraph>'
  1084. );
  1085. describe( 'in case of objects which do not allow text inside', () => {
  1086. test(
  1087. 'should select nearest object (o[]o) - both',
  1088. '<widget></widget>[]<widget></widget>',
  1089. 'both',
  1090. '[<widget></widget>]<widget></widget>'
  1091. );
  1092. test(
  1093. 'should select nearest object (o[]o) - forward',
  1094. '<widget></widget>[]<widget></widget>',
  1095. 'forward',
  1096. '<widget></widget>[<widget></widget>]'
  1097. );
  1098. test(
  1099. 'should select nearest object (o[]o) - backward',
  1100. '<widget></widget>[]<widget></widget>',
  1101. 'both',
  1102. '[<widget></widget>]<widget></widget>'
  1103. );
  1104. test(
  1105. 'should select nearest object (p[]o) - forward',
  1106. '<paragraph></paragraph>[]<widget></widget>',
  1107. 'forward',
  1108. '<paragraph></paragraph>[<widget></widget>]'
  1109. );
  1110. test(
  1111. 'should select nearest object (o[]p) - both',
  1112. '<widget></widget>[]<paragraph></paragraph>',
  1113. 'both',
  1114. '[<widget></widget>]<paragraph></paragraph>'
  1115. );
  1116. test(
  1117. 'should select nearest object (o[]p) - backward',
  1118. '<widget></widget>[]<paragraph></paragraph>',
  1119. 'backward',
  1120. '[<widget></widget>]<paragraph></paragraph>'
  1121. );
  1122. test(
  1123. 'should select nearest object ([]o) - both',
  1124. '[]<widget></widget><paragraph></paragraph>',
  1125. 'both',
  1126. '[<widget></widget>]<paragraph></paragraph>'
  1127. );
  1128. test(
  1129. 'should select nearest object ([]o) - forward',
  1130. '[]<widget></widget><paragraph></paragraph>',
  1131. 'forward',
  1132. '[<widget></widget>]<paragraph></paragraph>'
  1133. );
  1134. test(
  1135. 'should select nearest object (o[]) - both',
  1136. '<paragraph></paragraph><widget></widget>[]',
  1137. 'both',
  1138. '<paragraph></paragraph>[<widget></widget>]'
  1139. );
  1140. test(
  1141. 'should select nearest object (o[]) - backward',
  1142. '<paragraph></paragraph><widget></widget>[]',
  1143. 'both',
  1144. '<paragraph></paragraph>[<widget></widget>]'
  1145. );
  1146. } );
  1147. describe( 'in case of objects which allow text inside', () => {
  1148. test(
  1149. 'should select nearest object which allows text (o[]o) - both',
  1150. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  1151. 'both',
  1152. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  1153. );
  1154. test(
  1155. 'should select nearest object (o[]p) - both',
  1156. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  1157. 'both',
  1158. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1159. );
  1160. test(
  1161. 'should select nearest object which allows text ([]o) - both',
  1162. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  1163. 'both',
  1164. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  1165. );
  1166. } );
  1167. function test( testName, data, direction, expected ) {
  1168. it( testName, () => {
  1169. let range;
  1170. model.enqueueChange( 'transparent', () => {
  1171. setData( model, data );
  1172. range = schema.getNearestSelectionRange( selection.anchor, direction );
  1173. } );
  1174. if ( expected === null ) {
  1175. expect( range ).to.be.null;
  1176. } else {
  1177. model.change( writer => {
  1178. writer.setSelection( range );
  1179. } );
  1180. expect( getData( model ) ).to.equal( expected );
  1181. }
  1182. } );
  1183. }
  1184. } );
  1185. describe( 'findAllowedParent()', () => {
  1186. beforeEach( () => {
  1187. schema.register( '$root' );
  1188. schema.register( 'blockQuote', {
  1189. allowIn: '$root'
  1190. } );
  1191. schema.register( 'paragraph', {
  1192. allowIn: 'blockQuote'
  1193. } );
  1194. schema.register( '$text', {
  1195. allowIn: 'paragraph'
  1196. } );
  1197. } );
  1198. it( 'should return position ancestor that allows to insert given node to it', () => {
  1199. const node = new Element( 'paragraph' );
  1200. const allowedParent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1201. expect( allowedParent ).to.equal( r1bQ );
  1202. } );
  1203. it( 'should return position ancestor that allows to insert given node to it when position is already i such an element', () => {
  1204. const node = new Text( 'text' );
  1205. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1206. expect( parent ).to.equal( r1bQp );
  1207. } );
  1208. it( 'should return null when limit element is reached before allowed parent', () => {
  1209. schema.extend( 'blockQuote', {
  1210. isLimit: true
  1211. } );
  1212. schema.register( 'div', {
  1213. allowIn: '$root'
  1214. } );
  1215. const node = new Element( 'div' );
  1216. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1217. expect( parent ).to.null;
  1218. } );
  1219. it( 'should return null when object element is reached before allowed parent', () => {
  1220. schema.extend( 'blockQuote', {
  1221. isObject: true
  1222. } );
  1223. schema.register( 'div', {
  1224. allowIn: '$root'
  1225. } );
  1226. const node = new Element( 'div' );
  1227. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1228. expect( parent ).to.null;
  1229. } );
  1230. it( 'should return null when there is no allowed ancestor for given position', () => {
  1231. const node = new Element( 'section' );
  1232. const parent = schema.findAllowedParent( node, Position.createAt( r1bQp ) );
  1233. expect( parent ).to.null;
  1234. } );
  1235. } );
  1236. describe( 'removeDisallowedAttributes()', () => {
  1237. let model, doc, root;
  1238. beforeEach( () => {
  1239. model = new Model();
  1240. doc = model.document;
  1241. root = doc.createRoot();
  1242. schema = model.schema;
  1243. schema.register( 'paragraph', {
  1244. inheritAllFrom: '$block'
  1245. } );
  1246. schema.register( 'div', {
  1247. inheritAllFrom: '$block'
  1248. } );
  1249. schema.register( 'image', {
  1250. isObject: true
  1251. } );
  1252. schema.extend( '$block', {
  1253. allowIn: 'div'
  1254. } );
  1255. } );
  1256. it( 'should filter out disallowed attributes from given nodes', () => {
  1257. schema.extend( '$text', { allowAttributes: 'a' } );
  1258. schema.extend( 'image', { allowAttributes: 'b' } );
  1259. const text = new Text( 'foo', { a: 1, b: 1 } );
  1260. const image = new Element( 'image', { a: 1, b: 1 } );
  1261. root._appendChild( [ text, image ] );
  1262. model.change( writer => {
  1263. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1264. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  1265. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  1266. expect( writer.batch.operations ).to.length( 2 );
  1267. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1268. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1269. expect( getData( model, { withoutSelection: true } ) )
  1270. .to.equal( '<$text a="1">foo</$text><image b="1"></image>' );
  1271. } );
  1272. } );
  1273. it( 'should filter out disallowed attributes from all descendants of given nodes', () => {
  1274. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1275. // Allow 'a' on div>$text.
  1276. if ( ctx.endsWith( 'div $text' ) && attributeName == 'a' ) {
  1277. return true;
  1278. }
  1279. // Allow 'b' on div>paragraph>$text.
  1280. if ( ctx.endsWith( 'div paragraph $text' ) && attributeName == 'b' ) {
  1281. return true;
  1282. }
  1283. // Allow 'a' on div>image.
  1284. if ( ctx.endsWith( 'div image' ) && attributeName == 'a' ) {
  1285. return true;
  1286. }
  1287. // Allow 'b' on div>paragraph>image.
  1288. if ( ctx.endsWith( 'div paragraph image' ) && attributeName == 'b' ) {
  1289. return true;
  1290. }
  1291. } );
  1292. const foo = new Text( 'foo', { a: 1, b: 1 } );
  1293. const bar = new Text( 'bar', { a: 1, b: 1 } );
  1294. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  1295. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  1296. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  1297. const div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  1298. root._appendChild( [ div ] );
  1299. model.change( writer => {
  1300. schema.removeDisallowedAttributes( root.getChildren(), writer );
  1301. expect( writer.batch.operations ).to.length( 4 );
  1302. expect( writer.batch.operations[ 0 ] ).to.instanceof( AttributeOperation );
  1303. expect( writer.batch.operations[ 1 ] ).to.instanceof( AttributeOperation );
  1304. expect( writer.batch.operations[ 2 ] ).to.instanceof( AttributeOperation );
  1305. expect( writer.batch.operations[ 3 ] ).to.instanceof( AttributeOperation );
  1306. expect( getData( model, { withoutSelection: true } ) )
  1307. .to.equal(
  1308. '<div>' +
  1309. '<paragraph>' +
  1310. '<$text b="1">foo</$text>' +
  1311. '<image b="1"></image>' +
  1312. '</paragraph>' +
  1313. '<$text a="1">bar</$text>' +
  1314. '<image a="1"></image>' +
  1315. '</div>'
  1316. );
  1317. } );
  1318. } );
  1319. } );
  1320. describe( 'definitions compilation', () => {
  1321. describe( 'allowIn cases', () => {
  1322. it( 'passes $root>paragraph', () => {
  1323. schema.register( '$root' );
  1324. schema.register( 'paragraph', {
  1325. allowIn: '$root'
  1326. } );
  1327. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1328. } );
  1329. it( 'passes $root>paragraph and $root2>paragraph – support for array values', () => {
  1330. schema.register( '$root' );
  1331. schema.register( '$root2' );
  1332. schema.register( 'paragraph', {
  1333. allowIn: [ '$root', '$root2' ]
  1334. } );
  1335. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1336. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1337. } );
  1338. it( 'passes $root>paragraph[align] – attributes does not matter', () => {
  1339. schema.register( '$root' );
  1340. schema.register( 'paragraph', {
  1341. allowIn: '$root'
  1342. } );
  1343. expect( schema.checkChild( root1, r1p2 ) ).to.be.true;
  1344. } );
  1345. it( 'passes $root>div>div – in case of circular refs', () => {
  1346. schema.register( '$root' );
  1347. schema.register( 'div', {
  1348. allowIn: [ '$root', 'div' ]
  1349. } );
  1350. const div = new Element( 'div' );
  1351. root1._appendChild( div );
  1352. const div2 = new Element( 'div' );
  1353. expect( schema.checkChild( div, div2 ) ).to.be.true;
  1354. } );
  1355. it( 'passes $root>div>div – in case of circular refs, when div1==div2', () => {
  1356. schema.register( '$root' );
  1357. schema.register( 'div', {
  1358. allowIn: [ '$root', 'div' ]
  1359. } );
  1360. const div = new Element( 'div' );
  1361. root1._appendChild( div );
  1362. expect( schema.checkChild( div, div ) ).to.be.true;
  1363. } );
  1364. it( 'rejects $root>paragraph – non-registered paragraph', () => {
  1365. schema.register( '$root' );
  1366. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1367. } );
  1368. it( 'rejects $root>paragraph – registered different item', () => {
  1369. schema.register( '$root' );
  1370. schema.register( 'paragraph' );
  1371. schema.register( 'listItem', {
  1372. allowIn: '$root'
  1373. } );
  1374. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1375. } );
  1376. it( 'rejects $root>paragraph – paragraph allowed in different context', () => {
  1377. schema.register( '$root' );
  1378. schema.register( '$fancyRoot' );
  1379. schema.register( 'paragraph', {
  1380. allowIn: '$fancyRoot'
  1381. } );
  1382. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1383. } );
  1384. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root', () => {
  1385. schema.register( '$root' );
  1386. schema.register( 'paragraph', {
  1387. allowIn: '$root'
  1388. } );
  1389. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1390. } );
  1391. it( 'rejects $root>blockQuote>paragraph – since paragraph is only allowed in $root v2', () => {
  1392. schema.register( '$root' );
  1393. schema.register( 'blockQuote', {
  1394. allowIn: '$root'
  1395. } );
  1396. schema.register( 'paragraph', {
  1397. allowIn: '$root'
  1398. } );
  1399. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.false;
  1400. } );
  1401. it( 'rejects $root>blockQuote>paragraph>$text - since paragraph is not allowed in blockQuote', () => {
  1402. schema.register( '$root' );
  1403. schema.register( 'paragraph', {
  1404. allowIn: '$root'
  1405. } );
  1406. schema.register( '$text', {
  1407. allowIn: 'paragraph'
  1408. } );
  1409. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1410. } );
  1411. it( 'rejects $root>blockQuote>paragraph>$text - since blockQuote is not allowed in $root', () => {
  1412. schema.register( '$root' );
  1413. schema.register( 'blockQuote' );
  1414. schema.register( 'paragraph', {
  1415. allowIn: [ 'blockQuote', '$root' ]
  1416. } );
  1417. schema.register( '$text', {
  1418. allowIn: 'paragraph'
  1419. } );
  1420. expect( schema.checkChild( root1, r1bQp.getChild( 0 ) ) ).to.be.false;
  1421. } );
  1422. } );
  1423. describe( 'allowWhere cases', () => {
  1424. it( 'passes $root>paragraph – paragraph inherits from $block', () => {
  1425. schema.register( '$root' );
  1426. schema.register( '$block', {
  1427. allowIn: '$root'
  1428. } );
  1429. schema.register( 'paragraph', {
  1430. allowWhere: '$block'
  1431. } );
  1432. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1433. } );
  1434. it( 'supports the array syntax', () => {
  1435. schema.register( '$root' );
  1436. schema.register( '$root2' );
  1437. schema.register( '$block', {
  1438. allowIn: '$root'
  1439. } );
  1440. schema.register( '$block2', {
  1441. allowIn: '$root2'
  1442. } );
  1443. schema.register( 'paragraph', {
  1444. allowWhere: [ '$block', '$block2' ]
  1445. } );
  1446. expect( schema.checkChild( root1, r1p1 ), '$root' ).to.be.true;
  1447. expect( schema.checkChild( root2, r1p1 ), '$root2' ).to.be.true;
  1448. } );
  1449. // This checks if some inapropriate caching or preprocessing isn't applied by register().
  1450. it( 'passes $root>paragraph – paragraph inherits from $block, order of definitions does not matter', () => {
  1451. schema.register( '$root' );
  1452. schema.register( 'paragraph', {
  1453. allowWhere: '$block'
  1454. } );
  1455. schema.register( '$block', {
  1456. allowIn: '$root'
  1457. } );
  1458. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1459. } );
  1460. it( 'passes $root>paragraph – paragraph inherits from $specialBlock which inherits from $block', () => {
  1461. schema.register( '$root' );
  1462. schema.register( '$block', {
  1463. allowIn: '$root'
  1464. } );
  1465. schema.register( '$specialBlock', {
  1466. allowWhere: '$block'
  1467. } );
  1468. schema.register( 'paragraph', {
  1469. allowWhere: '$specialBlock'
  1470. } );
  1471. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1472. } );
  1473. it( 'rejects $root>paragraph – paragraph inherits from $block but $block is not allowed in $root', () => {
  1474. schema.register( '$root' );
  1475. schema.register( '$block' );
  1476. schema.register( 'paragraph', {
  1477. allowWhere: '$block'
  1478. } );
  1479. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1480. } );
  1481. it( 'rejects $root>paragraph>$text – paragraph inherits from $block but $block is not allowed in $root', () => {
  1482. schema.register( '$root' );
  1483. schema.register( '$block' );
  1484. schema.register( 'paragraph', {
  1485. allowWhere: '$block'
  1486. } );
  1487. schema.register( '$text', {
  1488. allowIn: 'paragraph'
  1489. } );
  1490. expect( schema.checkChild( root1, r1p1.getChild( 0 ) ) ).to.be.false;
  1491. } );
  1492. } );
  1493. describe( 'allowContentOf cases', () => {
  1494. it( 'passes $root2>paragraph – $root2 inherits from $root', () => {
  1495. schema.register( '$root' );
  1496. schema.register( 'paragraph', {
  1497. allowIn: '$root'
  1498. } );
  1499. schema.register( '$root2', {
  1500. allowContentOf: '$root'
  1501. } );
  1502. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1503. } );
  1504. it( 'supports the array syntax', () => {
  1505. schema.register( '$root' );
  1506. schema.register( '$root2' );
  1507. schema.register( 'paragraph', {
  1508. allowIn: '$root'
  1509. } );
  1510. schema.register( 'heading1', {
  1511. allowIn: '$root2'
  1512. } );
  1513. schema.register( '$root3', {
  1514. allowContentOf: [ '$root', '$root2' ]
  1515. } );
  1516. const root3 = new Element( '$root3' );
  1517. const heading1 = new Element( 'heading1' );
  1518. expect( schema.checkChild( root3, r1p1 ), 'paragraph' ).to.be.true;
  1519. expect( schema.checkChild( root3, heading1 ), 'heading1' ).to.be.true;
  1520. } );
  1521. it( 'passes $root2>paragraph – $root2 inherits from $root, order of definitions does not matter', () => {
  1522. schema.register( '$root' );
  1523. schema.register( '$root2', {
  1524. allowContentOf: '$root'
  1525. } );
  1526. schema.register( 'paragraph', {
  1527. allowIn: '$root'
  1528. } );
  1529. expect( schema.checkChild( root2, r1p1 ) ).to.be.true;
  1530. } );
  1531. it( 'passes $root>paragraph>$text – paragraph inherits content of $block', () => {
  1532. schema.register( '$root' );
  1533. schema.register( '$block' );
  1534. schema.register( 'paragraph', {
  1535. allowIn: '$root',
  1536. allowContentOf: '$block'
  1537. } );
  1538. schema.register( '$text', {
  1539. allowIn: '$block'
  1540. } );
  1541. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1542. } );
  1543. it( 'passes $root>blockQuote>paragraph – blockQuote inherits content of $root', () => {
  1544. schema.register( '$root' );
  1545. schema.register( 'blockQuote', {
  1546. allowIn: '$root',
  1547. allowContentOf: '$root'
  1548. } );
  1549. schema.register( 'paragraph', {
  1550. allowIn: '$root'
  1551. } );
  1552. expect( schema.checkChild( r1bQ, r1bQp ) ).to.be.true;
  1553. } );
  1554. it( 'rejects $root2>paragraph – $root2 inherits from $root, but paragraph is not allowed there anyway', () => {
  1555. schema.register( '$root' );
  1556. schema.register( 'paragraph' );
  1557. schema.register( '$root2', {
  1558. allowContentOf: '$root'
  1559. } );
  1560. expect( schema.checkChild( root2, r1p1 ) ).to.be.false;
  1561. } );
  1562. } );
  1563. describe( 'mix of allowContentOf and allowWhere', () => {
  1564. it( 'passes $root>paragraph>$text – paragraph inherits all from $block', () => {
  1565. schema.register( '$root' );
  1566. schema.register( '$block', {
  1567. allowIn: '$root'
  1568. } );
  1569. schema.register( 'paragraph', {
  1570. allowContentOf: '$block',
  1571. allowWhere: '$block'
  1572. } );
  1573. schema.register( '$text', {
  1574. allowIn: '$block'
  1575. } );
  1576. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1577. } );
  1578. it( 'passes $root>paragraph and $root2>paragraph – where $root2 inherits content of $root' +
  1579. 'and paragraph inherits allowWhere from $block', () => {
  1580. schema.register( '$root' );
  1581. schema.register( '$root2', {
  1582. allowContentOf: '$root'
  1583. } );
  1584. schema.register( '$block', {
  1585. allowIn: '$root'
  1586. } );
  1587. schema.register( 'paragraph', {
  1588. allowWhere: '$block'
  1589. } );
  1590. expect( schema.checkChild( root1, 'paragraph' ), 'root1' ).to.be.true;
  1591. expect( schema.checkChild( root2, 'paragraph' ), 'root2' ).to.be.true;
  1592. } );
  1593. it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1594. schema.register( 'b' );
  1595. schema.register( 'a', { allowIn: 'b' } );
  1596. schema.register( 'c', { allowContentOf: 'b' } );
  1597. schema.register( 'd', { allowContentOf: 'c' } );
  1598. const d = new Element( 'd' );
  1599. expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1600. } );
  1601. // This case won't pass becuase we compile the definitions in a pretty naive way.
  1602. // To make chains like this work we'd need to repeat compilation of allowContentOf definitions
  1603. // as long as the previous iteration found something to compile.
  1604. // This way, even though we'd not compile d<-c in the first run, we'd still find b<-c
  1605. // and since we've found something, we'd now try d<-c which would work.
  1606. //
  1607. // We ignore those situations for now as they are very unlikely to happen and would
  1608. // significantly raised the complexity of definition compilation.
  1609. //
  1610. // it( 'passes d>a where d inherits content of c which inherits content of b', () => {
  1611. // schema.register( 'b' );
  1612. // schema.register( 'a', { allowIn: 'b' } );
  1613. // schema.register( 'd', { allowContentOf: 'c' } );
  1614. // schema.register( 'c', { allowContentOf: 'b' } );
  1615. //
  1616. // const d = new Element( 'd' );
  1617. //
  1618. // expect( schema.checkChild( d, 'a' ) ).to.be.true;
  1619. // } );
  1620. } );
  1621. describe( 'inheritTypesFrom', () => {
  1622. it( 'inherit properties of another item', () => {
  1623. schema.register( '$block', {
  1624. isBlock: true,
  1625. isLimit: true
  1626. } );
  1627. schema.register( 'paragraph', {
  1628. inheritTypesFrom: '$block'
  1629. } );
  1630. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1631. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1632. } );
  1633. it( 'inherit properties of other items – support for arrays', () => {
  1634. schema.register( '$block', {
  1635. isBlock: true
  1636. } );
  1637. schema.register( '$block2', {
  1638. isLimit: true
  1639. } );
  1640. schema.register( 'paragraph', {
  1641. inheritTypesFrom: [ '$block', '$block2' ]
  1642. } );
  1643. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1644. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.true;
  1645. } );
  1646. it( 'does not override existing props', () => {
  1647. schema.register( '$block', {
  1648. isBlock: true,
  1649. isLimit: true
  1650. } );
  1651. schema.register( 'paragraph', {
  1652. inheritTypesFrom: '$block',
  1653. isLimit: false
  1654. } );
  1655. expect( schema.getDefinition( 'paragraph' ).isBlock ).to.be.true;
  1656. expect( schema.getDefinition( 'paragraph' ).isLimit ).to.be.false;
  1657. } );
  1658. } );
  1659. describe( 'inheritAllFrom', () => {
  1660. it( 'passes $root>paragraph – paragraph inherits allowIn from $block', () => {
  1661. schema.register( '$root' );
  1662. schema.register( '$block', {
  1663. allowIn: '$root'
  1664. } );
  1665. schema.register( 'paragraph', {
  1666. inheritAllFrom: '$block'
  1667. } );
  1668. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1669. } );
  1670. it( 'paragraph inherit properties of $block', () => {
  1671. schema.register( '$block', {
  1672. isBlock: true
  1673. } );
  1674. schema.register( 'paragraph', {
  1675. inheritAllFrom: '$block'
  1676. } );
  1677. expect( schema.isBlock( r1p1 ) ).to.be.true;
  1678. } );
  1679. it( 'passes $root>paragraph>$text – paragraph inherits allowed content of $block', () => {
  1680. schema.register( '$root' );
  1681. schema.register( '$block', {
  1682. allowIn: '$root'
  1683. } );
  1684. schema.register( '$text', {
  1685. allowIn: '$block'
  1686. } );
  1687. schema.register( 'paragraph', {
  1688. inheritAllFrom: '$block'
  1689. } );
  1690. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1691. } );
  1692. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1693. schema.register( '$root' );
  1694. schema.register( '$blockProto', {
  1695. allowIn: '$root'
  1696. } );
  1697. schema.register( '$block', {
  1698. allowWhere: '$blockProto'
  1699. } );
  1700. schema.register( 'paragraph', {
  1701. inheritAllFrom: '$block'
  1702. } );
  1703. expect( schema.checkChild( root1, r1p1 ) ).to.be.true;
  1704. } );
  1705. it( 'passes $root>paragraph>$text – paragraph inherits allowIn from $block through $block\'s allowWhere', () => {
  1706. schema.register( '$root' );
  1707. schema.register( '$blockProto' );
  1708. schema.register( '$block', {
  1709. allowContentOf: '$blockProto',
  1710. allowIn: '$root'
  1711. } );
  1712. schema.register( '$text', {
  1713. allowIn: '$blockProto'
  1714. } );
  1715. schema.register( 'paragraph', {
  1716. inheritAllFrom: '$block'
  1717. } );
  1718. expect( schema.checkChild( r1p1, r1p1.getChild( 0 ) ) ).to.be.true;
  1719. } );
  1720. } );
  1721. // We need to handle cases where some independent features registered definitions which might use
  1722. // optional elements (elements which might not have been registered).
  1723. describe( 'missing structure definitions', () => {
  1724. it( 'does not break when trying to check a child which is not registered', () => {
  1725. schema.register( '$root' );
  1726. expect( schema.checkChild( root1, 'foo404' ) ).to.be.false;
  1727. } );
  1728. it( 'does not break when trying to check registered child in a context which contains non-registered elements', () => {
  1729. const foo404 = new Element( 'foo404' );
  1730. root1._appendChild( foo404 );
  1731. schema.register( '$root' );
  1732. schema.register( '$text', {
  1733. allowIn: '$root'
  1734. } );
  1735. expect( schema.checkChild( foo404, '$text' ) ).to.be.false;
  1736. } );
  1737. it( 'does not break when used allowedIn pointing to an non-registered element', () => {
  1738. schema.register( '$root' );
  1739. schema.register( '$text', {
  1740. allowIn: 'foo404'
  1741. } );
  1742. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1743. } );
  1744. it( 'does not break when used allowWhere pointing to an non-registered element', () => {
  1745. schema.register( '$root' );
  1746. schema.register( '$text', {
  1747. allowWhere: 'foo404'
  1748. } );
  1749. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  1750. } );
  1751. it( 'does not break when used allowContentOf pointing to an non-registered element', () => {
  1752. schema.register( '$root', {
  1753. allowContentOf: 'foo404'
  1754. } );
  1755. schema.register( '$text', {
  1756. allowIn: '$root'
  1757. } );
  1758. expect( schema.checkChild( root1, '$text' ) ).to.be.true;
  1759. } );
  1760. it( 'checks whether allowIn uses a registered element', () => {
  1761. schema.register( 'paragraph', {
  1762. allowIn: '$root'
  1763. } );
  1764. // $root isn't registered!
  1765. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.false;
  1766. } );
  1767. it( 'does not break when inheriting all from an non-registered element', () => {
  1768. schema.register( 'paragraph', {
  1769. inheritAllFrom: '$block'
  1770. } );
  1771. expect( schema.checkChild( root1, r1p1 ) ).to.be.false;
  1772. } );
  1773. } );
  1774. describe( 'allowAttributes', () => {
  1775. it( 'passes paragraph[align]', () => {
  1776. schema.register( 'paragraph', {
  1777. allowAttributes: 'align'
  1778. } );
  1779. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1780. } );
  1781. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1782. schema.register( 'paragraph', {
  1783. allowAttributes: [ 'align', 'dir' ]
  1784. } );
  1785. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1786. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1787. } );
  1788. it( 'passes paragraph>$text[bold]', () => {
  1789. schema.register( 'paragraph' );
  1790. schema.register( '$text', {
  1791. allowIn: 'paragraph',
  1792. allowAttributes: 'bold'
  1793. } );
  1794. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  1795. } );
  1796. } );
  1797. describe( 'allowAttributesOf', () => {
  1798. it( 'passes paragraph[align] – paragraph inherits from $block', () => {
  1799. schema.register( '$block', {
  1800. allowAttributes: 'align'
  1801. } );
  1802. schema.register( 'paragraph', {
  1803. allowAttributesOf: '$block'
  1804. } );
  1805. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1806. } );
  1807. it( 'passes paragraph[align] and paragraph[dir] – support for array values', () => {
  1808. schema.register( '$block', {
  1809. allowAttributes: 'align'
  1810. } );
  1811. schema.register( '$block2', {
  1812. allowAttributes: 'dir'
  1813. } );
  1814. schema.register( 'paragraph', {
  1815. allowAttributesOf: [ '$block', '$block2' ]
  1816. } );
  1817. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1818. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1819. } );
  1820. it( 'passes paragraph[align] and paragraph[dir] – support for combined allowAttributes and allowAttributesOf', () => {
  1821. schema.register( '$block', {
  1822. allowAttributes: 'align'
  1823. } );
  1824. schema.register( 'paragraph', {
  1825. allowAttributes: 'dir',
  1826. allowAttributesOf: '$block'
  1827. } );
  1828. expect( schema.checkAttribute( r1p1, 'align' ), 'align' ).to.be.true;
  1829. expect( schema.checkAttribute( r1p1, 'dir' ), 'dir' ).to.be.true;
  1830. } );
  1831. // The support for allowAttributesOf is broken in the similar way as for allowContentOf (see the comment above).
  1832. // However, those situations are rather theoretical, so we're not going to waste time on them now.
  1833. } );
  1834. describe( 'inheritAllFrom', () => {
  1835. it( 'passes paragraph[align] – paragraph inherits attributes of $block', () => {
  1836. schema.register( '$block', {
  1837. allowAttributes: 'align'
  1838. } );
  1839. schema.register( 'paragraph', {
  1840. inheritAllFrom: '$block'
  1841. } );
  1842. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1843. } );
  1844. it( 'passes paragraph[align] – paragraph inherits attributes of $block through allowAttributesOf', () => {
  1845. schema.register( '$blockProto', {
  1846. allowAttributes: 'align'
  1847. } );
  1848. schema.register( '$block', {
  1849. allowAttributesOf: '$blockProto'
  1850. } );
  1851. schema.register( 'paragraph', {
  1852. inheritAllFrom: '$block'
  1853. } );
  1854. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.true;
  1855. } );
  1856. } );
  1857. describe( 'missing attribute definitions', () => {
  1858. it( 'does not crash when checking an attribute of a non-registered element', () => {
  1859. expect( schema.checkAttribute( r1p1, 'align' ) ).to.be.false;
  1860. } );
  1861. it( 'does not crash when inheriting attributes of a non-registered element', () => {
  1862. schema.register( 'paragraph', {
  1863. allowAttributesOf: '$block'
  1864. } );
  1865. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1866. } );
  1867. it( 'does not crash when inheriting all from a non-registered element', () => {
  1868. schema.register( 'paragraph', {
  1869. allowAttributesOf: '$block'
  1870. } );
  1871. expect( schema.checkAttribute( r1p1, 'whatever' ) ).to.be.false;
  1872. } );
  1873. } );
  1874. describe( 'missing types definitions', () => {
  1875. it( 'does not crash when inheriting types of an non-registered element', () => {
  1876. schema.register( 'paragraph', {
  1877. inheritTypesFrom: '$block'
  1878. } );
  1879. expect( schema.getDefinition( 'paragraph' ) ).to.be.an( 'object' );
  1880. } );
  1881. } );
  1882. } );
  1883. describe( 'real scenarios', () => {
  1884. let r1bQi, r1i, r1lI, r1h, r1bQlI;
  1885. const definitions = [
  1886. () => {
  1887. schema.register( 'paragraph', {
  1888. inheritAllFrom: '$block'
  1889. } );
  1890. },
  1891. () => {
  1892. schema.register( 'heading1', {
  1893. inheritAllFrom: '$block'
  1894. } );
  1895. },
  1896. () => {
  1897. schema.register( 'listItem', {
  1898. inheritAllFrom: '$block',
  1899. allowAttributes: [ 'listIndent', 'listType' ]
  1900. } );
  1901. },
  1902. () => {
  1903. schema.register( 'blockQuote', {
  1904. allowWhere: '$block',
  1905. allowContentOf: '$root'
  1906. } );
  1907. // Disallow blockQuote in blockQuote.
  1908. schema.addChildCheck( ( ctx, childDef ) => {
  1909. if ( childDef.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
  1910. return false;
  1911. }
  1912. } );
  1913. },
  1914. () => {
  1915. schema.register( 'image', {
  1916. allowWhere: '$block',
  1917. allowAttributes: [ 'src', 'alt' ],
  1918. isObject: true,
  1919. isBlock: true
  1920. } );
  1921. },
  1922. () => {
  1923. schema.register( 'caption', {
  1924. allowIn: 'image',
  1925. allowContentOf: '$block',
  1926. isLimit: true
  1927. } );
  1928. },
  1929. () => {
  1930. schema.extend( '$text', {
  1931. allowAttributes: [ 'bold', 'italic' ]
  1932. } );
  1933. // Disallow bold in heading1.
  1934. schema.addAttributeCheck( ( ctx, attributeName ) => {
  1935. if ( ctx.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
  1936. return false;
  1937. }
  1938. } );
  1939. },
  1940. () => {
  1941. schema.extend( '$block', {
  1942. allowAttributes: 'alignment'
  1943. } );
  1944. }
  1945. ];
  1946. beforeEach( () => {
  1947. schema.register( '$root', {
  1948. isLimit: true
  1949. } );
  1950. schema.register( '$block', {
  1951. allowIn: '$root',
  1952. isBlock: true
  1953. } );
  1954. schema.register( '$text', {
  1955. allowIn: '$block'
  1956. } );
  1957. for ( const definition of definitions ) {
  1958. definition();
  1959. }
  1960. // or...
  1961. //
  1962. // Use the below code to shuffle the definitions.
  1963. // Don't look here, Szymon!
  1964. //
  1965. // const definitionsCopy = definitions.slice();
  1966. //
  1967. // while ( definitionsCopy.length ) {
  1968. // const r = Math.floor( Math.random() * definitionsCopy.length );
  1969. // definitionsCopy.splice( r, 1 )[ 0 ]();
  1970. // }
  1971. root1 = new Element( '$root', null, [
  1972. new Element( 'paragraph', null, 'foo' ),
  1973. new Element( 'paragraph', { alignment: 'right' }, 'bar' ),
  1974. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  1975. new Element( 'heading1', null, 'foo' ),
  1976. new Element( 'blockQuote', null, [
  1977. new Element( 'paragraph', null, 'foo' ),
  1978. new Element( 'listItem', { listType: 'x', listIndent: 0 }, 'foo' ),
  1979. new Element( 'image', null, [
  1980. new Element( 'caption', null, 'foo' )
  1981. ] )
  1982. ] ),
  1983. new Element( 'image', null, [
  1984. new Element( 'caption', null, 'foo' )
  1985. ] )
  1986. ] );
  1987. r1p1 = root1.getChild( 0 );
  1988. r1p2 = root1.getChild( 1 );
  1989. r1lI = root1.getChild( 2 );
  1990. r1h = root1.getChild( 3 );
  1991. r1bQ = root1.getChild( 4 );
  1992. r1i = root1.getChild( 5 );
  1993. r1bQp = r1bQ.getChild( 0 );
  1994. r1bQlI = r1bQ.getChild( 1 );
  1995. r1bQi = r1bQ.getChild( 2 );
  1996. } );
  1997. it( 'passes $root>paragraph', () => {
  1998. expect( schema.checkChild( root1, 'paragraph' ) ).to.be.true;
  1999. } );
  2000. it( 'passes $root>paragraph>$text', () => {
  2001. expect( schema.checkChild( r1p1, '$text' ), 'paragraph' ).to.be.true;
  2002. expect( schema.checkChild( r1p2, '$text' ), 'paragraph[alignment]' ).to.be.true;
  2003. } );
  2004. it( 'passes $root>listItem', () => {
  2005. expect( schema.checkChild( root1, 'listItem' ) ).to.be.true;
  2006. } );
  2007. it( 'passes $root>listItem>$text', () => {
  2008. expect( schema.checkChild( r1lI, '$text' ) ).to.be.true;
  2009. } );
  2010. it( 'passes $root>blockQuote>paragraph', () => {
  2011. expect( schema.checkChild( r1bQ, 'paragraph' ) ).to.be.true;
  2012. } );
  2013. it( 'passes $root>blockQuote>paragraph>$text', () => {
  2014. expect( schema.checkChild( r1bQp, '$text' ) ).to.be.true;
  2015. } );
  2016. it( 'passes $root>blockQuote>listItem', () => {
  2017. expect( schema.checkChild( r1bQ, 'listItem' ) ).to.be.true;
  2018. } );
  2019. it( 'passes $root>blockQuote>listItem>$text', () => {
  2020. expect( schema.checkChild( r1bQlI, '$text' ) ).to.be.true;
  2021. } );
  2022. it( 'passes $root>blockQuote>image', () => {
  2023. expect( schema.checkChild( r1bQ, 'image' ) ).to.be.true;
  2024. } );
  2025. it( 'passes $root>blockQuote>image>caption', () => {
  2026. expect( schema.checkChild( r1bQi, 'caption' ) ).to.be.true;
  2027. } );
  2028. it( 'passes $root>blockQuote>image>caption>$text', () => {
  2029. expect( schema.checkChild( r1bQi.getChild( 0 ), '$text' ) ).to.be.true;
  2030. } );
  2031. it( 'passes $root>image', () => {
  2032. expect( schema.checkChild( root1, 'image' ) ).to.be.true;
  2033. } );
  2034. it( 'passes $root>image>caption', () => {
  2035. expect( schema.checkChild( r1i, 'caption' ) ).to.be.true;
  2036. } );
  2037. it( 'passes $root>image>caption>$text', () => {
  2038. expect( schema.checkChild( r1i.getChild( 0 ), '$text' ) ).to.be.true;
  2039. } );
  2040. it( 'rejects $root>$root', () => {
  2041. expect( schema.checkChild( root1, '$root' ) ).to.be.false;
  2042. } );
  2043. it( 'rejects $root>$text', () => {
  2044. expect( schema.checkChild( root1, '$text' ) ).to.be.false;
  2045. } );
  2046. it( 'rejects $root>caption', () => {
  2047. expect( schema.checkChild( root1, 'caption' ) ).to.be.false;
  2048. } );
  2049. it( 'rejects $root>paragraph>paragraph', () => {
  2050. expect( schema.checkChild( r1p1, 'paragraph' ) ).to.be.false;
  2051. } );
  2052. it( 'rejects $root>paragraph>paragraph>$text', () => {
  2053. // Edge case because p>p should not exist in the first place.
  2054. // But it's good to know that it blocks also this.
  2055. const p = new Element( 'p' );
  2056. r1p1._appendChild( p );
  2057. expect( schema.checkChild( p, '$text' ) ).to.be.false;
  2058. } );
  2059. it( 'rejects $root>paragraph>$block', () => {
  2060. expect( schema.checkChild( r1p1, '$block' ) ).to.be.false;
  2061. } );
  2062. it( 'rejects $root>paragraph>blockQuote', () => {
  2063. expect( schema.checkChild( r1p1, 'blockQuote' ) ).to.be.false;
  2064. } );
  2065. it( 'rejects $root>paragraph>image', () => {
  2066. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2067. } );
  2068. it( 'rejects $root>paragraph>caption', () => {
  2069. expect( schema.checkChild( r1p1, 'caption' ) ).to.be.false;
  2070. } );
  2071. it( 'rejects $root>blockQuote>blockQuote', () => {
  2072. expect( schema.checkChild( r1bQ, 'blockQuote' ) ).to.be.false;
  2073. } );
  2074. it( 'rejects $root>blockQuote>caption', () => {
  2075. expect( schema.checkChild( r1p1, 'image' ) ).to.be.false;
  2076. } );
  2077. it( 'rejects $root>blockQuote>$text', () => {
  2078. expect( schema.checkChild( r1bQ, '$text' ) ).to.be.false;
  2079. } );
  2080. it( 'rejects $root>image>$text', () => {
  2081. expect( schema.checkChild( r1i, '$text' ) ).to.be.false;
  2082. } );
  2083. it( 'rejects $root>image>paragraph', () => {
  2084. expect( schema.checkChild( r1i, 'paragraph' ) ).to.be.false;
  2085. } );
  2086. it( 'rejects $root>image>caption>paragraph', () => {
  2087. expect( schema.checkChild( r1i.getChild( 0 ), 'paragraph' ) ).to.be.false;
  2088. } );
  2089. it( 'rejects $root>image>caption>blockQuote', () => {
  2090. expect( schema.checkChild( r1i.getChild( 0 ), 'blockQuote' ) ).to.be.false;
  2091. } );
  2092. it( 'accepts attribute $root>paragraph[alignment]', () => {
  2093. expect( schema.checkAttribute( r1p1, 'alignment' ) ).to.be.true;
  2094. } );
  2095. it( 'accepts attribute $root>paragraph>$text[bold]', () => {
  2096. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'bold' ) ).to.be.true;
  2097. } );
  2098. it( 'accepts attribute $root>heading1>$text[italic]', () => {
  2099. expect( schema.checkAttribute( r1h.getChild( 0 ), 'italic' ) ).to.be.true;
  2100. } );
  2101. it( 'accepts attribute $root>blockQuote>paragraph>$text[bold]', () => {
  2102. expect( schema.checkAttribute( r1bQp.getChild( 0 ), 'bold' ) ).to.be.true;
  2103. } );
  2104. it( 'accepts attribute $root>listItem[alignment]', () => {
  2105. expect( schema.checkAttribute( r1lI, 'alignment' ) ).to.be.true;
  2106. } );
  2107. it( 'accepts attribute $root>listItem[indent]', () => {
  2108. expect( schema.checkAttribute( r1lI, 'listIndent' ) ).to.be.true;
  2109. } );
  2110. it( 'accepts attribute $root>listItem[type]', () => {
  2111. expect( schema.checkAttribute( r1lI, 'listType' ) ).to.be.true;
  2112. } );
  2113. it( 'accepts attribute $root>image[src]', () => {
  2114. expect( schema.checkAttribute( r1i, 'src' ) ).to.be.true;
  2115. } );
  2116. it( 'accepts attribute $root>image[alt]', () => {
  2117. expect( schema.checkAttribute( r1i, 'alt' ) ).to.be.true;
  2118. } );
  2119. it( 'accepts attribute $root>image>caption>$text[bold]', () => {
  2120. expect( schema.checkAttribute( r1i.getChild( 0 ).getChild( 0 ), 'bold' ) ).to.be.true;
  2121. } );
  2122. it( 'rejects attribute $root[indent]', () => {
  2123. expect( schema.checkAttribute( root1, 'listIndent' ) ).to.be.false;
  2124. } );
  2125. it( 'rejects attribute $root>paragraph[indent]', () => {
  2126. expect( schema.checkAttribute( r1p1, 'listIndent' ) ).to.be.false;
  2127. } );
  2128. it( 'accepts attribute $root>heading1>$text[bold]', () => {
  2129. expect( schema.checkAttribute( r1h.getChild( 0 ), 'bold' ) ).to.be.false;
  2130. } );
  2131. it( 'rejects attribute $root>paragraph>$text[alignment]', () => {
  2132. expect( schema.checkAttribute( r1p1.getChild( 0 ), 'alignment' ) ).to.be.false;
  2133. } );
  2134. it( 'rejects attribute $root>blockQuote[indent]', () => {
  2135. expect( schema.checkAttribute( r1bQ, 'listIndent' ) ).to.be.false;
  2136. } );
  2137. it( 'rejects attribute $root>blockQuote[alignment]', () => {
  2138. expect( schema.checkAttribute( r1bQ, 'alignment' ) ).to.be.false;
  2139. } );
  2140. it( 'rejects attribute $root>image[indent]', () => {
  2141. expect( schema.checkAttribute( r1i, 'listIndent' ) ).to.be.false;
  2142. } );
  2143. it( 'rejects attribute $root>image[alignment]', () => {
  2144. expect( schema.checkAttribute( r1i, 'alignment' ) ).to.be.false;
  2145. } );
  2146. it( '$root is limit', () => {
  2147. expect( schema.isLimit( '$root' ) ).to.be.true;
  2148. expect( schema.isBlock( '$root' ) ).to.be.false;
  2149. expect( schema.isObject( '$root' ) ).to.be.false;
  2150. } );
  2151. it( 'paragraph is block', () => {
  2152. expect( schema.isLimit( 'paragraph' ) ).to.be.false;
  2153. expect( schema.isBlock( 'paragraph' ) ).to.be.true;
  2154. expect( schema.isObject( 'paragraph' ) ).to.be.false;
  2155. } );
  2156. it( 'heading1 is block', () => {
  2157. expect( schema.isLimit( 'heading1' ) ).to.be.false;
  2158. expect( schema.isBlock( 'heading1' ) ).to.be.true;
  2159. expect( schema.isObject( 'heading1' ) ).to.be.false;
  2160. } );
  2161. it( 'listItem is block', () => {
  2162. expect( schema.isLimit( 'listItem' ) ).to.be.false;
  2163. expect( schema.isBlock( 'listItem' ) ).to.be.true;
  2164. expect( schema.isObject( 'listItem' ) ).to.be.false;
  2165. } );
  2166. it( 'image is block object', () => {
  2167. expect( schema.isLimit( 'image' ) ).to.be.true;
  2168. expect( schema.isBlock( 'image' ) ).to.be.true;
  2169. expect( schema.isObject( 'image' ) ).to.be.true;
  2170. } );
  2171. it( 'caption is limit', () => {
  2172. expect( schema.isLimit( 'caption' ) ).to.be.true;
  2173. expect( schema.isBlock( 'caption' ) ).to.be.false;
  2174. expect( schema.isObject( 'caption' ) ).to.be.false;
  2175. } );
  2176. } );
  2177. } );
  2178. describe( 'SchemaContext', () => {
  2179. let root;
  2180. beforeEach( () => {
  2181. root = new Element( '$root', null, [
  2182. new Element( 'blockQuote', { foo: 1 }, [
  2183. new Element( 'paragraph', { align: 'left' }, [
  2184. new Text( 'foo', { bold: true, italic: true } )
  2185. ] )
  2186. ] )
  2187. ] );
  2188. } );
  2189. describe( 'constructor()', () => {
  2190. it( 'creates context based on an array of strings', () => {
  2191. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2192. expect( ctx.length ).to.equal( 3 );
  2193. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2194. expect( ctx.getItem( 0 ).name ).to.equal( 'a' );
  2195. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2196. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2197. } );
  2198. it( 'creates context based on an array of elements', () => {
  2199. const blockQuote = root.getChild( 0 );
  2200. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2201. const ctx = new SchemaContext( [ blockQuote, text ] );
  2202. expect( ctx.length ).to.equal( 2 );
  2203. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', '$text' ] );
  2204. expect( ctx.getItem( 0 ).name ).to.equal( 'blockQuote' );
  2205. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2206. expect( ctx.getItem( 1 ).getAttribute( 'bold' ) ).to.be.true;
  2207. } );
  2208. it( 'creates context based on a mixed array of strings and elements', () => {
  2209. const blockQuote = root.getChild( 0 );
  2210. const text = blockQuote.getChild( 0 ).getChild( 0 );
  2211. const ctx = new SchemaContext( [ blockQuote, 'paragraph', text ] );
  2212. expect( ctx.length ).to.equal( 3 );
  2213. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'blockQuote', 'paragraph', '$text' ] );
  2214. } );
  2215. it( 'creates context based on a root element', () => {
  2216. const ctx = new SchemaContext( root );
  2217. expect( ctx.length ).to.equal( 1 );
  2218. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root' ] );
  2219. expect( Array.from( ctx.getItem( 0 ).getAttributeKeys() ) ).to.be.empty;
  2220. expect( ctx.getItem( 0 ).getAttribute( 'foo' ) ).to.be.undefined;
  2221. } );
  2222. it( 'creates context based on a nested element', () => {
  2223. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ) );
  2224. expect( ctx.length ).to.equal( 3 );
  2225. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2226. expect( Array.from( ctx.getItem( 1 ).getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2227. expect( ctx.getItem( 1 ).getAttribute( 'foo' ) ).to.equal( 1 );
  2228. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ) ).to.deep.equal( [ 'align' ] );
  2229. expect( ctx.getItem( 2 ).getAttribute( 'align' ) ).to.equal( 'left' );
  2230. } );
  2231. it( 'creates context based on a text node', () => {
  2232. const ctx = new SchemaContext( root.getChild( 0 ).getChild( 0 ).getChild( 0 ) );
  2233. expect( ctx.length ).to.equal( 4 );
  2234. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2235. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2236. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2237. } );
  2238. it( 'creates context based on a text proxy', () => {
  2239. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  2240. const textProxy = new TextProxy( text, 0, 1 );
  2241. const ctx = new SchemaContext( textProxy );
  2242. expect( ctx.length ).to.equal( 4 );
  2243. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph', '$text' ] );
  2244. expect( Array.from( ctx.getItem( 3 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'bold', 'italic' ] );
  2245. expect( ctx.getItem( 3 ).getAttribute( 'bold' ) ).to.be.true;
  2246. } );
  2247. it( 'creates context based on a position', () => {
  2248. const pos = Position.createAt( root.getChild( 0 ).getChild( 0 ) );
  2249. const ctx = new SchemaContext( pos );
  2250. expect( ctx.length ).to.equal( 3 );
  2251. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ '$root', 'blockQuote', 'paragraph' ] );
  2252. expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
  2253. } );
  2254. it( 'creates context based on a string', () => {
  2255. const ctx = new SchemaContext( 'paragraph' );
  2256. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2257. } );
  2258. it( 'creates context based on a SchemaContext instance', () => {
  2259. const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2260. const ctx = new SchemaContext( previousCtx );
  2261. expect( ctx ).to.equal( previousCtx );
  2262. } );
  2263. it( 'filters out DocumentFragment when it is a first item of context - array', () => {
  2264. const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
  2265. expect( ctx.length ).to.equal( 1 );
  2266. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2267. } );
  2268. it( 'filters out DocumentFragment when it is a first item of context - element', () => {
  2269. const p = new Element( 'paragraph' );
  2270. const docFrag = new DocumentFragment();
  2271. docFrag._appendChild( p );
  2272. const ctx = new SchemaContext( p );
  2273. expect( ctx.length ).to.equal( 1 );
  2274. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2275. } );
  2276. it( 'filters out DocumentFragment when it is a first item of context - position', () => {
  2277. const p = new Element( 'paragraph' );
  2278. const docFrag = new DocumentFragment();
  2279. docFrag._appendChild( p );
  2280. const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
  2281. expect( ctx.length ).to.equal( 1 );
  2282. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
  2283. } );
  2284. } );
  2285. describe( 'length', () => {
  2286. it( 'gets the number of items', () => {
  2287. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2288. expect( ctx.length ).to.equal( 3 );
  2289. } );
  2290. } );
  2291. describe( 'last', () => {
  2292. it( 'gets the last item', () => {
  2293. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2294. expect( ctx.last ).to.be.an( 'object' );
  2295. expect( ctx.last.name ).to.equal( 'c' );
  2296. } );
  2297. } );
  2298. describe( 'Symbol.iterator', () => {
  2299. it( 'exists', () => {
  2300. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2301. expect( ctx[ Symbol.iterator ] ).to.be.a( 'function' );
  2302. expect( Array.from( ctx ).map( item => item.name ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2303. } );
  2304. } );
  2305. describe( 'getItem()', () => {
  2306. it( 'returns item by index', () => {
  2307. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2308. expect( ctx.getItem( 1 ) ).to.be.an( 'object' );
  2309. expect( ctx.getItem( 1 ).name ).to.equal( 'b' );
  2310. } );
  2311. it( 'returns undefined if index exceeds the range', () => {
  2312. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2313. expect( ctx.getItem( 3 ) ).to.be.undefined;
  2314. } );
  2315. } );
  2316. describe( 'push()', () => {
  2317. it( 'creates new SchemaContext instance with new item - #string', () => {
  2318. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2319. const newCtx = ctx.push( 'd' );
  2320. expect( newCtx ).to.instanceof( SchemaContext );
  2321. expect( newCtx ).to.not.equal( ctx );
  2322. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2323. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2324. } );
  2325. it( 'creates new SchemaContext instance with new item - #text', () => {
  2326. const node = new Text( 'd' );
  2327. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2328. const newCtx = ctx.push( node );
  2329. expect( newCtx ).to.instanceof( SchemaContext );
  2330. expect( newCtx ).to.not.equal( ctx );
  2331. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', '$text' ] );
  2332. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2333. } );
  2334. it( 'creates new SchemaContext instance with new item - #element', () => {
  2335. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2336. const parent = new Element( 'parent', null, new Element( 'd' ) );
  2337. const newCtx = ctx.push( parent.getChild( 0 ) );
  2338. expect( newCtx ).to.instanceof( SchemaContext );
  2339. expect( newCtx ).to.not.equal( ctx );
  2340. expect( Array.from( newCtx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  2341. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2342. } );
  2343. } );
  2344. describe( 'getNames()', () => {
  2345. it( 'returns an iterator', () => {
  2346. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2347. expect( ctx.getNames().next ).to.be.a( 'function' );
  2348. } );
  2349. it( 'returns an iterator which returns all item names', () => {
  2350. const ctx = new SchemaContext( [ 'a', 'b', 'c' ] );
  2351. expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  2352. } );
  2353. } );
  2354. describe( 'endsWith()', () => {
  2355. it( 'returns true if the end of the context matches the query - 1 item', () => {
  2356. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2357. expect( ctx.endsWith( 'dom' ) ).to.be.true;
  2358. } );
  2359. it( 'returns true if the end of the context matches the query - 2 items', () => {
  2360. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2361. expect( ctx.endsWith( 'bom dom' ) ).to.be.true;
  2362. } );
  2363. it( 'returns true if the end of the context matches the query - full match of 3 items', () => {
  2364. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );
  2365. expect( ctx.endsWith( 'foo bar bom' ) ).to.be.true;
  2366. } );
  2367. it( 'returns true if the end of the context matches the query - full match of 1 items', () => {
  2368. const ctx = new SchemaContext( [ 'foo' ] );
  2369. expect( ctx.endsWith( 'foo' ) ).to.be.true;
  2370. } );
  2371. it( 'returns true if not only the end of the context matches the query', () => {
  2372. const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );
  2373. expect( ctx.endsWith( 'foo foo' ) ).to.be.true;
  2374. } );
  2375. it( 'returns false if query matches the middle of the context', () => {
  2376. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2377. expect( ctx.endsWith( 'bom' ) ).to.be.false;
  2378. } );
  2379. it( 'returns false if query matches the start of the context', () => {
  2380. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2381. expect( ctx.endsWith( 'foo' ) ).to.be.false;
  2382. } );
  2383. it( 'returns false if query does not match', () => {
  2384. const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );
  2385. expect( ctx.endsWith( 'dom bar' ) ).to.be.false;
  2386. } );
  2387. it( 'returns false if query is longer than context', () => {
  2388. const ctx = new SchemaContext( [ 'foo' ] );
  2389. expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
  2390. } );
  2391. } );
  2392. } );