matcher.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Matcher from '/ckeditor5/engine/treeview/matcher.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. describe( 'Matcher', () => {
  10. describe( 'add', () => {
  11. it( 'should allow to add pattern to matcher', () => {
  12. const matcher = new Matcher( 'div' );
  13. const el = new Element( 'p', { title: 'foobar' } );
  14. expect( matcher.match( el ) ).to.be.null;
  15. const pattern = { name: 'p', attribute: { title: 'foobar' } };
  16. matcher.add( pattern );
  17. const result = matcher.match( el );
  18. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  19. expect( result ).to.have.property( 'element' ).that.equal( el );
  20. expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
  21. expect( result.match ).to.have.property( 'attribute' ).that.is.an( 'array' );
  22. expect( result.match.attribute[ 0 ] ).to.equal( 'title' );
  23. expect( result ).to.be.an( 'object' );
  24. } );
  25. it( 'should allow to add more than one pattern', () => {
  26. const matcher = new Matcher();
  27. const el1 = new Element( 'p' );
  28. const el2 = new Element( 'div' );
  29. matcher.add( 'p', 'div' );
  30. let result = matcher.match( el1 );
  31. expect( result ).to.be.an( 'object' );
  32. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  33. expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'p' );
  34. result = matcher.match( el2 );
  35. expect( result ).to.be.an( 'object' );
  36. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  37. expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'div' );
  38. } );
  39. } );
  40. describe( 'match', () => {
  41. it( 'should match element name', () => {
  42. const matcher = new Matcher( 'p' );
  43. const el = new Element( 'p' );
  44. const el2 = new Element( 'div' );
  45. const result = matcher.match( el );
  46. expect( result ).to.be.an( 'object' );
  47. expect( result ).to.have.property( 'element' ).and.equal( el );
  48. expect( result ).to.have.property( 'pattern' );
  49. expect( result.pattern.name ).to.equal( 'p' );
  50. expect( result ).to.have.property( 'match' );
  51. expect( result.match.name ).to.be.true;
  52. expect( matcher.match( el2 ) ).to.be.null;
  53. } );
  54. it( 'should match element name with RegExp', () => {
  55. const pattern = { name: /^text...a$/ };
  56. const matcher = new Matcher( pattern );
  57. const el1 = new Element( 'textarea' );
  58. const el2 = new Element( 'div' );
  59. const result = matcher.match( el1 );
  60. expect( result ).to.be.an( 'object' );
  61. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  62. expect( result ).to.have.property( 'pattern' ).that.has.property( 'name' ).that.equal( pattern.name );
  63. expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
  64. expect( matcher.match( el2 ) ).to.be.null;
  65. } );
  66. it( 'should match element attributes', () => {
  67. const pattern = {
  68. attribute: {
  69. title: 'foobar'
  70. }
  71. };
  72. const matcher = new Matcher( pattern );
  73. const el1 = new Element( 'p', { title: 'foobar' } );
  74. const el2 = new Element( 'p', { title: 'foobaz' } );
  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. } );
  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. let result = matcher.match( el1 );
  93. expect( result ).to.be.an( 'object' );
  94. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  95. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  96. expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
  97. expect( result.match.attribute[ 0 ] ).equal( 'title' );
  98. result = matcher.match( el2 );
  99. expect( result ).to.be.an( 'object' );
  100. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  101. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  102. expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
  103. expect( result.match.attribute[ 0 ] ).equal( 'title' );
  104. } );
  105. it( 'should match element class names', () => {
  106. const pattern = { class: 'foobar' };
  107. const matcher = new Matcher( pattern );
  108. const el1 = new Element( 'p', { class: 'foobar' } );
  109. const result = matcher.match( el1 );
  110. expect( result ).to.be.an( 'object' );
  111. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  112. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  113. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  114. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  115. } );
  116. it( 'should match element class names using RegExp', () => {
  117. const pattern = { class: /fooba./ };
  118. const matcher = new Matcher( pattern );
  119. const el1 = new Element( 'p', { class: 'foobar' } );
  120. const el2 = new Element( 'p', { class: 'foobaz' } );
  121. let result = matcher.match( el1 );
  122. expect( result ).to.be.an( 'object' );
  123. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  124. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  125. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  126. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  127. result = matcher.match( el2 );
  128. expect( result ).to.be.an( 'object' );
  129. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  130. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  131. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  132. expect( result.match.class[ 0 ] ).equal( 'foobaz' );
  133. } );
  134. it( 'should match element styles', () => {
  135. const pattern = {
  136. style: {
  137. color: 'red'
  138. }
  139. };
  140. const matcher = new Matcher( pattern );
  141. const el1 = new Element( 'p', { style: 'color: red' } );
  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( 'style' ).that.is.an( 'array' );
  147. expect( result.match.style[ 0 ] ).equal( 'color' );
  148. } );
  149. it( 'should match element styles using RegExp', () => {
  150. const pattern = {
  151. style: {
  152. color: /^.*blue$/
  153. }
  154. };
  155. const matcher = new Matcher( pattern );
  156. const el1 = new Element( 'p', { style: 'color: blue' } );
  157. const el2 = new Element( 'p', { style: 'color: darkblue' } );
  158. let result = matcher.match( el1 );
  159. expect( result ).to.be.an( 'object' );
  160. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  161. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  162. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  163. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  164. result = matcher.match( el2 );
  165. expect( result ).to.be.an( 'object' );
  166. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  167. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  168. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  169. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  170. } );
  171. } );
  172. } );