<commit_before>test(
  'Runtime Size Test',
  [
    'ephox.katamari.api.Arr',
    'ephox.katamari.api.Fun',
    'ephox.sand.api.PlatformDetection',
    'ephox.snooker.resize.RuntimeSize',
    'ephox.sugar.api.dom.Insert',
    'ephox.sugar.api.dom.Remove',
    'ephox.sugar.api.node.Body',
    'ephox.sugar.api.node.Element',
    'ephox.sugar.api.properties.Attr',
    'ephox.sugar.api.properties.Css',
    'ephox.sugar.api.properties.Html',
    'ephox.sugar.api.search.SelectorFilter',
    'global!assert'
  ],
  function (Arr, Fun, PlatformDetection, RuntimeSize, Insert, Remove, Body, Element, Attr, Css, Html, SelectorFilter, assert) {
    var platform = PlatformDetection.detect();

    var random = function (min, max) {
      return Math.round(Math.random() * (max - min) + min);
    };

    var getOuterWidth = function (elm) {
      return Math.round(elm.dom().getBoundingClientRect().width);
    };

    var getOuterHeight = function (elm) {
      return Math.round(elm.dom().getBoundingClientRect().height);
    };

    var measureCells = function (getSize, table) {
      return Arr.map(SelectorFilter.descendants(table, 'td'), getSize);
    };

    var measureTable = function (table, getSize) {
      return {
        total: getSize(table),
        cells: measureCells(getSize, table)
      };
    };

    var setWidth = function (table, value) {
      Css.set(table, 'width', value);
    };

    var setHeight = function (table, value) {
      Css.set(table, 'height', value);
    };

    var resizeTableBy = function (table, setSize, tableInfo, delta) {
      setSize(table, '');
      Arr.map(SelectorFilter.descendants(table, 'td'), function (cell, i) {
        setSize(cell, (tableInfo.cells[i] + delta) + 'px');
      });
    };

    var fuzzyAssertEq = function (a, b, msg) {
      // Sometimes the widths of the cells are 1 px off due to rounding but the total table width is never off
      assert.eq(true, Math.abs(a - b) <= 1, msg);
    };

    var assertSize = function (s1, table, getOuterSize, message) {
      var s2 = measureTable(table, getOuterSize);
      var html = Html.getOuter(table);
      var cellAssertEq = platform.browser.isIE() || platform.browser.isEdge() ? fuzzyAssertEq : assert.eq;

      assert.eq(s1.total, s2.total, message + ', expected table size: ' + s1.total + ', actual: ' + s2.total + ', table: ' + html);

      Arr.each(s1.cells, function (cz1, i) {
        var cz2 = s2.cells[i];
        cellAssertEq(cz1, cz2, message + ', expected cell size: ' + cz1 + ', actual: ' + cz2 + ', table: ' + html);
      });
    };

    var randomValue = function (values) {
      var idx = random(0, values.length - 1);
      return values[idx];
    };

    var randomSize = function (min, max) {
      var n = random(min, max);
      return n > 0 ? n + 'px' : '0';
    };

    var randomBorder = function (min, max, color) {
      var n = random(min, max);
      return n > 0 ? n + 'px solid ' + color : '0';
    };

    var createTableH = function () {
      var table = Element.fromTag('table');
      var tbody = Element.fromTag('tbody');
      var tr = Element.fromTag('tr');

      Attr.set(table, 'border', '1');

      Css.setAll(table, {
        'border-collapse': 'collapse',
        'border-left': randomBorder(0, 5, 'red'),
        'border-right': randomBorder(0, 5, 'red'),
        'width': randomSize(100, 1000)
      });

      var cells = Arr.range(random(1, 5), function (_) {
        var cell = Element.fromTag('td');

        Css.setAll(cell, {
          'width': randomSize(1, 100),
          'height': '10px',
          'padding-left': randomSize(0, 5),
          'padding-right': randomSize(0, 5),
          'border-left': randomBorder(0, 5, 'green'),
          'border-right': randomBorder(0, 5, 'green')
        });

        var content = Element.fromTag('div');

        Css.setAll(content, {
          'width': randomSize(1, 200),
          'height': '10px'
        });

        Insert.append(cell, content);

        return cell;
      });

      Insert.append(table, tbody);
      Insert.append(tbody, tr);

      Arr.each(cells, function (cell) {
        Insert.append(tr, cell);
      });

      Insert.append(Body.body(), table);

      return table;
    };

    var createTableV = function () {
      var table = Element.fromTag('table');
      var tbody = Element.fromTag('tbody');

      Attr.set(table, 'border', '1');

      Css.setAll(table, {
        'border-collapse': 'collapse',
        'border-top': randomBorder(0, 5, 'red'),
        'border-bottom': randomBorder(0, 5, 'red'),
        'height': randomSize(100, 1000)
      });

      var rows = Arr.range(random(1, 5), function (_) {
        var row = Element.fromTag('tr');
        var cell = Element.fromTag('td');

        Css.setAll(cell, {
          'width': '10px',
          'height': randomSize(1, 100),
          'box-sizing': randomValue(['content-box', 'border-box']),
          'padding-top': randomSize(0, 5),
          'padding-bottom': randomSize(0, 5),
          'border-top': randomBorder(0, 5, 'green'),
          'border-bottom': randomBorder(0, 5, 'green')
        });

        var content = Element.fromTag('div');

        Css.setAll(content, {<commit_msg>TINY-1399: added cellspacing/cellpadding to test for size<commit_after>test(
  'Runtime Size Test',
  [
    'ephox.katamari.api.Arr',
    'ephox.katamari.api.Fun',
    'ephox.sand.api.PlatformDetection',
    'ephox.snooker.resize.RuntimeSize',
    'ephox.sugar.api.dom.Insert',
    'ephox.sugar.api.dom.Remove',
    'ephox.sugar.api.node.Body',
    'ephox.sugar.api.node.Element',
    'ephox.sugar.api.properties.Attr',
    'ephox.sugar.api.properties.Css',
    'ephox.sugar.api.properties.Html',
    'ephox.sugar.api.search.SelectorFilter',
    'global!assert'
  ],
  function (Arr, Fun, PlatformDetection, RuntimeSize, Insert, Remove, Body, Element, Attr, Css, Html, SelectorFilter, assert) {
    var platform = PlatformDetection.detect();

    var random = function (min, max) {
      return Math.round(Math.random() * (max - min) + min);
    };

    var getOuterWidth = function (elm) {
      return Math.round(elm.dom().getBoundingClientRect().width);
    };

    var getOuterHeight = function (elm) {
      return Math.round(elm.dom().getBoundingClientRect().height);
    };

    var measureCells = function (getSize, table) {
      return Arr.map(SelectorFilter.descendants(table, 'td'), getSize);
    };

    var measureTable = function (table, getSize) {
      return {
        total: getSize(table),
        cells: measureCells(getSize, table)
      };
    };

    var setWidth = function (table, value) {
      Css.set(table, 'width', value);
    };

    var setHeight = function (table, value) {
      Css.set(table, 'height', value);
    };

    var resizeTableBy = function (table, setSize, tableInfo, delta) {
      setSize(table, '');
      Arr.map(SelectorFilter.descendants(table, 'td'), function (cell, i) {
        setSize(cell, (tableInfo.cells[i] + delta) + 'px');
      });
    };

    var fuzzyAssertEq = function (a, b, msg) {
      // Sometimes the widths of the cells are 1 px off due to rounding but the total table width is never off
      assert.eq(true, Math.abs(a - b) <= 1, msg);
    };

    var assertSize = function (s1, table, getOuterSize, message) {
      var s2 = measureTable(table, getOuterSize);
      var html = Html.getOuter(table);
      var cellAssertEq = platform.browser.isIE() || platform.browser.isEdge() ? fuzzyAssertEq : assert.eq;

      assert.eq(s1.total, s2.total, message + ', expected table size: ' + s1.total + ', actual: ' + s2.total + ', table: ' + html);

      Arr.each(s1.cells, function (cz1, i) {
        var cz2 = s2.cells[i];
        cellAssertEq(cz1, cz2, message + ', expected cell size: ' + cz1 + ', actual: ' + cz2 + ', table: ' + html);
      });
    };

    var randomValue = function (values) {
      var idx = random(0, values.length - 1);
      return values[idx];
    };

    var randomSize = function (min, max) {
      var n = random(min, max);
      return n > 0 ? n + 'px' : '0';
    };

    var randomBorder = function (min, max, color) {
      var n = random(min, max);
      return n > 0 ? n + 'px solid ' + color : '0';
    };

    var createTableH = function () {
      var table = Element.fromTag('table');
      var tbody = Element.fromTag('tbody');
      var tr = Element.fromTag('tr');

      Attr.set(table, 'border', '1');
      Attr.set(table, 'cellpadding', random(0, 10).toString());
      Attr.set(table, 'cellspacing', random(0, 10).toString());

      Css.setAll(table, {
        'border-collapse': randomValue(['collapse', 'separate']),
        'border-left': randomBorder(0, 5, 'red'),
        'border-right': randomBorder(0, 5, 'red'),
        'width': randomSize(100, 1000)
      });

      var cells = Arr.range(random(1, 5), function (_) {
        var cell = Element.fromTag('td');

        Css.setAll(cell, {
          'width': randomSize(1, 100),
          'height': '10px',
          'padding-left': randomSize(0, 5),
          'padding-right': randomSize(0, 5),
          'border-left': randomBorder(0, 5, 'green'),
          'border-right': randomBorder(0, 5, 'green')
        });

        var content = Element.fromTag('div');

        Css.setAll(content, {
          'width': randomSize(1, 200),
          'height': '10px'
        });

        Insert.append(cell, content);

        return cell;
      });

      Insert.append(table, tbody);
      Insert.append(tbody, tr);

      Arr.each(cells, function (cell) {
        Insert.append(tr, cell);
      });

      Insert.append(Body.body(), table);

      return table;
    };

    var createTableV = function () {
      var table = Element.fromTag('table');
      var tbody = Element.fromTag('tbody');

      Attr.set(table, 'border', '1');
      Attr.set(table, 'cellpadding', random(0, 10).toString());
      Attr.set(table, 'cellspacing', random(0, 10).toString());

      Css.setAll(table, {
        'border-collapse': randomValue(['collapse', 'separate']),
        'border-top': randomBorder(0, 5, 'red'),
        'border-bottom': randomBorder(0, 5, 'red'),
        'height': randomSize(100, 1000)
      });

      var rows = Arr.range(random(1, 5), function (_) {
        var row = Element.fromTag('tr');
        var cell = Element.fromTag('td');

        Css.setAll(cell, {
          'width': '10px',
          'height': randomSize(1, 100),
          'box-sizing': randomValue(['content-box', 'border-box']),
          'padding-top': randomSize(0, 5),
          'padding-bottom': randomSize(0, 5),
          'border-top': randomBorder(0, 5, 'green'),
          'border-bottom': randomBorder(0, 5, 'green')
        });

        var content = Element.fromTag('div');

        Css.setAll(content, {
{'fraction_non_alphanumeric': 0.16598626832436444, 'fraction_numerical': 0.01707181295231026, 'mean_word_length': 2.507647250244061, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5435937', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3500, 'n_words': 786}