schema.js 88 KB

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