From 7dfaa3aede3b64425df16a2bd794605e211ff2e5 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 12 Aug 2014 08:45:48 +0800 Subject: [PATCH] MDL-46772 core: html_writer::table should respect tr attributes --- lib/outputcomponents.php | 8 +++++- lib/tests/html_writer_test.php | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 8aa9925c42a..ddc1f63db1e 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1611,7 +1611,13 @@ class html_writer { $row->attributes['class'] .= ' lastrow'; } - $output .= html_writer::start_tag('tr', array('class' => trim($row->attributes['class']), 'style' => $row->style, 'id' => $row->id)) . "\n"; + // Explicitly assigned properties should override those defined in the attributes. + $row->attributes['class'] = trim($row->attributes['class']); + $trattributes = array_merge($row->attributes, array( + 'id' => $row->id, + 'style' => $row->style, + )); + $output .= html_writer::start_tag('tr', $trattributes) . "\n"; $keys2 = array_keys($row->cells); $lastkey = end($keys2); diff --git a/lib/tests/html_writer_test.php b/lib/tests/html_writer_test.php index 0b599377cf7..bf6ecee5ca8 100644 --- a/lib/tests/html_writer_test.php +++ b/lib/tests/html_writer_test.php @@ -168,4 +168,51 @@ class core_html_writer_testcase extends basic_testcase { public function test_end_span() { $this->assertSame('', html_writer::end_span()); } + + public function test_table() { + $row = new html_table_row(); + + // The attribute will get overwritten by the ID. + $row->id = 'Bob'; + $row->attributes['id'] = 'will get overwritten'; + + // The data-name will be present in the output. + $row->attributes['data-name'] = 'Fred'; + $row->class = 'this is a table row'; + + $cell = new html_table_cell(); + + // The attribute will get overwritten by the ID. + $cell->id = 'Jeremy'; + $cell->attributes['id'] = 'will get overwritten'; + + // The data-name will be present in the output. + $cell->attributes['data-name'] = 'John'; + $cell->class = 'this is a table cell'; + + $row->cells[] = $cell; + + $table = new html_table(); + // The attribute will get overwritten by the ID. + $table->id = 'Jeffrey'; + $table->attributes['id'] = 'will get overwritten'; + + // The data-name will be present in the output. + $table->attributes['data-name'] = 'Colin'; + // The attribute will get overwritten by the ID above. + $table->data[] = $row; + + $output = html_writer::table($table); + + $expected = << + + + + + + +EOF; + $this->assertSame($expected, $output); + } }