<commit_before>import {Directive, HostBinding, Input, NgModule, OnDestroy, OnInit} from "@angular/core";
import {DomSanitizer} from "@angular/platform-browser";
import {CommonUtils} from "../../core/utils/common-utils";

export type HtmlCallback = (...args) => any;
type CallbackValues = { [callbackName: string]: HtmlCallback };

@Directive({
    selector: '[trustedHtml]',
})
export class JigsawTrustedHtml implements OnInit, OnDestroy {
    private static _callbacks = new Map<any, CallbackValues>();
    private static _contexts = [];

    private static _getContextMagicNumber(context: any): number {
        return JigsawTrustedHtml._contexts.indexOf(context);
    }

    private static _getContext(magicNumber: number): any {
        return JigsawTrustedHtml._contexts[magicNumber];
    }

    private static _registerContext(context: any): void {
        if (CommonUtils.isDefined(context) && JigsawTrustedHtml._getContextMagicNumber(context) == -1) {
            JigsawTrustedHtml._contexts.push(context);
        }
    }

    private static _jigsawInternalCallbackWrapper(callbackName: string, contextMagicNumber: number, ...args) {
        const context = JigsawTrustedHtml._getContext(contextMagicNumber);
        if (CommonUtils.isUndefined(context)) {
            console.error('no context found by magic number: ' + contextMagicNumber);
            return;
        }
        const callbacks = JigsawTrustedHtml._callbacks.get(context);
        if (CommonUtils.isUndefined(callbacks)) {
            console.error('no callback cache info found by magic number: ' + contextMagicNumber);
            return;
        }
        const callback = callbacks[callbackName];
        if (!(callback instanceof Function)) {
            console.error('no callback function found by callback name: ' + callbackName);
            console.log(`Hint: add a member method named ${callbackName} to your component's class.`);
            return;
        }
        CommonUtils.safeInvokeCallback(context, callback, args);
    }

    private static _declareCallback(context: any, name: string, callback: HtmlCallback) {
        if (CommonUtils.isUndefined(context)) {
            console.error('invalid context');
            return;
        }
        if (!(callback instanceof Function)) {
            console.error('invalid context');
            return;
        }

        let callbacks = JigsawTrustedHtml._callbacks.get(context);
        if (CommonUtils.isUndefined(callbacks)) {
            callbacks = {};
            JigsawTrustedHtml._callbacks.set(context, callbacks);
        }
        callbacks[name] = CommonUtils.isDefined(callback) ? callback : context;
    }

    private static _clearCallbacks(context: any) {
        JigsawTrustedHtml._callbacks.delete(context);
    }

    //====================================================

    constructor(private _sanitizer: DomSanitizer) {
        if (!window.hasOwnProperty('_jigsawInternalCallbackWrapper') || !(window['_jigsawInternalCallbackWrapper'] instanceof Function)) {
            window['_jigsawInternalCallbackWrapper'] = JigsawTrustedHtml._jigsawInternalCallbackWrapper;
        }
    }

    private _contextMagicNumber: number = -1;
    private _trustedHtmlContext: any;

    @Input()
    public get trustedHtmlContext(): any {
        return this._trustedHtmlContext;
    }

    public set trustedHtmlContext(value: any) {
        if (CommonUtils.isUndefined(value)) {
            return;
        }

        this._trustedHtmlContext = value;
        JigsawTrustedHtml._registerContext(value);
        this._contextMagicNumber = JigsawTrustedHtml._getContextMagicNumber(value);
    }

    private _safeHtml: any;
    private _trustedHtml: string;

    @Input()
    public get trustedHtml(): string {
        return this._trustedHtml;
    }

    public set trustedHtml(value: string) {
        this._trustedHtml = CommonUtils.isDefined(value) ? value.trim() : '';
        this._safeHtml = null;
    }

