|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Tables |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource Tables.php |
|
6
|
|
|
* @created 12.10.2015 |
|
7
|
|
|
* @package chillerlan\bbcode\Modules\Html5 |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2015 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\bbcode\Modules\Html5; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\bbcode\Modules\ModuleInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Transforms table tags into HTML5, as HTML5 as possible... |
|
19
|
|
|
* |
|
20
|
|
|
* @link http://www.w3.org/TR/html5/tabular-data.html |
|
21
|
|
|
*/ |
|
22
|
|
|
class Tables extends Html5BaseModule implements ModuleInterface{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* An array of tags the module is able to process |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
* @see \chillerlan\bbcode\Modules\Tagmap::$tags |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $tags = ['col', 'colgroup', 'caption', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* An optional array of tags contained in self::$tags which are marked as "single tag" |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
* @see \chillerlan\bbcode\Modules\Tagmap::$singletags |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $singletags = ['col']; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Transforms the bbcode, called from BaseModuleInterface |
|
42
|
|
|
* |
|
43
|
|
|
* @return string a transformed snippet |
|
44
|
|
|
* @see \chillerlan\bbcode\Modules\BaseModuleInterface::transform() |
|
45
|
|
|
* @internal |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __transform():string{ |
|
48
|
|
|
|
|
49
|
|
|
switch(true){ |
|
50
|
|
|
case $this->tag === 'caption' && !empty($this->content): |
|
51
|
|
|
return $this->caption(); |
|
52
|
|
|
case $this->tag === 'col': |
|
53
|
|
|
return $this->col(); |
|
54
|
|
|
case $this->tag === 'colgroup' && !empty($this->content): |
|
55
|
|
|
return $this->colgroup(); |
|
56
|
|
|
case $this->tag === 'table' && (!empty($this->content) || $this->tag === 'caption'): |
|
57
|
|
|
return $this->table(); |
|
58
|
|
|
case ($this->tagIn(['thead', 'tbody', 'tfoot']) && !empty($this->content)) || $this->tag === 'tr': |
|
59
|
|
|
return $this->rows(); |
|
60
|
|
|
case $this->tagIn(['td', 'th']): |
|
61
|
|
|
return $this->cells(); |
|
62
|
|
|
default: |
|
63
|
|
|
return ''; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Processes [table] |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function table():string{ |
|
74
|
|
|
|
|
75
|
|
|
return '<table'.$this->getCssClass(['bb-table']) |
|
76
|
|
|
.$this->getStyle(['width' => $this->getAttribute('width')]) |
|
77
|
|
|
.'>'.$this->eol($this->content).'</table>'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Processes [col] |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function col():string{ |
|
86
|
|
|
$span = $this->getAttribute('span'); |
|
87
|
|
|
|
|
88
|
|
|
return '<col'.($span ? ' span="'.intval($span).'"' : '').$this->getCssClass().' />'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Processes [colgroup] |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function colgroup():string{ |
|
97
|
|
|
$span = $this->getAttribute('span'); |
|
98
|
|
|
|
|
99
|
|
|
return '<colgroup'.($span ? ' span="'.intval($span).'"' : '').$this->getCssClass().'>'.$this->eol($this->content).'</colgroup>'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Processes [caption] |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function caption():string{ |
|
108
|
|
|
return '<caption>'.$this->eol($this->content, $this->eol_token).'</caption>'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Processes [tr|thead|tbody|tfoot] |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function rows():string{ |
|
117
|
|
|
return '<'.$this->tag.'>'.$this->eol($this->content).'</'.$this->tag.'>'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Processes [td|th] |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function cells():string{ |
|
126
|
|
|
$abbr = $this->getAttribute('abbr'); |
|
127
|
|
|
$colspan = $this->getAttribute('colspan'); |
|
128
|
|
|
$rowspan = $this->getAttribute('rowspan'); |
|
129
|
|
|
|
|
130
|
|
|
if($colspan){ |
|
131
|
|
|
$colspan = ' colspan="'.intval($colspan).'"'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if($rowspan){ |
|
135
|
|
|
$rowspan = ' rowspan="'.intval($rowspan).'"'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if($this->tag === 'th' && $abbr){ |
|
139
|
|
|
$abbr = ' abbr="'.$abbr.'"'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return '<'.$this->tag.$colspan.$rowspan.$abbr.$this->getCellStyle().'>' |
|
143
|
|
|
.$this->eol($this->content, $this->eol_token) |
|
144
|
|
|
.'</'.$this->tag.'>'; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getCellStyle():string{ |
|
151
|
|
|
$align = $this->getAttribute('align'); |
|
152
|
|
|
$valign = $this->getAttribute('valign'); |
|
153
|
|
|
$width = $this->getAttribute('width'); |
|
154
|
|
|
$style = []; |
|
155
|
|
|
|
|
156
|
|
|
switch(true){ |
|
157
|
|
|
case $align && in_array($align, self::TEXT_ALIGN): |
|
158
|
|
|
$style['text-align'] = $align; |
|
159
|
|
|
break; |
|
160
|
|
|
case $valign && in_array($valign, ['baseline', 'bottom', 'middle', 'top']): |
|
161
|
|
|
$style['vertical-align'] = $valign; |
|
162
|
|
|
break; |
|
163
|
|
|
case $width: |
|
164
|
|
|
$style['width'] = $width; |
|
165
|
|
|
break; |
|
166
|
|
|
case $this->getAttribute('nowrap'): |
|
167
|
|
|
$style['white-space'] = 'nowrap'; |
|
168
|
|
|
break; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $this->getStyle($style); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|