18
18
namespace PhpOffice \PhpWord \Shared ;
19
19
20
20
use PhpOffice \PhpWord \Element \Section ;
21
+ use PhpOffice \PhpWord \SimpleType \Jc ;
22
+ use PhpOffice \PhpWord \TestHelperDOCX ;
21
23
22
24
/**
23
25
* Test class for PhpOffice\PhpWord\Shared\Html
26
+ * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
24
27
*/
25
28
class HtmlTest extends \PHPUnit \Framework \TestCase
26
29
{
@@ -43,7 +46,7 @@ public function testAddHtml()
43
46
44
47
// Styles
45
48
$ content .= '<p style="text-decoration: underline; text-decoration: line-through; '
46
- . 'text-align: center; color: #999; background-color: #000;"> ' ;
49
+ . 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic; "> ' ;
47
50
foreach ($ styles as $ style ) {
48
51
$ content .= "< {$ style }> {$ style }</ {$ style }> " ;
49
52
}
@@ -67,4 +70,109 @@ public function testAddHtml()
67
70
$ content .= '–   ²³¼½¾ ' ;
68
71
Html::addHtml ($ section , $ content );
69
72
}
73
+
74
+ /**
75
+ * Test that html already in body element can be read
76
+ * @ignore
77
+ */
78
+ public function testParseFullHtml ()
79
+ {
80
+ $ section = new Section (1 );
81
+ Html::addHtml ($ section , '<body><p>test paragraph1</p><p>test paragraph2</p></body> ' , true );
82
+
83
+ $ this ->assertCount (2 , $ section ->getElements ());
84
+ }
85
+
86
+ /**
87
+ * Test underline
88
+ */
89
+ public function testParseUnderline ()
90
+ {
91
+ $ html = '<u>test</u> ' ;
92
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
93
+ $ section = $ phpWord ->addSection ();
94
+ Html::addHtml ($ section , $ html );
95
+
96
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
97
+ $ this ->assertTrue ($ doc ->elementExists ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' ));
98
+ $ this ->assertEquals ('single ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' , 'w:val ' ));
99
+ }
100
+
101
+ /**
102
+ * Test text-decoration style
103
+ */
104
+ public function testParseTextDecoration ()
105
+ {
106
+ $ html = '<span style="text-decoration: underline;">test</span> ' ;
107
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
108
+ $ section = $ phpWord ->addSection ();
109
+ Html::addHtml ($ section , $ html );
110
+
111
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
112
+ $ this ->assertTrue ($ doc ->elementExists ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' ));
113
+ $ this ->assertEquals ('single ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' , 'w:val ' ));
114
+ }
115
+
116
+ /**
117
+ * Test text-align style
118
+ */
119
+ public function testParseTextAlign ()
120
+ {
121
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
122
+ $ section = $ phpWord ->addSection ();
123
+ Html::addHtml ($ section , '<p style="text-align: left;">test</p> ' );
124
+ Html::addHtml ($ section , '<p style="text-align: right;">test</p> ' );
125
+ Html::addHtml ($ section , '<p style="text-align: center;">test</p> ' );
126
+ Html::addHtml ($ section , '<p style="text-align: justify;">test</p> ' );
127
+
128
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
129
+ $ this ->assertTrue ($ doc ->elementExists ('/w:document/w:body/w:p/w:pPr/w:jc ' ));
130
+ $ this ->assertEquals ('start ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[1]/w:pPr/w:jc ' , 'w:val ' ));
131
+ $ this ->assertEquals ('end ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[2]/w:pPr/w:jc ' , 'w:val ' ));
132
+ $ this ->assertEquals ('center ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[3]/w:pPr/w:jc ' , 'w:val ' ));
133
+ $ this ->assertEquals ('both ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[4]/w:pPr/w:jc ' , 'w:val ' ));
134
+ }
135
+
136
+ /**
137
+ * Test parsing paragraph and span styles
138
+ */
139
+ public function testParseParagraphAndSpanStyle ()
140
+ {
141
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
142
+ $ section = $ phpWord ->addSection ();
143
+ Html::addHtml ($ section , '<p style="text-align: center;"><span style="text-decoration: underline;">test</span></p> ' );
144
+
145
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
146
+ $ this ->assertTrue ($ doc ->elementExists ('/w:document/w:body/w:p/w:pPr/w:jc ' ));
147
+ $ this ->assertEquals ('center ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[1]/w:pPr/w:jc ' , 'w:val ' ));
148
+ $ this ->assertEquals ('single ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u ' , 'w:val ' ));
149
+ }
150
+
151
+ /**
152
+ * Test parsing table
153
+ */
154
+ public function testParseTable ()
155
+ {
156
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
157
+ $ section = $ phpWord ->addSection ();
158
+ $ html = '
159
+ <table style="width: 50%; border: 6px #0000FF solid;">
160
+ <thead>
161
+ <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
162
+ <th>a</th>
163
+ <th>b</th>
164
+ <th>c</th>
165
+ </tr>
166
+ </thead>
167
+ <tbody>
168
+ <tr><td>1</td><td colspan="2">2</td></tr>
169
+ <tr><td>4</td><td>5</td><td>6</td></tr>
170
+ </tbody>
171
+ </table> ' ;
172
+ Html::addHtml ($ section , $ html );
173
+
174
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
175
+ // echo $doc->printXml();
176
+ // $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
177
+ }
70
178
}
0 commit comments