schema.js 28 KB

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