8
0

matcher.js 18 KB

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