matcher.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 match = matcher.match( el );
  16. expect( match ).to.be.an( 'object' );
  17. expect( match ).to.have.property( 'element' );
  18. expect( match.element ).to.equal( el );
  19. expect( matcher.match( el2 ) ).to.be.null;
  20. } );
  21. it( 'should match element name with RegExp', () => {
  22. const matcher = new Matcher( { name: /^text...a$/ } );
  23. const el1 = new Element( 'textarea' );
  24. const el2 = new Element( 'div' );
  25. const match = matcher.match( el1 );
  26. expect( match ).to.be.an( 'object' );
  27. expect( match ).to.have.property( 'element' );
  28. expect( match.element ).to.equal( el1 );
  29. expect( matcher.match( el2 ) ).to.be.null;
  30. } );
  31. it( 'should match element attributes', () => {
  32. const matcher = new Matcher( {
  33. attribute: {
  34. title: 'foobar'
  35. }
  36. } );
  37. const el1 = new Element( 'p', { title: 'foobar' } );
  38. const el2 = new Element( 'p', { title: 'foobaz' } );
  39. const match = matcher.match( el1 );
  40. expect( match ).to.be.an( 'object' );
  41. expect( match ).to.have.property( 'element' );
  42. expect( match.element ).to.equal( el1 );
  43. expect( matcher.match( el2 ) ).to.be.null;
  44. } );
  45. it( 'should match element attributes using RegExp', () => {
  46. const matcher = new Matcher( {
  47. attribute: {
  48. title: /fooba./
  49. }
  50. } );
  51. const el1 = new Element( 'p', { title: 'foobar' } );
  52. const el2 = new Element( 'p', { title: 'foobaz' } );
  53. let match = matcher.match( el1 );
  54. expect( match ).to.be.an( 'object' );
  55. expect( match ).to.have.property( 'element' );
  56. expect( match.element ).to.equal( el1 );
  57. match = matcher.match( el2 );
  58. expect( match ).to.be.an( 'object' );
  59. expect( match ).to.have.property( 'element' );
  60. expect( match.element ).to.equal( el2 );
  61. } );
  62. it( 'should match element class names', () => {
  63. const matcher = new Matcher( { class: 'foobar' } );
  64. const el1 = new Element( 'p', { class: 'foobar' } );
  65. const match = matcher.match( el1 );
  66. expect( match ).to.be.an( 'object' );
  67. expect( match ).to.have.property( 'element' );
  68. expect( match.element ).to.equal( el1 );
  69. } );
  70. it( 'should match element class names using RegExp', () => {
  71. const matcher = new Matcher( { class: /fooba./ } );
  72. const el1 = new Element( 'p', { class: 'foobar' } );
  73. const el2 = new Element( 'p', { class: 'foobaz' } );
  74. let match = matcher.match( el1 );
  75. expect( match ).to.be.an( 'object' );
  76. expect( match ).to.have.property( 'element' );
  77. expect( match.element ).to.equal( el1 );
  78. match = matcher.match( el2 );
  79. expect( match ).to.be.an( 'object' );
  80. expect( match ).to.have.property( 'element' );
  81. expect( match.element ).to.equal( el2 );
  82. } );
  83. it( 'should match element styles', () => {
  84. const matcher = new Matcher( {
  85. style: {
  86. color: 'red'
  87. }
  88. } );
  89. const el1 = new Element( 'p', { style: 'color: red' } );
  90. const match = matcher.match( el1 );
  91. expect( match ).to.be.an( 'object' );
  92. expect( match ).to.have.property( 'element' );
  93. expect( match.element ).to.equal( el1 );
  94. } );
  95. it( 'should match element styles using RegExp', () => {
  96. const matcher = new Matcher( {
  97. style: {
  98. color: /^.*blue$/
  99. }
  100. } );
  101. const el1 = new Element( 'p', { style: 'color: blue' } );
  102. const el2 = new Element( 'p', { style: 'color: darkblue' } );
  103. let match = matcher.match( el1 );
  104. expect( match ).to.be.an( 'object' );
  105. expect( match ).to.have.property( 'element' );
  106. expect( match.element ).to.equal( el1 );
  107. match = matcher.match( el2 );
  108. expect( match ).to.be.an( 'object' );
  109. expect( match ).to.have.property( 'element' );
  110. expect( match.element ).to.equal( el2 );
  111. } );
  112. } );
  113. } );