Stacked tables in Mobile has wrong headers

Created on 31 July 2023, 11 months ago
Updated 14 August 2023, 11 months ago

Problem/Motivation

Entered this table in CKEditor 4

(from the database)

<div class="usa-table-container">
<table class="usa-table usa-table--striped usa-table--stacked">
	<caption>test caption</caption>
	<thead>
		<tr>
			<th scope="col">this</th>
			<th scope="col">is&nbsp;</th>
			<th scope="col">aAliquam aliquam nulla in ante aliquet porttitor</th>
			<th scope="col">test</th>
			<th scope="col">only</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>this</td>
			<td>is</td>
			<td>Aliquam aliquam nulla in ante aliquet porttitor</td>
			<td>a</td>
			<td>test</td>
		</tr>
		<tr>
			<td>if</td>
			<td>thisAliquam aliquam nulla in ante aliquet porttitor</td>
			<td>were</td>
			<td>a</td>
			<td>real</td>
		</tr>
		<tr>
			<td>Aliquam aliquam nulla in ante aliquet porttitor</td>
			<td>it</td>
			<td>would</td>
			<td>contain</td>
			<td>actual</td>
		</tr>
		<tr>
			<td>data</td>
			<td>that</td>
			<td>might</td>
			<td>Aliquam aliquam nulla in ante aliquet porttitor</td>
			<td>useful</td>
		</tr>
	</tbody>
</table>
</div>

after being run through the Filter(filter_table_attributes) I get

<table class="usa-table usa-table--striped usa-table--stacked"><caption>test caption</caption>
	<thead><tr><th scope="row">header 1</th>
			<th scope="row">header 2</th>
			<th scope="row">header 4</th>
			<th scope="row">header 4</th>
			<th scope="row">header 5</th>
		</tr></thead><tbody><tr><td data-label="header 1">cell 1,1</td>
			<td data-label="header 1">cell 1,2</td>
			<td data-label="header 1">cell 1,3</td>
			<td data-label="header 1">cell 1,4</td>
			<td data-label="header 1">cell 1,5</td>
		</tr><tr><td data-label="header 1">cell 2,1</td>
			<td data-label="header 1">cell 2,2</td>
			<td data-label="header 1">cell 2,3</td>
			<td data-label="header 1">cell 2,4</td>
			<td data-label="header 1">cell 2,5</td>
		</tr><tr><td data-label="header 1">cell 3,1</td>
			<td data-label="header 1">cell 3,2</td>
			<td data-label="header 1">cell 3,3</td>
			<td data-label="header 1">cell 3,4</td>
			<td data-label="header 1">cell 3,5</td>
		</tr><tr><td data-label="header 1">cell 4,1</td>
			<td data-label="header 1">cell 4,2</td>
			<td data-label="header 1">cell 4,3</td>
			<td data-label="header 1">cell 4,4</td>
			<td data-label="header 1">cell 4,5</td>
		</tr></tbody></table></div>

all rows with the same header

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jrglasgow Idaho

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jrglasgow
  • πŸ‡ΊπŸ‡ΈUnited States jrglasgow Idaho

    In looking at this I noticed a few things where the table row is getting processed...

            foreach ($rows as $row) {
              if ($skip_first) {
                $skip_first = FALSE;
                continue;
              }
              $counter = 0;
              $tbodyTags = $xpath->query('//tbody//td|//th', $row);
              foreach ($tbodyTags as $tag) {
                if ($tag->nodeName === 'th') {
                  $tag->setAttribute('scope', 'row');
                }
                else {
                  $data_label = $table_headers[$counter];
                  $tag->setAttribute('data-label', $data_label);
                }
              }
            }
    

    in the foreach loop internal to the row the $counter variable is never incremented.

    I also noticed that the xpath query $tbodyTags = $xpath->query('//tbody//td|//th', $row); returns a list of 25 items... so it was looping through not only the items in the current row, but it was also looping through all of the cells in the table for each row.

    I modified this to instead just grab the

    $row->childNodes</a> of the current row and this resolved the issue... the same section of code work if you have this instead
    <code>
            foreach ($rows as $row) {
              if ($skip_first) {
                $skip_first = FALSE;
                continue;
              }
              $counter = 0;
              foreach ($row->childNodes AS $tag) {
                switch ($tag->nodeName) {
                  case 'th':
                    $tag->setAttribute('scope', 'row');
                  case 'td':
                    $data_label = $table_headers[$counter];
                    $tag->setAttribute('data-label', $data_label);
                    $counter++;
                    break;
                  default:
                    error_log($tag->nodeName);
                }
              }
            }
    
  • Status changed to Needs review 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrglasgow Idaho

    here is my patch

  • @jrglasgow opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States jrglasgow Idaho

    found a couple of small issues with my patch

  • First commit to issue fork.
  • Status changed to Fixed 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Confirmed the issue and that your MR 62 fixed it thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024