schema.js 88 KB

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