| 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 |
/** @fileoverview Tests for {@link goog.html.sanitizer.noclobber} */ |
| 16 |
|
| 17 |
goog.module('goog.html.sanitizer.noclobberTest'); |
| 18 |
goog.setTestOnly(); |
| 19 |
|
| 20 |
var NodeType = goog.require('goog.dom.NodeType'); |
| 21 |
var noclobber = goog.require('goog.html.sanitizer.noclobber'); |
| 22 |
var testSuite = goog.require('goog.testing.testSuite'); |
| 23 |
var userAgentProduct = goog.require('goog.userAgent.product'); |
| 24 |
|
| 25 |
|
| 26 |
const antiClobberingSupported = |
| 27 |
!userAgentProduct.IE || document.documentMode >= 10; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param {string} html |
| 31 |
* @return {!Element} |
| 32 |
*/ |
| 33 |
function htmlToElement(html) { |
| 34 |
var div = document.createElement('div'); |
| 35 |
div.innerHTML = html; |
| 36 |
return div.children[0]; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param {string} name |
| 41 |
* @return {!Element} |
| 42 |
*/ |
| 43 |
function getClobberedElement(name) { |
| 44 |
return htmlToElement('<form id="foo"><input name="' + name + '"></form>'); |
| 45 |
} |
| 46 |
|
| 47 |
testSuite({ |
| 48 |
testElement() { |
| 49 |
if (!antiClobberingSupported) { |
| 50 |
return; |
| 51 |
} |
| 52 |
var element = getClobberedElement('attributes'); |
| 53 |
var attributes = noclobber.getElementAttributes(element); |
| 54 |
assertNotEquals(element.attributes, attributes); |
| 55 |
assertTrue(attributes instanceof NamedNodeMap); |
| 56 |
|
| 57 |
element = getClobberedElement('hasAttribute'); |
| 58 |
assertTrue(noclobber.hasElementAttribute(element, 'id')); |
| 59 |
assertFalse(noclobber.hasElementAttribute(element, 'bar')); |
| 60 |
|
| 61 |
element = getClobberedElement('getAttribute'); |
| 62 |
assertEquals('foo', noclobber.getElementAttribute(element, 'id')); |
| 63 |
|
| 64 |
element = getClobberedElement('setAttribute'); |
| 65 |
noclobber.setElementAttribute(element, 'id', 'bar'); |
| 66 |
assertEquals('bar', noclobber.getElementAttribute(element, 'id')); |
| 67 |
|
| 68 |
element = getClobberedElement('removeAttribute'); |
| 69 |
assertTrue(element.hasAttribute('id')); |
| 70 |
noclobber.removeElementAttribute(element, 'id'); |
| 71 |
assertFalse(element.hasAttribute('id')); |
| 72 |
|
| 73 |
element = getClobberedElement('innerHTML'); |
| 74 |
var innerHTML = noclobber.getElementInnerHTML(element); |
| 75 |
assertEquals('<input name="innerHTML">', innerHTML); |
| 76 |
|
| 77 |
element = getClobberedElement('style'); |
| 78 |
var style = noclobber.getElementStyle(element); |
| 79 |
assertTrue(style instanceof CSSStyleDeclaration); |
| 80 |
|
| 81 |
element = getClobberedElement('getElementsByTagName'); |
| 82 |
assertArrayEquals( |
| 83 |
Array.from(element.children), |
| 84 |
noclobber.getElementsByTagName(element, 'input')); |
| 85 |
|
| 86 |
element = htmlToElement( |
| 87 |
'<form><input name="sheet"><style>color:red</style></form>'); |
| 88 |
document.body.appendChild(element); // needs to be rooted into the DOM. |
| 89 |
assertEquals( |
| 90 |
element.children[1].sheet, |
| 91 |
noclobber.getElementStyleSheet(element.children[1])); |
| 92 |
|
| 93 |
element = getClobberedElement('matches'); |
| 94 |
assertTrue(noclobber.elementMatches(element, '#foo')); |
| 95 |
assertFalse(noclobber.elementMatches(element, '#bar')); |
| 96 |
}, |
| 97 |
|
| 98 |
testNode() { |
| 99 |
if (!antiClobberingSupported) { |
| 100 |
return; |
| 101 |
} |
| 102 |
var element = getClobberedElement('nodeName'); |
| 103 |
assertEquals('FORM', noclobber.getNodeName(element)); |
| 104 |
|
| 105 |
element = getClobberedElement('nodeType'); |
| 106 |
noclobber.assertNodeIsElement(element); |
| 107 |
assertEquals(NodeType.ELEMENT, noclobber.getNodeType(element)); |
| 108 |
|
| 109 |
element = getClobberedElement('parentNode'); |
| 110 |
assertEquals('DIV', noclobber.getParentNode(element).nodeName); |
| 111 |
|
| 112 |
element = getClobberedElement('childNodes'); |
| 113 |
assertTrue(noclobber.getChildNodes(element) instanceof NodeList); |
| 114 |
}, |
| 115 |
|
| 116 |
testCSSStyleDeclaration() { |
| 117 |
if (!antiClobberingSupported) { |
| 118 |
return; |
| 119 |
} |
| 120 |
var element = getClobberedElement('getPropertyValue'); |
| 121 |
element.style.setProperty('color', 'red'); |
| 122 |
assertEquals('red', noclobber.getCssPropertyValue(element.style, 'color')); |
| 123 |
assertEquals('', noclobber.getCssPropertyValue(element.style, 'foo')); |
| 124 |
|
| 125 |
element = getClobberedElement('setProperty'); |
| 126 |
noclobber.setCssProperty(element.style, 'color', 'red'); |
| 127 |
assertEquals('red', element.style.color); |
| 128 |
}, |
| 129 |
|
| 130 |
testCSSStyleDeclaration_notClobbered() { |
| 131 |
// On IE8 and IE9 the CSS functions should still work when the property is |
| 132 |
// not clobbered (assuming optionalAntiClobbering is true). |
| 133 |
var element = getClobberedElement('notClobbered'); |
| 134 |
if (element.style.setProperty) { |
| 135 |
element.style.setProperty('color', 'red'); |
| 136 |
} else { |
| 137 |
element.style.setAttribute('color', 'red'); |
| 138 |
} |
| 139 |
assertEquals( |
| 140 |
'red', |
| 141 |
noclobber.getCssPropertyValue( |
| 142 |
element.style, 'color', true /* opt_optionalAntiClobbering */)); |
| 143 |
|
| 144 |
element = getClobberedElement('notClobbered'); |
| 145 |
noclobber.setCssProperty( |
| 146 |
element.style, 'color', 'red', true /* opt_optionalAntiClobbering */); |
| 147 |
assertEquals('red', element.style.color); |
| 148 |
} |
| 149 |
}); |