1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Tables |
4
|
|
|
* |
5
|
|
|
* @filesource Tables.php |
6
|
|
|
* @created 24.04.2018 |
7
|
|
|
* @package chillerlan\BBCode\Output\HTML |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2018 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\BBCode\Output\HTML; |
14
|
|
|
|
15
|
|
|
class Tables extends HTMLModuleAbstract{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $tags = ['col', 'colgroup', 'caption', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $singletags = ['col']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function transform():string{ |
31
|
|
|
|
32
|
|
|
if(in_array($this->tag, ['td', 'th'], true)){ |
33
|
|
|
return $this->__cells(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if(in_array($this->tag, ['thead', 'tbody', 'tfoot'], true)){ |
37
|
|
|
return $this->__body(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
protected function table():string{ |
47
|
|
|
|
48
|
|
|
if(empty($this->content)){ |
49
|
|
|
return ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// @todo preg_match('/^[\d\.]+(px|pt|em|%)$/', $value): |
53
|
|
|
return '<table class="bb-table" style="width:'.$this->getAttribute('width', '100%').';">'.$this->eol($this->content).'</table>'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function col():string{ |
60
|
|
|
$span = $this->getAttribute('span'); |
61
|
|
|
|
62
|
|
|
return '<col'.($span ? ' span="'.intval($span).'"' : '').' />'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function colgroup():string{ |
69
|
|
|
|
70
|
|
|
if(empty($this->content)){ |
71
|
|
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$span = $this->getAttribute('span'); |
75
|
|
|
|
76
|
|
|
return '<colgroup'.($span ? ' span="'.intval($span).'"' : '').'>'.$this->eol($this->content).'</colgroup>'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function caption():string{ |
83
|
|
|
|
84
|
|
|
if(empty($this->content)){ |
85
|
|
|
return ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return '<caption>'.$this->eol($this->content, '<br/>').'</caption>'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function tr():string{ |
95
|
|
|
return '<tr>'.$this->eol($this->content).'</tr>'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function __body():string{ |
102
|
|
|
return !empty($this->content) ? '<'.$this->tag.'>'.$this->eol($this->content).'</'.$this->tag.'>' : ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Processes [td|th] |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function __cells():string{ |
111
|
|
|
$abbr = $this->getAttribute('abbr'); |
112
|
|
|
$colspan = $this->getAttribute('colspan'); |
113
|
|
|
$rowspan = $this->getAttribute('rowspan'); |
114
|
|
|
|
115
|
|
|
if($colspan){ |
116
|
|
|
$colspan = ' colspan="'.intval($colspan).'"'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if($rowspan){ |
120
|
|
|
$rowspan = ' rowspan="'.intval($rowspan).'"'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if($this->tag === 'th' && $abbr){ |
124
|
|
|
$abbr = ' abbr="'.$abbr.'"'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return '<'.$this->tag.$colspan.$rowspan.$abbr.$this->__getCellStyle().'>'.$this->eol($this->content, '<br/>').'</'.$this->tag.'>'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function __getCellStyle():string{ |
134
|
|
|
$align = $this->getAttribute('align'); |
135
|
|
|
$valign = $this->getAttribute('valign'); |
136
|
|
|
$width = $this->getAttribute('width'); |
137
|
|
|
$style = []; |
138
|
|
|
|
139
|
|
|
switch(true){ |
140
|
|
|
case $align && in_array($align, ['left', 'center', 'right', 'justify', 'start', 'end', 'inherit',]): |
141
|
|
|
$style['text-align'] = $align; |
142
|
|
|
break; |
143
|
|
|
case $valign && in_array($valign, ['baseline', 'bottom', 'middle', 'top']): |
144
|
|
|
$style['vertical-align'] = $valign; |
145
|
|
|
break; |
146
|
|
|
case $width: |
147
|
|
|
// @todo preg_match('/^[\d\.]+(px|pt|em|%)$/', $value): |
148
|
|
|
$style['width'] = $width; |
149
|
|
|
break; |
150
|
|
|
case $this->getAttribute('nowrap'): |
151
|
|
|
$style['white-space'] = 'nowrap'; |
152
|
|
|
break; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return !empty($style) ? ' style="'.implode(';', $style).'"' : ''; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|