All files / src/NodeLike MChildNodeLike.ts

4.76% Statements 3/63
0% Branches 0/32
12.5% Functions 1/8
4.76% Lines 3/63
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    2x                                                                                                                                                                                                                                             4x     2x
import IChildNodeLike                from './IChildNodeLike';
import INonDocumentTypeChildNodeLike from './INonDocumentTypeChildNodeLike';
import isIChildNodeLike              from '../TypeGuards/isIChildNodeLike';
import TConstructor                  from '../TypeAliases/TConstructor';
function MChildNodeLike<T extends TConstructor<object>>(Base: T) {
  class MChildNodeLike extends Base {
    before(...contents: Array<IChildNodeLike | string>): void {
      if (!isIChildNodeLike(this)) {
        throw new Error('The object implementing the MChildNodeLike mixin ' +
                        'does not pass the isIChildNodeLike type guard.');
      }
 
      const _this: IChildNodeLike = this;
      const parent = _this.parentNode;
      if (!parent) {
        throw new Error('The node has no parent.');
      }
 
      const ownerDocument = _this.ownerDocument;
      if (!ownerDocument) {
        throw new Error('The node has no owner document.');
      }
 
      let childNodes = parent.childNodes;
      let index = childNodes.indexOf(_this);
      if (index === -1) {
        throw new Error('The node on which before was called is not a ' +
                        'member of its parent\'s childNodes.');
      }
 
      contents.forEach((value: IChildNodeLike | string) => {
        let newNode = value;
        if (typeof value === 'string') {
          newNode = ownerDocument.createTextNode(value);
        }
 
        newNode = <IChildNodeLike>newNode;
        parent.insertBefore(newNode, _this);
      });
    }
 
    after(...contents: Array<INonDocumentTypeChildNodeLike | string>): void {
      if (!isIChildNodeLike(this)) {
        throw new Error('The object implementing the MChildNodeLike mixin ' +
                        'does not pass the isIChildNodeLike type guard.');
      }
 
      const _this: IChildNodeLike = this;
 
      const parent = _this.parentNode;
      if (!parent) {
        throw new Error('The node has no parent.');
      }
 
      const ownerDocument = _this.ownerDocument;
      if (!ownerDocument) {
        throw new Error('The node has no owner document.');
      }
 
      contents.forEach((value: INonDocumentTypeChildNodeLike | string) => {        
        let newNode = value;
        if (typeof value === 'string') {
          newNode = ownerDocument.createTextNode(value);
        }
 
        newNode = <INonDocumentTypeChildNodeLike>newNode;
        parent.insertBefore(newNode, _this);
      });
    }
 
    replaceWith(...contents: Array<IChildNodeLike | string>) {
      if (!isIChildNodeLike(this)) {
        throw new Error('The object implementing the MChildNodeLike mixin ' +
                        'does not pass the isIChildNodeLike type guard.');
      }
 
      const _this: IChildNodeLike = this;
 
      const parent = _this.parentNode;
      if (!parent) {
        throw new Error('The node has no parent.');
      }
 
      const ownerDocument = _this.ownerDocument;
      if (!ownerDocument) {
        throw new Error('The node has no owner document.');
      }
 
      const sibling = _this.nextSibling;
      parent.removeChild(_this);
      contents.forEach((value: IChildNodeLike | string) => {
        let newNode = value;
        if (typeof value === 'string') {
          newNode = ownerDocument.createTextNode(value);
        }
 
        newNode = <IChildNodeLike>newNode;
        if (sibling) {
          parent.insertBefore(sibling, newNode);
        } else {
          parent.appendChild(newNode);
        }
      });
    }
 
    remove() {
      if (!isIChildNodeLike(this)) {
        throw new Error('The object implementing the MChildNodeLike mixin ' +
                        'does not pass the isIChildNodeLike type guard.');
      }
 
      const _this: IChildNodeLike = this;
      const parent = _this.parentNode;
      if (!parent) {
        throw new Error('The node has no parent.');
      }
 
      parent.removeChild(_this);
    }
  }
 
  return MChildNodeLike;
}
 
export default MChildNodeLike;