Debian Codegraph @

closure-library/closure/goog/html/textextractor_test.js

raw
1 // Copyright 2017 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 /**
16 * @fileoverview Tests for the textExtractor module.
17 */
18
19 goog.module('goog.html.textExtractorTest');
20 goog.setTestOnly();
21
22 var testSuite = goog.require('goog.testing.testSuite');
23 var textExtractor = goog.require('goog.html.textExtractor');
24
25
26 /**
27 * Verifies that the expected text is extracted from the HTML string.
28 * @param {string} html The HTML string containing text mixed in HTML markup.
29 * @param {string} expectedText The expected text extracted from the HTML
30 * string.
31 */
32 function assertExtractedTextEquals(html, expectedText) {
33 var actualText = textExtractor.extractTextContent(html);
34 if (textExtractor.isSupported()) {
35 assertEquals(actualText, expectedText);
36 } else {
37 assertEquals(actualText, '');
38 }
39 }
40
41 testSuite({
42 testExtractTextContent_justText: function() {
43 var html = 'Hello';
44 assertExtractedTextEquals(html, html);
45 },
46
47 testExtractTextContent_basic: function() {
48 var html = '<p>Hello</p>';
49 var expectedText = 'Hello';
50 assertExtractedTextEquals(html, expectedText);
51 },
52
53 testExtractTextContent_removesScript: function() {
54 var html = '<p>Foo<script>Bar</script>Baz</p>';
55 var expectedText = 'FooBaz';
56 assertExtractedTextEquals(html, expectedText);
57 },
58
59 testExtractTextContent_blocks: function() {
60 var html = '<div>Foo</div><div>Bar</div>';
61 var expectedText = 'Foo\n\nBar';
62 assertExtractedTextEquals(html, expectedText);
63 },
64
65 testExtractTextContent_extraNewlines: function() {
66 var html = '<p>Foo</p>\n<p>Bar</p>';
67 var expectedText = 'Foo\n\nBar';
68 assertExtractedTextEquals(html, expectedText);
69 },
70
71 testExtractTextContent_inline: function() {
72 var html = '<h1>Foo<span>Bar</span></h1>';
73 var expectedText = 'FooBar';
74 assertExtractedTextEquals(html, expectedText);
75 },
76
77 testExtractTextContent_complex: function() {
78 var html = '<div>\n' +
79 ' \n' +
80 ' A\n' +
81 '\n' +
82 ' mind\n' +
83 '\n' +
84 ' needs books<br>as a sword needs a whetstone<p>' + // no line break
85 'if it is to <span style="display: block">keep</span> its edge.\n' +
86 ' </p>\n' +
87 '\n' +
88 '</div>';
89 var expectedText = 'A mind needs books\n' +
90 'as a sword needs a whetstone\n' +
91 'if it is to\n' +
92 'keep\n' +
93 'its edge.';
94 assertExtractedTextEquals(html, expectedText);
95 },
96
97 testExtractTextContent_newlines: function() {
98 var html = 'Hello\nWorld';
99 var expectedText = 'Hello World';
100 assertExtractedTextEquals(html, expectedText);
101 },
102
103 testExtractTextContent_br: function() {
104 var html = 'Hello\n<br>World';
105 var expectedText = 'Hello\nWorld';
106 assertExtractedTextEquals(html, expectedText);
107 },
108
109 testExtractTextContent_brAndBlock: function() {
110 var html = 'Hello\n\n<br>\n<p>World</p>';
111 var expectedText = 'Hello\n\nWorld';
112 assertExtractedTextEquals(html, expectedText);
113 }
114 });