<commit_before>/* global HTMLTemplateElement, HTMLImports, MutationObserver */

window.addEventListener('HTMLImportsLoaded', function () {
  importTemplates();
});

// NOTE: HTML Imports polyfill must come before we include `vr-markup`.
var hasImports = document.createElement('link').import;
if (!hasImports) {
  require('../lib/vendor/HTMLImports');
}

var VRMarkup = require('@mozvr/vr-markup');

var VRObject = VRMarkup.VRObject;
var VRUtils = VRMarkup.utils;

var internals = {};

var utils = {
  '$': function (sel) {
    if (sel && typeof sel === 'string') {
      sel = document.querySelector(sel);
    }
    return sel;
  },
  '$$': function (sel, parent) {
    if (sel && typeof sel === 'string') {
      sel = (parent || document).querySelectorAll(sel);
    }
    return Array.prototype.slice.call(sel);
  }
};

/**
 * ES6-style string formatting
 *
 * > format('${0}', ['zzz'])
 * "zzz"
 *
 * > format('${0}{1}', 1, 2)
 * "12"
 *
 * > format('${x}', {x: 1})
 * "1"
 *
 * > format('my favourite color is ${color=blue}', {x: 1})
 * "my favourite color is blue"
 *
 */
utils.format = (function () {
  var re = /\$?\{\s*([^}= ]+)(\s*=\s*(.+))?\s*\}/g;
  return function (s, args) {
    if (!s) { throw new Error('Format string is empty!'); }
    if (!args) { return; }
    if (!(args instanceof Array || args instanceof Object)) {
      args = Array.prototype.slice.call(arguments, 1);
    }
    return s.replace(re, function (_, name, rhs, defaultVal) {
      var val = args[name];

      if (typeof val === 'undefined') {
        return (defaultVal || '?').trim().replace(/^["']|["']$/g, '');
      }

      return (val || '?').trim().replace(/^["']|["']$/g, '');
    });
  };
})();

utils.template = function (s) {
  if (!s) { throw new Error('Template string is empty!'); }
  return function (args) { return utils.format(s, args); };
};

function importTemplates () {
  if (HTMLImports && !HTMLImports.useNative) {
    Object.keys(HTMLImports.importer.documents).forEach(function (key) {
      var doc = HTMLImports.importer.documents[key];
      utils.$$('template[is="vr-template"]', doc).forEach(function (template) {
        var templateEl = document.importNode(template, true);
        document.body.appendChild(templateEl);
      });
    });
  }
}

document.addEventListener('vr-markup-ready', function () {
  internals.vrMarkupReady = true;
});

module.exports = document.registerElement(
  'vr-template',
  {
    extends: 'template',
    prototype: Object.create(
      HTMLTemplateElement.prototype,
      {
        createdCallback: {
          value: function () {
            var self = this;
            self.placeholders = [];
            if (self.ownerDocument !== document) {
              setTimeout(function () {
                document.body.appendChild(self);
              });
            }
          }
        },

        attributeBlacklist: {
          value: {
            name: true,
            id: true
          }
        },

        attachedCallback: {
          value: function () {
            this.sceneEl = utils.$('vr-scene');
            if (internals.vrMarkupReady) {
              this.inject();
              return;
            }
            document.addEventListener('vr-markup-ready', this.attachEventListeners.bind(this));
            this.addEventListener('loaded', this.inject.bind(this));
          }
        },

        detachedCallback: {
          value: function () {
            var self = this;
            self.removeTemplateListener();<commit_msg>add comment about fixing native HTML Imports for Chrome<commit_after>/* global HTMLTemplateElement, HTMLImports, MutationObserver */

window.addEventListener('HTMLImportsLoaded', function () {
  importTemplates();
});

// NOTE: HTML Imports polyfill must come before we include `vr-markup`.
var hasImports = document.createElement('link').import;
if (!hasImports) {
  require('../lib/vendor/HTMLImports');
}

var VRMarkup = require('@mozvr/vr-markup');

var VRObject = VRMarkup.VRObject;
var VRUtils = VRMarkup.utils;

var internals = {};

var utils = {
  '$': function (sel) {
    if (sel && typeof sel === 'string') {
      sel = document.querySelector(sel);
    }
    return sel;
  },
  '$$': function (sel, parent) {
    if (sel && typeof sel === 'string') {
      sel = (parent || document).querySelectorAll(sel);
    }
    return Array.prototype.slice.call(sel);
  }
};

/**
 * ES6-style string formatting
 *
 * > format('${0}', ['zzz'])
 * "zzz"
 *
 * > format('${0}{1}', 1, 2)
 * "12"
 *
 * > format('${x}', {x: 1})
 * "1"
 *
 * > format('my favourite color is ${color=blue}', {x: 1})
 * "my favourite color is blue"
 *
 */
utils.format = (function () {
  var re = /\$?\{\s*([^}= ]+)(\s*=\s*(.+))?\s*\}/g;
  return function (s, args) {
    if (!s) { throw new Error('Format string is empty!'); }
    if (!args) { return; }
    if (!(args instanceof Array || args instanceof Object)) {
      args = Array.prototype.slice.call(arguments, 1);
    }
    return s.replace(re, function (_, name, rhs, defaultVal) {
      var val = args[name];

      if (typeof val === 'undefined') {
        return (defaultVal || '?').trim().replace(/^["']|["']$/g, '');
      }

      return (val || '?').trim().replace(/^["']|["']$/g, '');
    });
  };
})();

utils.template = function (s) {
  if (!s) { throw new Error('Template string is empty!'); }
  return function (args) { return utils.format(s, args); };
};

function importTemplates () {
  if (HTMLImports && !HTMLImports.useNative) {
    Object.keys(HTMLImports.importer.documents).forEach(function (key) {
      var doc = HTMLImports.importer.documents[key];
      utils.$$('template[is="vr-template"]', doc).forEach(function (template) {
        var templateEl = document.importNode(template, true);
        document.body.appendChild(templateEl);
      });
    });
  }
}

document.addEventListener('vr-markup-ready', function () {
  internals.vrMarkupReady = true;
});

module.exports = document.registerElement(
  'vr-template',
  {
    extends: 'template',
    prototype: Object.create(
      HTMLTemplateElement.prototype,
      {
        createdCallback: {
          value: function () {
            var self = this;
            self.placeholders = [];
            if (self.ownerDocument !== document) {
              // TODO: Fix native HTML Imports for Chrome.
              // See https://github.com/MozVR/vr-components/issues/53
              setTimeout(function () {
                document.body.appendChild(self);
              });
            }
          }
        },

        attributeBlacklist: {
          value: {
            name: true,
            id: true
          }
        },

        attachedCallback: {
          value: function () {
            this.sceneEl = utils.$('vr-scene');
            if (internals.vrMarkupReady) {
              this.inject();
              return;
            }
            document.addEventListener('vr-markup-ready', this.attachEventListeners.bind(this));
            this.addEventListener('loaded', this.inject.bind(this));
          }
        },

        detachedCallback: {
          value: function () {
            var self = this;
            self.removeTemplateListener();
{'fraction_non_alphanumeric': 0.1758654247184763, 'fraction_numerical': 0.0036146253301821214, 'mean_word_length': 2.411095305832148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '1026388', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2167, 'n_words': 460}