Debian Codegraph @

closure-library/closure/goog/html/textextractor.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 /**
17 * @fileoverview Contains utility methods to extract text content from HTML.
18 * @supported IE 10+, Chrome 26+, Firefox 22+, Safari 7.1+, Opera 15+
19 */
20
21 goog.provide('goog.html.textExtractor');
22
23 goog.require('goog.array');
24 goog.require('goog.dom.TagName');
25 goog.require('goog.html.sanitizer.HtmlSanitizer');
26 goog.require('goog.object');
27 goog.require('goog.userAgent');
28
29
30 /**
31 * Safely extracts text from an untrusted HTML string using the HtmlSanitizer.
32 * Compared to goog.html.utils.stripHtmlTags, it tries to be smarter about
33 * printing newlines between blocks and leave out textual content that would not
34 * be displayed to the user (such as SCRIPT and STYLE tags).
35 * @param {string} html The untrusted HTML string.
36 * @return {string}
37 */
38 // TODO(pelizzi): consider an optional bool parameter to also extract the text
39 // content of alt attributes and such.
40 goog.html.textExtractor.extractTextContent = function(html) {
41 if (!goog.html.textExtractor.isSupported()) {
42 return '';
43 }
44 // Disable all attributes except style to protect against DOM clobbering.
45 var sanitizer = new goog.html.sanitizer.HtmlSanitizer.Builder()
46 .onlyAllowAttributes(['style'])
47 .allowCssStyles()
48 .build();
49 // The default policy of the sanitizer strips the content of tags such as
50 // SCRIPT and STYLE, whose non-textual content would otherwise end up in the
51 // extracted text.
52 var sanitizedNodes = sanitizer.sanitizeToDomNode(html);
53 // textContent and innerText do not handle spacing between block elements
54 // properly. We need to reimplement a similar algorithm ourselves and account
55 // for spacing between block elements.
56 return goog.html.textExtractor.extractTextContentFromNode_(sanitizedNodes)
57 .trim();
58 };
59
60
61 /**
62 * Recursively extract text from the supplied DOM node and its descendants.
63 * @param {!Node} node
64 * @return {string}
65 * @private
66 */
67 goog.html.textExtractor.extractTextContentFromNode_ = function(node) {
68 switch (node.nodeType) {
69 case Node.ELEMENT_NODE:
70 var element = /** @type {!Element} */ (node);
71 if (element.tagName == goog.dom.TagName.BR) {
72 return '\n';
73 }
74 var result = goog.array
75 .map(
76 node.childNodes,
77 goog.html.textExtractor.extractTextContentFromNode_)
78 .join('');
79 if (goog.html.textExtractor.isBlockElement_(element)) {
80 result = '\n' + result + '\n';
81 }
82 return result;
83 case Node.TEXT_NODE:
84 return node.nodeValue.replace(/\s+/g, ' ').trim();
85 default:
86 return '';
87 }
88 };
89
90
91 /**
92 * A set of block elements.
93 * @private @const {!Object<!goog.dom.TagName, boolean>}
94 */
95 goog.html.textExtractor.BLOCK_ELEMENTS_ = goog.object.createSet(
96 goog.dom.TagName.ADDRESS, goog.dom.TagName.BLOCKQUOTE,
97 goog.dom.TagName.CENTER, goog.dom.TagName.DIV, goog.dom.TagName.DL,
98 goog.dom.TagName.FIELDSET, goog.dom.TagName.FORM, goog.dom.TagName.H1,
99 goog.dom.TagName.H2, goog.dom.TagName.H3, goog.dom.TagName.H4,
100 goog.dom.TagName.H5, goog.dom.TagName.H6, goog.dom.TagName.HR,
101 goog.dom.TagName.OL, goog.dom.TagName.P, goog.dom.TagName.PRE,
102 goog.dom.TagName.TABLE, goog.dom.TagName.UL);
103
104
105 /**
106 * Returns true whether this is a block element, i.e. the browser would visually
107 * separate the text content from the text content of the previous node.
108 * @param {!Element} element
109 * @return {boolean}
110 * @private
111 */
112 goog.html.textExtractor.isBlockElement_ = function(element) {
113 return element.style.display == 'block' ||
114 goog.html.textExtractor.BLOCK_ELEMENTS_.hasOwnProperty(element.tagName);
115 };
116
117
118 /**
119 * Whether the browser supports the text extractor. The extractor depends on the
120 * HTML Sanitizer, which only supports IE starting from version 10.
121 * Visible for testing.
122 * @return {boolean}
123 * @package
124 */
125 goog.html.textExtractor.isSupported = function() {
126 return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10);
127 };