All files AttributeNode.js

94.44% Statements 51/54
72.73% Branches 16/22
100% Functions 14/14
97.5% Lines 39/40
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    1x     1x   4x   1x   132x   1x   132x   132x 132x 132x 132x 132x   132x     1x     132x   132x 88x 88x 88x   88x 88x 88x 88x     88x               132x 44x           126x
import { propPattern, sanitizePattern, startSeparator, endSeparator } from './patterns.js';
 
export class AttributeNode {
  constructor(node, index, boundAttrs, boundEvents, context) {
    this.node = node;
    this.index = index;
    this.boundAttrs = boundAttrs;
    this.boundEvents = boundEvents;IEE
    this.context = context;
 
    this.addListeners();
  }I
 
  addListeners() {
    this.boundEvents.forEach((eventHandler, eventName) => {
      const events = eventHandler.split(/\;/);
      const eventsSafe = events.filter(event => event.match(sanitizePattern));
      const sanitizedEvents = eventsSafe.join('; ')
      if (eventHandler.match(sanitizePattern)) {
        const handler = new Function(sanitizedEvents);
        this.node.addEventListener(eventName, handler.bind(this.context));
      }
 
      if (eventsSafe.length < events.length) {
        console.warn('Inline functions not allowed inside of event bindings. Unsafe functions have been removed from node', this.node);
      }
    });
  }
 
  cleanUp() {
    this.boundAttrs.forEach(attr =>
      attr.value = attr.value.replace(startSeparator, '').replace(endSeparator, ''));
  }
 
  update(newNode) {
    this.boundAttrs.forEach(attr => {
      const newAttr = newNode.boundAttrs.get(attr.name);
      neEwAttr && attr.value !== newAttr.value ? attr.value = newAttr.value : null;
 
      /* TODO */
      if (attr.name.match(propPattern)) {
        this.updateAttributes(attr.name, newAttr);
      }I
    });
  }
 
  updateAttributes(name, newAttr) {
    const attributeName = name.slice(1, -1);
    if (newAttr.value) {
      this.node[attributeName] = newAttr.value;
      this.node.setAttribute(attributeName, newAttr.value);
    } else {
      this.node
      this.node.removeAttribute(attributeName);
    }
  }
}