<commit_before>/*
 * Adds emote flags to an element.
 */
function add_flags(element, parts) {
    for(var p = 1; p < parts.length; p++) {
        // Normalize case, and forbid things that don't look exactly as we expect
        var flag = parts[p].toLowerCase();
        if(/^[\w:!#\/]+$/.test(flag)) {
            element.classList.add("bpflag-" + sanitize_emote(flag));
        }
    }
}

/*
 * Mangle a recognized <a> element to be an emote. Applies all CSS, state, and
 * flags. Handles disabled emotes.
 */
function convert_emote_element(store, element, parts, name, info) {
    // Applied to all emote elements, no matter what
    element.classList.add("bpm-emote");

    // Attributes used in alt-text, to avoid extra lookups.
    element.setAttribute("data-bpm_emotename", name);
    element.setAttribute("data-bpm_srname", info.source_name);

    // Leave existing text alone. This is also relevant to the click toggle
    // and the alt-text converter, so record this fact for later use
    var can_modify_text = !element.textContent;
    var disabled = store.is_disabled(info);

    // Work out state variable. "e" is always present
    var state = "e";
    if(info.is_nsfw) {
        state += "n";
    }
    if(can_modify_text) {
        state += "T";
    }
    if(disabled) {
        state += "d" + disabled; // Add numerical code (only 1=NSFW is used)
    }
    element.setAttribute("data-bpm_state", state);

    if(disabled) {
        if(can_modify_text) {
            // Any existing text (generally, there shouldn't be any) will look
            // a little funny with our custom CSS, but there's not much we can
            // do about that.
            element.textContent = name;
        }

        if(store.prefs.hideDisabledEmotes) {
            // "Ignore" mode- don't minify it, just hide it completely
            element.classList.add("bpm-hidden");
        } else {
            element.classList.add("bpm-minified"); // Minify emote
            if(disabled === 1) {
                // NSFW emotes have a special look to them
                element.classList.add("bpm-nsfw");
            }
        }

        return;
    }

    if(store.prefs.stealthMode) {
        element.classList.add("bpm-minified"); // Minify emote
        if(can_modify_text) {
            element.textContent = name;
        }
        return;
    }

    // Apply the actual emote CSS
    element.classList.add(info.css_class);

    // Apply flags
    add_flags(element, parts);
}

/*<commit_msg>Code cleanup: combine two nearly identical paths<commit_after>/*
 * Adds emote flags to an element.
 */
function add_flags(element, parts) {
    for(var p = 1; p < parts.length; p++) {
        // Normalize case, and forbid things that don't look exactly as we expect
        var flag = parts[p].toLowerCase();
        if(/^[\w:!#\/]+$/.test(flag)) {
            element.classList.add("bpflag-" + sanitize_emote(flag));
        }
    }
}

/*
 * Mangle a recognized <a> element to be an emote. Applies all CSS, state, and
 * flags. Handles disabled emotes.
 */
function convert_emote_element(store, element, parts, name, info) {
    // Applied to all emote elements, no matter what
    element.classList.add("bpm-emote");

    // Attributes used in alt-text, to avoid extra lookups.
    element.setAttribute("data-bpm_emotename", name);
    element.setAttribute("data-bpm_srname", info.source_name);

    // Leave existing text alone. This is also relevant to the click toggle
    // and the alt-text converter, so record this fact for later use
    var can_modify_text = !element.textContent;
    var disabled = store.is_disabled(info);

    // Work out state variable. "e" is always present
    var state = "e";
    if(info.is_nsfw) {
        state += "n";
    }
    if(can_modify_text) {
        state += "T";
    }
    if(disabled) {
        state += "d" + disabled; // Add numerical code (only 1=NSFW is used)
    }
    element.setAttribute("data-bpm_state", state);

    if(disabled || store.prefs.stealthMode) {
        if(can_modify_text) {
            // Any existing text (generally, there shouldn't be any) will look
            // a little funny with our custom CSS, but there's not much we can
            // do about that.
            element.textContent = name;
        }

        // Combining these two prefs makes absolutely no sense, but try to do
        // something sane anyway
        if(store.prefs.hideDisabledEmotes && !store.prefs.stealthMode) {
            // "Ignore" mode- don't minify it, just hide it completely
            element.classList.add("bpm-hidden");
        } else {
            element.classList.add("bpm-minified"); // Minify emote
            if(disabled === 1) {
                // NSFW emotes have a special look to them
                element.classList.add("bpm-nsfw");
            }
        }

        return;
    }

    // Apply the actual emote CSS
    element.classList.add(info.css_class);

    // Apply flags
    add_flags(element, parts);
}

/*
{'fraction_non_alphanumeric': 0.11951951951951952, 'fraction_numerical': 0.0012012012012012011, 'mean_word_length': 2.176096630642085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '759647', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1492, 'n_words': 441}