Browse Source

Added check for restricted XML characters.

RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F].
master
Viktor Loktev 9 years ago
parent
commit
96b7dbef2a
4 changed files with 49 additions and 2 deletions
  1. 21
    2
      DocX/HelperFunctions.cs
  2. 6
    0
      DocX/bin/Release/DocX.XML
  3. BIN
      DocX/bin/Release/DocX.dll
  4. 22
    0
      UnitTests/DocXUnitTests.cs

+ 21
- 2
DocX/HelperFunctions.cs View File

public const string DOCUMENT_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"; public const string DOCUMENT_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
public const string TEMPLATE_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml"; public const string TEMPLATE_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";
public static bool IsNullOrWhiteSpace(this string value)
/// <summary>
/// List of restricted character in xml: [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]
/// See: https://www.w3.org/TR/xml11/#sec-xml11
/// </summary>
public static readonly char[] RestrictedXmlChar = new char[] {
'\x1','\x2','\x3','\x4','\x5','\x6','\x7','\x8','\xb','\xc','\xe','\xf',
'\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1e','\x1f',
'\x7f','\x80','\x81','\x82','\x83','\x84','\x86','\x87','\x88','\x89','\x8a','\x8b','\x8c','\x8d','\x8e','\x8f',
'\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9a','\x9b','\x9c','\x9d','\x9e','\x9f'
};
public static bool IsNullOrWhiteSpace(this string value)
{ {
if (value == null) return true; if (value == null) return true;
return string.IsNullOrEmpty(value.Trim()); return string.IsNullOrEmpty(value.Trim());
break; break;
default: default:
sb.Append(c);
// Check the character against restricted list:
// RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]
// See https://www.w3.org/TR/xml11/#sec-xml11
if( RestrictedXmlChar.Contains( c ) )
{
// skip the character
}
else
sb.Append(c);
break; break;
} }

+ 6
- 0
DocX/bin/Release/DocX.XML View File

The custom property to display. The custom property to display.
</summary> </summary>
</member> </member>
<member name="F:Novacode.HelperFunctions.RestrictedXmlChar">
<summary>
List of restricted character in xml: [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]
See: https://www.w3.org/TR/xml11/#sec-xml11
</summary>
</member>
<member name="M:Novacode.HelperFunctions.ContainsEveryChildOf(System.Xml.Linq.XElement,System.Xml.Linq.XElement,Novacode.MatchFormattingOptions)"> <member name="M:Novacode.HelperFunctions.ContainsEveryChildOf(System.Xml.Linq.XElement,System.Xml.Linq.XElement,Novacode.MatchFormattingOptions)">
<summary> <summary>
Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same Checks whether 'toCheck' has all children that 'desired' has and values of 'val' attributes are the same

BIN
DocX/bin/Release/DocX.dll View File


+ 22
- 0
UnitTests/DocXUnitTests.cs View File

} }
} }
[Test]
public void Test_InvalidCharacter()
{
using( var output = File.Open( Path.Combine( _directoryWithFiles, "InvalidCharacters.docx" ), FileMode.Create ) )
{
using( var doc = DocX.Create( output ) )
{
doc.InsertParagraph( "\b" );
Exception ex = null;
try
{
doc.Save();
}
catch( Exception e )
{
ex = e;
}
Assert.IsTrue( ex == null );
}
}
}
/// <summary> /// <summary>
/// TextRemove should not remove empty paragraphs in case the paragraph is alone in the cell. /// TextRemove should not remove empty paragraphs in case the paragraph is alone in the cell.
/// In the rest cases empty paragraph may be removed. /// In the rest cases empty paragraph may be removed.

Loading…
Cancel
Save