Skip to content

Commit cd8d388

Browse files
committed
fix: table attribute cannot applied on td element
1 parent 0c7b1d4 commit cd8d388

2 files changed

Lines changed: 266 additions & 2 deletions

File tree

‎system/View/Table.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,14 @@ public function generate($tableData = null)
300300
foreach ($this->heading as $heading) {
301301
$temp = $this->template['heading_cell_start'];
302302

303+
// Check tag with td or th
304+
// If not will return null
305+
preg_match('/(<)(td|th)/i', $temp, $valueHead);
306+
$strHeading = count($valueHead) === 3 ? $valueHead[1] . $valueHead[2] : null;
307+
303308
foreach ($heading as $key => $val) {
304309
if ($key !== 'data') {
305-
$temp = str_replace('<th', '<th ' . $key . '="' . $val . '"', $temp);
310+
$temp = $strHeading ? str_replace($strHeading, $strHeading . ' ' . $key . '="' . $val . '"', $temp) : $temp;
306311
}
307312
}
308313

@@ -360,9 +365,14 @@ public function generate($tableData = null)
360365
foreach ($this->footing as $footing) {
361366
$temp = $this->template['footing_cell_start'];
362367

368+
// Check tag with td or th
369+
// If not will return null
370+
preg_match('/(<)(td|th)/i', $temp, $valueFoot);
371+
$strFooting = count($valueFoot) === 3 ? $valueFoot[1] . $valueFoot[2] : null;
372+
363373
foreach ($footing as $key => $val) {
364374
if ($key !== 'data') {
365-
$temp = str_replace('<th', '<th ' . $key . '="' . $val . '"', $temp);
375+
$temp = $strFooting ? str_replace($strFooting, $strFooting . ' ' . $key . '="' . $val . '"', $temp) : $temp;
366376
}
367377
}
368378

‎tests/system/View/TableTest.php‎

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
final class TableTest extends CIUnitTestCase
2323
{
2424
private Table $table;
25+
private string $styleTableOne = 'background:#F99;text-align:right;width:5em;';
26+
private string $styleTableTwo = 'background:cyan;color:white;text-align:right;width:5em;';
2527

2628
protected function setUp(): void
2729
{
2830
$this->table = new MockTable();
31+
32+
// Reset Header & Footer
33+
$this->table->heading = [];
34+
$this->table->footing = [];
2935
}
3036

3137
// Setter Methods
@@ -92,6 +98,58 @@ public function testSetFooting()
9298
);
9399
}
94100

101+
public function testSetHeadingWithStyle()
102+
{
103+
$template = [
104+
'heading_cell_start' => '<td>',
105+
'heading_cell_end' => '</td>',
106+
];
107+
108+
$this->table->setTemplate($template);
109+
$this->table->setHeading([['data' => 'Name', 'class' => 'tdh'], ['data' => 'Amount', 'class' => 'tdf', 'style' => $this->styleTableOne]]);
110+
111+
$this->assertSame(
112+
[
113+
[
114+
'data' => 'Name',
115+
'class' => 'tdh',
116+
],
117+
[
118+
'data' => 'Amount',
119+
'class' => 'tdf',
120+
'style' => $this->styleTableOne,
121+
],
122+
],
123+
$this->table->heading
124+
);
125+
}
126+
127+
public function testSetFootingWithStyle()
128+
{
129+
$template = [
130+
'footing_cell_start' => '<td>',
131+
'footing_cell_end' => '</td>',
132+
];
133+
134+
$this->table->setTemplate($template);
135+
$this->table->setFooting([['data' => 'Total', 'class' => 'tdf'], ['data' => 3, 'class' => 'tdh', 'style' => $this->styleTableTwo]]);
136+
137+
$this->assertSame(
138+
[
139+
[
140+
'data' => 'Total',
141+
'class' => 'tdf',
142+
],
143+
[
144+
'data' => 3,
145+
'class' => 'tdh',
146+
'style' => $this->styleTableTwo,
147+
],
148+
],
149+
$this->table->footing
150+
);
151+
}
152+
95153
/**
96154
* @depends testPrepArgs
97155
*/
@@ -375,6 +433,202 @@ public function testGenerate()
375433
$this->assertStringContainsString('<td>12345</td>', $table);
376434
}
377435

