| 1 |
// Copyright 2008 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 |
|
| 15 |
goog.provide('goog.format.HtmlPrettyPrinterTest'); |
| 16 |
goog.setTestOnly('goog.format.HtmlPrettyPrinterTest'); |
| 17 |
|
| 18 |
goog.require('goog.format.HtmlPrettyPrinter'); |
| 19 |
goog.require('goog.testing.MockClock'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var COMPLEX_HTML = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' + |
| 23 |
'<!-- internal declarations -->]>' + |
| 24 |
'<html><head><title>My HTML</title><!-- my comment --></head>' + |
| 25 |
'<script> if(i<0)\nfoo; </script>' + |
| 26 |
'<body><h1>My Header</h1>My text.<br><b>My bold text.</b><hr>' + |
| 27 |
'<pre>My\npreformatted <br> HTML.</pre>5 < 10</body>' + |
| 28 |
'</html>'; |
| 29 |
var mockClock; |
| 30 |
var mockClockTicks; |
| 31 |
|
| 32 |
function setUp() { |
| 33 |
mockClockTicks = 0; |
| 34 |
mockClock = new goog.testing.MockClock(); |
| 35 |
mockClock.getCurrentTime = function() { return mockClockTicks++; }; |
| 36 |
mockClock.install(); |
| 37 |
} |
| 38 |
|
| 39 |
function tearDown() { |
| 40 |
if (mockClock) { |
| 41 |
mockClock.uninstall(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
function testSimpleHtml() { |
| 46 |
var actual = goog.format.HtmlPrettyPrinter.format('<br><b>bold</b>'); |
| 47 |
assertEquals('<br>\n<b>bold</b>\n', actual); |
| 48 |
assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual)); |
| 49 |
} |
| 50 |
|
| 51 |
function testSimpleHtmlMixedCase() { |
| 52 |
var actual = goog.format.HtmlPrettyPrinter.format('<BR><b>bold</b>'); |
| 53 |
assertEquals('<BR>\n<b>bold</b>\n', actual); |
| 54 |
assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual)); |
| 55 |
} |
| 56 |
|
| 57 |
function testComplexHtml() { |
| 58 |
var actual = goog.format.HtmlPrettyPrinter.format(COMPLEX_HTML); |
| 59 |
var expected = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' + |
| 60 |
'<!-- internal declarations -->]>\n' + |
| 61 |
'<html>\n' + |
| 62 |
'<head>\n' + |
| 63 |
'<title>My HTML</title>\n' + |
| 64 |
'<!-- my comment -->' + |
| 65 |
'</head>\n' + |
| 66 |
'<script> if(i<0)\nfoo; </script>\n' + |
| 67 |
'<body>\n' + |
| 68 |
'<h1>My Header</h1>\n' + |
| 69 |
'My text.<br>\n' + |
| 70 |
'<b>My bold text.</b>\n' + |
| 71 |
'<hr>\n' + |
| 72 |
'<pre>My\npreformatted <br> HTML.</pre>\n' + |
| 73 |
'5 < 10' + |
| 74 |
'</body>\n' + |
| 75 |
'</html>\n'; |
| 76 |
assertEquals(expected, actual); |
| 77 |
assertEquals(actual, goog.format.HtmlPrettyPrinter.format(actual)); |
| 78 |
} |
| 79 |
|
| 80 |
function testTimeout() { |
| 81 |
var pp = new goog.format.HtmlPrettyPrinter(3); |
| 82 |
var actual = pp.format(COMPLEX_HTML); |
| 83 |
var expected = '<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri" [' + |
| 84 |
'<!-- internal declarations -->]>\n' + |
| 85 |
'<html>\n' + |
| 86 |
'<head><title>My HTML</title><!-- my comment --></head>' + |
| 87 |
'<script> if(i<0)\nfoo; </script>' + |
| 88 |
'<body><h1>My Header</h1>My text.<br><b>My bold text.</b><hr>' + |
| 89 |
'<pre>My\npreformatted <br> HTML.</pre>5 < 10</body>' + |
| 90 |
'</html>\n'; |
| 91 |
assertEquals(expected, actual); |
| 92 |
} |
| 93 |
|
| 94 |
function testKeepLeadingIndent() { |
| 95 |
var original = ' <b>Bold</b> <i>Ital</i> '; |
| 96 |
var expected = ' <b>Bold</b> <i>Ital</i>\n'; |
| 97 |
assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original)); |
| 98 |
} |
| 99 |
|
| 100 |
function testTrimLeadingLineBreaks() { |
| 101 |
var original = '\n \t\r\n \n <b>Bold</b> <i>Ital</i> '; |
| 102 |
var expected = ' <b>Bold</b> <i>Ital</i>\n'; |
| 103 |
assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original)); |
| 104 |
} |
| 105 |
|
| 106 |
function testExtraLines() { |
| 107 |
var original = '<br>\ntombrat'; |
| 108 |
assertEquals(original + '\n', goog.format.HtmlPrettyPrinter.format(original)); |
| 109 |
} |
| 110 |
|
| 111 |
function testCrlf() { |
| 112 |
var original = '<br>\r\none\r\ntwo<br>'; |
| 113 |
assertEquals(original + '\n', goog.format.HtmlPrettyPrinter.format(original)); |
| 114 |
} |
| 115 |
|
| 116 |
function testEndInLineBreak() { |
| 117 |
assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo')); |
| 118 |
assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo\n')); |
| 119 |
assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo\n\n')); |
| 120 |
assertEquals('foo<br>\n', goog.format.HtmlPrettyPrinter.format('foo<br>')); |
| 121 |
assertEquals('foo<br>\n', goog.format.HtmlPrettyPrinter.format('foo<br>\n')); |
| 122 |
} |
| 123 |
|
| 124 |
function testTable() { |
| 125 |
var original = '<table>' + |
| 126 |
'<tr><td>one.one</td><td>one.two</td></tr>' + |
| 127 |
'<tr><td>two.one</td><td>two.two</td></tr>' + |
| 128 |
'</table>'; |
| 129 |
var expected = '<table>\n' + |
| 130 |
'<tr>\n<td>one.one</td>\n<td>one.two</td>\n</tr>\n' + |
| 131 |
'<tr>\n<td>two.one</td>\n<td>two.two</td>\n</tr>\n' + |
| 132 |
'</table>\n'; |
| 133 |
assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* We have a sanity check in HtmlPrettyPrinter to make sure the regex index |
| 139 |
* advances after every match. We should never hit this, but we include it on |
| 140 |
* the chance there is some corner case where the pattern would match but not |
| 141 |
* process a new token. It's not generally a good idea to break the |
| 142 |
* implementation to test behavior, but this is the easiest way to mimic a |
| 143 |
* bad internal state. |
| 144 |
*/ |
| 145 |
function testRegexMakesProgress() { |
| 146 |
var original = goog.format.HtmlPrettyPrinter.TOKEN_REGEX_; |
| 147 |
|
| 148 |
try { |
| 149 |
// This regex matches \B, an index between 2 word characters, so the regex |
| 150 |
// index does not advance when matching this. |
| 151 |
goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = |
| 152 |
/(?:\B|<!--.*?-->|<!.*?>|<(\/?)(\w+)[^>]*>|[^<]+|<)/g; |
| 153 |
|
| 154 |
// It would work on this string. |
| 155 |
assertEquals('f o o\n', goog.format.HtmlPrettyPrinter.format('f o o')); |
| 156 |
|
| 157 |
// But not this one. |
| 158 |
var ex = assertThrows( |
| 159 |
'should have failed for invalid regex - endless loop', |
| 160 |
goog.partial(goog.format.HtmlPrettyPrinter.format, COMPLEX_HTML)); |
| 161 |
assertEquals( |
| 162 |
'Regex failed to make progress through source html.', ex.message); |
| 163 |
} finally { |
| 164 |
goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = original; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* FF3.0 doesn't like \n between <code></li></code> and <code></ul></code>. |
| 171 |
* See b/1520665. |
| 172 |
*/ |
| 173 |
function testLists() { |
| 174 |
var original = '<ul><li>one</li><ul><li>two</li></UL><li>three</li></ul>'; |
| 175 |
var expected = |
| 176 |
'<ul><li>one</li>\n<ul><li>two</li></UL>\n<li>three</li></ul>\n'; |
| 177 |
assertEquals(expected, goog.format.HtmlPrettyPrinter.format(original)); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* We have a sanity check in HtmlPrettyPrinter to make sure the regex fully |
| 183 |
* tokenizes the string. We should never hit this, but we include it on the |
| 184 |
* chance there is some corner case where the pattern would miss a section of |
| 185 |
* original string. It's not generally a good idea to break the |
| 186 |
* implementation to test behavior, but this is the easiest way to mimic a |
| 187 |
* bad internal state. |
| 188 |
*/ |
| 189 |
function testAvoidDataLoss() { |
| 190 |
var original = goog.format.HtmlPrettyPrinter.TOKEN_REGEX_; |
| 191 |
|
| 192 |
try { |
| 193 |
// This regex does not match stranded '<' characters, so does not fully |
| 194 |
// tokenize the string. |
| 195 |
goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = |
| 196 |
/(?:<!--.*?-->|<!.*?>|<(\/?)(\w+)[^>]*>|[^<]+)/g; |
| 197 |
|
| 198 |
// It would work on this string. |
| 199 |
assertEquals('foo\n', goog.format.HtmlPrettyPrinter.format('foo')); |
| 200 |
|
| 201 |
// But not this one. |
| 202 |
var ex = assertThrows( |
| 203 |
'should have failed for invalid regex - data loss', |
| 204 |
goog.partial(goog.format.HtmlPrettyPrinter.format, COMPLEX_HTML)); |
| 205 |
assertEquals('Lost data pretty printing html.', ex.message); |
| 206 |
} finally { |
| 207 |
goog.format.HtmlPrettyPrinter.TOKEN_REGEX_ = original; |
| 208 |
} |
| 209 |
} |