matcher.js 17 KB

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