aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/dom/pattern/matcher_test.html
blob: 67ef94cb5b5f42d8fed0dda5a4f7d5d241c8db11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>


<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>goog.dom.pattern.Matcher Tests</title>
<script src="../../base.js"></script>
<script type="text/javascript">
  goog.require('goog.testing.jsunit');

  goog.require('goog.dom.pattern.EndTag');
  goog.require('goog.dom.pattern.FullTag');
  goog.require('goog.dom.pattern.Matcher');
  goog.require('goog.dom.pattern.Repeat');
  goog.require('goog.dom.pattern.Sequence');
  goog.require('goog.dom.pattern.StartTag');
  goog.require('goog.dom.pattern.callback.Counter');
  goog.require('goog.dom.pattern.callback.Test');
</script>
</head>
<body>
  <p id="p1">
    <span id="span1" style="color: red"></span>
  </p>
  <p id="p2">
    <span id="span2" style="color: blue">x</span>
  </p>
  <p id="p3">Text</p>
  <p id="p4">Other Text</p>
  <span></span>

  <div id="div1"><b>x</b><b>y</b><i>z</i></div>

  <p id="p5"><b id="b1">x</b><b id="b2">y</b><i id="i1">z</i></p>

<script type="text/javascript">
  function testMatcherAndStartTag() {
    var pattern = new goog.dom.pattern.StartTag('P');

    var counter = new goog.dom.pattern.callback.Counter();
    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, counter.getCallback());
    matcher.match(document.body);

    assertEquals('StartTag(p) should match 5 times in body', 5,
        counter.count);
  }

  function testMatcherAndStartTagTwice() {
    var pattern = new goog.dom.pattern.StartTag('P');

    var counter = new goog.dom.pattern.callback.Counter();
    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, counter.getCallback());
    matcher.match(document.body);

    assertEquals('StartTag(p) should match 5 times in body', 5,
        counter.count);

    // Make sure no state got mangled.
    counter.reset();
    matcher.match(document.body);

    assertEquals('StartTag(p) should match 5 times in body again', 5,
        counter.count);
  }

  function testMatcherAndStartTagAttributes() {
    var pattern = new goog.dom.pattern.StartTag('SPAN', {id: /./});

    var counter = new goog.dom.pattern.callback.Counter();
    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, counter.getCallback());
    matcher.match(document.body);

    assertEquals('StartTag(span,id) should match 2 times in body', 2,
        counter.count);
  }

  function testMatcherWithTwoPatterns() {
    var pattern1 = new goog.dom.pattern.StartTag('SPAN');
    var pattern2 = new goog.dom.pattern.StartTag('P');

    var counter = new goog.dom.pattern.callback.Counter();

    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern1, counter.getCallback());
    matcher.addPattern(pattern2, counter.getCallback());

    matcher.match(document.body);

    assertEquals('StartTag(span|p) should match 8 times in body', 8,
        counter.count);
  }

  function testMatcherWithQuit() {
    var pattern1 = new goog.dom.pattern.StartTag('SPAN');
    var pattern2 = new goog.dom.pattern.StartTag('P');

    var count = 0;
    var callback = function(node, position) {
      if (node.nodeName == 'SPAN') {
        throw goog.iter.StopIteration;
        return true;
      }
      count++;
    };

    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern1, callback);
    matcher.addPattern(pattern2, callback);

    matcher.match(document.body);

    assertEquals('Stopped span|p should match 1 time in body', 1, count);
  }

  function testMatcherWithReplace() {
    var pattern1 = new goog.dom.pattern.StartTag('B');
    var pattern2 = new goog.dom.pattern.StartTag('I');

    var count = 0;
    var callback = function(node, position) {
      count++;
      if (node.nodeName == 'B') {
        var i = goog.dom.createDom('I');
        node.parentNode.insertBefore(i, node);
        goog.dom.removeNode(node);

        position.setPosition(i);

        return true;
      }
    };

    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern1, callback);
    matcher.addPattern(pattern2, callback);

    matcher.match(goog.dom.getElement('div1'));

    assertEquals('i|b->i should match 5 times in div1', 5, count);
  }

  function testMatcherAndFullTag() {
    var pattern = new goog.dom.pattern.FullTag('P');

    var test = new goog.dom.pattern.callback.Test();

    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, test.getCallback());

    matcher.match(goog.dom.getElement('p1'));

    assert('FullTag(p) should match on p1', test.matched);

    test.reset();
    matcher.match(goog.dom.getElement('div1'));

    assert('FullTag(p) should not match on div1', !test.matched);
  }

  function testMatcherAndSequence() {
    var pattern = new goog.dom.pattern.Sequence([
      new goog.dom.pattern.StartTag('P'),
      new goog.dom.pattern.StartTag('SPAN'),
      new goog.dom.pattern.EndTag('SPAN'),
      new goog.dom.pattern.EndTag('P')
    ], true);

    var counter = new goog.dom.pattern.callback.Counter();
    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, counter.getCallback());
    matcher.match(document.body);

    assertEquals('Sequence should match 1 times in body', 1, counter.count);
  }

  function testMatcherAndRepeatFullTag() {
    var pattern = new goog.dom.pattern.Repeat(
        new goog.dom.pattern.FullTag('P'), 1);

    var count = 0;
    var tcount = 0;
    var matcher = new goog.dom.pattern.Matcher();
    matcher.addPattern(pattern, function() {
      count++;
      tcount += pattern.count;
    });
    matcher.match(document.body);

    assertEquals('Repeated p should match 2 times in body', 2, count);
    assertEquals('Repeated p should match 5 total times in body', 5, tcount);
  }
</script>
</body>
</html>