matcher.js 15 KB

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