|
Line 0
a/Source/WebKit/UIProcess/Automation/atoms/utils.js_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2022 Igalia S.L. |
| 3 |
* Copyright (C) 2017 Apple Inc. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
var utils = { }; |
| 28 |
|
| 29 |
utils.nodeIsElement = function(node) { |
| 30 |
return !!node && node.nodeType === Node.ELEMENT_NODE; |
| 31 |
} |
| 32 |
|
| 33 |
utils.enclosingNodeOrSelfMatchingPredicate = function(targetNode, predicate) { |
| 34 |
for (let node = targetNode; node && node !== targetNode.getRootNode(); node = node.parentNode) |
| 35 |
if (predicate(node)) |
| 36 |
return node; |
| 37 |
|
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
utils.parentElementForElement = function(element) { |
| 42 |
if (!element) |
| 43 |
return null; |
| 44 |
|
| 45 |
return utils.enclosingNodeOrSelfMatchingPredicate(element.parentNode, utils.nodeIsElement); |
| 46 |
} |
| 47 |
|
| 48 |
utils.cascadedStylePropertyForElement = function(element, property) { |
| 49 |
if (!element || !property) |
| 50 |
return null; |
| 51 |
|
| 52 |
let computedStyle = window.getComputedStyle(element); |
| 53 |
let computedStyleProperty = computedStyle.getPropertyValue(property); |
| 54 |
if (computedStyleProperty && computedStyleProperty !== "inherit") |
| 55 |
return computedStyleProperty; |
| 56 |
|
| 57 |
// Ideally getPropertyValue would return the 'used' or 'actual' value, but |
| 58 |
// it doesn't for legacy reasons. So we need to do our own poor man's cascade. |
| 59 |
// Fall back to the first non-'inherit' value found in an ancestor. |
| 60 |
// In any case, getPropertyValue will not return 'initial'. |
| 61 |
|
| 62 |
// FIXME: will this incorrectly inherit non-inheritable CSS properties? |
| 63 |
// I think all important non-inheritable properties (width, height, etc.) |
| 64 |
// for our purposes here are specially resolved, so this may not be an issue. |
| 65 |
// Specification is here: https://drafts.csswg.org/cssom/#resolved-values |
| 66 |
let parentElement = utils.parentElementForElement(element); |
| 67 |
return utils.cascadedStylePropertyForElement(parentElement, property); |
| 68 |
} |
| 69 |
|
| 70 |
utils.isShown = function(element) { |
| 71 |
function enclosingElementOrSelfMatchingPredicate(targetElement, predicate) { |
| 72 |
for (let element = targetElement; element && element !== targetElement.getRootNode(); element = utils.parentElementForElement(element)) |
| 73 |
if (predicate(element)) |
| 74 |
return element; |
| 75 |
|
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
function elementSubtreeHasNonZeroDimensions(element) { |
| 80 |
let boundingBox = element.getBoundingClientRect(); |
| 81 |
if (boundingBox.width > 0 && boundingBox.height > 0) |
| 82 |
return true; |
| 83 |
|
| 84 |
// Paths can have a zero width or height. Treat them as shown if the stroke width is positive. |
| 85 |
if (element.tagName.toUpperCase() === "PATH" && boundingBox.width + boundingBox.height > 0) { |
| 86 |
let strokeWidth = utils.cascadedStylePropertyForElement(element, "stroke-width"); |
| 87 |
return !!strokeWidth && (parseInt(strokeWidth, 10) > 0); |
| 88 |
} |
| 89 |
|
| 90 |
let cascadedOverflow = utils.cascadedStylePropertyForElement(element, "overflow"); |
| 91 |
if (cascadedOverflow === "hidden") |
| 92 |
return false; |
| 93 |
|
| 94 |
// If the container's overflow is not hidden and it has zero size, consider the |
| 95 |
// container to have non-zero dimensions if a child node has non-zero dimensions. |
| 96 |
return Array.from(element.childNodes).some((childNode) => { |
| 97 |
if (childNode.nodeType === Node.TEXT_NODE) |
| 98 |
return true; |
| 99 |
|
| 100 |
if (utils.nodeIsElement(childNode)) |
| 101 |
return elementSubtreeHasNonZeroDimensions(childNode); |
| 102 |
|
| 103 |
return false; |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
function elementOverflowsContainer(element) { |
| 108 |
let cascadedOverflow = utils.cascadedStylePropertyForElement(element, "overflow"); |
| 109 |
if (cascadedOverflow !== "hidden") |
| 110 |
return false; |
| 111 |
|
| 112 |
// FIXME: this needs to take into account the scroll position of the element, |
| 113 |
// the display modes of it and its ancestors, and the container it overflows. |
| 114 |
// See Selenium's bot.dom.getOverflowState atom for an exhaustive list of edge cases. |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
function isElementSubtreeHiddenByOverflow(element) { |
| 119 |
if (!element) |
| 120 |
return false; |
| 121 |
|
| 122 |
if (!elementOverflowsContainer(element)) |
| 123 |
return false; |
| 124 |
|
| 125 |
if (!element.childNodes.length) |
| 126 |
return false; |
| 127 |
|
| 128 |
// This element's subtree is hidden by overflow if all child subtrees are as well. |
| 129 |
return Array.from(element.childNodes).every((childNode) => { |
| 130 |
// Returns true if the child node is overflowed or otherwise hidden. |
| 131 |
// Base case: not an element, has zero size, scrolled out, or doesn't overflow container. |
| 132 |
if (!utils.nodeIsElement(childNode)) |
| 133 |
return true; |
| 134 |
|
| 135 |
if (!elementSubtreeHasNonZeroDimensions(childNode)) |
| 136 |
return true; |
| 137 |
|
| 138 |
// Recurse. |
| 139 |
return isElementSubtreeHiddenByOverflow(childNode); |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
// This is a partial reimplementation of Selenium's "element is displayed" algorithm. |
| 144 |
// When the W3C specification's algorithm stabilizes, we should implement that. |
| 145 |
|
| 146 |
if (!(element instanceof Element)) |
| 147 |
throw new Error("Cannot check the displayedness of a non-Element argument."); |
| 148 |
|
| 149 |
// If this command is misdirected to the wrong document, treat it as not shown. |
| 150 |
if (!document.contains(element)) |
| 151 |
return false; |
| 152 |
|
| 153 |
// Special cases for specific tag names. |
| 154 |
switch (element.tagName.toUpperCase()) { |
| 155 |
case "BODY": |
| 156 |
return true; |
| 157 |
|
| 158 |
case "SCRIPT": |
| 159 |
case "NOSCRIPT": |
| 160 |
return false; |
| 161 |
|
| 162 |
case "OPTGROUP": |
| 163 |
case "OPTION": |
| 164 |
// Option/optgroup are considered shown if the containing <select> is shown. |
| 165 |
let enclosingSelectElement = utils.enclosingNodeOrSelfMatchingPredicate(element, (e) => e.tagName.toUpperCase() === "SELECT"); |
| 166 |
return utils.isShown(enclosingSelectElement); |
| 167 |
|
| 168 |
case "INPUT": |
| 169 |
// <input type="hidden"> is considered not shown. |
| 170 |
if (element.type === "hidden") |
| 171 |
return false; |
| 172 |
break; |
| 173 |
|
| 174 |
case "MAP": |
| 175 |
// FIXME: Selenium has special handling for <map> elements. We don't do anything now. |
| 176 |
|
| 177 |
default: |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
if (utils.cascadedStylePropertyForElement(element, "visibility") !== "visible") |
| 182 |
return false; |
| 183 |
|
| 184 |
let hasAncestorWithZeroOpacity = !!enclosingElementOrSelfMatchingPredicate(element, (e) => { |
| 185 |
return Number(utils.cascadedStylePropertyForElement(e, "opacity")) === 0; |
| 186 |
}); |
| 187 |
let hasAncestorWithDisplayNone = !!enclosingElementOrSelfMatchingPredicate(element, (e) => { |
| 188 |
return utils.cascadedStylePropertyForElement(e, "display") === "none"; |
| 189 |
}); |
| 190 |
if (hasAncestorWithZeroOpacity || hasAncestorWithDisplayNone) |
| 191 |
return false; |
| 192 |
|
| 193 |
if (!elementSubtreeHasNonZeroDimensions(element)) |
| 194 |
return false; |
| 195 |
|
| 196 |
if (isElementSubtreeHiddenByOverflow(element)) |
| 197 |
return false; |
| 198 |
|
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
utils.getText = function(element) { |
| 203 |
var lines = []; |
| 204 |
|
| 205 |
function appendLinesFromTextNode(textNode, whitespace, textTransform) { |
| 206 |
// Remove zero-width characters. |
| 207 |
let text = textNode.nodeValue.replace(/[\u200b\u200e\u200f]/g, ''); |
| 208 |
|
| 209 |
// Canonicalize the new lines. |
| 210 |
text = text.replace(/(\r\n|\r|\n)/g, '\n'); |
| 211 |
|
| 212 |
let collapseAll = true; |
| 213 |
switch (whitespace) { |
| 214 |
case "normal": |
| 215 |
case "nowrap": |
| 216 |
// Collapse new lines. |
| 217 |
text = text.replace(/\n/g, ' '); |
| 218 |
break; |
| 219 |
case "pre": |
| 220 |
case "pre-wrap": |
| 221 |
// Convert all breaking spaces to be non-breaking. |
| 222 |
text = text.replace(/[ \f\t\v\u2028\u2029]/g, '\xa0'); |
| 223 |
collapseAll = false; |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
if (collapseAll) { |
| 228 |
// Collapse all breaking spaces. |
| 229 |
text = text.replace(/[\ \f\t\v\u2028\u2029]+/g, ' '); |
| 230 |
} |
| 231 |
|
| 232 |
switch (textTransform) { |
| 233 |
case "capitalize": |
| 234 |
text = text.replace(/(^|[^\d\p{L}\p{S}])([\p{Ll}|\p{S}])/gu, function() { |
| 235 |
return arguments[1] + arguments[2].toUpperCase(); |
| 236 |
}); |
| 237 |
break; |
| 238 |
case "uppercase": |
| 239 |
text = text.toUpperCase(); |
| 240 |
break; |
| 241 |
case "lowercase": |
| 242 |
text = text.toLowerCase(); |
| 243 |
break; |
| 244 |
} |
| 245 |
|
| 246 |
let currentLine = lines.pop() || ''; |
| 247 |
if (currentLine.endsWith(' ') && text.startsWith(' ')) |
| 248 |
text = text.substr(1); |
| 249 |
lines.push(currentLine + text); |
| 250 |
} |
| 251 |
|
| 252 |
function appendLines(element) { |
| 253 |
function currentLine() { |
| 254 |
return lines[lines.length - 1] || ''; |
| 255 |
} |
| 256 |
|
| 257 |
let isTableCell = false, display = null, isBlock = false; |
| 258 |
if (element.tagName.toUpperCase() === "BR") |
| 259 |
lines.push(''); |
| 260 |
else { |
| 261 |
isTableCell = element.tagName.toUpperCase() === "TD"; |
| 262 |
display = utils.cascadedStylePropertyForElement(element, "display"); |
| 263 |
isBlock = !isTableCell |
| 264 |
&& !(display === "inline" |
| 265 |
|| display === "inline-block" |
| 266 |
|| display === "inline-table" |
| 267 |
|| display === "none" |
| 268 |
|| display === "table-cell" |
| 269 |
|| display === "table-column" |
| 270 |
|| display === "table-column-group"); |
| 271 |
let previousElementSibling = element.previousElementSibling; |
| 272 |
let previousDisplay = previousElementSibling ? utils.cascadedStylePropertyForElement(previousElementSibling, "display") : ''; |
| 273 |
let elementFloat = utils.cascadedStylePropertyForElement(element, "float") || utils.cascadedStylePropertyForElement(element, "cssFloat"); |
| 274 |
let runIntoThis = previousDisplay == "run-in" && elementFloat == "none"; |
| 275 |
if (isBlock && !runIntoThis && /^[\s\xa0]*$/.test(currentLine())) |
| 276 |
lines.push(''); |
| 277 |
} |
| 278 |
|
| 279 |
let shown = utils.isShown(element); |
| 280 |
let whitespace = null, textTransform = null; |
| 281 |
if (shown) { |
| 282 |
whitespace = utils.cascadedStylePropertyForElement(element, "white-space"); |
| 283 |
textTransform = utils.cascadedStylePropertyForElement(element, "text-transform"); |
| 284 |
} |
| 285 |
|
| 286 |
Array.from(element.childNodes).forEach((childNode) => { |
| 287 |
switch (childNode.nodeType) { |
| 288 |
case Node.TEXT_NODE: |
| 289 |
if (shown) |
| 290 |
appendLinesFromTextNode(childNode, whitespace, textTransform); |
| 291 |
break; |
| 292 |
case Node.ELEMENT_NODE: |
| 293 |
appendLines(childNode); |
| 294 |
break; |
| 295 |
} |
| 296 |
}); |
| 297 |
|
| 298 |
let line = currentLine(); |
| 299 |
if ((isTableCell || display == "table-cell") && line && line.endsWith(' ')) |
| 300 |
lines[lines.length - 1] += ' '; |
| 301 |
|
| 302 |
if (isBlock && display != "run-in" && /^[\s\xa0]*$/.test(line)) |
| 303 |
lines.push(''); |
| 304 |
} |
| 305 |
|
| 306 |
appendLines(element); |
| 307 |
|
| 308 |
lines = lines.map((str) => str.replace(/^[^\S\xa0]+|[^\S\xa0]+$/g, '')); |
| 309 |
let trimmed = lines.join('\n').replace(/^[^\S\xa0]+|[^\S\xa0]+$/g, ''); |
| 310 |
|
| 311 |
// Replace non-breakable spaces with regular ones. |
| 312 |
return trimmed.replace(/\xa0/g, ' '); |
| 313 |
} |