<commit_before>'use strict';

var _ = require('underscore');
var ToString = require('./to_string');
var Context = require('./template_context');
var logger = require('./../utils/logger');
var ractiveTypes = require('./ractive_types');

var htmlToVdom = require('./html_to_vdom');

var whitespaceOnlyRegex = /^\s*$/;
/**
 * Used to parse loose interpolators inside a opening tag
 * @param  {Object}  templates Template object
 * @param  {Context} context   current Context to evaluate in
 * @return {Object}            Parsed dictionary of attribute names/values
 */
function parseStringAttrs(templates, context) {
  var stringAttrs = '';
  var toString = new ToString();
  for (var i = 0; i < templates.length; i++) {
    toString.clear();
    render(toString, templates[i], context);
    stringAttrs += ' ' + toString.getOutput() + ' ';
  }
  if (whitespaceOnlyRegex.test(stringAttrs)) {
    return null;
  }
  var node = htmlToVdom('<div ' + stringAttrs + '></div>');
  return node.properties.attributes;
}

/**
 * Unescaped values often have HTML in them that needs to be parsed out
 * @param  {String}        value Value to check
 * @return {String|Object}       String if it contains no HTML or VTree otherwise
 */
function parseUnescapedString(value) {
  // Naive check to avoid parsing if value contains nothing HTML-ish or HTML-entity-ish
  if (value.indexOf('<') > -1 || value.indexOf('&') > -1) {
    return htmlToVdom(value);
  } else {
    return value;
  }
}

/**
 * Render the value of an element's attribute
 * @param  {String|Object} values  VTree object for the attribute value
 * @param  {Context}       context current Context to evaluate in
 * @return {String}                String value for the attribute
 */
function renderAttributeString(values, context) {
  if (typeof values === 'string') {
    return values;
  }

  var buffer = '';
  var toString = new ToString();
  for (var i = 0; i < values.length; i++) {
    toString.clear();
    render(toString, values[i], context);
    buffer += toString.getOutput();
  }

  return buffer;
}

/**
 * Recursive function to render Template into VDom format
 * @param  {Object}  toVdom      Object collecting VNodes
 * @param  {Object}  template    Template to render
 * @param  {Context} context     Value for the current rendering context
 * @param  {Object}  partials    Object map of partial templates
 * @param  {Object}  parentView  ParentView to attach widgets to
 */
