<commit_before>/*
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * check if element is visible
 * @param  {HTMLElement} elem  element to check
 * @return {Boolean}           true if element is within viewport
 */
export default function isElementDisplayed (element: Element): boolean {
    function nodeIsElement(node?: Element) {
        if (!node) {
            return false
        }

        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
            return true

        default:
            return false
        }
    }

    function parentElementForElement(element?: Element) {
        if (!element) {
            return null
        }

        return enclosingNodeOrSelfMatchingPredicate(element.parentNode as ParentNode, nodeIsElement)
    }

    function enclosingNodeOrSelfMatchingPredicate(targetNode: ParentNode | HTMLElement, predicate: Function) {
        for (
            let node: ParentNode = targetNode;
            node && node !== (targetNode as Node).ownerDocument;
            node = (node as HTMLElement).parentNode as ParentNode
        )
            if (predicate(node)) {
                return node
            }

        return null
    }

    function enclosingElementOrSelfMatchingPredicate(targetElement: HTMLElement | Document, predicate: Function) {
        for (
            let element: HTMLElement | ParentNode = targetElement;
            element && element !== targetElement.ownerDocument;
            element = parentElementForElement(element as HTMLElement) as HTMLElement
        )
            if (predicate(element)) {
                return element
            }

        return null
    }

    function cascadedStylePropertyForElement(
        element?: Element | ParentNode | ShadowRoot,
        property?: string
    ): string | null {
        if (!element || !property) {
            return null
        }
        // if document-fragment, skip it and use element.host instead. This happens
        // when the element is inside a shadow root.
        // window.getComputedStyle errors on document-fragment.
        if (element instanceof ShadowRoot) {
            element = element.host
        }

        let computedStyle = window.getComputedStyle(element as Element)
        let computedStyleProperty = computedStyle.getPropertyValue(property)
        if (computedStyleProperty && computedStyleProperty !== 'inherit') {
            return computedStyleProperty
        }

        // Ideally getPropertyValue would return the 'used' or 'actual' value, but
        // it doesn't for legacy reasons. So we need to do our own poor man's cascade.
        // Fall back to the first non-'inherit' value found in an ancestor.
        // In any case, getPropertyValue will not return 'initial'.

        // FIXME: will this incorrectly inherit non-inheritable CSS properties?
        // I think all important non-inheritable properties (width, height, etc.)
        // for our purposes here are specially resolved, so this may not be an issue.
        // Specification is here: https://drafts.csswg.org/cssom/#resolved-values
        let parentElement = parentElementForElement(element as Element) as ParentNode
        return cascadedStylePropertyForElement(parentElement, property)
    }

    function elementSubtreeHasNonZeroDimensions(element: Element): boolean {
        let boundingBox = element.getBoundingClientRect()
        if (boundingBox.width > 0 && boundingBox.height > 0) {
            return true
        }<commit_msg>fix: access ShadowRoot via window.ShadowRoot (#9046)<commit_after>/*
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * check if element is visible
 * @param  {HTMLElement} elem  element to check
 * @return {Boolean}           true if element is within viewport
 */
export default function isElementDisplayed (element: Element): boolean {
    function nodeIsElement(node?: Element) {
        if (!node) {
            return false
        }

        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
            return true

        default:
            return false
        }
    }

    function parentElementForElement(element?: Element) {
        if (!element) {
            return null
        }

        return enclosingNodeOrSelfMatchingPredicate(element.parentNode as ParentNode, nodeIsElement)
    }

    function enclosingNodeOrSelfMatchingPredicate(targetNode: ParentNode | HTMLElement, predicate: Function) {
        for (
            let node: ParentNode = targetNode;
            node && node !== (targetNode as Node).ownerDocument;
            node = (node as HTMLElement).parentNode as ParentNode
        )
            if (predicate(node)) {
                return node
            }

        return null
    }

    function enclosingElementOrSelfMatchingPredicate(targetElement: HTMLElement | Document, predicate: Function) {
        for (
            let element: HTMLElement | ParentNode = targetElement;
            element && element !== targetElement.ownerDocument;
            element = parentElementForElement(element as HTMLElement) as HTMLElement
        )
            if (predicate(element)) {
                return element
            }

        return null
    }

    function cascadedStylePropertyForElement(
        element?: Element | ParentNode | ShadowRoot,
        property?: string
    ): string | null {
        if (!element || !property) {
            return null
        }
        // if document-fragment, skip it and use element.host instead. This happens
        // when the element is inside a shadow root.
        // window.getComputedStyle errors on document-fragment.
        if (element instanceof window.ShadowRoot) {
            element = element.host
        }

        let computedStyle = window.getComputedStyle(element as Element)
        let computedStyleProperty = computedStyle.getPropertyValue(property)
        if (computedStyleProperty && computedStyleProperty !== 'inherit') {
            return computedStyleProperty
        }

        // Ideally getPropertyValue would return the 'used' or 'actual' value, but
        // it doesn't for legacy reasons. So we need to do our own poor man's cascade.
        // Fall back to the first non-'inherit' value found in an ancestor.
        // In any case, getPropertyValue will not return 'initial'.

        // FIXME: will this incorrectly inherit non-inheritable CSS properties?
        // I think all important non-inheritable properties (width, height, etc.)
        // for our purposes here are specially resolved, so this may not be an issue.
        // Specification is here: https://drafts.csswg.org/cssom/#resolved-values
        let parentElement = parentElementForElement(element as Element) as ParentNode
        return cascadedStylePropertyForElement(parentElement, property)
    }

    function elementSubtreeHasNonZeroDimensions(element: Element): boolean {
        let boundingBox = element.getBoundingClientRect()
        if (boundingBox.width > 0 && boundingBox.height > 0) {
            return true
        }
{'fraction_non_alphanumeric': 0.068218954248366, 'fraction_numerical': 0.002042483660130719, 'mean_word_length': 2.6405204460966543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7585651', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2307, 'n_words': 939}