    @HostBinding('innerHtml')
    public get innerHtml(): any {<commit_msg>clear context before replacing it<commit_after>import {Directive, HostBinding, Input, NgModule, OnDestroy, OnInit} from "@angular/core";
import {DomSanitizer} from "@angular/platform-browser";
import {CommonUtils} from "../../core/utils/common-utils";

export type HtmlCallback = (...args) => any;
type CallbackValues = { [callbackName: string]: HtmlCallback };

@Directive({
    selector: '[trustedHtml]',
})
export class JigsawTrustedHtml implements OnInit, OnDestroy {
    private static _callbacks = new Map<any, CallbackValues>();
    private static _contexts = [];

    private static _getContextMagicNumber(context: any): number {
        return JigsawTrustedHtml._contexts.indexOf(context);
    }

    private static _getContext(magicNumber: number): any {
        return JigsawTrustedHtml._contexts[magicNumber];
    }

    private static _registerContext(context: any): void {
        if (CommonUtils.isDefined(context) && JigsawTrustedHtml._getContextMagicNumber(context) == -1) {
            JigsawTrustedHtml._contexts.push(context);
        }
    }

    private static _jigsawInternalCallbackWrapper(callbackName: string, contextMagicNumber: number, ...args) {
        const context = JigsawTrustedHtml._getContext(contextMagicNumber);
        if (CommonUtils.isUndefined(context)) {
            console.error('no context found by magic number: ' + contextMagicNumber);
            return;
        }
        const callbacks = JigsawTrustedHtml._callbacks.get(context);
        if (CommonUtils.isUndefined(callbacks)) {
            console.error('no callback cache info found by magic number: ' + contextMagicNumber);
            return;
        }
        const callback = callbacks[callbackName];
        if (!(callback instanceof Function)) {
            console.error('no callback function found by callback name: ' + callbackName);
            console.log(`Hint: add a member method named ${callbackName} to your component's class.`);
            return;
        }
        CommonUtils.safeInvokeCallback(context, callback, args);
    }

    private static _declareCallback(context: any, name: string, callback: HtmlCallback) {
        if (CommonUtils.isUndefined(context)) {
            console.error('invalid context');
            return;
        }
        if (!(callback instanceof Function)) {
            console.error('invalid context');
            return;
        }

        let callbacks = JigsawTrustedHtml._callbacks.get(context);
        if (CommonUtils.isUndefined(callbacks)) {
            callbacks = {};
            JigsawTrustedHtml._callbacks.set(context, callbacks);
        }
        callbacks[name] = CommonUtils.isDefined(callback) ? callback : context;
    }

    private static _clearCallbacks(context: any) {
        JigsawTrustedHtml._callbacks.delete(context);
    }

    //====================================================

    constructor(private _sanitizer: DomSanitizer) {
        if (!window.hasOwnProperty('_jigsawInternalCallbackWrapper') || !(window['_jigsawInternalCallbackWrapper'] instanceof Function)) {
            window['_jigsawInternalCallbackWrapper'] = JigsawTrustedHtml._jigsawInternalCallbackWrapper;
        }
    }

    private _contextMagicNumber: number = -1;
    private _trustedHtmlContext: any;

    @Input()
    public get trustedHtmlContext(): any {
        return this._trustedHtmlContext;
    }

    public set trustedHtmlContext(value: any) {
        if (CommonUtils.isUndefined(value)) {
            return;
        }
        if (CommonUtils.isDefined(this._trustedHtmlContext)) {
            JigsawTrustedHtml._clearCallbacks(this._trustedHtmlContext);
        }
        this._trustedHtmlContext = value;
        JigsawTrustedHtml._registerContext(value);
        this._contextMagicNumber = JigsawTrustedHtml._getContextMagicNumber(value);
    }

    private _safeHtml: any;
    private _trustedHtml: string;

    @Input()
    public get trustedHtml(): string {
        return this._trustedHtml;
    }

    public set trustedHtml(value: string) {
        this._trustedHtml = CommonUtils.isDefined(value) ? value.trim() : '';
        this._safeHtml = null;
    }

    @HostBinding('innerHtml')
    public get innerHtml(): any {
{'fraction_non_alphanumeric': 0.1148017941568675, 'fraction_numerical': 0.0004849072614862408, 'mean_word_length': 3.0620384047267355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4326349', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2186, 'n_words': 475}