<commit_before>'use strict';
var VDOM = {
  h: require('virtual-dom').h,
  diff: require('virtual-dom/diff'),
  patch: require('virtual-dom/patch')
};
var Rx = require('rx');
var DataFlowNode = require('./data-flow-node');
var CustomElements = require('./custom-elements');

function isElement(o) {
  return (
    typeof HTMLElement === 'object' ?
      o instanceof HTMLElement || o instanceof DocumentFragment : //DOM2
      o && typeof o === 'object' && o !== null &&
        (o.nodeType === 1 || o.nodeType === 11) &&
        typeof o.nodeName === 'string'
  );
}

function DOMUser(container) {
  // Find and prepare the container
  this._domContainer = (typeof container === 'string') ?
    document.querySelector(container) :
    container;
  this._rootNode$ = new Rx.ReplaySubject(1);
  // Check pre-conditions
  if (typeof container === 'string' && this._domContainer === null) {
    throw new Error('Cannot render into unknown element \'' + container + '\'');
  } else if (!isElement(this._domContainer)) {
    throw new Error('Given container is not a DOM element neither a selector string.');
  }
  // Create node
  var self = this;
  DataFlowNode.call(this, function injectIntoDOMUser(view) {
    return self._renderEvery(view.get('vtree$'));
  });
}

DOMUser.prototype = Object.create(DataFlowNode.prototype);

DOMUser.prototype._renderEvery = function renderEvery(vtree$) {
  var self = this;
  var rootNode;
  if (self._domContainer.cycleCustomElementProperties) {
    rootNode = self._domContainer;
  } else {
    rootNode = document.createElement('div');
    self._domContainer.innerHTML = '';
    self._domContainer.appendChild(rootNode);
  }
  self._rootNode$.onNext(rootNode);
  return vtree$.startWith(VDOM.h())
    .map(function renderingPreprocessing(vtree) {
      return self._replaceCustomElements(vtree);
    })
    .pairwise()
    .subscribe(function renderDiffAndPatch(pair) {
      try {
        var oldVTree = pair[0];
        var newVTree = pair[1];
        if (typeof newVTree === 'undefined') {
          return;
        }
        rootNode = VDOM.patch(rootNode, VDOM.diff(oldVTree, newVTree));
        self._rootNode$.onNext(rootNode);
      } catch (err) {
        console.error(err);
      }
    });
};

DOMUser.prototype._replaceCustomElements = function replaceCustomElements(vtree) {
  // Silently ignore corner cases
  if (!vtree || !DOMUser._customElements || vtree.type === 'VirtualText') {
    return vtree;
  }
  var tagName = (vtree.tagName || '').toUpperCase();
  // Replace vtree itself
  if (tagName && DOMUser._customElements.hasOwnProperty(tagName)) {
    return new DOMUser._customElements[tagName](vtree);
  }
  // Or replace children recursively
  if (Array.isArray(vtree.children)) {
    for (var i = vtree.children.length - 1; i >= 0; i--) {
      vtree.children[i] = this._replaceCustomElements(vtree.children[i]);
    }
  }
  return vtree;
};

DOMUser.prototype.event$ = function event$(selector, eventName) {
  if (typeof selector !== 'string') {<commit_msg>Persistent class names for root element in DOMUser<commit_after>'use strict';
var VDOM = {
  h: require('virtual-dom').h,
  diff: require('virtual-dom/diff'),
  patch: require('virtual-dom/patch')
};
var Rx = require('rx');
var DataFlowNode = require('./data-flow-node');
var CustomElements = require('./custom-elements');

function isElement(o) {
  return (
    typeof HTMLElement === 'object' ?
      o instanceof HTMLElement || o instanceof DocumentFragment : //DOM2
      o && typeof o === 'object' && o !== null &&
        (o.nodeType === 1 || o.nodeType === 11) &&
        typeof o.nodeName === 'string'
  );
}

function DOMUser(container) {
  // Find and prepare the container
  this._domContainer = (typeof container === 'string') ?
    document.querySelector(container) :
    container;
  this._originalClassName = this._domContainer.className;
  this._rootNode$ = new Rx.ReplaySubject(1);
  // Check pre-conditions
  if (typeof container === 'string' && this._domContainer === null) {
    throw new Error('Cannot render into unknown element \'' + container + '\'');
  } else if (!isElement(this._domContainer)) {
    throw new Error('Given container is not a DOM element neither a selector string.');
  }
  // Create node
  var self = this;
  DataFlowNode.call(this, function injectIntoDOMUser(view) {
    return self._renderEvery(view.get('vtree$'));
  });
}

DOMUser.prototype = Object.create(DataFlowNode.prototype);

DOMUser.prototype._renderEvery = function renderEvery(vtree$) {
  var self = this;
  var rootNode;
  if (self._domContainer.cycleCustomElementProperties) {
    rootNode = self._domContainer;
  } else {
    rootNode = document.createElement('div');
    self._domContainer.innerHTML = '';
    self._domContainer.appendChild(rootNode);
  }
  self._rootNode$.onNext(rootNode);
  return vtree$.startWith(VDOM.h())
    .map(function renderingPreprocessing(vtree) {
      return self._replaceCustomElements(vtree);
    })
    .pairwise()
    .subscribe(function renderDiffAndPatch(pair) {
      try {
        var oldVTree = pair[0];
        var newVTree = pair[1];
        if (typeof newVTree === 'undefined') {
          return;
        }
        rootNode = VDOM.patch(rootNode, VDOM.diff(oldVTree, newVTree));
        self._fixClassName();
        self._rootNode$.onNext(rootNode);
      } catch (err) {
        console.error(err);
      }
    });
};

DOMUser.prototype._fixClassName = function fixClassName() {
  this._domContainer.className = this._domContainer.className.replace(
    this._originalClassName.trim(), ''
  );
  this._domContainer.className += ' ' + this._originalClassName;
  this._domContainer.className = this._domContainer.className
    .trim()
    .split(/\s+/)
    .join(' ');
}

DOMUser.prototype._replaceCustomElements = function replaceCustomElements(vtree) {
  // Silently ignore corner cases
  if (!vtree || !DOMUser._customElements || vtree.type === 'VirtualText') {
    return vtree;
  }
  var tagName = (vtree.tagName || '').toUpperCase();
  // Replace vtree itself
  if (tagName && DOMUser._customElements.hasOwnProperty(tagName)) {
    return new DOMUser._customElements[tagName](vtree);
  }
  // Or replace children recursively
  if (Array.isArray(vtree.children)) {
    for (var i = vtree.children.length - 1; i >= 0; i--) {
      vtree.children[i] = this._replaceCustomElements(vtree.children[i]);
    }
  }
  return vtree;
};

DOMUser.prototype.event$ = function event$(selector, eventName) {
  if (typeof selector !== 'string') {
{'fraction_non_alphanumeric': 0.14592933947772657, 'fraction_numerical': 0.0027649769585253456, 'mean_word_length': 3.5090027700831024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6563300', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1963, 'n_words': 433}