Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit e71d542

Browse files
author
xiajianjun
committed
update,bug fixed
1 parent f3ce86b commit e71d542

File tree

6 files changed

+668
-496
lines changed

6 files changed

+668
-496
lines changed

src/html/Element.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* file: Element.php
99
*/
1010

11-
namespace inhere\tools\front;
11+
namespace inhere\tools\html;
1212

1313
use inhere\tools\StdBase;
1414
use inhere\tools\exceptions\InvalidConfigException;
@@ -134,7 +134,6 @@ protected function _handleChildAndContent()
134134

135135
// 替换 直接占有 内容的位置
136136
if ( isset($childs[self::REPLACE_TEXT]) ) {
137-
138137
$string = '';
139138
foreach ($childs[self::REPLACE_TEXT] as $child) {
140139
$string .= rtrim( (string)$child );
@@ -143,8 +142,8 @@ protected function _handleChildAndContent()
143142
$content = $string . "\n";
144143
// vd($content,-4);
145144
}
146-
if ( isset($childs[self::BEFORE_TEXT]) ) {
147145

146+
if ( isset($childs[self::BEFORE_TEXT]) ) {
148147
$string = '';
149148
foreach ($childs[self::BEFORE_TEXT] as $child) {
150149
$string .= rtrim( (string)$child );
@@ -154,7 +153,6 @@ protected function _handleChildAndContent()
154153
}
155154

156155
if ( isset($childs[self::AFTER_TEXT]) ) {
157-
158156
$string = '';
159157
foreach ($childs[self::AFTER_TEXT] as $child) {
160158
$string .= rtrim( (string)$child );
@@ -172,9 +170,7 @@ protected function _handleChildAndContent()
172170
*/
173171
public function isAloneTag($name)
174172
{
175-
$aloneTags = ['area','br','base','col','frame','hr','img','input','link','mate','option','param' ];
176-
177-
return in_array($name, $aloneTags);
173+
return Html::isAloneTag($name);
178174
}
179175

180176

src/html/Html.php

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,37 @@
1313

1414
class Html
1515
{
16-
static public $buildedTag;
16+
public static $buildedTag;
1717

18-
//Independent lables
19-
static public $aloneTags = [
20-
'area','br','base','col','frame','hr','img','input','link','mate' ,'param'
21-
];
18+
// Independent lables
19+
public static $aloneTags = [
20+
'area','br','base','col','frame','hr','img','input','link','mate' ,'param'
21+
];
2222

2323
// closing tag | Ditags
24-
static public $ditags = [];
24+
public static $ditags = [];
2525

26-
static public $eleAttr = [
27-
'id',
28-
'class',
29-
'style',
30-
'type',
31-
'href',
32-
'src',
33-
];
26+
public static $eleAttr = [ 'id', 'class', 'style', 'type', 'href', 'src', ];
3427

3528
/**
3629
* @param $name
3730
* @return bool
3831
*/
39-
static public function isAloneTag($name)
32+
public static function isAloneTag($name)
4033
{
41-
$aloneTags = [ 'area','br','base','col','frame','hr','img','input','link','mate' ,'param' ];
42-
43-
return in_array(trim($name), $aloneTags);
34+
return in_array(trim($name), static::$aloneTags);
4435
}
4536

4637
/**
4738
* style tag
4839
* @param string $content
4940
* @return string
5041
*/
51-
static public function style($content, array $attrs=[])
42+
public static function style($content, array $attrs=[])
5243
{
53-
$attrs = array_merge( array('type' =>"text/css"), $attrs );
44+
$attrs = array_merge( ['type' =>"text/css"], $attrs );
5445

55-
return self::tag('style', PHP_EOL . trim($content) . PHP_EOL,$attrs);
46+
return static::tag('style', PHP_EOL . trim($content) . PHP_EOL,$attrs);
5647
}
5748

5849
/**
@@ -61,22 +52,22 @@ static public function style($content, array $attrs=[])
6152
* @param array $attrs
6253
* @return string
6354
*/
64-
static public function link($href, array $attrs=[])
55+
public static function link($href, array $attrs=[])
6556
{
6657
$attrs = array_merge(
6758
[
6859
'type' => "text/css",
6960
'rel' => 'stylesheet',
70-
'href' => $href,
61+
'href' => $href,
7162
],
7263
$attrs );
7364

74-
return self::tag('link',null,$attrs);
65+
return static::tag('link',null,$attrs);
7566
}
7667

77-
static public function cssLink($href, array $attrs=[])
68+
public static function cssLink($href, array $attrs=[])
7869
{
79-
return self::link($href, $attrs);
70+
return static::link($href, $attrs);
8071
}
8172

8273
/**
@@ -85,11 +76,11 @@ static public function cssLink($href, array $attrs=[])
8576
* @param array $attrs
8677
* @return string
8778
*/
88-
static public function scriptCode($content=null, array $attrs=[])
79+
public static function scriptCode($content=null, array $attrs=[])
8980
{
9081
$attrs = array_merge( array('type' => 'text/javascript'), $attrs );
9182

92-
return self::tag('script', PHP_EOL . trim($content) . PHP_EOL,$attrs);
83+
return static::tag('script', PHP_EOL . trim($content) . PHP_EOL,$attrs);
9384
}
9485

9586
/**
@@ -98,7 +89,7 @@ static public function scriptCode($content=null, array $attrs=[])
9889
* @param array $attrs
9990
* @return string
10091
*/
101-
static public function script($src, array $attrs=[])
92+
public static function script($src, array $attrs=[])
10293
{
10394
$attrs = array_merge(
10495
[
@@ -107,22 +98,25 @@ static public function script($src, array $attrs=[])
10798
],
10899
$attrs );
109100

110-
return self::tag('script',null,$attrs);
101+
return static::tag('script',null,$attrs);
111102
}
112103

113-
static public function siteIcon($url)
104+
public static function siteIcon($url)
114105
{
115-
return '<link rel="icon" href="' . $url . '" type="image/x-icon"/>'."\n".
116-
'<link rel="shortcut icon" href="' . $url . '" type="image/x-icon"/>';
106+
return <<<EOF
107+
<link rel="icon" href="$url" type="image/x-icon"/>
108+
<link rel="shortcut icon" href="$url" type="image/x-icon"/>
109+
EOF;
117110
}
118111

119-
static public function a($content, $url, array $attrs=[])
112+
public static function a($content, $url, array $attrs=[])
120113
{
121-
$url = empty($url) ? 'javascript:void();' : $url;
122-
123-
$aLink = self::tag('a', $content, array_merge(['href'=>$url], $attrs) );
114+
$url = $url ? : 'javascript:void(0);';
124115

125-
return $aLink;
116+
return static::tag('a', $content, array_merge([
117+
'href' => $url,
118+
'title'=> $content,
119+
], $attrs) );
126120
}
127121

128122
/**
@@ -131,11 +125,11 @@ static public function a($content, $url, array $attrs=[])
131125
* @return string
132126
* @internal param string $alt
133127
*/
134-
static public function img($src, array $attrs=[])
128+
public static function img($src, array $attrs=[])
135129
{
136-
$newAttrs = array_merge( array('src'=>$src), $attrs);
130+
$newAttrs = array_merge(['src'=>$src], $attrs);
137131

138-
return self::tag('img',null,$newAttrs);
132+
return static::tag('img',null,$newAttrs);
139133
}
140134

141135
/**
@@ -144,11 +138,11 @@ static public function img($src, array $attrs=[])
144138
* @return string
145139
* @internal param string $type
146140
*/
147-
static public function button($content, array $attrs=[])
141+
public static function button($content, array $attrs=[])
148142
{
149143
$attrs = array_merge(['type'=>'button'], $attrs);
150144

151-
$button = self::tag('button',$content, $attrs);
145+
$button = static::tag('button',$content, $attrs);
152146

153147
return $button;
154148
}
@@ -159,32 +153,32 @@ static public function button($content, array $attrs=[])
159153
* @return string
160154
* @internal param string $content
161155
*/
162-
static public function input($type='text', array $attrs=[])
156+
public static function input($type='text', array $attrs=[])
163157
{
164158
$attrs = array_merge(['type'=>$type], $attrs);
165159

166-
$input = self::tag('input',null, $attrs );
160+
$input = static::tag('input',null, $attrs);
167161

168162
return $input;
169163
}
164+
170165
//////////////////////////////////////// form tag ////////////////////////////////////////
171166

172-
static public function startForm($action='', $method = 'post', array $attrs=[])
167+
public static function startForm($action='', $method = 'post', array $attrs=[])
173168
{
174169
$attrs = array_merge( [
175170
'action' => $action,
176171
'method' => $method,
177172
], $attrs );
178173

179-
return self::startTag('form', $attrs);
174+
return static::startTag('form', $attrs);
180175
}
181176

182-
static public function endForm()
177+
public static function endForm()
183178
{
184-
return self::endTag('form');
179+
return static::endTag('form');
185180
}
186181

187-
188182
//////////////////////////////////////// create tag ////////////////////////////////////////
189183

190184
//Independent element
@@ -194,11 +188,9 @@ static public function endForm()
194188
* @param array $attrs
195189
* @return string
196190
*/
197-
static public function tag($name, $content='', array $attrs=[])
191+
public static function tag($name, $content='', array $attrs=[])
198192
{
199-
$name = strtolower(trim($name));
200-
201-
if ( !$name ) {
193+
if ( !$name = strtolower(trim($name)) ) {
202194
return '';
203195
}
204196

@@ -212,8 +204,8 @@ static public function tag($name, $content='', array $attrs=[])
212204
unset($attrs['text']);
213205
}
214206

215-
$eleString = self::startTag($name, $attrs) . $content;
216-
$eleString .= self::isAloneTag($name) ? "\n" : self::endTag($name);
207+
$eleString = static::startTag($name, $attrs) . $content;
208+
$eleString .= static::isAloneTag($name) ? "\n" : static::endTag($name);
217209

218210
return $eleString;
219211
}
@@ -224,28 +216,28 @@ static public function tag($name, $content='', array $attrs=[])
224216
* @param array $attrs
225217
* @return string
226218
*/
227-
static public function startTag($name, array $attrs=[])
219+
public static function startTag($name, array $attrs=[])
228220
{
229-
return sprintf("\n<%s %s>", strtolower(trim($name)), self::_buildAttr($attrs));
221+
return sprintf("\n<%s %s>", strtolower(trim($name)), static::_buildAttr($attrs));
230222
}
231223

232224
/**
233225
* @param $name
234226
* @return string
235227
*/
236-
static public function closeTag($name)
228+
public static function closeTag($name)
237229
{
238-
return self::endTag($name);
230+
return static::endTag($name);
239231
}
240232

241233
/**
242234
* 结束标签
243235
* @param string$name
244236
* @return string
245237
*/
246-
static public function endTag($name)
238+
public static function endTag($name)
247239
{
248-
return "</".strtolower(trim($name)).">\n";
240+
return '</'.strtolower(trim($name)).">\n";
249241
}
250242

251243
/**
@@ -269,7 +261,7 @@ static protected function _buildAttr($attr, $value='')
269261
$attrs = [];
270262

271263
foreach ($attr as $name => $val) {
272-
$attrs[] = self::_buildAttr($name,$val);
264+
$attrs[] = static::_buildAttr($name,$val);
273265
}
274266

275267
return implode(' ', $attrs);
@@ -286,7 +278,7 @@ static protected function _buildAttr($attr, $value='')
286278
* @return string the encoded data
287279
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
288280
*/
289-
static public function encode($text, $charset= 'utf-8')
281+
public static function encode($text, $charset= 'utf-8')
290282
{
291283
return htmlspecialchars($text,ENT_QUOTES, 'utf-8');
292284
}
@@ -297,7 +289,7 @@ static public function encode($text, $charset= 'utf-8')
297289
* @return string the decoded data
298290
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
299291
*/
300-
static public function decode($text)
292+
public static function decode($text)
301293
{
302294
return htmlspecialchars_decode($text,ENT_QUOTES);
303295
}
@@ -307,9 +299,9 @@ static public function decode($text)
307299
* @return array the encoded data
308300
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
309301
*/
310-
static public function encodeArray($data, $charset= 'utf-8')
302+
public static function encodeArray($data, $charset= 'utf-8')
311303
{
312-
$d =[];
304+
$d = [];
313305

314306
foreach($data as $key=>$value) {
315307
if (is_string($key)) {
@@ -319,7 +311,7 @@ static public function encodeArray($data, $charset= 'utf-8')
319311
if (is_string($value)) {
320312
$value = htmlspecialchars($value,ENT_QUOTES,$charset);
321313
} elseif (is_array($value)) {
322-
$value = self::encodeArray($value);
314+
$value = static::encodeArray($value);
323315
}
324316

325317
$d[$key] = $value;

0 commit comments

Comments
 (0)