schema.js 96 KB

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