schema.js 102 KB

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