matcher.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 element class names', () => {
  108. const pattern = { class: 'foobar' };
  109. const matcher = new Matcher( pattern );
  110. const el1 = new Element( 'p', { class: 'foobar' } );
  111. const result = matcher.match( el1 );
  112. expect( result ).to.be.an( 'object' );
  113. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  114. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  115. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  116. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  117. expect( new Matcher( { class: 'baz' } ).match( el1 ) ).to.be.null;
  118. } );
  119. it( 'should match element class names using RegExp', () => {
  120. const pattern = { class: /fooba./ };
  121. const matcher = new Matcher( pattern );
  122. const el1 = new Element( 'p', { class: 'foobar' } );
  123. const el2 = new Element( 'p', { class: 'foobaz' } );
  124. const el3 = new Element( 'p', { class: 'qux' } );
  125. let result = matcher.match( el1 );
  126. expect( result ).to.be.an( 'object' );
  127. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  128. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  129. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  130. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  131. result = matcher.match( el2 );
  132. expect( result ).to.be.an( 'object' );
  133. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  134. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  135. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  136. expect( result.match.class[ 0 ] ).equal( 'foobaz' );
  137. expect( matcher.match( el3 ) ).to.be.null;
  138. } );
  139. it( 'should match element styles', () => {
  140. const pattern = {
  141. style: {
  142. color: 'red'
  143. }
  144. };
  145. const matcher = new Matcher( pattern );
  146. const el1 = new Element( 'p', { style: 'color: red' } );
  147. const el2 = new Element( 'p', { style: 'position: absolute' } );
  148. const result = matcher.match( el1 );
  149. expect( result ).to.be.an( 'object' );
  150. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  151. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  152. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  153. expect( result.match.style[ 0 ] ).equal( 'color' );
  154. expect( matcher.match( el2 ) ).to.be.null;
  155. expect( new Matcher( { style: { color: 'blue' } } ).match( el1 ) ).to.be.null;
  156. } );
  157. it( 'should match element styles using RegExp', () => {
  158. const pattern = {
  159. style: {
  160. color: /^.*blue$/
  161. }
  162. };
  163. const matcher = new Matcher( pattern );
  164. const el1 = new Element( 'p', { style: 'color: blue' } );
  165. const el2 = new Element( 'p', { style: 'color: darkblue' } );
  166. const el3 = new Element( 'p', { style: 'color: red' } );
  167. let result = matcher.match( el1 );
  168. expect( result ).to.be.an( 'object' );
  169. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  170. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  171. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  172. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  173. result = matcher.match( el2 );
  174. expect( result ).to.be.an( 'object' );
  175. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  176. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  177. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  178. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  179. expect( matcher.match( el3 ) ).to.be.null;
  180. } );
  181. it( 'should allow to use function as a pattern', () => {
  182. const match = { name: true };
  183. const pattern = element => {
  184. if ( element.name === 'div' && element.childCount > 0 ) {
  185. return match;
  186. }
  187. return null;
  188. };
  189. const matcher = new Matcher( pattern );
  190. const el1 = new Element( 'p' );
  191. const el2 = new Element( 'div', null, [ el1 ] );
  192. expect( matcher.match( el1 ) ).to.be.null;
  193. const result = matcher.match( el2 );
  194. expect( result ).to.be.an( 'object' );
  195. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  196. expect( result ).to.have.property( 'match' ).that.equal( match );
  197. } );
  198. it( 'should return first matched element', () => {
  199. const pattern = {
  200. name: 'p'
  201. };
  202. const el1 = new Element( 'div' );
  203. const el2 = new Element( 'p' );
  204. const el3 = new Element( 'span' );
  205. const matcher = new Matcher( pattern );
  206. const result = matcher.match( el1, el2, el3 );
  207. expect( result ).to.be.an( 'object' );
  208. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  209. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  210. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  211. expect( result.match ).to.have.property( 'name' ).that.is.true;
  212. } );
  213. it( 'should match multiple attributes', () => {
  214. const pattern = {
  215. name: 'a',
  216. attribute: {
  217. name: 'foo',
  218. title: 'bar'
  219. }
  220. };
  221. const matcher = new Matcher( pattern );
  222. const el = new Element( 'a', {
  223. name: 'foo',
  224. title: 'bar'
  225. } );
  226. const result = matcher.match( el );
  227. expect( result ).to.be.an( 'object' );
  228. expect( result ).to.have.property( 'element' ).that.equal( el );
  229. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  230. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  231. expect( result.match ).to.have.property( 'attribute' ).that.is.an( 'array' );
  232. expect( result.match.attribute[ 0 ] ).to.equal( 'name' );
  233. expect( result.match.attribute[ 1 ] ).to.equal( 'title' );
  234. } );
  235. it( 'should match multiple classes', () => {
  236. const pattern = {
  237. name: 'a',
  238. class: [ 'foo', 'bar' ]
  239. };
  240. const matcher = new Matcher( pattern );
  241. const el = new Element( 'a' );
  242. el.addClass( 'foo', 'bar', 'baz' );
  243. const result = matcher.match( el );
  244. expect( result ).to.be.an( 'object' );
  245. expect( result ).to.have.property( 'element' ).that.equal( el );
  246. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  247. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  248. expect( result.match ).to.have.property( 'class' ).that.is.an( 'array' );
  249. expect( result.match.class[ 0 ] ).to.equal( 'foo' );
  250. expect( result.match.class[ 1 ] ).to.equal( 'bar' );
  251. } );
  252. it( 'should match multiple styles', () => {
  253. const pattern = {
  254. name: 'a',
  255. style: {
  256. color: 'red',
  257. position: 'relative'
  258. }
  259. };
  260. const matcher = new Matcher( pattern );
  261. const el = new Element( 'a' );
  262. el.setStyle( {
  263. color: 'red',
  264. position: 'relative'
  265. } );
  266. const result = matcher.match( el );
  267. expect( result ).to.be.an( 'object' );
  268. expect( result ).to.have.property( 'element' ).that.equal( el );
  269. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  270. expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
  271. expect( result.match ).to.have.property( 'style' ).that.is.an( 'array' );
  272. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  273. expect( result.match.style[ 1 ] ).to.equal( 'position' );
  274. } );
  275. } );
  276. describe( 'matchAll', () => {
  277. it( 'should return all matched elements with correct patterns', () => {
  278. const matcher = new Matcher( 'p', 'div' );
  279. const el1 = new Element( 'p' );
  280. const el2 = new Element( 'div' );
  281. const el3 = new Element( 'span' );
  282. const result = matcher.matchAll( el1, el2, el3 );
  283. expect( result ).to.be.an( 'array' );
  284. expect( result.length ).to.equal( 2 );
  285. expect( result[ 0 ] ).to.have.property( 'element' ).that.equal( el1 );
  286. expect( result[ 0 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
  287. expect( result[ 0 ].pattern ).to.have.property( 'name' ).that.is.equal( 'p' );
  288. expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  289. expect( result[ 0 ].match ).to.have.property( 'name' ).that.is.true;
  290. expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
  291. expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
  292. expect( result[ 1 ].pattern ).to.have.property( 'name' ).that.is.equal( 'div' );
  293. expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  294. expect( result[ 1 ].match ).to.have.property( 'name' ).that.is.true;
  295. expect( matcher.matchAll( el3 ) ).to.be.null;
  296. } );
  297. it( 'should return all matched elements when using RegExp pattern', () => {
  298. const pattern = { class: /^red-.*/ };
  299. const matcher = new Matcher( pattern );
  300. const el1 = new Element( 'p' );
  301. const el2 = new Element( 'p' );
  302. const el3 = new Element( 'p' );
  303. el1.addClass( 'red-foreground' );
  304. el2.addClass( 'red-background' );
  305. el3.addClass( 'blue-text' );
  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.equal( pattern );
  311. expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  312. expect( result[ 0 ].match ).to.have.property( 'class' ).that.is.an( 'array' );
  313. expect( result[ 0 ].match.class[ 0 ] ).to.equal( 'red-foreground' );
  314. expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
  315. expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.equal( pattern );
  316. expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
  317. expect( result[ 1 ].match ).to.have.property( 'class' ).that.is.an( 'array' );
  318. expect( result[ 1 ].match.class[ 0 ] ).to.equal( 'red-background' );
  319. expect( matcher.matchAll( el3 ) ).to.be.null;
  320. } );
  321. } );
  322. describe( 'getElementName', () => {
  323. it( 'should return null if there are no patterns in the matcher instance', () => {
  324. const matcher = new Matcher();
  325. expect( matcher.getElementName() ).to.be.null;
  326. } );
  327. it( 'should return null if pattern has no name property', () => {
  328. const matcher = new Matcher( { class: 'foo' } );
  329. expect( matcher.getElementName() ).to.be.null;
  330. } );
  331. it( 'should return null if pattern has name property specified as RegExp', () => {
  332. const matcher = new Matcher( { name: /foo.*/ } );
  333. expect( matcher.getElementName() ).to.be.null;
  334. } );
  335. it( 'should return element name if matcher has one patter with name property specified as string', () => {
  336. const matcher = new Matcher( { name: 'div' } );
  337. expect( matcher.getElementName() ).to.equal( 'div' );
  338. } );
  339. it( 'should return null if matcher has more than one pattern', () => {
  340. const matcher = new Matcher( { name: 'div' }, { class: 'foo' } );
  341. expect( matcher.getElementName() ).to.be.null;
  342. } );
  343. it( 'should return null for matching function', () => {
  344. const matcher = new Matcher( () => {} );
  345. expect( matcher.getElementName() ).to.be.null;
  346. } );
  347. it( 'should return null for matching named function', () => {
  348. const matcher = new Matcher( function matchFunction() {} );
  349. expect( matcher.getElementName() ).to.be.null;
  350. } );
  351. } );
  352. } );