8
0

schema.js 34 KB

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