<commit_before>/**
 * We.js client side lib
 */

(function (window) {

if ($.timepicker) {
  $.datepicker.setDefaults($.timepicker.regional[window.WE_BOOTSTRAP_CONFIG.locale]);
  $.timepicker.setDefaults($.timepicker.regional[window.WE_BOOTSTRAP_CONFIG.locale]);
}

var we = {
  autoInitialize: true,
  config: {},
  element: null,
  isAdmin: false,
  initialize: function initialize(cb) {
    if (!cb) cb = function(){};

    if (window.WE_BOOTSTRAP_CONFIG) {
      this.config = window.WE_BOOTSTRAP_CONFIG;
    }

    this.element = $('body');

    var regions = document.querySelectorAll('div[data-we-region]');

    for (var i = 0; i < regions.length; i++) {
      we.structure.regions[regions[i].id] = window.$(regions[i]);
    }

    this.setElementEvents();
    //this.handlerErrorMessage();

    if (location.pathname.substring(0, 6) === '/admin') this.isAdmin = true;

    if (this.config.widgetContext) {
      // add current page headers for ajax requests
      $.ajaxSetup({
        headers: {
          'wejs-context': this.config.widgetContext,
          'wejs-theme': this.config.theme
        }
      });
    }

    cb();
  },
  setElementEvents: function setElementEvents() {
    var self = this;
    // we.js button event
    $('body').on('click', 'button[we-action=event]', we.handleWeElementEvent);
    $('body').on('click', 'a[we-action=event]', we.handleWeElementEvent);

    // - form submit feature
    $('body').on('submit', 'form[we-submit=ajax]', function (event) {
      event.preventDefault();

      var form = $(this);
      var formData = {};
      form.serializeArray().forEach(function (d) {
        formData[d.name] = d.value;
      });

      var url = form.attr('action');

      $.ajax({
        url: url + '?responseType=json',
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(formData)
      }).then(function (r) {
        we.events.emit('model-update', 'widget', r.widget);
      });
    });

    we.events.on('model-update', self.model.liveUpdate);
    we.events.on('model-delete', self.model.liveDelete);
  },
  handleWeElementEvent: function (e) {
    e.preventDefault();

    var element = $(e.target);
    var weEvent = element.attr('event');

    if (!weEvent)
      return console.warn('data-event attribute is required for data-event handler', e);
    we.events.emit(weEvent, element, e);
  },

  get: function(obj, path, def) {
    /**
     * Retrieve nested item from object/array
     * @param {Object|Array} obj
     * @param {String} path dot separated
     * @param {*} def default value ( if result undefined )
     * @returns {*}
     */
    var i, len;
    for(i = 0,path = path.split('.'), len = path.length; i < len; i++){
      if(!obj || typeof obj !== 'object') return def;
      obj = obj[path[i]];
    }
    if(obj === undefined) return def;
    return obj;
  },
  utils: {}
};

we.structure = {
  addWidgetModalFormId: '#AddWidgetFormModal',
  updateWidgetModalFormId: '#updateWidgetFormModal',
  sortWidgetModalFormId: '#sortWidgetFormModal',

  showLayoutEditor: function showLayoutEditor() {
    $('#we-layout-start-edit-btn').hide();
    $('#we-layout-stop-edit-btn').show();
    $('body').addClass('we-editing-layout');
  },
  hideLayoutEditor: function hideLayoutEditor() {
    $('#we-layout-start-edit-btn').show();<commit_msg>add we.utils.sanitize for sanitize user added html<commit_after>/**
 * We.js client side lib
 */

(function (window) {

if ($.timepicker) {
  $.datepicker.setDefaults($.timepicker.regional[window.WE_BOOTSTRAP_CONFIG.locale]);
  $.timepicker.setDefaults($.timepicker.regional[window.WE_BOOTSTRAP_CONFIG.locale]);
}

var we = {
  autoInitialize: true,
  config: {},
  element: null,
  isAdmin: false,
  initialize: function initialize(cb) {
    if (!cb) cb = function(){};

    if (window.WE_BOOTSTRAP_CONFIG) {
      this.config = window.WE_BOOTSTRAP_CONFIG;
    }

    this.element = $('body');

    var regions = document.querySelectorAll('div[data-we-region]');

    for (var i = 0; i < regions.length; i++) {
      we.structure.regions[regions[i].id] = window.$(regions[i]);
    }

    this.setElementEvents();
    //this.handlerErrorMessage();

    if (location.pathname.substring(0, 6) === '/admin') this.isAdmin = true;

    if (this.config.widgetContext) {
      // add current page headers for ajax requests
      $.ajaxSetup({
        headers: {
          'wejs-context': this.config.widgetContext,
          'wejs-theme': this.config.theme
        }
      });
    }

    cb();
  },
  setElementEvents: function setElementEvents() {
    var self = this;
    // we.js button event
    $('body').on('click', 'button[we-action=event]', we.handleWeElementEvent);
    $('body').on('click', 'a[we-action=event]', we.handleWeElementEvent);

    // - form submit feature
    $('body').on('submit', 'form[we-submit=ajax]', function (event) {
      event.preventDefault();

      var form = $(this);
      var formData = {};
      form.serializeArray().forEach(function (d) {
        formData[d.name] = d.value;
      });

      var url = form.attr('action');

      $.ajax({
        url: url + '?responseType=json',
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(formData)
      }).then(function (r) {
        we.events.emit('model-update', 'widget', r.widget);
      });
    });

    we.events.on('model-update', self.model.liveUpdate);
    we.events.on('model-delete', self.model.liveDelete);
  },
  handleWeElementEvent: function (e) {
    e.preventDefault();

    var element = $(e.target);
    var weEvent = element.attr('event');

    if (!weEvent)
      return console.warn('data-event attribute is required for data-event handler', e);
    we.events.emit(weEvent, element, e);
  },
  get: function(obj, path, def) {
    /**
     * Retrieve nested item from object/array
     * @param {Object|Array} obj
     * @param {String} path dot separated
     * @param {*} def default value ( if result undefined )
     * @returns {*}
     */
    var i, len;
    for(i = 0,path = path.split('.'), len = path.length; i < len; i++){
      if(!obj || typeof obj !== 'object') return def;
      obj = obj[path[i]];
    }
    if(obj === undefined) return def;
    return obj;
  },
  utils: {
    /**
     * Sanitize user input htmls, use before add any user text content
     *
     * @param  {String} str
     * @return {String}     Safe text to add in html
     */
    sanitize: function sanitize(str) {
      return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    }
  }
};

we.structure = {
  addWidgetModalFormId: '#AddWidgetFormModal',
  updateWidgetModalFormId: '#updateWidgetFormModal',
  sortWidgetModalFormId: '#sortWidgetFormModal',

  showLayoutEditor: function showLayoutEditor() {
    $('#we-layout-start-edit-btn').hide();
    $('#we-layout-stop-edit-btn').show();
    $('body').addClass('we-editing-layout');
  },
  hideLayoutEditor: function hideLayoutEditor() {
    $('#we-layout-start-edit-btn').show();
{'fraction_non_alphanumeric': 0.1748740906547286, 'fraction_numerical': 0.0013989927252378287, 'mean_word_length': 2.932343234323432, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, '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': '4904407', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2290, 'n_words': 472}