8
0

schema.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 Document from '../../../src/model/document';
  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. doc = new Document();
  235. root = doc.createRoot( 'div' );
  236. root.insertChildren( 0, [
  237. new Element( 'div' ),
  238. new Element( 'header' ),
  239. new Element( 'p' )
  240. ] );
  241. schema.registerItem( 'div', '$block' );
  242. schema.registerItem( 'header', '$block' );
  243. schema.registerItem( 'p', '$block' );
  244. schema.allow( { name: '$block', inside: 'div' } );
  245. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  246. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  247. } );
  248. it( 'should return true if given element is allowed by schema at given position', () => {
  249. // Block should be allowed in root.
  250. expect( schema.check( { name: '$block', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  251. // P is block and block should be allowed in root.
  252. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  253. // P is allowed in DIV by the set rule.
  254. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  255. // Inline is allowed in any block and is allowed with attribute bold.
  256. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  257. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  258. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ) } ) ).to.be.true;
  259. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  260. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  261. // Header is allowed in DIV.
  262. expect( schema.check( { name: 'header', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  263. // Inline is allowed in block and root is DIV, which is block.
  264. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  265. } );
  266. it( 'should return false if given element is not allowed by schema at given position', () => {
  267. // P with attribute is not allowed anywhere.
  268. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ), attributes: 'bold' } ) ).to.be.false;
  269. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  270. // Bold text is not allowed in header
  271. expect( schema.check( { name: '$text', inside: new Position( root, [ 1, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  272. } );
  273. it( 'should return false if given element is not registered in schema', () => {
  274. expect( schema.check( { name: 'new', inside: new Position( root, [ 0 ] ) } ) ).to.be.false;
  275. } );
  276. } );
  277. describe( 'bug #732', () => {
  278. // Ticket case.
  279. it( 'should return false if given element is allowed in the root but not deeper', () => {
  280. schema.registerItem( 'paragraph', '$block' );
  281. expect( schema.check( { name: 'paragraph', inside: [ '$root', 'paragraph' ] } ) ).to.be.false;
  282. } );
  283. // Two additional, real life cases accompanying the ticket case.
  284. it( 'should return true if checking whether text is allowed in $root > paragraph', () => {
  285. schema.registerItem( 'paragraph', '$block' );
  286. expect( schema.check( { name: '$text', inside: [ '$root', 'paragraph' ] } ) ).to.be.true;
  287. } );
  288. it( 'should return true if checking whether text is allowed in paragraph', () => {
  289. schema.registerItem( 'paragraph', '$block' );
  290. expect( schema.check( { name: '$text', inside: [ 'paragraph' ] } ) ).to.be.true;
  291. } );
  292. // Veryfing the matching algorithm.
  293. // The right ends of the element to check and "inside" paths must match.
  294. describe( 'right ends of paths must match', () => {
  295. beforeEach( () => {
  296. schema.registerItem( 'a' );
  297. schema.registerItem( 'b' );
  298. schema.registerItem( 'c' );
  299. schema.registerItem( 'd' );
  300. schema.registerItem( 'e' );
  301. schema.allow( { name: 'a', inside: [ 'b', 'c', 'd' ] } );
  302. schema.allow( { name: 'e', inside: [ 'a' ] } );
  303. } );
  304. // Simple chains created by a single allow() call.
  305. it( 'a inside b, c', () => {
  306. expect( schema.check( { name: 'a', inside: [ 'b', 'c' ] } ) ).to.be.false;
  307. } );
  308. it( 'a inside b', () => {
  309. expect( schema.check( { name: 'a', inside: [ 'b' ] } ) ).to.be.false;
  310. } );
  311. it( 'a inside b, c, d', () => {
  312. expect( schema.check( { name: 'a', inside: [ 'b', 'c', 'd' ] } ) ).to.be.true;
  313. } );
  314. it( 'a inside c, d', () => {
  315. expect( schema.check( { name: 'a', inside: [ 'c', 'd' ] } ) ).to.be.true;
  316. } );
  317. it( 'a inside d', () => {
  318. expect( schema.check( { name: 'a', inside: [ 'd' ] } ) ).to.be.true;
  319. } );
  320. // "Allowed in" chains created by two separate allow() calls (`e inside a` and `a inside b,c,d`).
  321. it( 'e inside a, d', () => {
  322. expect( schema.check( { name: 'e', inside: [ 'd', 'a' ] } ) ).to.be.true;
  323. } );
  324. it( 'e inside b, c, d', () => {
  325. expect( schema.check( { name: 'e', inside: [ 'b', 'c', 'd' ] } ) ).to.be.false;
  326. } );
  327. } );
  328. } );
  329. } );
  330. describe( 'itemExtends()', () => {
  331. it( 'should return true if given item extends another given item', () => {
  332. schema.registerItem( 'div', '$block' );
  333. schema.registerItem( 'myDiv', 'div' );
  334. expect( schema.itemExtends( 'div', '$block' ) ).to.be.true;
  335. expect( schema.itemExtends( 'myDiv', 'div' ) ).to.be.true;
  336. expect( schema.itemExtends( 'myDiv', '$block' ) ).to.be.true;
  337. } );
  338. it( 'should return false if given item does not extend another given item', () => {
  339. schema.registerItem( 'div' );
  340. schema.registerItem( 'myDiv', 'div' );
  341. expect( schema.itemExtends( 'div', '$block' ) ).to.be.false;
  342. expect( schema.itemExtends( 'div', 'myDiv' ) ).to.be.false;
  343. } );
  344. it( 'should throw if one or both given items are not registered in schema', () => {
  345. expect( () => {
  346. schema.itemExtends( 'foo', '$block' );
  347. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  348. expect( () => {
  349. schema.itemExtends( '$block', 'foo' );
  350. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  351. } );
  352. } );
  353. describe( '_normalizeQueryPath()', () => {
  354. it( 'should normalize string with spaces to an array of strings', () => {
  355. expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );
  356. } );
  357. it( 'should normalize model position to an array of strings', () => {
  358. const doc = new Document();
  359. const root = doc.createRoot();
  360. root.insertChildren( 0, [
  361. new Element( 'div', null, [
  362. new Element( 'header' )
  363. ] )
  364. ] );
  365. const position = new Position( root, [ 0, 0, 0 ] );
  366. expect( Schema._normalizeQueryPath( position ) ).to.deep.equal( [ '$root', 'div', 'header' ] );
  367. } );
  368. it( 'should normalize array with strings and model elements to an array of strings and drop unrecognized parts', () => {
  369. const input = [
  370. '$root',
  371. [ 'div' ],
  372. new Element( 'div' ),
  373. null,
  374. new Element( 'p' ),
  375. 'strong'
  376. ];
  377. expect( Schema._normalizeQueryPath( input ) ).to.deep.equal( [ '$root', 'div', 'p', 'strong' ] );
  378. } );
  379. } );
  380. describe( 'checkAttributeInSelection()', () => {
  381. const attribute = 'bold';
  382. let doc, schema;
  383. beforeEach( () => {
  384. doc = new Document();
  385. doc.createRoot();
  386. schema = doc.schema;
  387. schema.registerItem( 'p', '$block' );
  388. schema.registerItem( 'h1', '$block' );
  389. schema.registerItem( 'img', '$inline' );
  390. schema.registerItem( 'figure' );
  391. // Bold text is allowed only in P.
  392. schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  393. schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  394. // Disallow bold on image.
  395. schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
  396. // Figure must have name attribute and optional title attribute.
  397. schema.requireAttributes( 'figure', [ 'name' ] );
  398. schema.allow( { name: 'figure', attributes: [ 'title', 'name' ], inside: '$root' } );
  399. } );
  400. describe( 'when selection is collapsed', () => {
  401. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  402. setData( doc, '<p>f[]oo</p>' );
  403. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  404. } );
  405. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  406. setData( doc, '<h1>[]</h1>' );
  407. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  408. setData( doc, '[]' );
  409. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  410. } );
  411. } );
  412. describe( 'when selection is not collapsed', () => {
  413. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  414. // Simple selection on a few characters.
  415. setData( doc, '<p>[foo]</p>' );
  416. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  417. // Selection spans over characters but also include nodes that can't have attribute.
  418. setData( doc, '<p>fo[o<img />b]ar</p>' );
  419. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  420. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  421. setData( doc, '[<p>foo<img />bar</p><h1></h1>]' );
  422. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  423. // Selection on empty P. P can have the attribute.
  424. setData( doc, '[<p></p>]' );
  425. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.true;
  426. } );
  427. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  428. // Selection on DIV which can't have bold text.
  429. setData( doc, '[<h1></h1>]' );
  430. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  431. // Selection on two images which can't be bold.
  432. setData( doc, '<p>foo[<img /><img />]bar</p>' );
  433. expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
  434. } );
  435. it( 'should return true when checking element with required attribute', () => {
  436. setData( doc, '[<figure name="figure"></figure>]' );
  437. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  438. } );
  439. it( 'should return true when checking element when attribute is already present', () => {
  440. setData( doc, '[<figure name="figure" title="title"></figure>]' );
  441. expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
  442. } );
  443. } );
  444. } );
  445. describe( 'getValidRanges()', () => {
  446. const attribute = 'bold';
  447. let doc, root, schema, ranges;
  448. beforeEach( () => {
  449. doc = new Document();
  450. schema = doc.schema;
  451. root = doc.createRoot();
  452. schema.registerItem( 'p', '$block' );
  453. schema.registerItem( 'h1', '$block' );
  454. schema.registerItem( 'img', '$inline' );
  455. schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  456. schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  457. setData( doc, '<p>foo<img />bar</p>' );
  458. ranges = [ Range.createOn( root.getChild( 0 ) ) ];
  459. } );
  460. it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
  461. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  462. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  463. } );
  464. it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
  465. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  466. schema.allow( { name: '$text', inside: 'img' } );
  467. expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
  468. } );
  469. it( 'should return two ranges when attribute is not allowed on one item', () => {
  470. schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
  471. schema.allow( { name: '$text', inside: 'img' } );
  472. setData( doc, '[<p>foo<img>xxx</img>bar</p>]' );
  473. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  474. const sel = new Selection();
  475. sel.setRanges( validRanges );
  476. expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
  477. } );
  478. it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
  479. schema.allow( { name: '$text', inside: 'img' } );
  480. schema.allow( { name: '$text', attributes: 'bold', inside: 'img' } );
  481. setData( doc, '[<p>foo<img>xxx</img>bar</p>]' );
  482. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  483. const sel = new Selection();
  484. sel.setRanges( validRanges );
  485. expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
  486. } );
  487. it( 'should not leak beyond the given ranges', () => {
  488. setData( doc, '<p>[foo<img></img>bar]x[bar<img></img>foo]</p>' );
  489. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  490. const sel = new Selection();
  491. sel.setRanges( validRanges );
  492. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img></img>[bar]x[bar]<img></img>[foo]</p>' );
  493. } );
  494. it( 'should correctly handle a range which ends in a disallowed position', () => {
  495. schema.allow( { name: '$text', inside: 'img' } );
  496. setData( doc, '<p>[foo<img>bar]</img>bom</p>' );
  497. const validRanges = schema.getValidRanges( doc.selection.getRanges(), attribute );
  498. const sel = new Selection();
  499. sel.setRanges( validRanges );
  500. expect( stringify( root, sel ) ).to.equal( '<p>[foo]<img>bar</img>bom</p>' );
  501. } );
  502. it( 'should split range into two ranges and omit disallowed element', () => {
  503. // Disallow bold on img.
  504. doc.schema.disallow( { name: 'img', attributes: 'bold', inside: 'p' } );
  505. const result = schema.getValidRanges( ranges, attribute );
  506. expect( result ).to.length( 2 );
  507. expect( result[ 0 ].start.path ).to.members( [ 0 ] );
  508. expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
  509. expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
  510. expect( result[ 1 ].end.path ).to.members( [ 1 ] );
  511. } );
  512. } );
  513. describe( 'getLimitElement()', () => {
  514. let doc, root;
  515. beforeEach( () => {
  516. doc = new Document();
  517. schema = doc.schema;
  518. root = doc.createRoot();
  519. schema.registerItem( 'div', '$block' );
  520. schema.registerItem( 'article', '$block' );
  521. schema.registerItem( 'section', '$block' );
  522. schema.registerItem( 'paragraph', '$block' );
  523. schema.registerItem( 'widget', '$block' );
  524. schema.registerItem( 'image', '$block' );
  525. schema.registerItem( 'caption', '$block' );
  526. schema.allow( { name: 'image', inside: 'widget' } );
  527. schema.allow( { name: 'caption', inside: 'image' } );
  528. schema.allow( { name: 'paragraph', inside: 'article' } );
  529. schema.allow( { name: 'article', inside: 'section' } );
  530. schema.allow( { name: 'section', inside: 'div' } );
  531. schema.allow( { name: 'widget', inside: 'div' } );
  532. } );
  533. it( 'always returns $root element if any other limit was not defined', () => {
  534. schema.limits.clear();
  535. setData( doc, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  536. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  537. } );
  538. it( 'returns the limit element which is the closest element to common ancestor for collapsed selection', () => {
  539. schema.limits.add( 'article' );
  540. schema.limits.add( 'section' );
  541. setData( doc, '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' );
  542. const article = root.getNodeByPath( [ 0, 0, 0 ] );
  543. expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
  544. } );
  545. it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
  546. schema.limits.add( 'article' );
  547. schema.limits.add( 'section' );
  548. setData( doc, '<div><section><article>[foo</article><article>bar]</article></section></div>' );
  549. const section = root.getNodeByPath( [ 0, 0 ] );
  550. expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
  551. } );
  552. it( 'works fine with multi-range selections', () => {
  553. schema.limits.add( 'article' );
  554. schema.limits.add( 'widget' );
  555. schema.limits.add( 'div' );
  556. setData(
  557. doc,
  558. '<div>' +
  559. '<section>' +
  560. '<article>' +
  561. '<paragraph>[foo]</paragraph>' +
  562. '</article>' +
  563. '</section>' +
  564. '<widget>' +
  565. '<image>' +
  566. '<caption>b[a]r</caption>' +
  567. '</image>' +
  568. '</widget>' +
  569. '</div>'
  570. );
  571. const div = root.getNodeByPath( [ 0 ] );
  572. expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
  573. } );
  574. it( 'works fine with multi-range selections even if limit elements are not defined', () => {
  575. schema.limits.clear();
  576. setData(
  577. doc,
  578. '<div>' +
  579. '<section>' +
  580. '<article>' +
  581. '<paragraph>[foo]</paragraph>' +
  582. '</article>' +
  583. '</section>' +
  584. '</div>' +
  585. '<section>b[]ar</section>'
  586. );
  587. expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
  588. } );
  589. } );
  590. describe( 'removeDisallowedAttributes()', () => {
  591. let doc, root;
  592. beforeEach( () => {
  593. doc = new Document();
  594. root = doc.createRoot();
  595. schema = doc.schema;
  596. schema.registerItem( 'paragraph', '$block' );
  597. schema.registerItem( 'div', '$block' );
  598. schema.registerItem( 'image' );
  599. schema.objects.add( 'image' );
  600. schema.allow( { name: '$block', inside: 'div' } );
  601. } );
  602. describe( 'filtering attributes from nodes', () => {
  603. let text, image;
  604. beforeEach( () => {
  605. schema.allow( { name: '$text', attributes: [ 'a' ], inside: '$root' } );
  606. schema.allow( { name: 'image', attributes: [ 'b' ], inside: '$root' } );
  607. text = new Text( 'foo', { a: 1, b: 1 } );
  608. image = new Element( 'image', { a: 1, b: 1 } );
  609. } );
  610. it( 'should filter out disallowed attributes from given nodes', () => {
  611. const root = doc.getRoot();
  612. const batch = doc.batch();
  613. root.appendChildren( [ text, image ] );
  614. schema.removeDisallowedAttributes( [ text, image ], '$root', batch );
  615. expect( Array.from( text.getAttributeKeys() ) ).to.deep.equal( [ 'a' ] );
  616. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'b' ] );
  617. expect( batch.deltas ).to.length( 2 );
  618. expect( batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  619. expect( batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  620. } );
  621. } );
  622. describe( 'filtering attributes from child nodes', () => {
  623. let div;
  624. beforeEach( () => {
  625. schema.allow( { name: '$text', attributes: [ 'a' ], inside: 'div' } );
  626. schema.allow( { name: '$text', attributes: [ 'b' ], inside: 'div paragraph' } );
  627. schema.allow( { name: 'image', attributes: [ 'a' ], inside: 'div' } );
  628. schema.allow( { name: 'image', attributes: [ 'b' ], inside: 'div paragraph' } );
  629. const foo = new Text( 'foo', { a: 1, b: 1 } );
  630. const bar = new Text( 'bar', { a: 1, b: 1 } );
  631. const imageInDiv = new Element( 'image', { a: 1, b: 1 } );
  632. const imageInParagraph = new Element( 'image', { a: 1, b: 1 } );
  633. const paragraph = new Element( 'paragraph', [], [ foo, imageInParagraph ] );
  634. div = new Element( 'div', [], [ paragraph, bar, imageInDiv ] );
  635. } );
  636. it( 'should filter out disallowed attributes from child nodes (batch)', () => {
  637. const root = doc.getRoot();
  638. const batch = doc.batch();
  639. root.appendChildren( [ div ] );
  640. schema.removeDisallowedAttributes( [ div ], '$root', batch );
  641. expect( batch.deltas ).to.length( 4 );
  642. expect( batch.deltas[ 0 ] ).to.instanceof( AttributeDelta );
  643. expect( batch.deltas[ 1 ] ).to.instanceof( AttributeDelta );
  644. expect( batch.deltas[ 2 ] ).to.instanceof( AttributeDelta );
  645. expect( batch.deltas[ 3 ] ).to.instanceof( AttributeDelta );
  646. expect( getData( doc, { withoutSelection: true } ) )
  647. .to.equal(
  648. '<div>' +
  649. '<paragraph>' +
  650. '<$text b="1">foo</$text>' +
  651. '<image b="1"></image>' +
  652. '</paragraph>' +
  653. '<$text a="1">bar</$text>' +
  654. '<image a="1"></image>' +
  655. '</div>'
  656. );
  657. } );
  658. } );
  659. describe( 'allowed parameters', () => {
  660. let frag;
  661. beforeEach( () => {
  662. schema.allow( { name: '$text', attributes: [ 'a' ], inside: '$root' } );
  663. schema.allow( { name: '$text', attributes: [ 'b' ], inside: 'paragraph' } );
  664. frag = new DocumentFragment( [
  665. new Text( 'foo', { a: 1 } ),
  666. new Element( 'paragraph', [], [ new Text( 'bar', { a: 1, b: 1 } ) ] ),
  667. new Text( 'biz', { b: 1 } )
  668. ] );
  669. } );
  670. it( 'should accept iterable as nodes', () => {
  671. schema.removeDisallowedAttributes( frag.getChildren(), '$root', doc.batch() );
  672. expect( stringify( frag ) )
  673. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  674. } );
  675. it( 'should accept Position as inside', () => {
  676. schema.removeDisallowedAttributes( frag.getChildren(), Position.createAt( root ), doc.batch() );
  677. expect( stringify( frag ) )
  678. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  679. } );
  680. it( 'should accept Node as inside', () => {
  681. schema.removeDisallowedAttributes( frag.getChildren(), [ root ], doc.batch() );
  682. expect( stringify( frag ) )
  683. .to.equal( '<$text a="1">foo</$text><paragraph><$text b="1">bar</$text></paragraph>biz' );
  684. } );
  685. } );
  686. it( 'should not filter out allowed combination of attributes', () => {
  687. schema.allow( { name: 'image', attributes: [ 'a', 'b' ] } );
  688. schema.requireAttributes( 'image', [ 'a', 'b' ] );
  689. const image = new Element( 'image', { a: 1, b: 1 } );
  690. schema.removeDisallowedAttributes( [ image ], '$root', doc.batch() );
  691. expect( Array.from( image.getAttributeKeys() ) ).to.deep.equal( [ 'a', 'b' ] );
  692. } );
  693. } );
  694. } );