Merge branch 'MDL-38133-master' of git://github.com/sammarshallou/moodle
This commit is contained in:
@@ -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 <div> 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 <div> 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 <div> tag. (Shortcut function.)
|
||||
*
|
||||
* @return string HTML code for close div tag
|
||||
*/
|
||||
public static function end_div() {
|
||||
return self::end_tag('div');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <span> 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 <span> 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 <span> tag. (Shortcut function.)
|
||||
*
|
||||
* @return string HTML code for close span tag
|
||||
*/
|
||||
public static function end_span() {
|
||||
return self::end_tag('span');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -89,4 +89,84 @@ class html_writer_testcase extends basic_testcase {
|
||||
$this->assertEquals('<div class="score">0</div>',
|
||||
html_writer::nonempty_tag('div', '0', array('class' => 'score')));
|
||||
}
|
||||
|
||||
public function test_div() {
|
||||
// All options.
|
||||
$this->assertEquals('<div class="frog" id="kermit">ribbit</div>',
|
||||
html_writer::div('ribbit', 'frog', array('id' => 'kermit')));
|
||||
// Combine class from attributes and $class.
|
||||
$this->assertEquals('<div class="amphibian frog">ribbit</div>',
|
||||
html_writer::div('ribbit', 'frog', array('class' => 'amphibian')));
|
||||
// Class only.
|
||||
$this->assertEquals('<div class="frog">ribbit</div>',
|
||||
html_writer::div('ribbit', 'frog'));
|
||||
// Attributes only.
|
||||
$this->assertEquals('<div id="kermit">ribbit</div>',
|
||||
html_writer::div('ribbit', '', array('id' => 'kermit')));
|
||||
// No options.
|
||||
$this->assertEquals('<div>ribbit</div>',
|
||||
html_writer::div('ribbit'));
|
||||
}
|
||||
|
||||
public function test_start_div() {
|
||||
// All options.
|
||||
$this->assertEquals('<div class="frog" id="kermit">',
|
||||
html_writer::start_div('frog', array('id' => 'kermit')));
|
||||
// Combine class from attributes and $class.
|
||||
$this->assertEquals('<div class="amphibian frog">',
|
||||
html_writer::start_div('frog', array('class' => 'amphibian')));
|
||||
// Class only.
|
||||
$this->assertEquals('<div class="frog">',
|
||||
html_writer::start_div('frog'));
|
||||
// Attributes only.
|
||||
$this->assertEquals('<div id="kermit">',
|
||||
html_writer::start_div('', array('id' => 'kermit')));
|
||||
// No options.
|
||||
$this->assertEquals('<div>',
|
||||
html_writer::start_div());
|
||||
}
|
||||
|
||||
public function test_end_div() {
|
||||
$this->assertEquals('</div>', html_writer::end_div());
|
||||
}
|
||||
|
||||
public function test_span() {
|
||||
// All options.
|
||||
$this->assertEquals('<span class="frog" id="kermit">ribbit</span>',
|
||||
html_writer::span('ribbit', 'frog', array('id' => 'kermit')));
|
||||
// Combine class from attributes and $class.
|
||||
$this->assertEquals('<span class="amphibian frog">ribbit</span>',
|
||||
html_writer::span('ribbit', 'frog', array('class' => 'amphibian')));
|
||||
// Class only.
|
||||
$this->assertEquals('<span class="frog">ribbit</span>',
|
||||
html_writer::span('ribbit', 'frog'));
|
||||
// Attributes only.
|
||||
$this->assertEquals('<span id="kermit">ribbit</span>',
|
||||
html_writer::span('ribbit', '', array('id' => 'kermit')));
|
||||
// No options.
|
||||
$this->assertEquals('<span>ribbit</span>',
|
||||
html_writer::span('ribbit'));
|
||||
}
|
||||
|
||||
public function test_start_span() {
|
||||
// All options.
|
||||
$this->assertEquals('<span class="frog" id="kermit">',
|
||||
html_writer::start_span('frog', array('id' => 'kermit')));
|
||||
// Combine class from attributes and $class.
|
||||
$this->assertEquals('<span class="amphibian frog">',
|
||||
html_writer::start_span('frog', array('class' => 'amphibian')));
|
||||
// Class only.
|
||||
$this->assertEquals('<span class="frog">',
|
||||
html_writer::start_span('frog'));
|
||||
// Attributes only.
|
||||
$this->assertEquals('<span id="kermit">',
|
||||
html_writer::start_span('', array('id' => 'kermit')));
|
||||
// No options.
|
||||
$this->assertEquals('<span>',
|
||||
html_writer::start_span());
|
||||
}
|
||||
|
||||
public function test_end_span() {
|
||||
$this->assertEquals('</span>', html_writer::end_span());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user