schema.js 95 KB

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