schema.js 103 KB

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