8
0

matcher.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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( 'match', () => {
  11. it( 'should match element name', () => {
  12. const matcher = new Matcher( 'p' );
  13. const el = new Element( 'p' );
  14. const el2 = new Element( 'div' );
  15. const result = matcher.match( el );
  16. expect( result ).to.be.an( 'object' );
  17. expect( result ).to.have.property( 'element' ).and.equal( el );
  18. expect( result ).to.have.property( 'pattern' );
  19. expect( result.pattern.name ).to.equal( 'p' );
  20. expect( result ).to.have.property( 'match' );
  21. expect( result.match.name ).to.be.true;
  22. expect( matcher.match( el2 ) ).to.be.null;
  23. } );
  24. it( 'should match element name with RegExp', () => {
  25. const pattern = { name: /^text...a$/ };
  26. const matcher = new Matcher( pattern );
  27. const el1 = new Element( 'textarea' );
  28. const el2 = new Element( 'div' );
  29. const 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.has.property( 'name' ).that.equal( pattern.name );
  33. expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
  34. expect( matcher.match( el2 ) ).to.be.null;
  35. } );
  36. it( 'should match element attributes', () => {
  37. const pattern = {
  38. attribute: {
  39. title: 'foobar'
  40. }
  41. };
  42. const matcher = new Matcher( pattern );
  43. const el1 = new Element( 'p', { title: 'foobar' } );
  44. const el2 = new Element( 'p', { title: 'foobaz' } );
  45. const result = matcher.match( el1 );
  46. expect( result ).to.be.an( 'object' );
  47. expect( result ).to.have.property( 'element' ).and.equal( el1 );
  48. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  49. expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
  50. expect( result.match.attribute[ 0 ] ).equal( 'title' );
  51. expect( matcher.match( el2 ) ).to.be.null;
  52. } );
  53. it( 'should match element attributes using RegExp', () => {
  54. const pattern = {
  55. attribute: {
  56. title: /fooba./
  57. }
  58. };
  59. const matcher = new Matcher( pattern );
  60. const el1 = new Element( 'p', { title: 'foobar' } );
  61. const el2 = new Element( 'p', { title: 'foobaz' } );
  62. let 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.equal( pattern );
  66. expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
  67. expect( result.match.attribute[ 0 ] ).equal( 'title' );
  68. result = matcher.match( el2 );
  69. expect( result ).to.be.an( 'object' );
  70. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  71. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  72. expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
  73. expect( result.match.attribute[ 0 ] ).equal( 'title' );
  74. } );
  75. it( 'should match element class names', () => {
  76. const pattern = { class: 'foobar' };
  77. const matcher = new Matcher( pattern );
  78. const el1 = new Element( 'p', { class: 'foobar' } );
  79. const result = matcher.match( el1 );
  80. expect( result ).to.be.an( 'object' );
  81. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  82. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  83. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  84. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  85. } );
  86. it( 'should match element class names using RegExp', () => {
  87. const pattern = { class: /fooba./ };
  88. const matcher = new Matcher( pattern );
  89. const el1 = new Element( 'p', { class: 'foobar' } );
  90. const el2 = new Element( 'p', { class: 'foobaz' } );
  91. let result = matcher.match( el1 );
  92. expect( result ).to.be.an( 'object' );
  93. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  94. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  95. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  96. expect( result.match.class[ 0 ] ).equal( 'foobar' );
  97. result = matcher.match( el2 );
  98. expect( result ).to.be.an( 'object' );
  99. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  100. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  101. expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
  102. expect( result.match.class[ 0 ] ).equal( 'foobaz' );
  103. } );
  104. it( 'should match element styles', () => {
  105. const pattern = {
  106. style: {
  107. color: 'red'
  108. }
  109. };
  110. const matcher = new Matcher( pattern );
  111. const el1 = new Element( 'p', { style: 'color: red' } );
  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( 'style' ).that.is.an( 'array' );
  117. expect( result.match.style[ 0 ] ).equal( 'color' );
  118. } );
  119. it( 'should match element styles using RegExp', () => {
  120. const pattern = {
  121. style: {
  122. color: /^.*blue$/
  123. }
  124. };
  125. const matcher = new Matcher( pattern );
  126. const el1 = new Element( 'p', { style: 'color: blue' } );
  127. const el2 = new Element( 'p', { style: 'color: darkblue' } );
  128. let result = matcher.match( el1 );
  129. expect( result ).to.be.an( 'object' );
  130. expect( result ).to.have.property( 'element' ).that.equal( el1 );
  131. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  132. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  133. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  134. result = matcher.match( el2 );
  135. expect( result ).to.be.an( 'object' );
  136. expect( result ).to.have.property( 'element' ).that.equal( el2 );
  137. expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
  138. expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
  139. expect( result.match.style[ 0 ] ).to.equal( 'color' );
  140. } );
  141. } );
  142. } );