8
0

schema.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as Schema, SchemaItem } from '../../../src/model/schema';
  6. import Model from '../../../src/model/model';
  7. import Element from '../../../src/model/element';
  8. import Text from '../../../src/model/text';
  9. import DocumentFragment from '../../../src/model/documentfragment';
  10. import Position from '../../../src/model/position';
  11. import Range from '../../../src/model/range';
  12. import Selection from '../../../src/model/selection';
  13. import AttributeDelta from '../../../src/model/delta/attributedelta';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { setData, getData, stringify } from '../../../src/dev-utils/model';
  17. testUtils.createSinonSandbox();
  18. describe( 'Schema', () => {
  19. let schema;
  20. beforeEach( () => {
  21. schema = new Schema();
  22. } );
  23. describe( 'constructor()', () => {
  24. it( 'should register base items: inline, block, root', () => {
  25. testUtils.sinon.spy( Schema.prototype, 'registerItem' );
  26. schema = new Schema();
  27. expect( schema.registerItem.calledWithExactly( '$root', null ) );
  28. expect( schema.registerItem.calledWithExactly( '$block', null ) );
  29. expect( schema.registerItem.calledWithExactly( '$inline', null ) );
  30. } );
  31. it( 'should allow block in root', () => {
  32. expect( schema.check( { name: '$block', inside: [ '$root' ] } ) ).to.be.true;
  33. } );
  34. it( 'should allow inline in block', () => {
  35. expect( schema.check( { name: '$inline', inside: [ '$block' ] } ) ).to.be.true;
  36. } );
  37. it( 'should create the objects set', () => {
  38. expect( schema.objects ).to.be.instanceOf( Set );
  39. } );
  40. it( 'should create the limits set', () => {
  41. expect( schema.limits ).to.be.instanceOf( Set );
  42. } );
  43. it( 'should mark $root as a limit element', () => {
  44. expect( schema.limits.has( '$root' ) ).to.be.true;
  45. } );
  46. describe( '$clipboardHolder', () => {
  47. it( 'should allow $block', () => {
  48. expect( schema.check( { name: '$block', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  49. } );
  50. it( 'should allow $inline', () => {
  51. expect( schema.check( { name: '$inline', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  52. } );
  53. it( 'should allow $text', () => {
  54. expect( schema.check( { name: '$text', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  55. } );
  56. } );
  57. } );
  58. describe( 'registerItem()', () => {
  59. it( 'should register in schema item under given name', () => {
  60. schema.registerItem( 'new' );
  61. expect( schema.hasItem( 'new' ) ).to.be.true;
  62. } );
  63. it( 'should build correct base chains', () => {
  64. schema.registerItem( 'first' );
  65. schema.registerItem( 'secondA', 'first' );
  66. schema.registerItem( 'secondB', 'first' );
  67. schema.registerItem( 'third', 'secondA' );
  68. expect( schema._extensionChains.get( 'first' ) ).to.deep.equal( [ 'first' ] );
  69. expect( schema._extensionChains.get( 'secondA' ) ).to.deep.equal( [ 'first', 'secondA' ] );
  70. expect( schema._extensionChains.get( 'secondB' ) ).to.deep.equal( [ 'first', 'secondB' ] );
  71. expect( schema._extensionChains.get( 'third' ) ).to.deep.equal( [ 'first', 'secondA', 'third' ] );
  72. } );
  73. it( 'should make registered item inherit allows from base item', () => {
  74. schema.registerItem( 'image', '$inline' );
  75. expect( schema.check( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
  76. } );
  77. it( 'should throw if item with given name has already been registered in schema', () => {
  78. schema.registerItem( 'new' );
  79. expect( () => {
  80. schema.registerItem( 'new' );
  81. } ).to.throw( CKEditorError, /model-schema-item-exists/ );
  82. } );
  83. it( 'should throw if base item has not been registered in schema', () => {
  84. expect( () => {
  85. schema.registerItem( 'new', 'old' );
  86. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  87. } );
  88. } );
  89. describe( 'hasItem()', () => {
  90. it( 'should return true if given item name has been registered in schema', () => {
  91. expect( schema.hasItem( '$block' ) ).to.be.true;
  92. } );
  93. it( 'should return false if given item name has not been registered in schema', () => {
  94. expect( schema.hasItem( 'new' ) ).to.be.false;
  95. } );
  96. } );
  97. describe( '_getItem()', () => {
  98. it( 'should return SchemaItem registered under given name', () => {
  99. schema.registerItem( 'new' );
  100. const item = schema._getItem( 'new' );
  101. expect( item ).to.be.instanceof( SchemaItem );
  102. } );
  103. it( 'should throw if there is no item registered under given name', () => {
  104. expect( () => {
  105. schema._getItem( 'new' );
  106. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  107. } );
  108. } );
  109. describe( 'allow()', () => {
  110. it( 'should add passed query to allowed in schema', () => {
  111. schema.registerItem( 'p', '$block' );
  112. schema.registerItem( 'div', '$block' );
  113. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
  114. schema.allow( { name: 'p', inside: 'div' } );
  115. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
  116. } );
  117. } );
  118. describe( 'disallow()', () => {
  119. it( 'should add passed query to disallowed in schema', () => {
  120. schema.registerItem( 'p', '$block' );
  121. schema.registerItem( 'div', '$block' );
  122. schema.allow( { name: '$block', attributes: 'bold', inside: 'div' } );
  123. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.true;
  124. schema.disallow( { name: 'p', attributes: 'bold', inside: 'div' } );
  125. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.false;
  126. } );
  127. } );
  128. describe( 'check()', () => {
  129. describe( 'string or array of strings as inside', () => {
  130. it( 'should return false if given element is not registered in schema', () => {
  131. expect( schema.check( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
  132. } );
  133. it( 'should handle path given as string', () => {
  134. expect( schema.check( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
  135. } );
  136. it( 'should handle attributes', () => {
  137. schema.registerItem( 'p', '$block' );
  138. schema.allow( { name: 'p', inside: '$block' } );
  139. expect( schema.check( { name: 'p', inside: [ '$block' ] } ) ).to.be.true;
  140. expect( schema.check( { name: 'p', inside: [ '$block' ], attributes: 'bold' } ) ).to.be.false;
  141. } );
  142. it( 'should support required attributes', () => {
  143. schema.registerItem( 'a', '$inline' );
  144. schema.requireAttributes( 'a', [ 'name' ] );
  145. schema.requireAttributes( 'a', [ 'href' ] );
  146. schema.allow( { name: 'a', inside: '$block', attributes: [ 'name', 'href', 'title', 'target' ] } );
  147. // Even though a is allowed in $block thanks to inheriting from $inline, we require href or name attribute.
  148. expect( schema.check( { name: 'a', inside: '$block' } ) ).to.be.false;
  149. // Even though a with title is allowed, we have to meet at least on required attributes set.
  150. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'title' ] } ) ).to.be.false;
  151. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name' ] } ) ).to.be.true;
  152. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'href' ] } ) ).to.be.true;
  153. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'href' ] } ) ).to.be.true;
  154. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'title', 'target' ] } ) ).to.be.true;
  155. } );
  156. it( 'should not require attributes from parent schema items', () => {
  157. schema.registerItem( 'parent' );
  158. schema.registerItem( 'child', 'parent' );
  159. schema.allow( { name: 'parent', inside: '$block' } );
  160. schema.requireAttributes( 'parent', [ 'required' ] );
  161. // Even though we require "required" attribute on parent, the requirement should not be inherited.
  162. expect( schema.check( { name: 'child', inside: '$block' } ) ).to.be.true;
  163. } );
  164. it( 'should support multiple attributes', () => {
  165. // Let's take example case, where image item has to have a pair of "alt" and "src" attributes.
  166. // Then it could have other attribute which is allowed on inline elements, i.e. "bold".
  167. schema.registerItem( 'img', '$inline' );
  168. schema.requireAttributes( 'img', [ 'alt', 'src' ] );
  169. schema.allow( { name: '$inline', inside: '$block', attributes: 'bold' } );
  170. schema.allow( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } );
  171. // Image without any attributes is not allowed.
  172. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  173. // Image can't have just alt or src.
  174. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  175. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src' ] } ) ).to.be.false;
  176. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } ) ).to.be.true;
  177. // Because of inherting from $inline, image can have bold
  178. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'bold' ] } ) ).to.be.true;
  179. // But it can't have only bold without alt or/and src.
  180. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'bold' ] } ) ).to.be.false;
  181. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src', 'bold' ] } ) ).to.be.false;
  182. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'bold' ] } ) ).to.be.false;
  183. // Even if image has src and alt, it can't have attributes that weren't allowed
  184. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'attr' ] } ) ).to.be.false;
  185. } );
  186. it( 'should omit path elements that are added to schema', () => {
  187. expect( schema.check( { name: '$inline', inside: '$block new $block' } ) ).to.be.true;
  188. } );
  189. it( 'should ignore attributes stored in elements by document selection', () => {
  190. expect( schema.check( { name: '$block', attributes: 'selection:foo', inside: '$root' } ) ).to.be.true;
  191. } );
  192. it( 'should disallow attribute stored in an element if that attribute was explicitly disallowed', () => {
  193. schema.disallow( { name: '$block', attributes: [ 'selection:foo' ], inside: '$root' } );
  194. expect( schema.check( { name: '$block', attributes: [ 'selection:foo' ], inside: '$root' } ) ).to.be.false;
  195. } );
  196. } );
  197. describe( 'array of elements as inside', () => {
  198. beforeEach( () => {
  199. schema.registerItem( 'div', '$block' );
  200. schema.registerItem( 'header', '$block' );
  201. schema.registerItem( 'p', '$block' );
  202. schema.registerItem( 'img', '$inline' );
  203. schema.allow( { name: '$block', inside: 'div' } );
  204. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  205. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  206. } );
  207. it( 'should return true if given element is allowed by schema at given position', () => {
  208. // P is block and block is allowed in DIV.
  209. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  210. // IMG is inline and inline is allowed in block.
  211. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  212. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ] } ) ).to.be.true;
  213. // Inline is allowed in any block and is allowed with attribute bold.
  214. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  215. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  216. // Inline is allowed in header which is allowed in DIV.
  217. expect( schema.check( { name: 'header', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  218. expect( schema.check( { name: 'img', inside: [ new Element( 'header' ) ] } ) ).to.be.true;
  219. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ), new Element( 'header' ) ] } ) ).to.be.true;
  220. } );
  221. it( 'should return false if given element is not allowed by schema at given position', () => {
  222. // P with attribute is not allowed.
  223. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ], attributes: 'bold' } ) ).to.be.false;
  224. // Bold text is not allowed in header
  225. expect( schema.check( { name: '$text', inside: [ new Element( 'header' ) ], attributes: 'bold' } ) ).to.be.false;
  226. } );
  227. it( 'should return false if given element is not registered in schema', () => {
  228. expect( schema.check( { name: 'new', inside: [ new Element( 'div' ) ] } ) ).to.be.false;
  229. } );
  230. } );
  231. describe( 'position as inside', () => {
  232. let doc, root;
  233. beforeEach( () => {
  234. const model = new Model();
  235. doc = model.document;
  236. root = doc.createRoot( 'div' );
  237. root.insertChildren( 0, [
  238. new Element( 'div' ),
  239. new Element( 'header' ),
  240. new Element( 'p' )
  241. ] );
  242. schema.registerItem( 'div', '$block' );
  243. schema.registerItem( 'header', '$block' );
  244. schema.registerItem( 'p', '$block' );
  245. schema.allow( { name: '$block', inside: 'div' } );
  246. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  247. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  248. } );
  249. it( 'should return true if given element is allowed by schema at given position', () => {
  250. // Block should be allowed in root.
  251. expect( schema.check( { name: '$block', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  252. // P is block and block should be allowed in root.
  253. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  254. // P is allowed in DIV by the set rule.
  255. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  256. // Inline is allowed in any block and is allowed with attribute bold.
  257. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  258. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  259. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ) } ) ).to.be.true;
  260. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  261. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  262. // Header is allowed in DIV.
  263. expect( schema.check( { name: 'header', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  264. // Inline is allowed in block and root is DIV, which is block.
  265. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  266. } );
  267. it( 'should return false if given element is not allowed by schema at given position', () => {
  268. // P with attribute is not allowed anywhere.
  269. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ), attributes: 'bold' } ) ).to.be.false;
  270. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  271. // Bold text is not allowed in header
  272. expect( schema.check( { name: '$text', inside: new Position( root, [ 1, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  273. } );
  274. it( 'should return false if given element is not registered in schema', () => {
  275. expect( schema.check( { name: 'new', inside: new Position( root, [ 0 ] ) } ) ).to.be.false;
  276. } );
  277. } );
  278. describe( 'bug #732', () => {
  279. // Ticket case.
  280. it( 'should return false if given element is allowed in the root but not deeper', () => {
  281. schema.registerItem( 'paragraph', '$block' );
  282. expect( schema.check( { name: 'paragraph', inside: [ '$root', 'paragraph' ] } ) ).to.be.false;
  283. } );
  284. // Two additional, real life cases accompanying the ticket case.
  285. it( 'should return true if checking whether text is allowed in $root > paragraph', () => {
  286. schema.registerItem( 'paragraph', '$block' );
  287. expect( schema.check( { name: '$text', inside: [ '$root', 'paragraph' ] } ) ).to.be.true;
  288. } );
  289. it( 'should return true if checking whether text is allowed in paragraph', () => {
  290. schema.registerItem( 'paragraph', '$block' );
  291. expect( schema.check( { name: '$text', inside: [ 'paragraph' ] } ) ).to.be.true;
  292. } );
  293. // Veryfing the matching algorithm.
  294. // The right ends of the element to check and "inside" paths must match.
  295. describe( 'right ends of paths must match', () => {
  296. beforeEach( () => {
  297. schema.registerItem( 'a' );
  298. schema.registerItem( 'b' );
  299. schema.registerItem( 'c' );
  300. schema.registerItem( 'd' );
  301. schema.registerItem( 'e' );
  302. schema.allow( { name: 'a', inside: [ 'b', 'c', 'd' ] } );
  303. schema.allow( { name: 'e', inside: [ 'a' ] } );
  304. } );
  305. // Simple chains created by a single allow() call.
  306. it( 'a inside b, c', () => {
  307. expect( schema.check( { name: 'a', inside: [ 'b', 'c' ] } ) ).to.be.false;
  308. } );
  309. it( 'a inside b', () => {
  310. expect( schema.check( { name: 'a', inside: [ 'b' ] } ) ).to.be.false;
  311. } );
  312. it( 'a inside b, c, d', () => {
  313. expect( schema.check( { name: 'a', inside: [ 'b', 'c', 'd' ] } ) ).to.be.true;
  314. } );
  315. it( 'a inside c, d', () => {
  316. expect( schema.check( { name: 'a', inside: [ 'c', 'd' ] } ) ).to.be.true;
  317. } );
  318. it( 'a inside d', () => {
  319. expect( schema.check( { name: 'a', inside: [ 'd' ] } ) ).to.be.true;
  320. } );
  321. // "Allowed in" chains created by two separate allow() calls (`e inside a` and `a inside b,c,d`).
  322. it( 'e inside a, d', () => {
  323. expect( schema.check( { name: 'e', inside: [ 'd', 'a' ] } ) ).to.be.true;
  324. } );
  325. it( 'e inside b, c, d', () => {
  326. expect( schema.check( { name: 'e', inside: [ 'b', 'c', 'd' ] } ) ).to.be.false;
  327. } );
  328. } );
  329. } );
  330. } );
  331. describe( 'itemExtends()', () => {
  332. it( 'should return true if given item extends another given item', () => {
  333. schema.registerItem( 'div', '$block' );
  334. schema.registerItem( 'myDiv', 'div' );
  335. expect( schema.itemExtends( 'div', '$block' ) ).to.be.true;
  336. expect( schema.itemExtends( 'myDiv', 'div' ) ).to.be.true;
  337. expect( schema.itemExtends( 'myDiv', '$block' ) ).to.be.true;
  338. } );
  339. it( 'should return false if given item does not extend another given item', () => {
  340. schema.registerItem( 'div' );
  341. schema.registerItem( 'myDiv', 'div' );
  342. expect( schema.itemExtends( 'div', '$block' ) ).to.be.false;
  343. expect( schema.itemExtends( 'div', 'myDiv' ) ).to.be.false;
  344. } );
  345. it( 'should throw if one or both given items are not registered in schema', () => {
  346. expect( () => {
  347. schema.itemExtends( 'foo', '$block' );
  348. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  349. expect( () => {
  350. schema.itemExtends( '$block', 'foo' );
  351. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  352. } );
  353. } );
  354. describe( '_normalizeQueryPath()', () => {
  355. it( 'should normalize string with spaces to an array of strings', () => {
  356. expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );
  357. } );
  358. it( 'should normalize model position to an array of strings', () => {
  359. const model = new Model();
  360. const doc = model.document;
  361. const root = doc.createRoot();
  362. root.insertChildren( 0, [
  363. new Element( 'div', null, [
  364. new Element( 'header' )
  365. ] )
  366. ] );
  367. const position = new Position( root, [ 0, 0, 0 ] );
  368. expect( Schema._normalizeQueryPath( position ) ).to.deep.equal( [ '$root', 'div', 'header' ] );
  369. } );
  370. it( 'should normalize array with strings and model elements to an array of strings and drop unrecognized parts', () => {
  371. const input = [
  372. '$root',
  373. [ 'div' ],
  374. new Element( 'div' ),
  375. null,
  376. new Element( 'p' ),
  377. 'strong'
  378. ];
  379. expect( Schema._normalizeQueryPath( input ) ).to.deep.equal( [ '$root', 'div', 'p', 'strong' ] );
  380. } );
  381. } );
  382. describe( 'checkAttributeInSelection()', () => {
  383. const attribute = 'bold';
  384. let model, doc, schema;
  385. beforeEach( () => {
  386. model = new Model();
  387. doc = model.document;
  388. doc.createRoot();
  389. schema = model.schema;
  390. schema.registerItem( 'p', '$block' );
  391. schema.registerItem( 'h1', '$block' );
  392. schema.registerItem( 'img', '$inline' );
  393. schema.registerItem( 'figure' );
  394. // Bold text is allowed only in P.
  395. schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  396. schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  397. // Disallow bold on image.
  398. schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
  399. // Figure must have name attribute and optional title attribute.
  400. schema.requireAttributes( 'figure', [ 'name' ] );
  401. schema.allow( { name: 'figure', attributes: [ 'title', 'name' ], inside: '$root' } );
  402. } );
  403. describe( 'when selection is collapsed', () => {
  404. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  405. setData( model, '<p>f[]oo</p>' );
  406. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  407. } );
  408. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  409. setData( model, '<h1>[]</h1>' );
  410. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  411. setData( model, '[]' );
  412. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  413. } );
  414. } );
  415. describe( 'when selection is not collapsed', () => {
  416. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  417. // Simple selection on a few characters.
  418. setData( model, '<p>[foo]</p>' );
  419. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  420. // Selection spans over characters but also include nodes that can't have attribute.
  421. setData( model, '<p>fo[o<img />b]ar</p>' );
  422. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  423. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  424. setData( model, '[<p>foo<img />bar</p><h1></h1>]' );
  425. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  426. // Selection on empty P. P can have the attribute.
  427. setData( model, '[<p></p>]' );
  428. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  429. } );
  430. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  431. // Selection on DIV which can't have bold text.
  432. setData( model, '[<h1></h1>]' );
  433. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  434. // Selection on two images which can't be bold.
  435. setData( model, '<p>foo[<img /><img />]bar</p>' );
  436. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  437. } );
  438. it( 'should return true when checking element with required attribute', () => {
  439. setData( model, '[<figure name="figure"></figure>]' );
  440. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  441. } );
  442. it( 'should return true when checking element when attribute is already present', () => {
  443. setData( model, '[<figure name="figure" title="title"></figure>]' );
  444. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  445. } );
  446. } );
  447. } );
  448. describe( 'getValidRanges()', () => {
  449. const attribute = 'bold';
  450. let model, doc, root, schema, ranges;
  451. beforeEach( () => {
  452. model = new Model();
  453. doc = model.document;
  454. schema = model.schema;
  455. root = doc.createRoot();
  456. schema.registerItem( 'p', '$block' );
  457. schema.registerItem( 'h1', '$block' );
  458. schema.registerItem( 'img', '$inline' );
  459. schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  460. schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  461. setData( model, '<p>foo<img />bar</p>' );
  462. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  463. } );
  464. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  465. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  466. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  467. } );
  468. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  469. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  470. schema.allow( { name: '$text', inside: 'img' } );
  471. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  472. } );
  473. it( 'should return two ranges when attribute is not allowed on one item', () => {
  474. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  475. schema.allow( { name: '$text', inside: 'img' } );
  476. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  477. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  478. const sel = new Selection();
  479. sel.setRanges( validRanges );
  480. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  481. } );
  482. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  483. schema.allow( { name: '$text', inside: 'img' } );
  484. schema.allow( { name: '$text', attributes: 'bold', inside: 'img' } );
  485. setData( model, '[<p>foo<img>xxx</img>bar</p>]' );
  486. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  487. const sel = new Selection();
  488. sel.setRanges( validRanges );
  489. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  490. } );
  491. it( 'should not leak beyond the given ranges', () => {
  492. setData( model, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  493. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  494. const sel = new Selection();
  495. sel.setRanges( validRanges );
  496. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  497. } );
  498. it( 'should correctly handle a range which ends in a disallowed position', () => {
  499. schema.allow( { name: '$text', inside: 'img' } );
  500. setData( model, '<p>[foo<img>bar]</img>bom</p>' );
  501. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  502. const sel = new Selection();
  503. sel.setRanges( validRanges );
  504. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  505. } );
  506. it( 'should split range into two ranges and omit disallowed element', () => {
  507. // Disallow bold on img.
  508. model.schema.disallow( { name: 'img', attributes: 'bold', inside: 'p' } );
  509. const result = schema.getValidRanges( ranges, attribute );
  510. expect( result ).to.length( 2 );
  511. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  512. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  513. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  514. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  515. } );
  516. } );
  517. describe( 'getLimitElement()', () => {
  518. let model, doc, root;
  519. beforeEach( () => {
  520. model = new Model();
  521. doc = model.document;
  522. schema = model.schema;
  523. root = doc.createRoot();
  524. schema.registerItem( 'div', '$block' );
  525. schema.registerItem( 'article', '$block' );
  526. schema.registerItem( 'section', '$block' );
  527. schema.registerItem( 'paragraph', '$block' );
  528. schema.registerItem( 'widget', '$block' );
  529. schema.registerItem( 'image', '$block' );
  530. schema.registerItem( 'caption', '$block' );
  531. schema.allow( { name: 'image', inside: 'widget' } );
  532. schema.allow( { name: 'caption', inside: 'image' } );
  533. schema.allow( { name: 'paragraph', inside: 'article' } );
  534. schema.allow( { name: 'article', inside: 'section' } );
  535. schema.allow( { name: 'section', inside: 'div' } );
  536. schema.allow( { name: 'widget', inside: 'div' } );
  537. } );
  538. it( 'always returns $root element if any other limit was not defined', () => {
  539. schema.limits.clear();
  540. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  541. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  542. } );
  543. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  544. schema.limits.add( 'article' );
  545. schema.limits.add( 'section' );
  546. setData( model, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  547. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  548. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  549. } );
  550. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  551. schema.limits.add( 'article' );
  552. schema.limits.add( 'section' );
  553. setData( model, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  554. const section = root.getNodeByPath( [ 0, 0 ] );
  555. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  556. } );
  557. it( 'works fine with multi-range selections', () => {
  558. schema.limits.add( 'article' );
  559. schema.limits.add( 'widget' );
  560. schema.limits.add( 'div' );
  561. setData(
  562. model,
  563. '<div>' +
  564. '<section>' +
  565. '<article>' +
  566. '<paragraph>[foo]</paragraph>' +
  567. '</article>' +
  568. '</section>' +
  569. '<widget>' +
  570. '<image>' +
  571. '<caption>b[a]r</caption>' +
  572. '</image>' +
  573. '</widget>' +
  574. '</div>'
  575. );
  576. const div = root.getNodeByPath( [ 0 ] );
  577. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  578. } );
  579. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  580. schema.limits.clear();
  581. setData(
  582. model,
  583. '<div>' +
  584. '<section>' +
  585. '<article>' +
  586. '<paragraph>[foo]</paragraph>' +
  587. '</article>' +
  588. '</section>' +
  589. '</div>' +
  590. '<section>b[]ar</section>'
  591. );
  592. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  593. } );
  594. } );
  595. describe( 'removeDisallowedAttributes()', () => {
  596. let model, doc, root;
  597. beforeEach( () => {
  598. model = new Model();
  599. doc = model.document;
  600. root = doc.createRoot();
  601. schema = model.schema;
  602. schema.registerItem( 'paragraph', '$block' );
  603. schema.registerItem( 'div', '$block' );
  604. schema.registerItem( 'image' );
  605. schema.objects.add( 'image' );
  606. schema.allow( { name: '$block', inside: 'div' } );
  607. } );
  608. describe( 'filtering attributes from nodes', () => {
  609. let text, image;
  610. beforeEach( () => {
  611. schema.allow( { name: '$text', attributes: [ 'a' ], inside: '$root' } );
  612. schema.allow( { name: 'image', attributes: [ 'b' ], inside: '$root' } );
  613. text = new Text( 'foo', { a: 1, b: 1 } );
  614. image = new Element( 'image', { a: 1, b: 1 } );
  615. } );
  616. it( 'should filter out disallowed attributes from given nodes', () => {
  617. const root = doc.getRoot();
  618. root.appendChildren( [ text, image ] );
  619. model.change( writer => {
  620. schema.removeDisallowedAttributes( [ text, image ], '$root', writer );
  621. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  622. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  623. expect( writer.batch.deltas ).to.length( 2 );
  624. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  625. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  626. } );
  627. } );
  628. } );
  629. describe( 'filtering attributes from child nodes', () => {
  630. let div;
  631. beforeEach( () => {
  632. schema.allow( { name: '$text', attributes: [ 'a' ], inside: 'div' } );
  633. schema.allow( { name: '$text', attributes: [ 'b' ], inside: 'div paragraph' } );
  634. schema.allow( { name: 'image', attributes: [ 'a' ], inside: 'div' } );
  635. schema.allow( { name: 'image', attributes: [ 'b' ], inside: 'div paragraph' } );
  636. const foo = new Text( 'foo', { a: 1, b: 1 } );
  637. const bar = new Text( 'bar', { a: 1, b: 1 } );
  638. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  639. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  640. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  641. div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  642. } );
  643. it( 'should filter out disallowed attributes from child nodes', () => {
  644. const root = doc.getRoot();
  645. root.appendChildren( [ div ] );
  646. model.change( writer => {
  647. schema.removeDisallowedAttributes( [ div ], '$root', writer );
  648. expect( writer.batch.deltas ).to.length( 4 );
  649. expect( writer.batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  650. expect( writer.batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  651. expect( writer.batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  652. expect( writer.batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  653. expect( getData( model, { withoutSelection: true } ) )
  654. .to.equal(
  655. '<div>' +
  656. '<paragraph>' +
  657. '<$text b="1">foo</$text>' +
  658. '<image b="1"></image>' +
  659. '</paragraph>' +
  660. '<$text a="1">bar</$text>' +
  661. '<image a="1"></image>' +
  662. '</div>'
  663. );
  664. } );
  665. } );
  666. } );
  667. describe( 'allowed parameters', () => {
  668. let frag;
  669. beforeEach( () => {
  670. schema.allow( { name: '$text', attributes: [ 'a' ], inside: '$root' } );
  671. schema.allow( { name: '$text', attributes: [ 'b' ], inside: 'paragraph' } );
  672. frag = new DocumentFragment( [
  673. new Text( 'foo', { a: 1 } ),
  674. new Element( 'paragraph', [], [ new Text( 'bar', { a: 1, b: 1 } ) ] ),
  675. new Text( 'biz', { b: 1 } )
  676. ] );
  677. } );
  678. it( 'should accept iterable as nodes', () => {
  679. model.change( writer => {
  680. schema.removeDisallowedAttributes( frag.getChildren(), '$root', writer );
  681. } );
  682. expect( stringify( frag ) )
  683. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  684. } );
  685. it( 'should accept Position as inside', () => {
  686. model.change( writer => {
  687. schema.removeDisallowedAttributes( frag.getChildren(), Position.createAt( root ), writer );
  688. } );
  689. expect( stringify( frag ) )
  690. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  691. } );
  692. it( 'should accept Node as inside', () => {
  693. model.change( writer => {
  694. schema.removeDisallowedAttributes( frag.getChildren(), [ root ], writer );
  695. } );
  696. expect( stringify( frag ) )
  697. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  698. } );
  699. } );
  700. it( 'should not filter out allowed combination of attributes', () => {
  701. schema.allow( { name: 'image', attributes: [ 'a', 'b' ] } );
  702. schema.requireAttributes( 'image', [ 'a', 'b' ] );
  703. const image = new Element( 'image', { a: 1, b: 1 } );
  704. model.change( writer => {
  705. schema.removeDisallowedAttributes( [ image ], '$root', writer );
  706. } );
  707. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'a', 'b' ] );
  708. } );
  709. } );
  710. } );