<commit_before>import { CommonModule } from '@angular/common';
import { AfterContentInit, AfterViewInit, ChangeDetectionStrategy, Component, ContentChildren, Directive, ElementRef, EventEmitter, Input, NgModule, OnDestroy, Output, QueryList, TemplateRef, ViewEncapsulation } from '@angular/core';
import { PrimeTemplate } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { RippleModule } from 'primeng/ripple';

type ButtonIconPosition = 'left' | 'right' | 'top' | 'bottom';

@Directive({
    selector: '[pButton]',
    host: {
        class: 'p-element'
    }
})
export class ButtonDirective implements AfterViewInit, OnDestroy {
    @Input() iconPos: ButtonIconPosition = 'left';

    @Input() loadingIcon: string = 'pi pi-spinner pi-spin';

    public _label: string;

    public _icon: string;

    public _loading: boolean = false;

    public initialized: boolean;

    public _initialStyleClass: string;

    constructor(public el: ElementRef) {}

    ngAfterViewInit() {
        this._initialStyleClass = this.el.nativeElement.className;
        DomHandler.addMultipleClasses(this.el.nativeElement, this.getStyleClass());

        if (this.icon || this.loading) {
            this.createIconEl();
        }

        if (this.label) {
            let labelElement = document.createElement('span');
            if (this.icon && !this.label) {
                labelElement.setAttribute('aria-hidden', 'true');
            }
            labelElement.className = 'p-button-label';
            labelElement.appendChild(document.createTextNode(this.label));

            this.el.nativeElement.appendChild(labelElement);
        }

        this.initialized = true;
    }

    getStyleClass(): string {
        let styleClass = 'p-button p-component';
        if (this.icon && !this.label) {
            styleClass = styleClass + ' p-button-icon-only';
        }

        if (this.loading) {
            styleClass = styleClass + ' p-disabled p-button-loading';
            if (!this.icon && this.label) styleClass = styleClass + ' p-button-loading-label-only';
        }

        return styleClass;
    }

    setStyleClass() {
        let styleClass = this.getStyleClass();
        this.el.nativeElement.className = styleClass + ' ' + this._initialStyleClass;
    }

    createIconEl() {
        let iconElement = document.createElement('span');
        iconElement.className = 'p-button-icon';
        iconElement.setAttribute('aria-hidden', 'true');
        let iconPosClass = this.label ? 'p-button-icon-' + this.iconPos : null;

        if (iconPosClass) {
            DomHandler.addClass(iconElement, iconPosClass);
        }

        let iconClass = this.getIconClass();

        if (iconClass) {
            DomHandler.addMultipleClasses(iconElement, iconClass);
        }

        let labelEl = DomHandler.findSingle(this.el.nativeElement, '.p-button-label');

        if (labelEl) this.el.nativeElement.insertBefore(iconElement, labelEl);
        else this.el.nativeElement.appendChild(iconElement);
    }

    getIconClass() {
        return this.loading ? 'p-button-loading-icon ' + this.loadingIcon : this._icon;
    }

    setIconClass() {
        let iconElement = DomHandler.findSingle(this.el.nativeElement, '.p-button-icon');
        if (iconElement) {
            if (this.iconPos) iconElement.className = 'p-button-icon p-button-icon-' + this.iconPos + ' ' + this.getIconClass();
            else iconElement.className = 'p-button-icon ' + this.getIconClass();
        } else {
            this.createIconEl();
        }
    }
<commit_msg>Fixed #11942 - Button: Render problem when using icons and text not via input<commit_after>import { CommonModule } from '@angular/common';
import { AfterContentInit, AfterViewInit, ChangeDetectionStrategy, Component, ContentChildren, Directive, ElementRef, EventEmitter, Input, NgModule, OnDestroy, Output, QueryList, TemplateRef, ViewEncapsulation } from '@angular/core';
import { PrimeTemplate } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { RippleModule } from 'primeng/ripple';
import { ObjectUtils } from 'primeng/utils';

type ButtonIconPosition = 'left' | 'right' | 'top' | 'bottom';

@Directive({
    selector: '[pButton]',
    host: {
        class: 'p-element'
    }
})
export class ButtonDirective implements AfterViewInit, OnDestroy {
    @Input() iconPos: ButtonIconPosition = 'left';

    @Input() loadingIcon: string = 'pi pi-spinner pi-spin';

    public _label: string;

    public _icon: string;

    public _loading: boolean = false;

    public initialized: boolean;

    public _initialStyleClass: string;

    constructor(public el: ElementRef) {}

    ngAfterViewInit() {
        this._initialStyleClass = this.el.nativeElement.className;
        DomHandler.addMultipleClasses(this.el.nativeElement, this.getStyleClass());

        if (this.icon || this.loading) {
            this.createIconEl();
        }

        if (this.label) {
            let labelElement = document.createElement('span');
            if (this.icon && !this.label) {
                labelElement.setAttribute('aria-hidden', 'true');
            }
            labelElement.className = 'p-button-label';
            labelElement.appendChild(document.createTextNode(this.label));

            this.el.nativeElement.appendChild(labelElement);
        }

        this.initialized = true;
    }

    getStyleClass(): string {
        let styleClass = 'p-button p-component';
        if (this.icon && !this.label && ObjectUtils.isEmpty(this.el.nativeElement.textContent)) {
            styleClass = styleClass + ' p-button-icon-only';
        }

        if (this.loading) {
            styleClass = styleClass + ' p-disabled p-button-loading';
            if (!this.icon && this.label) styleClass = styleClass + ' p-button-loading-label-only';
        }

        return styleClass;
    }

    setStyleClass() {
        let styleClass = this.getStyleClass();
        this.el.nativeElement.className = styleClass + ' ' + this._initialStyleClass;
    }

    createIconEl() {
        let iconElement = document.createElement('span');
        iconElement.className = 'p-button-icon';
        iconElement.setAttribute('aria-hidden', 'true');
        let iconPosClass = this.label ? 'p-button-icon-' + this.iconPos : null;

        if (iconPosClass) {
            DomHandler.addClass(iconElement, iconPosClass);
        }

        let iconClass = this.getIconClass();

        if (iconClass) {
            DomHandler.addMultipleClasses(iconElement, iconClass);
        }

        this.el.nativeElement.insertBefore(iconElement, this.el.nativeElement.firstChild);
    }

    getIconClass() {
        return this.loading ? 'p-button-loading-icon ' + this.loadingIcon : this._icon;
    }

    setIconClass() {
        let iconElement = DomHandler.findSingle(this.el.nativeElement, '.p-button-icon');
        if (iconElement) {
            if (this.iconPos) iconElement.className = 'p-button-icon p-button-icon-' + this.iconPos + ' ' + this.getIconClass();
            else iconElement.className = 'p-button-icon ' + this.getIconClass();
        } else {
            this.createIconEl();
        }
    }

{'fraction_non_alphanumeric': 0.12423483583750695, 'fraction_numerical': 0.0006956037840845854, 'mean_word_length': 2.6566632756866735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5306945', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1996, 'n_words': 409}