matcher.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Matcher from '../../src/view/matcher';
  6. import Element from '../../src/view/element';
  7. import Document from '../../src/view/document';
  8. import { StylesProcessor } from '../../src/view/stylesmap';
  9. describe( 'Matcher', () => {
  10. let document;
  11. beforeEach( () => {
  12. document = new Document( new StylesProcessor() );
  13. } );
  14. describe( 'add', () => {
  15. it( 'should allow to add pattern to matcher', () => {
  16. const matcher = new Matcher( 'div' );
  17. const el = new Element( document, 'p', { title: 'foobar' } );
  18. expect( matcher.match( el ) ).to.be.null;
  19. const pattern = { name: 'p', attributes: { title: 'foobar' } };
  20. matcher.add( pattern );
  21. const result = matcher.match( el );
  22. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  23. expect( result ).to.have.property( 'element' ).that.equal( el );
  24. expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
  25. expect( result.match ).to.have.property( 'attributes' ).that.is.an( 'array' );
  26. expect( result.match.attributes[ 0 ] ).to.equal( 'title' );
  27. expect( result ).to.be.an( 'object' );
  28. } );
  29. it( 'should allow to add more than one pattern', () => {
  30. const matcher = new Matcher();
  31. const el1 = new Element( document, 'p' );
  32. const el2 = new Element( document, 'div' );
  33. matcher.add( 'p', 'div' );
  34. let result = matcher.match( el1 );
  35. expect( result ).to.be.an( 'object' );
  36. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  37. expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'p' );
  38. result = matcher.match( el2 );
  39. expect( result ).to.be.an( 'object' );
  40. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  41. expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'div' );
  42. } );
  43. } );
  44. describe( 'match', () => {
  45. it( 'should match element name', () => {
  46. const matcher = new Matcher( 'p' );
  47. const el = new Element( document, 'p' );
  48. const el2 = new Element( document, 'div' );
  49. const result = matcher.match( el );
  50. expect( result ).to.be.an( 'object' );
  51. expect( result ).to.have.property( 'element' ).and.equal( el );
  52. expect( result ).to.have.property( 'pattern' );
  53. expect( result.pattern.name ).to.equal( 'p' );
  54. expect( result ).to.have.property( 'match' );
  55. expect( result.match.name ).to.be.true;
  56. expect( matcher.match( el2 ) ).to.be.null;
  57. } );
  58. it( 'should match element name with RegExp', () => {
  59. const pattern = /^text...a$/;
  60. const matcher = new Matcher( pattern );
  61. const el1 = new Element( document, 'textarea' );
  62. const el2 = new Element( document, 'div' );
  63. const result = matcher.match( el1 );
  64. expect( result ).to.be.an( 'object' );
  65. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  66. expect( result ).to.have.property( 'pattern' ).that.has.property( 'name' ).that.equal( pattern );
  67. expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
  68. expect( matcher.match( el2 ) ).to.be.null;
  69. } );
  70. it( 'should match element attributes', () => {
  71. const pattern = {
  72. attributes: {
  73. title: 'foobar'
  74. }
  75. };
  76. const matcher = new Matcher( pattern );
  77. const el1 = new Element( document, 'p', { title: 'foobar' } );
  78. const el2 = new Element( document, 'p', { title: 'foobaz' } );
  79. const el3 = new Element( document, 'p', { name: 'foobar' } );
  80. const result = matcher.match( el1 );
  81. expect( result ).to.be.an( 'object' );
  82. expect( result ).to.have.property( 'element' ).and.equal( el1 );
  83. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  84. expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
  85. expect( result.match.attributes[ 0 ] ).equal( 'title' );
  86. expect( matcher.match( el2 ) ).to.be.null;
  87. expect( matcher.match( el3 ) ).to.be.null;
  88. } );
  89. it( 'should match element attributes using RegExp', () => {
  90. const pattern = {
  91. attributes: {
  92. title: /fooba./
  93. }
  94. };
  95. const matcher = new Matcher( pattern );
  96. const el1 = new Element( document, 'p', { title: 'foobar' } );
  97. const el2 = new Element( document, 'p', { title: 'foobaz' } );
  98. const el3 = new Element( document, 'p', { title: 'qux' } );
  99. let result = matcher.match( el1 );
  100. expect( result ).to.be.an( 'object' );
  101. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  102. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  103. expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
  104. expect( result.match.attributes[ 0 ] ).equal( 'title' );
  105. result = matcher.match( el2 );
  106. expect( result ).to.be.an( 'object' );
  107. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  108. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  109. expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
  110. expect( result.match.attributes[ 0 ] ).equal( 'title' );
  111. expect( matcher.match( el3 ) ).to.be.null;
  112. } );
  113. it( 'should match if element has given attribute', () => {
  114. const pattern = {
  115. attributes: {
  116. title: true
  117. }
  118. };
  119. const matcher = new Matcher( pattern );
  120. const el1 = new Element( document, 'p', { title: 'foobar' } );
  121. const el2 = new Element( document, 'p', { title: '' } );
  122. const el3 = new Element( document, 'p' );
  123. let result = matcher.match( el1 );
  124. expect( result ).to.be.an( 'object' );
  125. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  126. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  127. expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
  128. expect( result.match.attributes[ 0 ] ).equal( 'title' );
  129. result = matcher.match( el2 );
  130. expect( result ).to.be.an( 'object' );
  131. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  132. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  133. expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
  134. expect( result.match.attributes[ 0 ] ).equal( 'title' );
  135. expect( matcher.match( el3 ) ).to.be.null;
  136. } );
  137. it( 'should match element class names', () => {
  138. const pattern = { classes: 'foobar' };
  139. const matcher = new Matcher( pattern );
  140. const el1 = new Element( document, 'p', { class: 'foobar' } );
  141. const result = matcher.match( el1 );
  142. expect( result ).to.be.an( 'object' );
  143. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  144. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  145. expect( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
  146. expect( result.match.classes[ 0 ] ).equal( 'foobar' );
  147. expect( new Matcher( { classes: 'baz' } ).match( el1 ) ).to.be.null;
  148. } );
  149. it( 'should match element class names using RegExp', () => {
  150. const pattern = { classes: /fooba./ };
  151. const matcher = new Matcher( pattern );
  152. const el1 = new Element( document, 'p', { class: 'foobar' } );
  153. const el2 = new Element( document, 'p', { class: 'foobaz' } );
  154. const el3 = new Element( document, 'p', { class: 'qux' } );
  155. let result = matcher.match( el1 );
  156. expect( result ).to.be.an( 'object' );
  157. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  158. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  159. expect( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
  160. expect( result.match.classes[ 0 ] ).equal( 'foobar' );
  161. result = matcher.match( el2 );
  162. expect( result ).to.be.an( 'object' );
  163. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  164. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  165. expect( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
  166. expect( result.match.classes[ 0 ] ).equal( 'foobaz' );
  167. expect( matcher.match( el3 ) ).to.be.null;
  168. } );
  169. it( 'should match element styles', () => {
  170. const pattern = {
  171. styles: {
  172. color: 'red'
  173. }
  174. };
  175. const matcher = new Matcher( pattern );
  176. const el1 = new Element( document, 'p', { style: 'color: red' } );
  177. const el2 = new Element( document, 'p', { style: 'position: absolute' } );
  178. const result = matcher.match( el1 );
  179. expect( result ).to.be.an( 'object' );
  180. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  181. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  182. expect( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
  183. expect( result.match.styles[ 0 ] ).equal( 'color' );
  184. expect( matcher.match( el2 ) ).to.be.null;
  185. expect( new Matcher( { styles: { color: 'blue' } } ).match( el1 ) ).to.be.null;
  186. } );
  187. it( 'should match element styles using RegExp', () => {
  188. const pattern = {
  189. styles: {
  190. color: /^.*blue$/
  191. }
  192. };
  193. const matcher = new Matcher( pattern );
  194. const el1 = new Element( document, 'p', { style: 'color: blue' } );
  195. const el2 = new Element( document, 'p', { style: 'color: darkblue' } );
  196. const el3 = new Element( document, 'p', { style: 'color: red' } );
  197. let result = matcher.match( el1 );
  198. expect( result ).to.be.an( 'object' );
  199. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  200. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  201. expect( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
  202. expect( result.match.styles[ 0 ] ).to.equal( 'color' );
  203. result = matcher.match( el2 );
  204. expect( result ).to.be.an( 'object' );
  205. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  206. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  207. expect( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
  208. expect( result.match.styles[ 0 ] ).to.equal( 'color' );
  209. expect( matcher.match( el3 ) ).to.be.null;
  210. } );
  211. it( 'should allow to use function as a pattern', () => {
  212. const match = { name: true };
  213. const pattern = element => {
  214. if ( element.name === 'div' && element.childCount > 0 ) {
  215. return match;
  216. }
  217. return null;
  218. };
  219. const matcher = new Matcher( pattern );
  220. const el1 = new Element( document, 'p' );
  221. const el2 = new Element( document, 'div', null, [ el1 ] );
  222. expect( matcher.match( el1 ) ).to.be.null;
  223. const result = matcher.match( el2 );
  224. expect( result ).to.be.an( 'object' );
  225. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  226. expect( result ).to.have.property( 'match' ).that.equal( match );
  227. } );
  228. it( 'should return first matched element', () => {
  229. const pattern = {
  230. name: 'p'
  231. };
  232. const el1 = new Element( document, 'div' );
  233. const el2 = new Element( document, 'p' );
  234. const el3 = new Element( document, 'span' );
  235. const matcher = new Matcher( pattern );
  236. const result = matcher.match( el1, el2, el3 );
  237. expect( result ).to.be.an( 'object' );
  238. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  239. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  240. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  241. expect( result.match ).to.have.property( 'name' ).that.is.true;
  242. } );
  243. it( 'should match multiple attributes', () => {
  244. const pattern = {
  245. name: 'a',
  246. attributes: {
  247. name: 'foo',
  248. title: 'bar'
  249. }
  250. };
  251. const matcher = new Matcher( pattern );
  252. const el = new Element( document, 'a', {
  253. name: 'foo',
  254. title: 'bar'
  255. } );
  256. const result = matcher.match( el );
  257. expect( result ).to.be.an( 'object' );
  258. expect( result ).to.have.property( 'element' ).that.equal( el );
  259. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  260. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  261. expect( result.match ).to.have.property( 'attributes' ).that.is.an( 'array' );
  262. expect( result.match.attributes[ 0 ] ).to.equal( 'name' );
  263. expect( result.match.attributes[ 1 ] ).to.equal( 'title' );
  264. } );
  265. it( 'should match multiple classes', () => {
  266. const pattern = {
  267. name: 'a',
  268. classes: [ 'foo', 'bar' ]
  269. };
  270. const matcher = new Matcher( pattern );
  271. const el = new Element( document, 'a' );
  272. el._addClass( [ 'foo', 'bar', 'baz' ] );
  273. const result = matcher.match( el );
  274. expect( result ).to.be.an( 'object' );
  275. expect( result ).to.have.property( 'element' ).that.equal( el );
  276. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  277. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  278. expect( result.match ).to.have.property( 'classes' ).that.is.an( 'array' );
  279. expect( result.match.classes[ 0 ] ).to.equal( 'foo' );
  280. expect( result.match.classes[ 1 ] ).to.equal( 'bar' );
  281. } );
  282. it( 'should match multiple styles', () => {
  283. const pattern = {
  284. name: 'a',
  285. styles: {
  286. color: 'red',
  287. position: 'relative'
  288. }
  289. };
  290. const matcher = new Matcher( pattern );
  291. const el = new Element( document, 'a' );
  292. el._setStyle( {
  293. color: 'red',
  294. position: 'relative'
  295. } );
  296. const result = matcher.match( el );
  297. expect( result ).to.be.an( 'object' );
  298. expect( result ).to.have.property( 'element' ).that.equal( el );
  299. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  300. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  301. expect( result.match ).to.have.property( 'styles' ).that.is.an( 'array' );
  302. expect( result.match.styles[ 0 ] ).to.equal( 'color' );
  303. expect( result.match.styles[ 1 ] ).to.equal( 'position' );
  304. } );
  305. } );
  306. describe( 'matchAll', () => {
  307. it( 'should return all matched elements with correct patterns', () => {
  308. const matcher = new Matcher( 'p', 'div' );
  309. const el1 = new Element( document, 'p' );
  310. const el2 = new Element( document, 'div' );
  311. const el3 = new Element( document, 'span' );
  312. const result = matcher.matchAll( el1, el2, el3 );
  313. expect( result ).to.be.an( 'array' );
  314. expect( result.length ).to.equal( 2 );
  315. expect( result[ 0 ] ).to.have.property( 'element' ).that.equal( el1 );
  316. expect( result[ 0 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
  317. expect( result[ 0 ].pattern ).to.have.property( 'name' ).that.is.equal( 'p' );
  318. expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  319. expect( result[ 0 ].match ).to.have.property( 'name' ).that.is.true;
  320. expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
  321. expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
  322. expect( result[ 1 ].pattern ).to.have.property( 'name' ).that.is.equal( 'div' );
  323. expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  324. expect( result[ 1 ].match ).to.have.property( 'name' ).that.is.true;
  325. expect( matcher.matchAll( el3 ) ).to.be.null;
  326. } );
  327. it( 'should return all matched elements when using RegExp pattern', () => {
  328. const pattern = { classes: /^red-.*/ };
  329. const matcher = new Matcher( pattern );
  330. const el1 = new Element( document, 'p' );
  331. const el2 = new Element( document, 'p' );
  332. const el3 = new Element( document, 'p' );
  333. el1._addClass( 'red-foreground' );
  334. el2._addClass( 'red-background' );
  335. el3._addClass( 'blue-text' );
  336. const result = matcher.matchAll( el1, el2, el3 );
  337. expect( result ).to.be.an( 'array' );
  338. expect( result.length ).to.equal( 2 );
  339. expect( result[ 0 ] ).to.have.property( 'element' ).that.equal( el1 );
  340. expect( result[ 0 ] ).to.have.property( 'pattern' ).that.is.equal( pattern );
  341. expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  342. expect( result[ 0 ].match ).to.have.property( 'classes' ).that.is.an( 'array' );
  343. expect( result[ 0 ].match.classes[ 0 ] ).to.equal( 'red-foreground' );
  344. expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
  345. expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.equal( pattern );
  346. expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  347. expect( result[ 1 ].match ).to.have.property( 'classes' ).that.is.an( 'array' );
  348. expect( result[ 1 ].match.classes[ 0 ] ).to.equal( 'red-background' );
  349. expect( matcher.matchAll( el3 ) ).to.be.null;
  350. } );
  351. } );
  352. describe( 'getElementName', () => {
  353. it( 'should return null if there are no patterns in the matcher instance', () => {
  354. const matcher = new Matcher();
  355. expect( matcher.getElementName() ).to.be.null;
  356. } );
  357. it( 'should return null if pattern has no name property', () => {
  358. const matcher = new Matcher( { classes: 'foo' } );
  359. expect( matcher.getElementName() ).to.be.null;
  360. } );
  361. it( 'should return null if pattern has name property specified as RegExp', () => {
  362. const matcher = new Matcher( { name: /foo.*/ } );
  363. expect( matcher.getElementName() ).to.be.null;
  364. } );
  365. it( 'should return element name if matcher has one patter with name property specified as string', () => {
  366. const matcher = new Matcher( { name: 'div' } );
  367. expect( matcher.getElementName() ).to.equal( 'div' );
  368. } );
  369. it( 'should return null if matcher has more than one pattern', () => {
  370. const matcher = new Matcher( { name: 'div' }, { classes: 'foo' } );
  371. expect( matcher.getElementName() ).to.be.null;
  372. } );
  373. it( 'should return null for matching function', () => {
  374. const matcher = new Matcher( () => {} );
  375. expect( matcher.getElementName() ).to.be.null;
  376. } );
  377. it( 'should return null for matching named function', () => {
  378. const matcher = new Matcher( function matchFunction() {} );
  379. expect( matcher.getElementName() ).to.be.null;
  380. } );
  381. } );
  382. } );