schema.js 85 KB

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