| 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 |
* Utilities intended for refactoring legacy code; allows classifying strings |
| 17 |
* into plain text that does not contain HTML and HTML. Please do NOT use in new |
| 18 |
* code. |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.textAssert'); |
| 22 |
|
| 23 |
goog.require('goog.asserts'); |
| 24 |
goog.require('goog.dom'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
|
| 27 |
/** |
| 28 |
* Assert that the string is plain text that does not have HTML, i.e. not |
| 29 |
* affected by HTML escaping. Otherwise, this raises an error if assertions are |
| 30 |
* enabled. It does NOT sanitize nor make any change to the input string. It |
| 31 |
* should only be used when the assertion failure is benign, such as printing |
| 32 |
* spurious tags. DO NOT count on this to remove unsafe HTML. It is only meant |
| 33 |
* for legacy refactoring. Please do NOT use in new code. |
| 34 |
* @param {string} text |
| 35 |
* @return {string} |
| 36 |
*/ |
| 37 |
goog.dom.textAssert.assertHtmlFree = function(text) { |
| 38 |
if (goog.asserts.ENABLE_ASSERTS) { |
| 39 |
var elmt = goog.dom.createElement(goog.dom.TagName.BODY); |
| 40 |
elmt.textContent = text; |
| 41 |
goog.asserts.assert( |
| 42 |
elmt.innerHTML == elmt.textContent, |
| 43 |
'String has HTML original: %s, escaped: %s', text, elmt.innerHTML); |
| 44 |
} |
| 45 |
return text; |
| 46 |
}; |