All files / library/platform/default agent-node.ts

95.55% Statements 43/45
75% Branches 15/20
100% Functions 10/10
95.23% Lines 40/42

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 77 78 79 80 81 82 83 84 85 86          1x         16x 16x 16x 16x   16x 16x 16x   8x 8x 8x 8x 3x       1x 1x 1x 1x     1x   1x     3x 2x       2x 1x   1x 1x   2x     5x 3x 1x 2x 1x 1x   1x 1x   3x 3x     2x 1x   1x             1x                  
import type {
  MPElementData,
  MPTextData,
} from 'angular-miniprogram/platform/type';
 
export class AgentNode {
  selector!: string | unknown;
  name!: string;
  parent!: AgentNode | undefined;
  nextSibling!: AgentNode | undefined;
  attribute: Record<string, string> = {};
  style: Record<string, string> = {};
  property: Record<string, unknown> = {};
  classList = new Set<string>();
  value!: string;
  children: AgentNode[] = [];
  listener: Record<string, Function> = {};
  constructor(public type: 'element' | 'comment' | 'text') {}
  appendChild(child: AgentNode) {
    const lastChildIndex = this.children.length - 1;
    this.children.push(child);
    child.parent = this;
    if (lastChildIndex > -1) {
      this.children[lastChildIndex].nextSibling = child;
    }
  }
  setParent(parent: AgentNode) {
    const oldParent = this.parent;
    Eif (oldParent) {
      const index = oldParent.children.findIndex((item) => item === this);
      Iif (index === -1) {
        throw new Error('没有在之前的父级上找到该节点' + this);
      }
      oldParent.children.splice(index, 1);
    }
    parent.appendChild(this);
  }
  insertBefore(newChild: AgentNode, refChild: AgentNode) {
    const refIndex = this.children.findIndex((item) => item === refChild);
    Iif (refIndex === -1) {
      throw new Error('未找到引用子节点' + refChild);
    }
 
    if (refIndex === 0) {
      newChild.nextSibling = refChild;
    } else {
      this.children[refIndex - 1].nextSibling = newChild;
      newChild.nextSibling = refChild;
    }
    this.children.splice(refIndex, 0, newChild);
  }
  removeChild(child: AgentNode) {
    const index = this.children.findIndex((item) => item === child);
    if (index === 0) {
      this.children.shift();
    } else if (index + 1 === this.children.length) {
      this.children[index - 1].nextSibling = undefined;
      this.children.pop();
    } else {
      this.children[index - 1].nextSibling = this.children[index + 1];
      this.children.splice(index, 1);
    }
    child.nextSibling = undefined;
    child.parent = undefined;
  }
  toView(): MPTextData | MPElementData {
    if (this.type === 'text') {
      return { value: this.value };
    } else {
      return {
        class:
          Array.from(this.classList).join(' ') +
          (this.attribute.class ? ' ' + this.attribute.class : ''),
 
        style:
          Object.entries(this.style)
            .map(([style, value]) => `${style}:${value}`)
            .join(';') +
          (this.attribute.style ? ';' + this.attribute.style : ''),
 
        property: { ...this.property },
      };
    }
  }
}