Update tables code to only target unwrapped tables

Bug: T367248
Change-Id: If4267e083cd6721bdb80d32a84c1fd9306096b50
This commit is contained in:
bwang 2024-10-02 14:04:07 -05:00
parent 02e22cdfd0
commit 5d83c55f5e
3 changed files with 78 additions and 39 deletions

View file

@ -3,7 +3,7 @@ const init = () => {
if ( !config.VectorWrapTablesTemporary ) {
return;
}
const tables = document.querySelectorAll( '.mw-parser-output table.wikitable' );
const tables = document.querySelectorAll( '.mw-parser-output > table.wikitable' );
let numberBigTables = 0;
Array.from( tables ).forEach( ( table ) => {
const styles = window.getComputedStyle( table );

View file

@ -2,28 +2,30 @@
exports[`tables doesnt wrap floated tables 1`] = `
"
<table class=\\"wikitable\\" style=\\"float:right\\">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
"
<div class=\\"mw-parser-output\\">
<table class=\\"wikitable\\" style=\\"float:right\\">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
<div>
</div></div>"
`;
exports[`tables doesnt wrap nested tables 1`] = `
"
<section class=\\"mw-parser-output\\">
<div class=\\"mw-parser-output\\">
<div class=\\"noresize\\"><table class=\\"wikitable\\">
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table class=\\"wikitable\\"><tbody><tr><th>table table table</th></tr></tbody></table></td><td></td></tr>
</tbody>
</table></div>
</section>
</div>
"
`;
exports[`tables doesnt wrap tables that are already wrapped 1`] = `
exports[`tables doesnt wrap tables that already have noresize 1`] = `
"
<div class=\\"mw-parser-output\\">
<div class=\\"noresize\\">
@ -37,20 +39,36 @@ exports[`tables doesnt wrap tables that are already wrapped 1`] = `
"
`;
exports[`tables doesnt wrap tables that are already wrapped 1`] = `
"
<div class=\\"mw-parser-output\\">
<div>
<table class=\\"wikitable\\">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
</div>
</div>
"
`;
exports[`tables doesnt wrap tables that are not wikitables 1`] = `
"
<table>
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table><tbody><tr><th>table table table</th></tr></tbody></table></td><td></td></tr>
</tbody>
</table>
"
<div class=\\"mw-parser-output\\">
<table>
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table><tbody><tr><th>table table table</th></tr></tbody></table></td><td></td></tr>
</tbody>
</table>
<div>
</div></div>"
`;
exports[`tables wraps multiple table with div 1`] = `
"
<section class=\\"mw-parser-output\\">
<div class=\\"mw-parser-output\\">
<div class=\\"noresize\\"><table class=\\"wikitable\\">
<tbody><tr><th>table table table</th></tr></tbody>
</table></div>
@ -61,16 +79,16 @@ exports[`tables wraps multiple table with div 1`] = `
<div class=\\"noresize\\"><table class=\\"wikitable\\">
<tbody><tr><th>table table table</th></tr></tbody>
</table></div>
</section>
</div>
"
`;
exports[`tables wraps table with div 1`] = `
"
<section class=\\"mw-parser-output\\">
<div class=\\"mw-parser-output\\">
<div class=\\"noresize\\"><table class=\\"wikitable\\">
<tbody><tr><th>table table table</th></tr></tbody>
</table></div>
</section>
</div>
"
`;

View file

@ -3,11 +3,11 @@ const tables = require( '../../../resources/skins.vector.js/tables.js' ).init;
describe( 'tables', () => {
test( 'wraps table with div', () => {
document.body.innerHTML = `
<section class="mw-parser-output">
<div class="mw-parser-output">
<table class="wikitable">
<tbody><tr><th>table table table</th></tr></tbody>
</table>
</section>
</div>
`;
tables();
@ -16,7 +16,7 @@ describe( 'tables', () => {
test( 'wraps multiple table with div', () => {
document.body.innerHTML = `
<section class="mw-parser-output">
<div class="mw-parser-output">
<table class="wikitable">
<tbody><tr><th>table table table</th></tr></tbody>
</table>
@ -27,7 +27,7 @@ describe( 'tables', () => {
<table class="wikitable">
<tbody><tr><th>table table table</th></tr></tbody>
</table>
</section>
</div>
`;
tables();
@ -36,14 +36,14 @@ describe( 'tables', () => {
test( 'doesnt wrap nested tables', () => {
document.body.innerHTML = `
<section class="mw-parser-output">
<div class="mw-parser-output">
<table class="wikitable">
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table class="wikitable"><tbody><tr><th>table table table</th></tr></tbody></table><td></tr>
</tbody>
</table>
</section>
</div>
`;
tables();
@ -52,19 +52,21 @@ describe( 'tables', () => {
test( 'doesnt wrap tables that are not wikitables', () => {
document.body.innerHTML = `
<table>
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table><tbody><tr><th>table table table</th></tr></tbody></table><td></tr>
</tbody>
</table>
<div class="mw-parser-output">
<table>
<tbody>
<tr><th>table table table</th></tr>
<tr><td><table><tbody><tr><th>table table table</th></tr></tbody></table><td></tr>
</tbody>
</table>
<div>
`;
tables();
expect( document.body.innerHTML ).toMatchSnapshot();
} );
test( 'doesnt wrap tables that are already wrapped', () => {
test( 'doesnt wrap tables that already have noresize', () => {
document.body.innerHTML = `
<div class="mw-parser-output">
<div class="noresize">
@ -81,13 +83,32 @@ describe( 'tables', () => {
expect( document.body.innerHTML ).toMatchSnapshot();
} );
test( 'doesnt wrap tables that are already wrapped', () => {
document.body.innerHTML = `
<div class="mw-parser-output">
<div>
<table class="wikitable">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
</div>
</div>
`;
tables();
expect( document.body.innerHTML ).toMatchSnapshot();
} );
test( 'doesnt wrap floated tables', () => {
document.body.innerHTML = `
<table class="wikitable" style="float:right">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
<div class="mw-parser-output">
<table class="wikitable" style="float:right">
<tbody>
<tr><th>table table table</th></tr>
</tbody>
</table>
<div>
`;
tables();