Skip to content

Commit 2d60f32

Browse files
authored
Merge pull request #1 from oleibman/master
Master
2 parents b8346af + 00f9bb5 commit 2d60f32

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

src/PhpWord/Escaper/Rtf.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ class Rtf extends AbstractEscaper
2626
{
2727
protected function escapeAsciiCharacter($code)
2828
{
29-
if (20 > $code || $code >= 80) {
30-
return '{\u' . $code . '}';
29+
if ($code == 9) {
30+
return '{\\tab}';
31+
}
32+
if (0x20 > $code || $code >= 0x80) {
33+
return '{\\u' . $code . '}';
34+
}
35+
if ($code == 123 || $code == 125 || $code == 92) { // open or close brace or backslash
36+
return '\\' . chr($code);
3137
}
3238

3339
return chr($code);
3440
}
3541

3642
protected function escapeMultibyteCharacter($code)
3743
{
38-
return '\uc0{\u' . $code . '}';
44+
return '\\uc0{\\u' . $code . '}';
3945
}
4046

4147
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Escaper;
19+
20+
/**
21+
* Test class for PhpOffice\PhpWord\Escaper\RTF
22+
*/
23+
class RtfEscaper2Test extends \PHPUnit\Framework\TestCase
24+
{
25+
const HEADER = '\\pard\\nowidctlpar {\\cf0\\f0 ';
26+
const TRAILER = '}\\par';
27+
28+
public function escapestring($str)
29+
{
30+
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
31+
$parentWriter = new \PhpOffice\PhpWord\Writer\RTF();
32+
$element = new \PhpOffice\PhpWord\Element\Text($str);
33+
$txt = new \PhpOffice\PhpWord\Writer\RTF\Element\Text($parentWriter, $element);
34+
$txt2 = trim($txt->write());
35+
36+
return $txt2;
37+
}
38+
39+
public function expect($str)
40+
{
41+
return self::HEADER . $str . self::TRAILER;
42+
}
43+
44+
/**
45+
* Test special characters which require escaping
46+
*/
47+
public function testSpecial()
48+
{
49+
$str = 'Special characters { open brace } close brace \\ backslash';
50+
$expect = $this->expect('Special characters \\{ open brace \\} close brace \\\\ backslash');
51+
$this->assertEquals($expect, $this->escapestring($str));
52+
}
53+
54+
/**
55+
* Test accented character
56+
*/
57+
public function testAccent()
58+
{
59+
$str = 'Voilà - string with accented char';
60+
$expect = $this->expect('Voil\\uc0{\\u224} - string with accented char');
61+
$this->assertEquals($expect, $this->escapestring($str));
62+
}
63+
64+
/**
65+
* Test Hebrew
66+
*/
67+
public function testHebrew()
68+
{
69+
$str = 'Hebrew - שלום';
70+
$expect = $this->expect('Hebrew - \\uc0{\\u1513}\\uc0{\\u1500}\\uc0{\\u1493}\\uc0{\\u1501}');
71+
$this->assertEquals($expect, $this->escapestring($str));
72+
}
73+
74+
/**
75+
* Test tab
76+
*/
77+
public function testTab()
78+
{
79+
$str = "Here's a tab\tfollowed by more characters.";
80+
$expect = $this->expect("Here's a tab{\\tab}followed by more characters.");
81+
$this->assertEquals($expect, $this->escapestring($str));
82+
}
83+
}

tests/PhpWord/Writer/RTF/ElementTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
*/
2525
class ElementTest extends \PHPUnit\Framework\TestCase
2626
{
27+
public function removeCr($field)
28+
{
29+
return str_replace("\r\n", "\n", $field->write());
30+
}
31+
2732
/**
2833
* Test unmatched elements
2934
*/
@@ -46,7 +51,7 @@ public function testPageField()
4651
$element = new \PhpOffice\PhpWord\Element\Field('PAGE');
4752
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
4853

49-
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $field->write());
54+
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $this->removeCr($field));
5055
}
5156

5257
public function testNumpageField()
@@ -55,7 +60,7 @@ public function testNumpageField()
5560
$element = new \PhpOffice\PhpWord\Element\Field('NUMPAGES');
5661
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
5762

58-
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $field->write());
63+
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $this->removeCr($field));
5964
}
6065

6166
public function testDateField()
@@ -64,7 +69,7 @@ public function testDateField()
6469
$element = new \PhpOffice\PhpWord\Element\Field('DATE', array('dateformat' => 'd MM yyyy H:mm:ss'));
6570
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
6671

67-
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $field->write());
72+
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $this->removeCr($field));
6873
}
6974

7075
public function testIndexField()
@@ -73,6 +78,6 @@ public function testIndexField()
7378
$element = new \PhpOffice\PhpWord\Element\Field('INDEX');
7479
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
7580

76-
$this->assertEquals("{}\\par\n", $field->write());
81+
$this->assertEquals("{}\\par\n", $this->removeCr($field));
7782
}
7883
}

0 commit comments

Comments
 (0)