schema.js 25 KB

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