8
0

matcher.js 18 KB

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