From caf9f2330e9686a478a915d7bc9868abf813e151 Mon Sep 17 00:00:00 2001 From: "Mark N. Amoin" Date: Mon, 7 Nov 2016 17:41:46 +0800 Subject: [PATCH] WIP. --- application/Config/DocTypes.php | 33 ++ system/Helpers/html_helper.php | 600 ++++++++++++++++++++++++++++++++ 2 files changed, 633 insertions(+) create mode 100755 application/Config/DocTypes.php create mode 100755 system/Helpers/html_helper.php diff --git a/application/Config/DocTypes.php b/application/Config/DocTypes.php new file mode 100755 index 000000000000..37830984c31f --- /dev/null +++ b/application/Config/DocTypes.php @@ -0,0 +1,33 @@ + '', + 'xhtml1-strict' => '', + 'xhtml1-trans' => '', + 'xhtml1-frame' => '', + 'xhtml-basic11' => '', + 'html5' => '', + 'html4-strict' => '', + 'html4-trans' => '', + 'html4-frame' => '', + 'mathml1' => '', + 'mathml2' => '', + 'svg10' => '', + 'svg11' => '', + 'svg11-basic' => '', + 'svg11-tiny' => '', + 'xhtml-math-svg-xh' => '', + 'xhtml-math-svg-sh' => '', + 'xhtml-rdfa-1' => '', + 'xhtml-rdfa-2' => '' + ]; +} \ No newline at end of file diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php new file mode 100755 index 000000000000..dc93c6c7f759 --- /dev/null +++ b/system/Helpers/html_helper.php @@ -0,0 +1,600 @@ +' + . $content + . ''; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ul')) +{ + /** + * Unordered List + * + * Generates an HTML unordered list from an single or + * multi-dimensional array. + * + * @param array $list + * @param string $attributes + * @return string + */ + function ul(array $list, string $attributes = ''): string + { + return _list('ul', $list, $attributes); + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ol')) +{ + /** + * Ordered List + * + * Generates an HTML ordered list from an single or multi-dimensional array. + * + * @param array $list + * @param string $attributes + * @return string + */ + function ol(array $list, string $attributes = ''): string + { + return _list('ol', $list, $attributes); + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('_list')) +{ + /** + * Generates the list + * + * Generates an HTML ordered list from an single or multi-dimensional array. + * + * @param string $type + * @param array $list + * @param string $attributes + * @param int $depth + * @return string + */ + function _list + ( + string $type = 'ul', + array $list = [], + string $attributes = '', + int $depth = 0 + ): string + { + // If an array wasn't submitted there's nothing to do... + if ( ! is_array($list)) + { + return $list; + } + + // Set the indentation based on the depth + $out = str_repeat(' ', $depth) + // Write the opening list tag + . '<' . $type . _stringify_attributes($attributes) . ">\n"; + + + // Cycle through the list elements. If an array is + // encountered we will recursively call _list() + + static $_last_list_item = ''; + foreach ($list as $key => $val) + { + $_last_list_item = $key; + + $out .= str_repeat(' ', $depth + 2) . '
  • '; + + if ( ! is_array($val)) + { + $out .= $val; + } + else + { + $out .= $_last_list_item + . "\n" + . _list($type, $val, '', $depth + 4) + . str_repeat(' ', $depth + 2); + } + + $out .= "
  • \n"; + } + + // Set the indentation for the closing tag and apply it + return $out . str_repeat(' ', $depth) . '\n"; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('img')) +{ + /** + * Image + * + * Generates an element + * + * @param mixed $src + * @param bool $indexPage + * @param string $attributes + * @return string + */ + function img + ( + $src = '', + bool $indexPage = false, + string $attributes = '' + ): string + { + if ( ! is_array($src) ) + { + $src = ['src' => $src]; + } + + // If there is no alt attribute defined, set it to an empty string + if ( ! isset($src['alt'])) + { + $src['alt'] = ''; + } + + $img = ' $v) + { + if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) + { + //$config = new \Config\App(); + if ($indexPage === true) + { + $img .= ' src="' . get_instance()->config->site_url($v) + . '"'; + } + else + { + $img .= ' src="' + . get_instance()->config->slash_item('base_url') + . $v + . '"'; + } + } + else + { + $img .= ' ' . $k . '="' . $v . '"'; + } + } + + return $img . _stringify_attributes($attributes) . ' />'; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('doctype')) +{ + /** + * Doctype + * + * Generates a page document type declaration + * + * Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans, + * xhtml-frame, html4-strict, html4-trans, and html4-frame. + * All values are saved in the doctypes config file. + * + * @param mixed $type The doctype to be generated + * @return string + */ + function doctype($type = 'xhtml1-strict'): string + { + $doctypes = null; + $env = ENVIRONMENT; + $doctypes = Config\DocTypes::$list; + $customDocTypesPath = APPPATH . "Config/{$env}/DocTypes.php"; + if (file_exists($customDocTypesPath)) + { + $customDocTypesNs = "Config\{$env}\DocTypes"; + $doctypes = $customDocTypesNs::$list; + } + return isset($doctypes[$type]) ? $doctypes[$type] : false; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('script_tag')) +{ + /** + * Script + * + * Generates link to a JS file + * + * @param mixed script hrefs or an array + * @param string title + * @param bool should indexPage be added to the css path + * @return string + */ + function script_tag + ( + $source = '', + string $title = '', + bool $indexPage = false + ): string + { + $CI =& get_instance(); + $script = '\n"; + } + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('link_tag')) +{ + /** + * Link + * + * Generates link to a CSS file + * + * @param mixed stylesheet hrefs or an array + * @param string rel + * @param string type + * @param string title + * @param string media + * @param bool should indexPage be added to the css path + * @return string + */ + function link_tag + ( + $href = '', + string $rel = 'stylesheet', + string $type = 'text/css', + string $title = '', + string $media = '', + bool $indexPage = false + ): string + { + $CI =& get_instance(); + $link = ' $v) + { + if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v)) + { + if ($indexPage === true) + { + $link .= 'href="' + .$CI->config->site_url($v) + . '" '; + } + else + { + $link .= 'href="' + . $CI->config->slash_item('base_url') + . $v + . '" '; + } + } + else + { + $link .= $k + . '="' + . $v + . '" '; + } + } + } + else + { + if (preg_match('#^([a-z]+:)?//#i', $href)) + { + $link .= 'href="' . $href . '" '; + } + elseif ($indexPage === true) + { + $link .= 'href="' . $CI->config->site_url($href) . '" '; + } + else + { + $link .= 'href="' + . $CI->config->slash_item('base_url') + . $href + . '" '; + } + + $link .= 'rel="' . $rel . '" type="' . $type . '" '; + + if ($media !== '') + { + $link .= 'media="' . $media . '" '; + } + + if ($title !== '') + { + $link .= 'title="' . $title . '" '; + } + } + + return $link . "/>\n"; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('video')) +{ + /** + * @param array $options Either source or track. + * @param string $unsupportedMessage [] + * @param string $attributes [] + * @return string + * + * Example: + * video + * ( + * [ + * source('movie.mp4', 'video/mp4'), + * source('movie.ogg', 'video/ogg') + * ], + * 'Your browser does not support the video tag.' + * ); + */ + function video + ( + array $options, + string $unsupportedMessage = '', + string $attributes = '' + ): string + { + return _media('video', $options, $unsupportedMessage, $attributes); + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('audio')) +{ + /** + * @param array $options Either source or track. + * @param string $unsupportedMessage [] + * @param string $attributes [] + * @return string + * + * Example: + * audio + * ( + * [ + * source('sound.ogg', 'audio/ogg'), + * source('sound.mpeg', 'audio/mpeg') + * ], + * 'Your browser does not support the audio tag.' + * ); + */ + function audio + ( + array $options, + string $unsupportedMessage = '', + string $attributes = '' + ): string + { + return _media('audio', $options, $unsupportedMessage, $attributes); + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('_media')) +{ + /** + * + */ + function _media + ( + string $name, + array $options, + string $unsupportedMessage = '', + string $attributes = '' + ): string + { + $media = '<' . $name; + + if(!empty($attributes)) + { + $media .= " $attributes"; + } + + foreach($options as $option) + { + $media .= $option; + if(! empty($unsupportedMessage)) + { + $media .= $unsupportedMessage; + } + } + $media .= '>'; + + return $media; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('source')) +{ + /** + * + */ + function source(string $name, string $mimeType): string + { + return ''; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('object')) +{ + /** + * + */ + function track + ( + string $source, + string $kind, + string $sourceLanguage, + string $label + ): string + { + return ''; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('object')) +{ + function object(string $data): string + { + return ''; + } +} + +// ------------------------------------------------------------------------ + +if ( ! function_exists('embed')) +{ + function embed(string $source): string + { + return ''; + } +} + +//http://www.w3schools.com/html/html_media.asp +//http://www.w3schools.com/tags/ref_av_dom.asp \ No newline at end of file