diff --git a/addons/web_editor/static/src/js/backend/convert_inline.js b/addons/web_editor/static/src/js/backend/convert_inline.js index fef60d8c542..28141a8f212 100644 --- a/addons/web_editor/static/src/js/backend/convert_inline.js +++ b/addons/web_editor/static/src/js/backend/convert_inline.js @@ -144,7 +144,7 @@ function bootstrapToTable($editable) { for (const container of [...containers].filter(n => [...n.children].some(c => c.classList.contains('row')))) { // The width of the table was stored in a temporary attribute. Fetch it // for use in `_applyColspan` and remove the attribute at the end. - const containerWidth = container.getAttribute('o-temp-width'); + const containerWidth = parseFloat(container.getAttribute('o-temp-width')); // TABLE const table = _createTable(container.attributes); @@ -713,8 +713,8 @@ function fontToImg($editable) { font.style.setProperty('line-height', 'normal'); const intrinsicWidth = _getWidth(font); const intrinsicHeight = _getHeight(font); - const hPadding = width && (width - intrinsicWidth) / 2; - const vPadding = height && (height - intrinsicHeight) / 2; + const hPadding = width && intrinsicWidth && (width - intrinsicWidth) / 2; + const vPadding = height && intrinsicHeight && (height - intrinsicHeight) / 2; let padding = ''; if (hPadding || vPadding) { padding = vPadding ? vPadding + 'px ' : '0 '; @@ -1338,7 +1338,7 @@ function _getStylePropertyValue(element, propertyName) { * @returns {Number} */ function _getWidth(element) { - return parseFloat(getComputedStyle(element).width.replace('px', '')); + return parseFloat(getComputedStyle(element).width.replace('px', '')) || 0; } /** * Equivalent to JQuery's `height` method. Returns the element's visible height. @@ -1347,7 +1347,7 @@ function _getWidth(element) { * @returns {Number} */ function _getHeight(element) { - return parseFloat(getComputedStyle(element).height.replace('px', '')); + return parseFloat(getComputedStyle(element).height.replace('px', '')) || 0; } /** * Return true if the given element is hidden. diff --git a/addons/web_editor/static/tests/convert_inline_tests.js b/addons/web_editor/static/tests/convert_inline_tests.js index 8bc48aa67e7..3b13f53718f 100644 --- a/addons/web_editor/static/tests/convert_inline_tests.js +++ b/addons/web_editor/static/tests/convert_inline_tests.js @@ -2,80 +2,89 @@ import convertInline from '@web_editor/js/backend/convert_inline'; import {getGridHtml, getTableHtml, getRegularGridHtml, getRegularTableHtml, removeComments} from 'web_editor.test_utils'; +const TEST_WIDTH = 800; +const TEST_HEIGHT = 600; +const round = (value, precision = 2) => Math.round(value * 10**precision)/10**precision; + QUnit.module('web_editor', {}, function () { QUnit.module('convert_inline', {}, function () { - QUnit.module('Convert Bootstrap grids to tables'); + QUnit.module('Convert Bootstrap grids to tables', { + beforeEach: function () { + this.$editable = $(`
`); + $('#qunit-fixture').append(this.$editable); + } + }); // Test bootstrapToTable, cardToTable and listGroupToTable QUnit.test('convert a single-row regular grid', async function (assert) { assert.expect(4); // 1x1 - let $editable = $(`
${getRegularGridHtml(1, 1)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(1, 1, 12, 100), + this.$editable.html(getRegularGridHtml(1, 1)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(1, 1, 12, 100, TEST_WIDTH), "should have converted a 1x1 grid to an equivalent table"); // 1x2 - $editable = $(`
${getRegularGridHtml(1, 2)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(1, 2, 6, 50), + this.$editable.html(getRegularGridHtml(1, 2)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(1, 2, 6, 50, TEST_WIDTH), "should have converted a 1x2 grid to an equivalent table"); // 1x3 - $editable = $(`
${getRegularGridHtml(1, 3)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(1, 3, 4, 33.33), + this.$editable.html(getRegularGridHtml(1, 3)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(1, 3, 4, 33.33, TEST_WIDTH), "should have converted a 1x3 grid to an equivalent table"); // 1x12 - $editable = $(`
${getRegularGridHtml(1, 12)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(1, 12, 1, 8.33), + this.$editable.html(getRegularGridHtml(1, 12)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH), "should have converted a 1x12 grid to an equivalent table"); }); QUnit.test('convert a single-row regular overflowing grid', async function (assert) { assert.expect(4); // 1x13 - let $editable = $(`
${getRegularGridHtml(1, 13)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), - getRegularTableHtml(1, 12, 1, 8.33).slice(0, -8) + - `(0, 12)` + + this.$editable.html(getRegularGridHtml(1, 13)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).slice(0, -8) + + `(0, 12)` + ``, "should have converted a 1x13 grid to an equivalent table (overflowing)"); // 1x14 - $editable = $(`
${getRegularGridHtml(1, 14)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), - getRegularTableHtml(1, 12, 1, 8.33).slice(0, -8) + - `(0, 12)` + - `(0, 13)` + + this.$editable.html(getRegularGridHtml(1, 14)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).slice(0, -8) + + `(0, 12)` + + `(0, 13)` + ``, "should have converted a 1x14 grid to an equivalent table (overflowing)"); // 1x25 - $editable = $(`
${getRegularGridHtml(1, 25)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), - getRegularTableHtml(1, 12, 1, 8.33).slice(0, -8) + - getRegularTableHtml(1, 12, 1, 8.33).replace(/\(0, (\d+)\)/g, (s, c) => `(0, ${+c + 12})`) + this.$editable.html(getRegularGridHtml(1, 25)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).slice(0, -8) + + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).replace(/\(0, (\d+)\)/g, (s, c) => `(0, ${+c + 12})`) .replace(/^/, '').slice(0, -8) + - `(0, 24)` + + `(0, 24)` + ``, "should have converted a 1x25 grid to an equivalent table (overflowing)"); // 1x26 - $editable = $(`
${getRegularGridHtml(1, 26)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), - getRegularTableHtml(1, 12, 1, 8.33).slice(0, -8) + - getRegularTableHtml(1, 12, 1, 8.33).replace(/\(0, (\d+)\)/g, (s, c) => `(0, ${+c + 12})`) + this.$editable.html(getRegularGridHtml(1, 26)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).slice(0, -8) + + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).replace(/\(0, (\d+)\)/g, (s, c) => `(0, ${+c + 12})`) .replace(/^/, '').slice(0, -8) + - `(0, 24)` + - `(0, 25)` + + `(0, 24)` + + `(0, 25)` + ``, "should have converted a 1x26 grid to an equivalent table (overflowing)"); }); @@ -83,67 +92,67 @@ QUnit.module('convert_inline', {}, function () { assert.expect(4); // 2x1 - let $editable = $(`
${getRegularGridHtml(2, 1)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(2, 1, 12, 100), + this.$editable.html(getRegularGridHtml(2, 1)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(2, 1, 12, 100, TEST_WIDTH), "should have converted a 2x1 grid to an equivalent table"); // 2x[1,2] - $editable = $(`
${getRegularGridHtml(2, [1, 2])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(2, [1, 2], [12, 6], [100, 50], true), + this.$editable.html(getRegularGridHtml(2, [1, 2])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(2, [1, 2], [12, 6], [100, 50], TEST_WIDTH), "should have converted a 2x[1,2] grid to an equivalent table"); // 3x3 - $editable = $(`
${getRegularGridHtml(3, 3)}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(3, 3, 4, 33.33), + this.$editable.html(getRegularGridHtml(3, 3)); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(3, 3, 4, 33.33, TEST_WIDTH), "should have converted a 3x3 grid to an equivalent table"); // 3x[3,2,1] - $editable = $(`
${getRegularGridHtml(3, [3,2,1])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getRegularTableHtml(3, [3, 2, 1], [4, 6, 12], [33.33, 50, 100], true), + this.$editable.html(getRegularGridHtml(3, [3,2,1])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getRegularTableHtml(3, [3, 2, 1], [4, 6, 12], [33.33, 50, 100], TEST_WIDTH), "should have converted a 3x[3,2,1] grid to an equivalent table"); }); QUnit.test('convert a multi-row regular overflowing grid', async function (assert) { assert.expect(4); // 2x[13,1] - let $editable = $(`
${getRegularGridHtml(2, [13, 1])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), - getRegularTableHtml(1, 12, 1, 8.33).slice(0, -8) + - `(0, 12)` + + this.$editable.html(getRegularGridHtml(2, [13, 1])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), + getRegularTableHtml(1, 12, 1, 8.33, TEST_WIDTH).slice(0, -8) + + `(0, 12)` + `` + // 13 overflowed the row by 1 -> fill up - `(1, 0)`, // 1 col with no size == col-12 + `(1, 0)`, // 1 col with no size == col-12 "should have converted a 2x[13,1] grid to an equivalent table (overflowing)"); // 2x[1,13] - $editable = $(`
${getRegularGridHtml(2, [1, 13])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual($editable.html(), - getRegularTableHtml(2, [1, 12], [12, 1], [100, 8.33]).slice(0, -8) + - `(1, 12)` + + this.$editable.html(getRegularGridHtml(2, [1, 13])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(this.$editable.html(), + getRegularTableHtml(2, [1, 12], [12, 1], [100, 8.33], TEST_WIDTH).slice(0, -8) + + `(1, 12)` + ``, // 13 overflowed the row by 1 -> fill up "should have converted a 2x[1,13] grid to an equivalent table (overflowing)"); // 3x[1,13,6] - $editable = $(`
${getRegularGridHtml(3, [1, 13, 6])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual($editable.html(), - getRegularTableHtml(2, [1, 12], [12, 1], [100, 8.33]).slice(0, -8) + - `(1, 12)` + + this.$editable.html(getRegularGridHtml(3, [1, 13, 6])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(this.$editable.html(), + getRegularTableHtml(2, [1, 12], [12, 1], [100, 8.33], TEST_WIDTH).slice(0, -8) + + `(1, 12)` + `` + // 13 overflowed the row by 1 -> fill up - getRegularTableHtml(1, 6, 2, 16.67).replace(/\(0,/g, `(2,`).replace(/^/, ''), + getRegularTableHtml(1, 6, 2, 16.67, TEST_WIDTH).replace(/\(0,/g, `(2,`).replace(/^/, ''), "should have converted a 3x[1,13,6] grid to an equivalent table (overflowing)"); // 3x[1,6,13] - $editable = $(`
${getRegularGridHtml(3, [1, 6, 13])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual($editable.html(), - getRegularTableHtml(3, [1, 6, 12], [12, 2, 1], [100, 16.67, 8.33]).slice(0, -8) + - `(2, 12)` + + this.$editable.html(getRegularGridHtml(3, [1, 6, 13])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(this.$editable.html(), + getRegularTableHtml(3, [1, 6, 12], [12, 2, 1], [100, 16.67, 8.33], TEST_WIDTH).slice(0, -8) + + `(2, 12)` + `` + // 13 overflowed the row by 1 -> fill up ``, "should have converted a 3x[1,6,13] grid to an equivalent table (overflowing)"); @@ -152,96 +161,96 @@ QUnit.module('convert_inline', {}, function () { assert.expect(2); // 1x2 - let $editable = $(`
${getGridHtml([[8, 4]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([[[8, 66.67], [4, 33.33]]], true), + this.$editable.html(getGridHtml([[8, 4]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([[[8, 66.67], [4, 33.33]]], TEST_WIDTH), "should have converted a 1x2 irregular grid to an equivalent table"); // 1x3 - $editable = $(`
${getGridHtml([[2, 3, 7]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([[[2, 16.67], [3, 25], [7, 58.33]]], true), + this.$editable.html(getGridHtml([[2, 3, 7]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([[[2, 16.67], [3, 25], [7, 58.33]]], TEST_WIDTH), "should have converted a 1x3 grid to an equivalent table"); }); QUnit.test('convert a single-row irregular overflowing grid', async function (assert) { assert.expect(2); // 1x2 - let $editable = $(`
${getGridHtml([[8, 5]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([ + this.$editable.html(getGridHtml([[8, 5]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([ [[8, 66.67], [4, 33.33, '']], [[5, 41.67, '(0, 1)'], [7, 58.33, '']], - ], true), + ], TEST_WIDTH), "should have converted a 1x2 irregular overflowing grid to an equivalent table"); // 1x3 - $editable = $(`
${getGridHtml([[7, 6, 9]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([ + this.$editable.html(getGridHtml([[7, 6, 9]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([ [[7, 58.33], [5, 41.67, '']], [[6, 50, '(0, 1)'], [6, 50, '']], [[9, 75, '(0, 2)'], [3, 25, '']], - ], true), + ], TEST_WIDTH), "should have converted a 1x3 irregular overflowing grid to an equivalent table"); }); QUnit.test('convert a multi-row irregular grid', async function (assert) { assert.expect(2); // 2x2 - let $editable = $(`
${getGridHtml([[1, 11], [2, 10]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([[[1, 8.33], [11, 91.67]], [[2, 16.67], [10, 83.33]]], true), + this.$editable.html(getGridHtml([[1, 11], [2, 10]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([[[1, 8.33], [11, 91.67]], [[2, 16.67], [10, 83.33]]], TEST_WIDTH), "should have converted a 2x2 irregular grid to an equivalent table"); // 2x[2,3] - $editable = $(`
${getGridHtml([[3, 9], [4, 6, 2]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), getTableHtml([[[3, 25], [9, 75]], [[4, 33.33], [6, 50], [2, 16.67]]], true), + this.$editable.html(getGridHtml([[3, 9], [4, 6, 2]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([[[3, 25], [9, 75]], [[4, 33.33], [6, 50], [2, 16.67]]], TEST_WIDTH), "should have converted a 2x[2,3] irregular grid to an equivalent table"); }); QUnit.test('convert a multi-row irregular overflowing grid', async function (assert) { assert.expect(3); // 2x2 (both rows overflow) - let $editable = $(`
${getGridHtml([[6, 8], [7, 9]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), + this.$editable.html(getGridHtml([[6, 8], [7, 9]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([ [[6, 50], [6, 50, '']], [[8, 66.67, '(0, 1)'], [4, 33.33, '']], [[7, 58.33, '(1, 0)'], [5, 41.67, '']], [[9, 75, '(1, 1)'], [3, 25, '']], - ], true), + ], TEST_WIDTH), "should have converted a 2x[1,13] irregular grid to an equivalent table (both rows overflowing)"); // 2x[2,3] (first row overflows) - $editable = $(`
${getGridHtml([[5, 8], [4, 2, 6]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), + this.$editable.html(getGridHtml([[5, 8], [4, 2, 6]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([ [[5, 41.67], [7, 58.33, '']], [[8, 66.67, '(0, 1)'], [4, 33.33, '']], [[4, 33.33, '(1, 0)'], [2, 16.67, '(1, 1)'], [6, 50, '(1, 2)']], - ], true), + ], TEST_WIDTH), "should have converted a 2x[2,3] irregular grid to an equivalent table (first row overflowing)"); // 2x[3,2] (second row overflows) - $editable = $(`
${getGridHtml([[4, 2, 6], [5, 8]])}
`); - convertInline.bootstrapToTable($editable); - assert.strictEqual(removeComments($editable.html()), + this.$editable.html(getGridHtml([[4, 2, 6], [5, 8]])); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(removeComments(this.$editable.html()), getTableHtml([ [[4, 33.33], [2, 16.67], [6, 50]], [[5, 41.67], [7, 58.33, '']], [[8, 66.67, '(1, 1)'], [4, 33.33, '']], - ], true), + ], TEST_WIDTH), "should have converted a 2x[3,2] irregular grid to an equivalent table (second row overflowing)"); }); QUnit.test('convert a card to a table', async function (assert) { assert.expect(1); - const $editable = $( - `
` + + this.$editable.html( + `
` + `
` + `HEADER` + `
` + @@ -252,9 +261,9 @@ QUnit.module('convert_inline', {}, function () { `` + - `
`); - convertInline.cardToTable($editable); - assert.strictEqual($editable.html(), + `
`); + convertInline.cardToTable(this.$editable); + assert.strictEqual(this.$editable.html(), getRegularTableHtml(3, 1, 12, 100) .replace('role=\"presentation\"', 'role=\"presentation\" class=\"card\"') .replace(/]*>\(0, 0\)<\/td>/, @@ -283,8 +292,8 @@ QUnit.module('convert_inline', {}, function () { QUnit.test('convert a list group to a table', async function (assert) { assert.expect(1); - const $editable = $( - `
    ` + + this.$editable.html( + `
      ` + `
    • ` + `(0, 0)` + `
    • ` + @@ -295,9 +304,9 @@ QUnit.module('convert_inline', {}, function () { `
    • ` + `(2, 0)` + `
    • ` + - `
`); - convertInline.listGroupToTable($editable); - assert.strictEqual($editable.html(), + ``); + convertInline.listGroupToTable(this.$editable); + assert.strictEqual(this.$editable.html(), getRegularTableHtml(3, 1, 12, 100) .split('style="').join('class="list-group-flush" style="') .replace(/]*>(\(0, 0\))<\/td>/, '$1') @@ -308,19 +317,20 @@ QUnit.module('convert_inline', {}, function () { QUnit.test('convert a grid with offsets to a table', async function (assert) { assert.expect(2); - let $editable = $('
(0, 0)
'); - convertInline.bootstrapToTable($editable); - assert.strictEqual($editable.html(), - getTableHtml([[[4, 33.33, ''], [6, 50, '(0, 0)'], [2, 16.67, '']]]), + this.$editable.html('
(0, 0)
'); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(this.$editable.html(), + getTableHtml([[[4, 33.33, ''], [6, 50, '(0, 0)'], [2, 16.67, '']]], TEST_WIDTH) + .replace(``, ''), "should have converted a column with an offset to two columns, then completed the column"); - $editable = $('
(0, 0)
(0, 1)
'); - convertInline.bootstrapToTable($editable); - assert.strictEqual($editable.html(), + this.$editable.html('
(0, 0)
(0, 1)
'); + convertInline.bootstrapToTable(this.$editable); + assert.strictEqual(this.$editable.html(), getTableHtml([ [[4, 33.33, ''], [6, 50, '(0, 0)'], [1, 8.33, ''], [1, 8.33, '']], - [[6, 50, '(0, 1)'], [6, 50, '']], - ]), + [[6, 50, '(0, 1)'], [6, 50, '']] + ], TEST_WIDTH), "should have converted a column with an offset to two columns, then completed the column (overflowing)"); }); diff --git a/addons/web_editor/static/tests/test_utils.js b/addons/web_editor/static/tests/test_utils.js index 4ea6ce0cea1..26123eab1c7 100644 --- a/addons/web_editor/static/tests/test_utils.js +++ b/addons/web_editor/static/tests/test_utils.js @@ -780,7 +780,8 @@ function getGridHtml(matrix) { * cell is represented by a tuple of numbers [colspan, width (in percent)]. A * cell can have a string as third value to represent its text content. The * default text content of each cell is its coordinates `(row index, column - * index)`. + * index)`. If the cell has a number as third value, it will be used as the + * max-width of the cell (in pixels). * Eg: [ // (note: extra attrs and styles apply) * [ // * [1, 8], // @@ -792,17 +793,18 @@ function getGridHtml(matrix) { * ], // * ] //
(0, 0)
* - * @param {Array>>} matrix + * @param {Array>>} matrix + * @param {Number} [containerWidth] * @returns {string} */ -function getTableHtml(matrix) { +function getTableHtml(matrix, containerWidth) { return ( `` + matrix.map((row, iRow) => ( `` + row.map((col, iCol) => ( - `` )).join('') + `` @@ -841,16 +843,17 @@ function getRegularGridHtml(nRows, nCols) { * @param {Number|Number[]} nCols * @param {Number|Number[]} colspan * @param {Number|Number[]} width + * @param {Number} containerWidth * @returns {string} */ -function getRegularTableHtml(nRows, nCols, colspan, width) { +function getRegularTableHtml(nRows, nCols, colspan, width, containerWidth) { const matrix = new Array(nRows).fill().map((_, iRow) => ( new Array(Array.isArray(nCols) ? nCols[iRow] : nCols).fill().map(() => ([ Array.isArray(colspan) ? colspan[iRow] : colspan, Array.isArray(width) ? width[iRow] : width, ]))) ); - return getTableHtml(matrix); + return getTableHtml(matrix, containerWidth); } /** * Take an HTML string and returns that string stripped from any HTML comments.
` + - (col.length === 3 ? col[2] : `(${iRow}, ${iCol})`) + + `` + + (typeof col[2] === 'string' ? col[2] : `(${iRow}, ${iCol})`) + `