All files / src/modules nodeFactory.ts

4% Statements 2/50
0% Branches 0/36
0% Functions 0/3
4% Lines 2/50
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        1x                                                                                                                                           1x
import IAbstractSyntaxNode       from '../Parser/IAbstractSyntaxNode';
import IDocumentLike             from '../NodeLike/ParentNodeLike/DocumentLike/IDocumentLike';
import INonDocumentTypeChildNode from '../NodeLike/INonDocumentTypeChildNodeLike';
import TAbstractSyntaxContent    from '../TypeAliases/TAbstractSyntaxContent';
import TextLike                  from '../NodeLike/CharacterDataLike/TextLike/TextLike';
function nodeFactory(value: TAbstractSyntaxContent, document: IDocumentLike): INonDocumentTypeChildNode {
  if (typeof value === 'string') {
    return new TextLike(value, document);
  } else if (value.type === 'variable') {
    const element = document.createElement('tw-variable');
    const key = 'data-name';
    const name = value.name ? value.name : 'UNKNOWN';
    element.setAttribute(key, name);
    return element;
  } else if (value.type === 'string') {
    const element = document.createElement('tw-string');
    const key = 'data-subtype';
    const subtype = value.subtype ? value.subtype : 'UNKNOWN';
    element.setAttribute(key, subtype);
    element.textContent = <string>value.value;
    return element;
  } else if (value.type === 'number') {
    const element = document.createElement('tw-number');
    element.textContent = <string>value.value;
  } else if (value.type === 'invocation' ||
    value.type === 'element' ||
    value.type === 'link')
  {
    let tagName;
    if (value.type === 'invocation') {
      tagName = 'tw-invocation';
    } else if (value.type === 'link') {
      tagName = 'tw-link';
    } else {
      tagName = value.tagName ? value.tagName : 'tw-unknown-element';
    }
 
    const element = document.createElement(tagName);
    const key = 'data-subtype';
    const subtype = value.subtype ? value.subtype : 'UNKNOWN';
    element.setAttribute(key, subtype);
    if (value.type === 'invocation') {
      const key = 'data-name';
      element.setAttribute(key, value.name);
    }
 
    const args = value.arguments;
    const children = value.children;
    const nodes = args.map((child: IAbstractSyntaxNode) => {
      return nodeFactory(child, document);
    });
 
    if (children && typeof children === 'object' && children.length >= 1) {
      const body = document.createElement('tw-invocation-body');
      const childNodes = children.map((child) => {
        return nodeFactory(child, document);
      });
 
      body.append(...childNodes);
      nodes.push(body);
    }
 
    element.append(...nodes);
 
    return element;
  } else if (value.type === 'comment') {
    return document.createComment(<string>value.value);
  } else if (value.type === 'processingInstruction') {
    return document.createProcessingInstruction(value.target, value.data);
  }
    
  throw new Error('No condition was matched for constructing a node.');
}
 
export default nodeFactory;