forked from ifrpl/muyo
/
html.php
194 lines (184 loc) · 3.4 KB
/
html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
class Doctype
{
const VER_HTML_4_01 = 0;
const VER_XHTML_1_0 = 1;
const VER_XHTML_1_1 = 2;
const VER_XHTML_BASIC_1_1 = 3;
const VER_HTML_5 = 4;
const VER_MATH_ML_1_01 = 5;
//FIXME: compound support
public static $ver = array(
self::VER_HTML_4_01,
self::VER_XHTML_1_0,
self::VER_XHTML_1_1,
self::VER_XHTML_BASIC_1_1,
self::VER_HTML_5,
self::VER_MATH_ML_1_01,
);
private static $value=self::VER_HTML_5;
/**
* @param int $value
*/
public static function set( $value )
{
if( debug_assert_array_contains( self::$ver, $value ) )
{
self::$value = $value;
}
}
/**
* @return int
*/
public static function get()
{
return self::$value;
}
/**
* @param int $version
* @return bool
*/
public static function equals( $version )
{
return debug_assert_array_contains( self::$ver, $version ) && self::$value===$version;
}
/**
* @return bool
*/
public static function isXhtml()
{
return array_contains(
array(self::VER_XHTML_1_0, self::VER_XHTML_1_1, self::VER_XHTML_BASIC_1_1),
self::get()
);
}
}
if( !function_exists('html_doctype_set') )
{
/**
* @param int $version
* @see Doctype::$ver
*/
function html_doctype_set($version)
{
Doctype::set( $version );
}
}
if( !function_exists('html_doctype_get') )
{
/**
* @return int
* @see Doctype::$ver
*/
function html_doctype_get()
{
return Doctype::get();
}
}
if( !function_exists('html_tag') )
{
/**
* @param string $name
* @param array $attr
* @param string $content
* @return string
*/
function html_tag( $name, $attr, $content )
{
$attrChain = array(
array_filter_key_dg(function( $val, $key )
{
$skip = $val === false;
return !$skip;
})
);
if( Doctype::isXhtml() )
{
$attrChain []= array_map_val_dg(function( $val, $key )
{
if( true === $val )
{
$val = $key;
}
return $val;
});
}
$attrChain []= array_map_key_dg(function( $val, $key )
{
return preg_replace( '/([\t\n\f \/>"\'=]+)/', '', $key );
});
$flags = htmlspecialchars_flags();
$attrChain []= array_map_val_dg(function( $val, $key )use( $flags )
{
$val = str_wrap( htmlspecialchars( $val, ENT_QUOTES|$flags ), '"' );
return " {$key}={$val}";
});
$attrChain []= array_implode_dg('');
$attr = call_user_func_array( 'array_chain', array_merge( array($attr), $attrChain ) );
if( empty($content) )
{
if( Doctype::isXhtml() )
{
$ret = "<{$name}{$attr}/>";
}
else
{
$ret = "<{$name}{$attr}>";
}
}
else
{
$ret = "<{$name}{$attr}>{$content}</{$name}>";
}
return $ret;
}
}
if( !function_exists('html_from_string') )
{
/**
* @param string $string
* @return string
*/
function html_from_string( $string )
{
if( debug_assert( is_string($string), var_dump_human_compact( $string ) ) )
{
$ret = htmlspecialchars( $string, htmlspecialchars_flags() );
}
else
{
$ret = '';
}
return $ret;
}
}
if( !function_exists('htmlspecialchars_flags') )
{
/**
* @return int
*/
function htmlspecialchars_flags()
{
switch( Doctype::get() )
{
case Doctype::VER_HTML_4_01:
$flags = ENT_HTML401;
break;
case Doctype::VER_HTML_5:
$flags = ENT_HTML5;
break;
default:
if( Doctype::isXhtml() )
{
$flags = ENT_XHTML;
}
else
{
debug_enforce( false, "Unhandled doctype ".Doctype::get() );
$flags = null;
}
break;
}
return $flags;
}
}