matcher_test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.pattern.matcherTest');
  15. goog.setTestOnly('goog.dom.pattern.matcherTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.pattern.EndTag');
  19. goog.require('goog.dom.pattern.FullTag');
  20. goog.require('goog.dom.pattern.Matcher');
  21. goog.require('goog.dom.pattern.Repeat');
  22. goog.require('goog.dom.pattern.Sequence');
  23. goog.require('goog.dom.pattern.StartTag');
  24. goog.require('goog.dom.pattern.callback.Counter');
  25. goog.require('goog.dom.pattern.callback.Test');
  26. goog.require('goog.iter.StopIteration');
  27. goog.require('goog.testing.jsunit');
  28. function testMatcherAndStartTag() {
  29. var pattern = new goog.dom.pattern.StartTag('P');
  30. var counter = new goog.dom.pattern.callback.Counter();
  31. var matcher = new goog.dom.pattern.Matcher();
  32. matcher.addPattern(pattern, counter.getCallback());
  33. matcher.match(document.body);
  34. assertEquals('StartTag(p) should match 5 times in body', 5, counter.count);
  35. }
  36. function testMatcherAndStartTagTwice() {
  37. var pattern = new goog.dom.pattern.StartTag('P');
  38. var counter = new goog.dom.pattern.callback.Counter();
  39. var matcher = new goog.dom.pattern.Matcher();
  40. matcher.addPattern(pattern, counter.getCallback());
  41. matcher.match(document.body);
  42. assertEquals('StartTag(p) should match 5 times in body', 5, counter.count);
  43. // Make sure no state got mangled.
  44. counter.reset();
  45. matcher.match(document.body);
  46. assertEquals(
  47. 'StartTag(p) should match 5 times in body again', 5, counter.count);
  48. }
  49. function testMatcherAndStartTagAttributes() {
  50. var pattern = new goog.dom.pattern.StartTag('SPAN', {id: /./});
  51. var counter = new goog.dom.pattern.callback.Counter();
  52. var matcher = new goog.dom.pattern.Matcher();
  53. matcher.addPattern(pattern, counter.getCallback());
  54. matcher.match(document.body);
  55. assertEquals(
  56. 'StartTag(span,id) should match 2 times in body', 2, counter.count);
  57. }
  58. function testMatcherWithTwoPatterns() {
  59. var pattern1 = new goog.dom.pattern.StartTag('SPAN');
  60. var pattern2 = new goog.dom.pattern.StartTag('P');
  61. var counter = new goog.dom.pattern.callback.Counter();
  62. var matcher = new goog.dom.pattern.Matcher();
  63. matcher.addPattern(pattern1, counter.getCallback());
  64. matcher.addPattern(pattern2, counter.getCallback());
  65. matcher.match(document.body);
  66. assertEquals(
  67. 'StartTag(span|p) should match 8 times in body', 8, counter.count);
  68. }
  69. function testMatcherWithQuit() {
  70. var pattern1 = new goog.dom.pattern.StartTag('SPAN');
  71. var pattern2 = new goog.dom.pattern.StartTag('P');
  72. var count = 0;
  73. var callback = function(node, position) {
  74. if (node.nodeName == goog.dom.TagName.SPAN) {
  75. throw goog.iter.StopIteration;
  76. return true;
  77. }
  78. count++;
  79. };
  80. var matcher = new goog.dom.pattern.Matcher();
  81. matcher.addPattern(pattern1, callback);
  82. matcher.addPattern(pattern2, callback);
  83. matcher.match(document.body);
  84. assertEquals('Stopped span|p should match 1 time in body', 1, count);
  85. }
  86. function testMatcherWithReplace() {
  87. var pattern1 = new goog.dom.pattern.StartTag('B');
  88. var pattern2 = new goog.dom.pattern.StartTag('I');
  89. var count = 0;
  90. var callback = function(node, position) {
  91. count++;
  92. if (node.nodeName == goog.dom.TagName.B) {
  93. var i = goog.dom.createDom(goog.dom.TagName.I);
  94. node.parentNode.insertBefore(i, node);
  95. goog.dom.removeNode(node);
  96. position.setPosition(i);
  97. return true;
  98. }
  99. };
  100. var matcher = new goog.dom.pattern.Matcher();
  101. matcher.addPattern(pattern1, callback);
  102. matcher.addPattern(pattern2, callback);
  103. matcher.match(goog.dom.getElement('div1'));
  104. assertEquals('i|b->i should match 5 times in div1', 5, count);
  105. }
  106. function testMatcherAndFullTag() {
  107. var pattern = new goog.dom.pattern.FullTag('P');
  108. var test = new goog.dom.pattern.callback.Test();
  109. var matcher = new goog.dom.pattern.Matcher();
  110. matcher.addPattern(pattern, test.getCallback());
  111. matcher.match(goog.dom.getElement('p1'));
  112. assert('FullTag(p) should match on p1', test.matched);
  113. test.reset();
  114. matcher.match(goog.dom.getElement('div1'));
  115. assert('FullTag(p) should not match on div1', !test.matched);
  116. }
  117. function testMatcherAndSequence() {
  118. var pattern = new goog.dom.pattern.Sequence(
  119. [
  120. new goog.dom.pattern.StartTag('P'),
  121. new goog.dom.pattern.StartTag('SPAN'),
  122. new goog.dom.pattern.EndTag('SPAN'), new goog.dom.pattern.EndTag('P')
  123. ],
  124. true);
  125. var counter = new goog.dom.pattern.callback.Counter();
  126. var matcher = new goog.dom.pattern.Matcher();
  127. matcher.addPattern(pattern, counter.getCallback());
  128. matcher.match(document.body);
  129. assertEquals('Sequence should match 1 times in body', 1, counter.count);
  130. }
  131. function testMatcherAndRepeatFullTag() {
  132. var pattern =
  133. new goog.dom.pattern.Repeat(new goog.dom.pattern.FullTag('P'), 1);
  134. var count = 0;
  135. var tcount = 0;
  136. var matcher = new goog.dom.pattern.Matcher();
  137. matcher.addPattern(pattern, function() {
  138. count++;
  139. tcount += pattern.count;
  140. });
  141. matcher.match(document.body);
  142. assertEquals('Repeated p should match 2 times in body', 2, count);
  143. assertEquals('Repeated p should match 5 total times in body', 5, tcount);
  144. }