<commit_before>import {iterateCSSRules, iterateCSSDeclarations, getCSSVariables, replaceCSSRelativeURLsWithAbsolute, removeCSSComments, replaceCSSFontFace, replaceCSSVariables, getCSSURLValue, cssImportRegex, getCSSBaseBath} from './css-rules';
import {getModifiableCSSDeclaration, ModifiableCSSDeclaration, ModifiableCSSRule} from './modify-css';
import {bgFetch} from './network';
import {watchForNodePosition, removeNode} from '../utils/dom';
import {logWarn} from '../utils/log';
import {createAsyncTasksQueue} from '../utils/throttle';
import {isDeepSelectorSupported, isHostSelectorSupported} from '../../utils/platform';
import {getMatches} from '../../utils/text';
import {FilterConfig} from '../../definitions';
import {getAbsoluteURL} from './url';

declare global {
    interface HTMLStyleElement {
        sheet: CSSStyleSheet;
    }
    interface HTMLLinkElement {
        sheet: CSSStyleSheet;
    }
    interface SVGStyleElement {
        sheet: CSSStyleSheet;
    }
}

export interface StyleManager {
    details(): {variables: Map<string, string>};
    render(filter: FilterConfig, variables: Map<string, string>): void;
    pause(): void;
    destroy(): void;
    watch(): void;
    restore(): void;
}

export const STYLE_SELECTOR = (() => {
    let selectors = [
        'html /deep/ link[rel*="stylesheet" i]:not([disabled])',
        'html /deep/ style',
        ':host /deep/ link[rel*="stylesheet" i]:not([disabled])',
        ':host /deep/ style',
        ':host link[rel*="stylesheet" i]:not([disabled])',
        ':host style',
    ];
    if (!isDeepSelectorSupported()) {
        selectors = selectors.map((s) => s.replace('/deep/ ', ''));
    }
    if (!isHostSelectorSupported()) {
        selectors = selectors.filter((s) => !s.startsWith(':host'));
    }
    return selectors.join(', ');
})();

export function shouldManageStyle(element: Node) {
    return (
        (
            (element instanceof HTMLStyleElement) ||
            (element instanceof SVGStyleElement) ||
            (
                element instanceof HTMLLinkElement &&
                element.rel &&
                element.rel.toLowerCase().includes('stylesheet') &&
                !element.disabled
            )
        ) &&
        !element.classList.contains('darkreader') &&
        element.media !== 'print'
    );
}

const asyncQueue = createAsyncTasksQueue();

export function manageStyle(element: HTMLLinkElement | HTMLStyleElement, {update, loadingStart, loadingEnd}): StyleManager {

    const prevStyles: HTMLStyleElement[] = [];
    let next: Element = element;
    while ((next = next.nextElementSibling) && next.matches('.darkreader')) {
        prevStyles.push(next as HTMLStyleElement);
    }
    let corsCopy: HTMLStyleElement = prevStyles.find((el) => el.matches('.darkreader--cors')) || null;
    let syncStyle: HTMLStyleElement | SVGStyleElement = prevStyles.find((el) => el.matches('.darkreader--sync')) || null;

    let corsCopyPositionWatcher: ReturnType<typeof watchForNodePosition> = null;
    let syncStylePositionWatcher: ReturnType<typeof watchForNodePosition> = null;

    let cancelAsyncOperations = false;

    function isCancelled() {<commit_msg>No override for Stylus CSS (#2152)<commit_after>import {iterateCSSRules, iterateCSSDeclarations, getCSSVariables, replaceCSSRelativeURLsWithAbsolute, removeCSSComments, replaceCSSFontFace, replaceCSSVariables, getCSSURLValue, cssImportRegex, getCSSBaseBath} from './css-rules';
import {getModifiableCSSDeclaration, ModifiableCSSDeclaration, ModifiableCSSRule} from './modify-css';
import {bgFetch} from './network';
import {watchForNodePosition, removeNode} from '../utils/dom';
import {logWarn} from '../utils/log';
import {createAsyncTasksQueue} from '../utils/throttle';
import {isDeepSelectorSupported, isHostSelectorSupported} from '../../utils/platform';
import {getMatches} from '../../utils/text';
import {FilterConfig} from '../../definitions';
import {getAbsoluteURL} from './url';

declare global {
    interface HTMLStyleElement {
        sheet: CSSStyleSheet;
    }
    interface HTMLLinkElement {
        sheet: CSSStyleSheet;
    }
    interface SVGStyleElement {
        sheet: CSSStyleSheet;
    }
}

export interface StyleManager {
    details(): {variables: Map<string, string>};
    render(filter: FilterConfig, variables: Map<string, string>): void;
    pause(): void;
    destroy(): void;
    watch(): void;
    restore(): void;
}

export const STYLE_SELECTOR = (() => {
    let selectors = [
        'html /deep/ link[rel*="stylesheet" i]:not([disabled])',
        'html /deep/ style',
        ':host /deep/ link[rel*="stylesheet" i]:not([disabled])',
        ':host /deep/ style',
        ':host link[rel*="stylesheet" i]:not([disabled])',
        ':host style',
    ];
    if (!isDeepSelectorSupported()) {
        selectors = selectors.map((s) => s.replace('/deep/ ', ''));
    }
    if (!isHostSelectorSupported()) {
        selectors = selectors.filter((s) => !s.startsWith(':host'));
    }
    return selectors.join(', ');
})();

export function shouldManageStyle(element: Node) {
    return (
        (
            (element instanceof HTMLStyleElement) ||
            (element instanceof SVGStyleElement) ||
            (
                element instanceof HTMLLinkElement &&
                element.rel &&
                element.rel.toLowerCase().includes('stylesheet') &&
                !element.disabled
            )
        ) &&
        !element.classList.contains('darkreader') &&
        element.media !== 'print' &&
        !element.classList.contains('stylus')
    );
}

const asyncQueue = createAsyncTasksQueue();

export function manageStyle(element: HTMLLinkElement | HTMLStyleElement, {update, loadingStart, loadingEnd}): StyleManager {

    const prevStyles: HTMLStyleElement[] = [];
    let next: Element = element;
    while ((next = next.nextElementSibling) && next.matches('.darkreader')) {
        prevStyles.push(next as HTMLStyleElement);
    }
    let corsCopy: HTMLStyleElement = prevStyles.find((el) => el.matches('.darkreader--cors')) || null;
    let syncStyle: HTMLStyleElement | SVGStyleElement = prevStyles.find((el) => el.matches('.darkreader--sync')) || null;

    let corsCopyPositionWatcher: ReturnType<typeof watchForNodePosition> = null;
    let syncStylePositionWatcher: ReturnType<typeof watchForNodePosition> = null;

    let cancelAsyncOperations = false;

    function isCancelled() {
{'fraction_non_alphanumeric': 0.14188140329090346, 'fraction_numerical': 0.0006209251785159888, 'mean_word_length': 3.443448275862069, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, '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': '4217188', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1788, 'n_words': 402}