diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 74f0d370894..814f8ddabf9 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1733,6 +1733,97 @@ class html_writer { return $label; } + + /** + * Combines a class parameter with other attributes. Aids in code reduction + * because the class parameter is very frequently used. + * + * If the class attribute is specified both in the attributes and in the + * class parameter, the two values are combined with a space between. + * + * @param string $class Optional CSS class (or classes as space-separated list) + * @param array $attributes Optional other attributes as array + * @return array Attributes (or null if still none) + */ + private static function add_class($class = '', array $attributes = null) { + if ($class !== '') { + $classattribute = array('class' => $class); + if ($attributes) { + if (array_key_exists('class', $attributes)) { + $attributes['class'] = trim($attributes['class'] . ' ' . $class); + } else { + $attributes = $classattribute + $attributes; + } + } else { + $attributes = $classattribute; + } + } + return $attributes; + } + + /** + * Creates a
tag. (Shortcut function.) + * + * @param string $content HTML content of tag + * @param string $class Optional CSS class (or classes as space-separated list) + * @param array $attributes Optional other attributes as array + * @return string HTML code for div + */ + public static function div($content, $class = '', array $attributes = null) { + return self::tag('div', $content, self::add_class($class, $attributes)); + } + + /** + * Starts a
tag. (Shortcut function.) + * + * @param string $class Optional CSS class (or classes as space-separated list) + * @param array $attributes Optional other attributes as array + * @return string HTML code for open div tag + */ + public static function start_div($class = '', array $attributes = null) { + return self::start_tag('div', self::add_class($class, $attributes)); + } + + /** + * Ends a
tag. (Shortcut function.) + * + * @return string HTML code for close div tag + */ + public static function end_div() { + return self::end_tag('div'); + } + + /** + * Creates a tag. (Shortcut function.) + * + * @param string $content HTML content of tag + * @param string $class Optional CSS class (or classes as space-separated list) + * @param array $attributes Optional other attributes as array + * @return string HTML code for span + */ + public static function span($content, $class = '', array $attributes = null) { + return self::tag('span', $content, self::add_class($class, $attributes)); + } + + /** + * Starts a tag. (Shortcut function.) + * + * @param string $class Optional CSS class (or classes as space-separated list) + * @param array $attributes Optional other attributes as array + * @return string HTML code for open span tag + */ + public static function start_span($class = '', array $attributes = null) { + return self::start_tag('span', self::add_class($class, $attributes)); + } + + /** + * Ends a tag. (Shortcut function.) + * + * @return string HTML code for close span tag + */ + public static function end_span() { + return self::end_tag('span'); + } } /** diff --git a/lib/tests/htmlwriter_test.php b/lib/tests/htmlwriter_test.php index eaa82db4f6e..97e12850c62 100644 --- a/lib/tests/htmlwriter_test.php +++ b/lib/tests/htmlwriter_test.php @@ -89,4 +89,84 @@ class html_writer_testcase extends basic_testcase { $this->assertEquals('
0
', html_writer::nonempty_tag('div', '0', array('class' => 'score'))); } + + public function test_div() { + // All options. + $this->assertEquals('
ribbit
', + html_writer::div('ribbit', 'frog', array('id' => 'kermit'))); + // Combine class from attributes and $class. + $this->assertEquals('
ribbit
', + html_writer::div('ribbit', 'frog', array('class' => 'amphibian'))); + // Class only. + $this->assertEquals('
ribbit
', + html_writer::div('ribbit', 'frog')); + // Attributes only. + $this->assertEquals('
ribbit
', + html_writer::div('ribbit', '', array('id' => 'kermit'))); + // No options. + $this->assertEquals('
ribbit
', + html_writer::div('ribbit')); + } + + public function test_start_div() { + // All options. + $this->assertEquals('
', + html_writer::start_div('frog', array('id' => 'kermit'))); + // Combine class from attributes and $class. + $this->assertEquals('
', + html_writer::start_div('frog', array('class' => 'amphibian'))); + // Class only. + $this->assertEquals('
', + html_writer::start_div('frog')); + // Attributes only. + $this->assertEquals('
', + html_writer::start_div('', array('id' => 'kermit'))); + // No options. + $this->assertEquals('
', + html_writer::start_div()); + } + + public function test_end_div() { + $this->assertEquals('
', html_writer::end_div()); + } + + public function test_span() { + // All options. + $this->assertEquals('ribbit', + html_writer::span('ribbit', 'frog', array('id' => 'kermit'))); + // Combine class from attributes and $class. + $this->assertEquals('ribbit', + html_writer::span('ribbit', 'frog', array('class' => 'amphibian'))); + // Class only. + $this->assertEquals('ribbit', + html_writer::span('ribbit', 'frog')); + // Attributes only. + $this->assertEquals('ribbit', + html_writer::span('ribbit', '', array('id' => 'kermit'))); + // No options. + $this->assertEquals('ribbit', + html_writer::span('ribbit')); + } + + public function test_start_span() { + // All options. + $this->assertEquals('', + html_writer::start_span('frog', array('id' => 'kermit'))); + // Combine class from attributes and $class. + $this->assertEquals('', + html_writer::start_span('frog', array('class' => 'amphibian'))); + // Class only. + $this->assertEquals('', + html_writer::start_span('frog')); + // Attributes only. + $this->assertEquals('', + html_writer::start_span('', array('id' => 'kermit'))); + // No options. + $this->assertEquals('', + html_writer::start_span()); + } + + public function test_end_span() { + $this->assertEquals('', html_writer::end_span()); + } }