getkirby-v2 / toolkit

This is the deprecated toolkit for Kirby v2.
http://getkirby.com
81 stars 50 forks source link

Add slash to self-closing void elements #161

Closed webgefrickel closed 7 years ago

webgefrickel commented 8 years ago

I ran into an issue with a very strict HTML/Style-Validator used internally on another project with kirby. The issue is with void-HTML-elements, an the HTML-Validator was not to fond of sth. like this

<link rel="stylesheet" href="...">

compared to the expected

<link rel="stylesheet" href="..." />

(with the closing />)

I know that this is absolutely not necessary according to the HTML5-spec, but would you be OK with an option to include this in the toolkit? This would (if I understand the toolkit correctly and dug into it deep enough) be just a minor change in the bootstrap.php and the function lib/html.php - tag, like:

if(!defined('VOID')) define('VOID', ' /');

and then in lib/html.php

  public static function tag($name, $content = null, $attr = array()) {
    ...
    if(static::isVoid($name)) {
      $html .= VOID . '>';
    } else {
      $html .= '>' . $content . '</' . $name . '>';
    }
    return $html;
  }

One could then easily change this behaviour in say the index.php for the kirby instance with one the two possibilities (or make it a boolean?):

// if(!defined('VOID')) define('VOID', ' /');
if(!defined('VOID')) define('VOID', '');

I wanted to ask first before creating a pull request :)

bastianallgeier commented 7 years ago

It has been ages, but it's possible now.

You can use the html::$void attribute to set the ending:

html::$void = ' />';
echo html::tag('hr');
// <hr />