aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/format/htmlprettyprinter_test.html
blob: a814397ea076b938857486d271fdb435b1590d5f (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
205
<!DOCTYPE html>
<html>
<!--
Copyright 2008 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>Closure Unit Tests - goog.format.HtmlPrettyPrinter</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.format.HtmlPrettyPrinter');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
var COMPLEX_HTML = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' +
    '<!-- internal declarations -->]>' +
    '<html><head><title>My HTML</title><!-- my comment --></head>' +
    '<body><h1>My Header</h1>My text.<br><b>My bold text.</b><hr>' +
    '<pre>My\npreformatted <br> HTML.</pre>5 < 10</body>' +
    '</html>';
var mockClock;
var mockClockTicks;

function setUp() {
  mockClockTicks = 0;
  mockClock = new goog.testing.MockClock();
  mockClock.getCurrentTime = function() {
    return mockClockTicks++;
  };
  mockClock.install();
}

function tearDown() {
  if (mockClock) {
    mockClock.uninstall();
  }
}

function testSimpleHtml() {
  var actual = goog.format.HtmlPrettyPrinter.format('<br><b>bold</b>');
  assertEquals('<br>\n<b>bold</b>\n', actual);
  assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual));
};

function testSimpleHtmlMixedCase() {
  var actual = goog.format.HtmlPrettyPrinter.format('<BR><b>bold</b>');
  assertEquals('<BR>\n<b>bold</b>\n', actual);
  assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual));
}

function testComplexHtml() {
  var actual = goog.format.HtmlPrettyPrinter.format(COMPLEX_HTML);
  var expected = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' +
    '<!-- internal declarations -->]>\n' +
    '<html>\n' +
    '<head>\n' +
    '<title>My HTML</title>\n' +
    '<!-- my comment -->' +
    '</head>\n' +
    '<body>\n' +
    '<h1>My Header</h1>\n' +
    'My text.<br>\n' +
    '<b>My bold text.</b>\n' +
    '<hr>\n' +
    '<pre>My\npreformatted <br> HTML.</pre>\n' +
    '5 < 10' +
    '</body>\n' +
    '</html>\n';
  assertEquals(expected, actual);
  assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual));
}

function testTimeout() {
  var pp = new goog.format.HtmlPrettyPrinter(3);
  var actual = pp.format(COMPLEX_HTML);
  var expected = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' +
    '<!-- internal declarations -->]>\n' +
    '<html>\n' +
    '<head><title>My HTML</title><!-- my comment --></head>' +
    '<body><h1>My Header</h1>My text.<br><b>My bold text.</b><hr>' +
    '<pre>My\npreformatted <br> HTML.</pre>5 < 10</body>' +
    '</html>\n';
  assertEquals(expected, actual);
}

function testKeepLeadingIndent() {
  var original = ' <b>Bold</b> <i>Ital</i> ';
  var expected = ' <b>Bold</b> <i>Ital</i>\n';
  assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original));
}

function testTrimLeadingLineBreaks() {
  var original = '\n \t\r\n  \n <b>Bold</b> <i>Ital</i> ';
  var expected = ' <b>Bold</b> <i>Ital</i>\n';
  assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original));
}

function testExtraLines() {
  var original = '<br>\ntombrat';
  assertEquals(original + '\n', goog.format.HtmlPrettyPrinter.format(original));
}

function testCrlf() {
  var original = '<br>\r\none\r\ntwo<br>';
  assertEquals(original + '\n', goog.format.HtmlPrettyPrinter.format(original));
}

function testEndInLineBreak() {
  assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo'));
  assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo\n'));
  assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo\n\n'));
  assertEquals('foo<br>\n', goog.format.HtmlPrettyPrinter.format('foo<br>'));
  assertEquals('foo<br>\n', goog.format.HtmlPrettyPrinter.format('foo<br>\n'));
}

function testTable() {
  var original = '<table>' +
    '<tr><td>one.one</td><td>one.two</td></tr>' +
    '<tr><td>two.one</td><td>two.two</td></tr>' +
    '</table>';
  var expected = '<table>\n' +
    '<tr>\n<td>one.one</td>\n<td>one.two</td>\n</tr>\n' +
    '<tr>\n<td>two.one</td>\n<td>two.two</td>\n</tr>\n' +
    '</table>\n';
  assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original));
}

/**
 * We have a sanity check in HtmlPrettyPrinter to make sure the regex index
 * advances after every match. We should never hit this, but we include it on
 * the chance there is some corner case where the pattern would match but not
 * process a new token. It's not generally a good idea to break the
 * implementation to test behavior, but this is the easiest way to mimic a
 * bad internal state.
 */
function testRegexMakesProgress() {
  var original = goog.format.HtmlPrettyPrinter.TOKEN_REGEX_;

  try {
    // This regex matches \B, an index between 2 word characters, so the regex
    // index does not advance when matching this.
    goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ =
        /(?:\B|<!--.*?-->|<!.*?>|<(\/?)(\w+)[^>]*>|[^<]+|<)/g;

    // It would work on this string.
    assertEquals('f o o\n', goog.format.HtmlPrettyPrinter.format('f o o'));

    // But not this one.
    var ex = assertThrows('should have failed for invalid regex - endless loop',
        goog.partial(goog.format.HtmlPrettyPrinter.format, COMPLEX_HTML));
    assertEquals('Regex failed to make progress through source html.',
        ex.message);
  } finally {
    goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = original;
  }
}

/**
 * FF3.0 doesn't like \n between </li> and </ul>. See bug 1520665.
 */
function testLists() {
  var original = "<ul><li>one</li><ul><li>two</li></UL><li>three</li></ul>";
  var expected =
    "<ul><li>one</li>\n<ul><li>two</li></UL>\n<li>three</li></ul>\n";
  assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original));
}

/**
 * We have a sanity check in HtmlPrettyPrinter to make sure the regex fully
 * tokenizes the string. We should never hit this, but we include it on the
 * chance there is some corner case where the pattern would miss a section of
 * original string. It's not generally a good idea to break the
 * implementation to test behavior, but this is the easiest way to mimic a
 * bad internal state.
 */
function testAvoidDataLoss() {
  var original = goog.format.HtmlPrettyPrinter.TOKEN_REGEX_;

  try {
    // This regex does not match stranded '<' characters, so does not fully
    // tokenize the string.
    goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ =
        /(?:<!--.*?-->|<!.*?>|<(\/?)(\w+)[^>]*>|[^<]+)/g;

    // It would work on this string.
    assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo'));

    // But not this one.
    var ex = assertThrows('should have failed for invalid regex - data loss',
        goog.partial(goog.format.HtmlPrettyPrinter.format, COMPLEX_HTML));
    assertEquals('Lost data pretty printing html.', ex.message);
  } finally {
    goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = original;
  }
}

</script>
</body>
</html>