Merge pull request #1755 from oleibman/master
Improvements in RTF writer
This commit is contained in:
commit
5940d18ab2
|
|
@ -26,8 +26,14 @@ class Rtf extends AbstractEscaper
|
||||||
{
|
{
|
||||||
protected function escapeAsciiCharacter($code)
|
protected function escapeAsciiCharacter($code)
|
||||||
{
|
{
|
||||||
if (20 > $code || $code >= 80) {
|
if ($code == 9) {
|
||||||
return '{\u' . $code . '}';
|
return '{\\tab}';
|
||||||
|
}
|
||||||
|
if (0x20 > $code || $code >= 0x80) {
|
||||||
|
return '{\\u' . $code . '}';
|
||||||
|
}
|
||||||
|
if ($code == 123 || $code == 125 || $code == 92) { // open or close brace or backslash
|
||||||
|
return '\\' . chr($code);
|
||||||
}
|
}
|
||||||
|
|
||||||
return chr($code);
|
return chr($code);
|
||||||
|
|
@ -35,7 +41,7 @@ class Rtf extends AbstractEscaper
|
||||||
|
|
||||||
protected function escapeMultibyteCharacter($code)
|
protected function escapeMultibyteCharacter($code)
|
||||||
{
|
{
|
||||||
return '\uc0{\u' . $code . '}';
|
return '\\uc0{\\u' . $code . '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
||||||
|
* word processing documents.
|
||||||
|
*
|
||||||
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
||||||
|
* General Public License version 3 as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the LICENSE
|
||||||
|
* file that was distributed with this source code. For the full list of
|
||||||
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
||||||
|
*
|
||||||
|
* @see https://github.com/PHPOffice/PHPWord
|
||||||
|
* @copyright 2010-2018 PHPWord contributors
|
||||||
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpWord\Escaper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for PhpOffice\PhpWord\Escaper\RTF
|
||||||
|
*/
|
||||||
|
class RtfEscaper2Test extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
const HEADER = '\\pard\\nowidctlpar {\\cf0\\f0 ';
|
||||||
|
const TRAILER = '}\\par';
|
||||||
|
|
||||||
|
public function escapestring($str)
|
||||||
|
{
|
||||||
|
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
|
||||||
|
$parentWriter = new \PhpOffice\PhpWord\Writer\RTF();
|
||||||
|
$element = new \PhpOffice\PhpWord\Element\Text($str);
|
||||||
|
$txt = new \PhpOffice\PhpWord\Writer\RTF\Element\Text($parentWriter, $element);
|
||||||
|
$txt2 = trim($txt->write());
|
||||||
|
|
||||||
|
return $txt2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function expect($str)
|
||||||
|
{
|
||||||
|
return self::HEADER . $str . self::TRAILER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test special characters which require escaping
|
||||||
|
*/
|
||||||
|
public function testSpecial()
|
||||||
|
{
|
||||||
|
$str = 'Special characters { open brace } close brace \\ backslash';
|
||||||
|
$expect = $this->expect('Special characters \\{ open brace \\} close brace \\\\ backslash');
|
||||||
|
$this->assertEquals($expect, $this->escapestring($str));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test accented character
|
||||||
|
*/
|
||||||
|
public function testAccent()
|
||||||
|
{
|
||||||
|
$str = 'Voilà - string with accented char';
|
||||||
|
$expect = $this->expect('Voil\\uc0{\\u224} - string with accented char');
|
||||||
|
$this->assertEquals($expect, $this->escapestring($str));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Hebrew
|
||||||
|
*/
|
||||||
|
public function testHebrew()
|
||||||
|
{
|
||||||
|
$str = 'Hebrew - שלום';
|
||||||
|
$expect = $this->expect('Hebrew - \\uc0{\\u1513}\\uc0{\\u1500}\\uc0{\\u1493}\\uc0{\\u1501}');
|
||||||
|
$this->assertEquals($expect, $this->escapestring($str));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test tab
|
||||||
|
*/
|
||||||
|
public function testTab()
|
||||||
|
{
|
||||||
|
$str = "Here's a tab\tfollowed by more characters.";
|
||||||
|
$expect = $this->expect("Here's a tab{\\tab}followed by more characters.");
|
||||||
|
$this->assertEquals($expect, $this->escapestring($str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,11 @@ use PhpOffice\PhpWord\Writer\RTF;
|
||||||
*/
|
*/
|
||||||
class ElementTest extends \PHPUnit\Framework\TestCase
|
class ElementTest extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
public function removeCr($field)
|
||||||
|
{
|
||||||
|
return str_replace("\r\n", "\n", $field->write());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test unmatched elements
|
* Test unmatched elements
|
||||||
*/
|
*/
|
||||||
|
|
@ -46,7 +51,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
||||||
$element = new \PhpOffice\PhpWord\Element\Field('PAGE');
|
$element = new \PhpOffice\PhpWord\Element\Field('PAGE');
|
||||||
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
||||||
|
|
||||||
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $field->write());
|
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $this->removeCr($field));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNumpageField()
|
public function testNumpageField()
|
||||||
|
|
@ -55,7 +60,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
||||||
$element = new \PhpOffice\PhpWord\Element\Field('NUMPAGES');
|
$element = new \PhpOffice\PhpWord\Element\Field('NUMPAGES');
|
||||||
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
||||||
|
|
||||||
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $field->write());
|
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $this->removeCr($field));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDateField()
|
public function testDateField()
|
||||||
|
|
@ -64,7 +69,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
||||||
$element = new \PhpOffice\PhpWord\Element\Field('DATE', array('dateformat' => 'd MM yyyy H:mm:ss'));
|
$element = new \PhpOffice\PhpWord\Element\Field('DATE', array('dateformat' => 'd MM yyyy H:mm:ss'));
|
||||||
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
||||||
|
|
||||||
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $field->write());
|
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $this->removeCr($field));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIndexField()
|
public function testIndexField()
|
||||||
|
|
@ -73,6 +78,6 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
||||||
$element = new \PhpOffice\PhpWord\Element\Field('INDEX');
|
$element = new \PhpOffice\PhpWord\Element\Field('INDEX');
|
||||||
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
|
||||||
|
|
||||||
$this->assertEquals("{}\\par\n", $field->write());
|
$this->assertEquals("{}\\par\n", $this->removeCr($field));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue