All files / builder/mini-program-compiler/parse-node element.ts

100% Statements 34/34
100% Branches 12/12
100% Functions 10/10
100% Lines 34/34

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 73 74 75 76    1x     1x   1028x 1028x 1028x 1028x 1028x 1028x   1028x 1028x 1028x 1028x 1028x     1028x 1028x 121x   110x     1028x 181x 101x     1028x 311x     1028x         10x       1028x 1028x 1028x 605x 423x 31x         885x     1028x   1028x     885x                      
import type { Element } from '../../angular-internal/ast.type';
import { ComponentContext } from './component-context';
import { NgElementMeta, NgNodeKind, NgNodeMeta, ParsedNode } from './interface';
import type { MatchedComponent, MatchedDirective } from './type';
 
export class ParsedNgElement implements ParsedNode<NgElementMeta> {
  private tagName!: string;
  private children: ParsedNode<NgNodeMeta>[] = [];
  attributeObject: Record<string, string> = {};
  kind = NgNodeKind.Element;
  inputs: string[] = [];
  outputs: string[] = [];
  singleClosedTag = false;
  constructor(
    private node: Element,
    public parent: ParsedNode<NgNodeMeta> | undefined,
    private componentMeta: MatchedComponent | undefined,
    public index: number,
    private directiveMeta: MatchedDirective | undefined
  ) {}
  private analysis() {
    this.getTagName();
    this.node.attributes
      .filter((item) => item.name !== 'class' && item.name !== 'style')
      .forEach((item) => {
        this.attributeObject[item.name] = item.value;
      });
 
    this.node.inputs.forEach((input) => {
      if (input.type === 0) {
        this.inputs.push(input.name);
      }
    });
    this.node.outputs.forEach((output) => {
      this.outputs.push(output.name);
    });
 
    if (
      !this.node.endSourceSpan ||
      this.node.startSourceSpan.end.offset ===
        this.node.endSourceSpan.end.offset
    ) {
      this.singleClosedTag = true;
    }
  }
  private getTagName() {
    const originTagName = this.node.name;
    this.tagName = originTagName;
    if (/^(div|p|h1|h2|h3|h4|h5|h6|span)$/.test(originTagName)) {
      this.tagName = 'view';
    } else if (originTagName === 'ng-container') {
      this.tagName = 'block';
    }
  }
 
  appendNgNodeChild(child: ParsedNode<NgNodeMeta>) {
    this.children.push(child);
  }
  getNodeMeta(): NgElementMeta {
    this.analysis();
 
    return {
      kind: NgNodeKind.Element,
      tagName: this.tagName,
      children: this.children.map((child) => child.getNodeMeta()),
      inputs: this.inputs,
      outputs: this.outputs,
      attributes: this.attributeObject,
      singleClosedTag: this.singleClosedTag,
      componentMeta: this.componentMeta,
      index: this.index,
      directiveMeta: this.directiveMeta,
    };
  }
}