From 103abc5f6d665311ecb2912ea14d4ef7a89b1527 Mon Sep 17 00:00:00 2001 From: Jan Chren Date: Fri, 5 Sep 2014 23:11:02 +0200 Subject: [PATCH] assets attributes implemented --- system/src/Grav/Common/Assets.php | 44 ++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 78e389767..906b9615f 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -262,7 +262,7 @@ class Assets * * @return string */ - public function css() + public function css($attributes = []) { if( ! $this->css) return null; @@ -271,21 +271,21 @@ class Assets usort($this->css, function ($a, $b) {return $a['priority'] - $b['priority'];}); $this->css = array_reverse($this->css); - + $attributes = $this->attributes(array_merge([ 'type' => 'text/css', 'rel' => 'stylesheet' ], $attributes)); $output = ''; if($this->css_pipeline) { - $output .= ''."\n"; + $output .= ''."\n"; foreach ($this->css_no_pipeline as $file) { - $output .= ''."\n"; + $output .= ''."\n"; } return $output; } foreach($this->css as $file) - $output .= ''."\n"; + $output .= ''."\n"; return $output; } @@ -295,7 +295,7 @@ class Assets * * @return string */ - public function js() + public function js($attributes = []) { if( ! $this->js) return null; @@ -304,22 +304,48 @@ class Assets usort($this->js, function ($a, $b) {return $a['priority'] - $b['priority'];}); $this->js = array_reverse($this->js); + $attributes = $this->attributes(array_merge([ 'type' => 'text/javascript' ], $attributes)); + $output = ''; if($this->js_pipeline) { - $output .= ''."\n"; + $output .= ''."\n"; foreach ($this->js_no_pipeline as $file) { - $output .= ''."\n"; + $output .= ''."\n"; } return $output; } foreach($this->js as $file) - $output .= ''."\n"; + $output .= ''."\n"; return $output; } + /** + * Build an HTML attribute string from an array. + * + * @param array $attributes + * @return string + */ + protected function attributes(array $attributes){ + $html = ''; + + foreach ( $attributes as $key => $value) + { + // For numeric keys we will assume that the key and the value are the same + // as this will convert HTML attributes such as "required" to a correct + // form like required="required" instead of using incorrect numerics. + if (is_numeric($key)) $key = $value; + if (is_array($value)) $value = implode(' ', $value); + + $element = $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8', false).'"'; + $html .= ' '.$element; + } + + return $html; + } + /** * Add/replace collection. *