1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ariadne Component Library. |
5
|
|
|
* |
6
|
|
|
* (c) Muze <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace arc; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class contains a html parser and is a proxy for the html Writer. |
16
|
|
|
* Any method statically called on this class |
17
|
|
|
* will reroute the call to the HTML writer instance at |
18
|
|
|
* \arc\html::$writer. Except the following methods: |
19
|
2 |
|
* parse, name, value, attribute, doctype, preamble, comment, cdata |
20
|
|
|
* If you need those, use the Writer instance directly |
21
|
2 |
|
*/ |
22
|
2 |
|
class html extends xml |
23
|
1 |
|
{ |
24
|
2 |
|
/** |
25
|
|
|
* @var html\Writer The writer instance to use by default |
26
|
|
|
*/ |
27
|
3 |
|
public static $writer=null; |
28
|
|
|
|
29
|
3 |
|
public static function __callStatic( $name, $args ) |
30
|
3 |
|
{ |
31
|
|
|
if ( !isset(self::$writer) ) { |
32
|
|
|
self::$writer = new html\Writer(); |
33
|
|
|
} |
34
|
|
|
return call_user_func_array( [ self::$writer, $name ], $args ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* This parses an HTML string and returns a Proxy |
39
|
|
|
* @param string|Proxy $html |
40
|
|
|
* @param string $encoding |
41
|
|
|
* @return Proxy |
42
|
|
|
* @throws \arc\Exception |
43
|
|
|
*/ |
44
|
|
|
public static function parse( $html=null, $encoding = null ) |
45
|
|
|
{ |
46
|
|
|
$parser = new html\Parser(); |
47
|
|
|
return $parser->parse( $html, $encoding ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns a guaranteed valid HTML attribute value. Removes illegal characters. |
52
|
|
|
* @param string|array|bool $value |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
static public function value( $value ) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if ( is_array( $value ) ) { |
58
|
|
|
$content = array_reduce( $value, function( $result, $value ) |
59
|
|
|
{ |
60
|
|
|
return $result . ' ' . static::value( $value ); |
61
|
|
|
} ); |
62
|
|
|
} else if ( is_bool( $value ) ) { |
63
|
|
|
$content = $value ? 'true' : 'false'; |
64
|
|
|
} else { |
65
|
|
|
$content = htmlspecialchars( (string) $value, ENT_QUOTES, 'UTF-8' ); |
66
|
|
|
} |
67
|
|
|
return $content; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns a guaranteed valid HTML attribute. Removes illegal characters. |
72
|
|
|
* Allows for 'naked' attributes, without a value. |
73
|
|
|
* @param string $name |
74
|
|
|
* @param string|array|bool $value |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
static public function attribute( $name, $value ) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ($name === $value) { |
80
|
|
|
return ' ' . self::name( $name ); |
81
|
|
|
} else if (is_numeric( $name )) { |
82
|
|
|
return ' ' . self::name( $value ); |
83
|
|
|
} else { |
84
|
|
|
return \arc\xml::attribute( $name, $value ); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns a HTML doctype string |
90
|
|
|
* @param string $version The doctype version to use, available are: 'html5', 'html4' (strict), 'transitional' (html4) and 'xhtml' |
91
|
|
|
* @retun string |
92
|
|
|
*/ |
93
|
|
|
static public function doctype( $version='html5' ) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$doctypes = [ |
96
|
|
|
'html5' => '<!doctype html>', |
97
|
|
|
'html4' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', |
98
|
|
|
'transitional' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', |
99
|
|
|
'frameset' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">', |
100
|
|
|
'xhtml' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' |
101
|
|
|
]; |
102
|
|
|
return isset( $doctypes[$version] ) ? $doctypes[$version] : $doctypes['html5']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|