All files / src/NodeLike/ParentNodeLike/ElementLike ElementLike.ts

5.88% Statements 4/68
0% Branches 0/28
0% Functions 0/27
5.88% Lines 4/68
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 1792x 2x           2x                                                                                                                                                                                                                                                                                                                                                     2x
import AbstractElementLike           from './AbstractElementLike';
import AttributeLike                 from './AttributeLike/AttributeLike';
import IChildNodeLike                from '../../IChildNodeLike';
import IClassListLike                from './ClassListLike/IClassListLike';
import IDocumentLike                 from '../DocumentLike/IDocumentLike';
import IElementLike                  from './IElementLike';
import IParentNodeLike               from '../IParentNodeLike';
import isIElementLike                from '../../../TypeGuards/isIElementLike';
import INonDocumentTypeChildNodeLike from '../../INonDocumentTypeChildNodeLike';
class ElementLike extends AbstractElementLike {
  readonly nodeType: number = 1;
  readonly tagName:  string;
 
  get nodeName(): string {
    return this.tagName;
  }
 
  get nodeValue(): null {
    return null;
  }
 
  get textContent(): string {
    return this.childNodes.reduce((str: string, node: INonDocumentTypeChildNodeLike): string => {
      return str + (node.textContent || '');
    }, '');
  };
 
  set textContent(content: string) {
    this.childNodes.forEach((node: INonDocumentTypeChildNodeLike) => {
      this.removeChild(node);
    });
 
    const textNode = this.ownerDocument.createTextNode(content);
    this.__childNodes = this.__childNodes.add(textNode);
    textNode.__setParentNode(this);
  }
 
  get attributes(): object {
    return this.__attributes.toObject();
  }
 
  get id(): string {
    const attr = this.__attributes.get('id');
    if (attr) {
      return attr.value;
    } else {
      return '';
    }
  }
 
  set id(value: string) {
    const name = 'id';
    this.__attributes.set(name, new AttributeLike(name, value));
  }
 
  get className(): string {
    const attr = this.__attributes.get('class');
    if (attr) {
      return attr.value;
    } else {
      return '';
    }
  }
 
  set className(value: string) {
    const name = 'class';
    this.__attributes.set(name, new AttributeLike(name, value));
  }
 
  get classList(): IClassListLike {
    return this.__classList;
  }
 
  get ownerDocument(): IDocumentLike {
    return this.__ownerDocument;
  }
 
  get parentNode(): IParentNodeLike | null {
    return this.__parentNode;
  }
 
  get parentElement(): IElementLike | null {
    if (isIElementLike(this.__parentNode)) {
      return this.__parentNode;
    }
 
    return null;
  }
 
  get previousSibling(): IChildNodeLike | null {
    return this.__previousSibling;
  }
 
  get nextSibling(): INonDocumentTypeChildNodeLike | null {
    return this.__nextSibling;
  }
 
  get previousElementSibling(): IElementLike | null {
    /* An element with no parent cannot have siblings. */
    if (!this.__parentNode) {
      return null;
    }
 
    let node = this.previousSibling;
    let counter = 0;
    const length = this.__parentNode.childNodes.length;
    while (node) {
      if (counter > length) {
        throw new Error('Possible infinite loop detected. A previousSibling ' +
                        'property may be misset.');
      }
 
      if (isIElementLike(node)) {
        return node;
      }
      
      node = node.previousSibling;
      counter += 1;
    }
 
    return null;
  }
  
  get nextElementSibling(): IElementLike | null {
    /* An element with no parent cannot have siblings. */
    if (!this.__parentNode) {
      return null;
    }
 
    let node = this.nextSibling;
    let counter = 0;
    const length = this.__parentNode.childNodes.length;
    while (node) {
      if (counter > length) {
        throw new Error('Possible infinite loop detected. A previousSibling ' +
                        'property may be misset.');
      }
 
      if (isIElementLike(node)) {
        return node;
      }
 
      node = node.nextSibling;
      counter += 1;
    }
 
    return null;
  }
 
  get childNodes(): Array<INonDocumentTypeChildNodeLike> {
    return this.__childNodes.toArray();
  }
 
  get firstChild(): INonDocumentTypeChildNodeLike | null {
    return this.__childNodes.first() || null;
  }
  
  get lastChild(): INonDocumentTypeChildNodeLike | null {
    return this.__childNodes.last() || null;
  }
 
  get children(): Array<IElementLike> {
    return this.__children.toArray();
  }
 
  get firstElementChild(): IElementLike | null {
    return this.__children.first() || null;
  }
 
  get lastElementChild(): IElementLike | null {
    return this.__children.last() || null;
  }
 
  get childElementCount(): number {
    return this.children.length;
  }
}
 
export default ElementLike;