function render(toVdom, template, context, partials, parentView) {
  var result, i, j, value, valueLength;
  if (typeof template === 'undefined') {
    return;
  } else if (typeof template === 'string') {
    toVdom.createObject(template);
  } else if (Context.isArray(template)) {
    for (i = 0; i < template.length; i++) {
      render(toVdom, template[i], context, partials, parentView);
    }
  } else if (template.type === 'Widget') {
    // Widgets are how we attach Views to subtrees
    // If we have a parentView, we're rendering Vdom, if not this is rendering to Dom or string, so ignore
    if (parentView) {
      // If we have a parent view, attach this as a widget so the child view will render and attach
      toVdom.createObject(new template.constructor(template.template, template.childView, context, parentView));
    } else {
      // If we aren't in the context of a view, just render out the widget without attaching the child view
      toVdom.createObject(template.template.toVdom(context));
    }
    return result;
  }

  switch (template.t) {
    // <!-- comment -->
    case ractiveTypes.COMMENT:
      var toString = new ToString();
      render(toString, template.c, context, partials, parentView);
      toVdom.createComment(toString.getOutput());
      return;
    // {{value}} or {{{value}}} or {{& value}}
    case ractiveTypes.INTERPOLATOR:
    case ractiveTypes.TRIPLE:
      value = context.lookup(Context.getInterpolatorKey(template));
      if (value != null) {
        value = value.toString();
        if (template.t === ractiveTypes.TRIPLE) {
          // TRIPLE is unescaped content, so parse that out into VDOM as needed
          value = parseUnescapedString(value);
        }
        toVdom.createObject(value);
      }
      break;

    // {{> partial}}
    case ractiveTypes.PARTIAL:
      var partialName = Context.getInterpolatorKey(template);
      result = null;
      if (partials[partialName]) {
        var partialTemplate = partials[partialName];
        if (partialTemplate.toVdom) {
          toVdom.createObject(partialTemplate.toVdom(context, parentView));
        } else {
          render(toVdom, partialTemplate, context, partials[partialName].partials || partials, parentView);
        }
      } else {
        // @TODO, perhaps return this string as the partial result so it renders to the page?
        logger.warn('Warning: no partial registered with the name ' + partialName);
      }
      break;<commit_msg>Adding checks to account for planned components integration<commit_after>'use strict';

var _ = require('underscore');
var ToString = require('./to_string');
var Context = require('./template_context');
var logger = require('./../utils/logger');
var ractiveTypes = require('./ractive_types');
var virtualDomImplementation = require('../vdom/virtual_dom_implementation');
var isWidget = virtualDomImplementation.isWidget;
var isVNode = virtualDomImplementation.isVNode;

var htmlToVdom = require('./html_to_vdom');

var whitespaceOnlyRegex = /^\s*$/;
/**
 * Used to parse loose interpolators inside a opening tag
 * @param  {Object}  templates Template object
 * @param  {Context} context   current Context to evaluate in
 * @return {Object}            Parsed dictionary of attribute names/values
 */
function parseStringAttrs(templates, context) {
  var stringAttrs = '';
  var toString = new ToString();
  for (var i = 0; i < templates.length; i++) {
    toString.clear();
    render(toString, templates[i], context);
    stringAttrs += ' ' + toString.getOutput() + ' ';
  }
  if (whitespaceOnlyRegex.test(stringAttrs)) {
    return null;
  }
  var node = htmlToVdom('<div ' + stringAttrs + '></div>');
  return node.properties.attributes;
}

/**
 * Unescaped values often have HTML in them that needs to be parsed out
 * @param  {String}        value Value to check
 * @return {String|Object}       String if it contains no HTML or VTree otherwise
 */
function parseUnescapedString(value) {
  // Naive check to avoid parsing if value contains nothing HTML-ish or HTML-entity-ish
  if (value.indexOf('<') > -1 || value.indexOf('&') > -1) {
    return htmlToVdom(value);
  } else {
    return value;
  }
}

/**
 * Render the value of an element's attribute
 * @param  {String|Object} values  VTree object for the attribute value
 * @param  {Context}       context current Context to evaluate in
 * @return {String}                String value for the attribute
 */
function renderAttributeString(values, context) {
  if (typeof values === 'string') {
    return values;
  }

  var buffer = '';
  var toString = new ToString();
  for (var i = 0; i < values.length; i++) {
    toString.clear();
    render(toString, values[i], context);
    buffer += toString.getOutput();
  }

  return buffer;
}

/**
 * Recursive function to render Template into VDom format
 * @param  {Object}  toVdom      Object collecting VNodes
 * @param  {Object}  template    Template to render
 * @param  {Context} context     Value for the current rendering context
 * @param  {Object}  partials    Object map of partial templates
 * @param  {Object}  parentView  ParentView to attach widgets to
 */
function render(toVdom, template, context, partials, parentView) {
  var result, i, j, value, valueLength;
  if (typeof template === 'undefined') {
    return;
  } else if (typeof template === 'string') {
    toVdom.createObject(template);
  } else if (Context.isArray(template)) {
    for (i = 0; i < template.length; i++) {
      render(toVdom, template[i], context, partials, parentView);
    }
  } else if (template.type === 'Widget') {
    // Widgets are how we attach Views to subtrees
    // If we have a parentView, we're rendering Vdom, if not this is rendering to Dom or string, so ignore
    if (parentView) {
      // If we have a parent view, attach this as a widget so the child view will render and attach
      toVdom.createObject(new template.constructor(template.template, template.childView, context, parentView));
    } else {
      // If we aren't in the context of a view, just render out the widget without attaching the child view
      toVdom.createObject(template.template.toVdom(context));
    }
    return result;
  }

  switch (template.t) {
    // <!-- comment -->
    case ractiveTypes.COMMENT:
      var toString = new ToString();
      render(toString, template.c, context, partials, parentView);
      toVdom.createComment(toString.getOutput());
      return;
    // {{value}} or {{{value}}} or {{& value}}
    case ractiveTypes.INTERPOLATOR:
    case ractiveTypes.TRIPLE:
      value = context.lookup(Context.getInterpolatorKey(template));
      if (value != null) {
        // If value is already a widget or vnode, add it wholesale
        if (isWidget(value) || isVNode(value)) {
          toVdom.createObject(value);
        } else {
          value = value.toString();
          if (template.t === ractiveTypes.TRIPLE) {
            // TRIPLE is unescaped content, so parse that out into VDOM as needed
            value = parseUnescapedString(value);
          }
          toVdom.createObject(value);
        }
      }
      break;

    // {{> partial}}
    case ractiveTypes.PARTIAL:
      var partialName = Context.getInterpolatorKey(template);
      result = null;
      if (partials[partialName]) {
        var partialTemplate = partials[partialName];
        if (partialTemplate.toVdom) {
          toVdom.createObject(partialTemplate.toVdom(context, parentView));
        } else {
          render(toVdom, partialTemplate, context, partials[partialName].partials || partials, parentView);
        }
      } else {
        // @TODO, perhaps return this string as the partial result so it renders to the page?
        logger.warn('Warning: no partial registered with the name ' + partialName);
      }
      break;
{'fraction_non_alphanumeric': 0.1175265775870477, 'fraction_numerical': 0.0009753242953281966, 'mean_word_length': 3.0999600159936027, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '4205314', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2911, 'n_words': 953}