436+
public function testGenerateTdWithClassStyle()
437+
{
438+
$template = [
439+
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
440+
'thead_open' => '<thead>',
441+
'thead_close' => '</thead>',
442+
'heading_row_start' => '<tr>',
443+
'heading_row_end' => '</tr>',
444+
'heading_cell_start' => '<td>',
445+
'heading_cell_end' => '</td>',
446+
'tfoot_open' => '<tfoot>',
447+
'tfoot_close' => '</tfoot>',
448+
'footing_row_start' => '<tr>',
449+
'footing_row_end' => '</tr>',
450+
'footing_cell_start' => '<td>',
451+
'footing_cell_end' => '</td>',
452+
'tbody_open' => '<tbody>',
453+
'tbody_close' => '</tbody>',
454+
'row_start' => '<tr>',
455+
'row_end' => '</tr>',
456+
'cell_start' => '<td>',
457+
'cell_end' => '</td>',
458+
'row_alt_start' => '<tr>',
459+
'row_alt_end' => '</tr>',
460+
'cell_alt_start' => '<td>',
461+
'cell_alt_end' => '</td>',
462+
'table_close' => '</table>',
463+
];
464+
465+
$this->table->setTemplate($template);
466+
$this->table->setHeading([['data' => 'Name', 'class' => 'tdk'], ['data' => 'Amount', 'class' => 'tdr', 'style' => $this->styleTableOne]]);
467+
468+
$this->table->addRow(['Fred', 1]);
469+
$this->table->addRow(['Mary', 3]);
470+
$this->table->addRow(['John', 6]);
471+
472+
$this->table->setFooting([['data' => 'Total', 'class' => 'thk'], ['data' => '<small class="text-light">IDR <span class="badge badge-info">10</span></small>', 'class' => 'thr', 'style' => $this->styleTableTwo]]);
473+
474+
$table = $this->table->generate();
475+
476+
// Header
477+
$this->assertStringContainsString('<td class="tdk">Name</td>', $table);
478+
$this->assertStringContainsString('<td style="' . $this->styleTableOne . '" class="tdr">Amount</td>', $table);
479+
480+
// Footer
481+
$this->assertStringContainsString('<td class="thk">Total</td>', $table);
482+
$this->assertStringContainsString('<td style="' . $this->styleTableTwo . '" class="thr"><small class="text-light">IDR <span class="badge badge-info">10</span></small></td>', $table);
483+
}
484+
485+
public function testGenerateThWithClassStyle()
486+
{
487+
$template = [
488+
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
489+
'thead_open' => '<thead>',
490+
'thead_close' => '</thead>',
491+
'heading_row_start' => '<tr>',
492+
'heading_row_end' => '</tr>',
493+
'heading_cell_start' => '<th>',
494+
'heading_cell_end' => '</th>',
495+
'tfoot_open' => '<tfoot>',
496+
'tfoot_close' => '</tfoot>',
497+
'footing_row_start' => '<tr>',
498+
'footing_row_end' => '</tr>',
499+
'footing_cell_start' => '<th>',
500+
'footing_cell_end' => '</th>',
501+
'tbody_open' => '<tbody>',
502+
'tbody_close' => '</tbody>',
503+
'row_start' => '<tr>',
504+
'row_end' => '</tr>',
505+
'cell_start' => '<td>',
506+
'cell_end' => '</td>',
507+
'row_alt_start' => '<tr>',
508+
'row_alt_end' => '</tr>',
509+
'cell_alt_start' => '<td>',
510+
'cell_alt_end' => '</td>',
511+
'table_close' => '</table>',
512+
];
513+
514+
$this->table->setTemplate($template);
515+
$this->table->setHeading([['data' => 'Name', 'class' => 'tdk'], ['data' => 'Amount', 'class' => 'tdr', 'style' => $this->styleTableOne]]);
516+
517+
$this->table->addRow(['Fred', 1]);
518+
$this->table->addRow(['Mary', 3]);
519+
$this->table->addRow(['John', 6]);
520+
521+
$this->table->setFooting([['data' => 'Total', 'class' => 'thk'], ['data' => '<small class="text-light">IDR <span class="badge badge-info">10</span></small>', 'class' => 'thr', 'style' => $this->styleTableTwo]]);
522+
523+
$table = $this->table->generate();
524+
525+
// Header
526+
$this->assertStringContainsString('<th class="tdk">Name</th>', $table);
527+
$this->assertStringContainsString('<th style="' . $this->styleTableOne . '" class="tdr">Amount</th>', $table);
528+
529+
// Footer
530+
$this->assertStringContainsString('<th class="thk">Total</th>', $table);
531+
$this->assertStringContainsString('<th style="' . $this->styleTableTwo . '" class="thr"><small class="text-light">IDR <span class="badge badge-info">10</span></small></th>', $table);
532+
}
533+
534+
public function testGenerateInvalidHeadingFooting()
535+
{
536+
$template = [
537+
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
538+
'thead_open' => '<thead>',
539+
'thead_close' => '</thead>',
540+
'heading_row_start' => '<tr>',
541+
'heading_row_end' => '</tr>',
542+
'heading_cell_start' => '<header>',
543+
'heading_cell_end' => '</header>',
544+
'tfoot_open' => '<tfoot>',
545+
'tfoot_close' => '</tfoot>',
546+
'footing_row_start' => '<tr>',
547+
'footing_row_end' => '</tr>',
548+
'footing_cell_start' => '<footer>',
549+
'footing_cell_end' => '</footer>',
550+
'tbody_open' => '<tbody>',
551+
'tbody_close' => '</tbody>',
552+
'row_start' => '<tr>',
553+
'row_end' => '</tr>',
554+
'cell_start' => '<td>',
555+
'cell_end' => '</td>',
556+
'row_alt_start' => '<tr>',
557+
'row_alt_end' => '</tr>',
558+
'cell_alt_start' => '<td>',
559+
'cell_alt_end' => '</td>',
560+
'table_close' => '</table>',
561+
];
562+
563+
$this->table->setTemplate($template);
564+
$this->table->setHeading([['data' => 'Name', 'class' => 'tdk'], ['data' => 'Amount', 'class' => 'tdr', 'style' => $this->styleTableOne]]);
565+
566+
$this->table->addRow(['Fred', 1]);
567+
$this->table->addRow(['Mary', 3]);
568+
$this->table->addRow(['John', 6]);
569+
570+
$this->table->setFooting([['data' => 'Total', 'class' => 'thk'], ['data' => '<small class="text-light">IDR <span class="badge badge-info">10</span></small>', 'class' => 'thr', 'style' => $this->styleTableTwo]]);
571+
572+
$table = $this->table->generate();
573+
574+
// Header
575+
$this->assertStringContainsString('<header>Name</header>', $table);
576+
$this->assertStringContainsString('<header>Amount</header>', $table);
577+
578+
// Footer
579+
$this->assertStringContainsString('<footer>Total</footer>', $table);
580+
$this->assertStringContainsString('<footer><small class="text-light">IDR <span class="badge badge-info">10</span></small></footer>', $table);
581+
}
582+
583+
public function testGenerateInvalidHeadingFootingHTML()
584+
{
585+
$template = [
586+
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
587+
'thead_open' => '<thead>',
588+
'thead_close' => '</thead>',
589+
'heading_row_start' => '<tr>',
590+
'heading_row_end' => '</tr>',
591+
'heading_cell_start' => 'th>',
592+
'heading_cell_end' => '</th>',
593+
'tfoot_open' => '<tfoot>',
594+
'tfoot_close' => '</tfoot>',
595+
'footing_row_start' => '<tr>',
596+
'footing_row_end' => '</tr>',
597+
'footing_cell_start' => 'td>',
598+
'footing_cell_end' => '</td>',
599+
'tbody_open' => '<tbody>',
600+
'tbody_close' => '</tbody>',
601+
'row_start' => '<tr>',
602+
'row_end' => '</tr>',
603+
'cell_start' => '<td>',
604+
'cell_end' => '</td>',
605+
'row_alt_start' => '<tr>',
606+
'row_alt_end' => '</tr>',
607+
'cell_alt_start' => '<td>',
608+
'cell_alt_end' => '</td>',
609+
'table_close' => '</table>',
610+
];
611+
612+
$this->table->setTemplate($template);
613+
$this->table->setHeading([['data' => 'Name', 'class' => 'tdk'], ['data' => 'Amount', 'class' => 'tdr', 'style' => $this->styleTableOne]]);
614+
615+
$this->table->addRow(['Fred', 1]);
616+
$this->table->addRow(['Mary', 3]);
617+
$this->table->addRow(['John', 6]);
618+
619+
$this->table->setFooting([['data' => 'Total', 'class' => 'thk'], ['data' => '<small class="text-light">IDR <span class="badge badge-info">10</span></small>', 'class' => 'thr', 'style' => $this->styleTableTwo]]);
620+
621+
$table = $this->table->generate();
622+
623+
// Header
624+
$this->assertStringContainsString('th>Name</th>', $table);
625+
$this->assertStringContainsString('th>Amount</th>', $table);
626+
627+
// Footer
628+
$this->assertStringContainsString('td>Total</td>', $table);
629+
$this->assertStringContainsString('td><small class="text-light">IDR <span class="badge badge-info">10</span></small></td>', $table);
630+
}
631+
378632
public function testGenerateEmptyCell()
379633
{
380634
// Prepare the data

0 commit comments

Comments
 (0)