All files / ip-input si-ip6-input.directive.ts

83.33% Statements 10/12
66.66% Branches 2/3
100% Functions 4/4
81.81% Lines 9/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                    1x       9x 12x       327x       311x 311x           311x             311x         2117x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { computed, Directive } from '@angular/core';
import {
  AbstractControl,
  ControlValueAccessor,
  NG_VALIDATORS,
  NG_VALUE_ACCESSOR,
  ValidationErrors,
  Validator
} from '@angular/forms';
 
import { splitIpV6Sections } from './address-utils';
import { ipV6Validator } from './address-validators';
import { AddrInputEvent, SiIpInputDirective } from './si-ip-input.directive';
 
@Directive({
  selector: 'input[siIpV6]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiIp6InputDirective,
      multi: true
    },
    {
      provide: NG_VALIDATORS,
      useExisting: SiIp6InputDirective,
      multi: true
    }
  ],
  exportAs: 'siIpV6'
})
export class SiIp6InputDirective
  extends SiIpInputDirective
  implements ControlValueAccessor, Validator
{
  readonly validatorFn = computed(() =>
    ipV6Validator({ zeroCompression: true, cidr: this.cidr() })
  );
 
  validate(control: AbstractControl): ValidationErrors | null {
    return this.validatorFn()(control);
  }
 
  maskInput(e: AddrInputEvent): void {
    const { value, pos, type } = e;
    Iif (!value) {
      this.renderer.setProperty(this.inputEl, 'value', '');
      return;
    }
 
    // TODO: Restore cursor position
    const sections = splitIpV6Sections({
      type,
      input: value,
      pos,
      zeroCompression: true,
      cidr: this.cidr()
    });
    this.renderer.setProperty(
      this.inputEl,
      'value',
      sections
        .splice(0, this.cidr() ? 17 : 15)
        .map(s => s.value)
        .join('')
    );
  }
}