Skip to content

Commit

Permalink
Update source to latest
Browse files Browse the repository at this point in the history
Update source to latest
  • Loading branch information
JonathanMagnan committed Aug 19, 2018
1 parent b81dfc9 commit 3a6f0d2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
14 changes: 8 additions & 6 deletions src/HtmlAgilityPack.Shared/HtmlAttribute.cs
Expand Up @@ -83,10 +83,12 @@ public int ValueLength
get { return _valuelength; }
}

/// <summary>
/// Gets the qualified name of the attribute.
/// </summary>
public string Name
public bool UseOriginalName { get; set; } = false;

/// <summary>
/// Gets the qualified name of the attribute.
/// </summary>
public string Name
{
get
{
Expand All @@ -95,8 +97,8 @@ public string Name
_name = _ownerdocument.Text.Substring(_namestartindex, _namelength);
}

return _name.ToLowerInvariant();
}
return UseOriginalName ? _name : _name.ToLowerInvariant();
}
set
{
if (value == null)
Expand Down
48 changes: 42 additions & 6 deletions src/HtmlAgilityPack.Shared/HtmlDocument.cs
Expand Up @@ -60,6 +60,7 @@ public partial class HtmlDocument
private int _remainderOffset;
private ParseState _state;
private Encoding _streamencoding;
private bool _useHtmlEncodingForStream;

/// <summary>The HtmlDocument Text. Careful if you modify it.</summary>
public string Text;
Expand Down Expand Up @@ -313,7 +314,20 @@ public static string GetXmlName(string name)
return GetXmlName(name, false, false);
}

public static string GetXmlName(string name, bool isAttribute, bool preserveXmlNamespaces)
#if !METRO
public void UseAttributeOriginalName(string tagName)
{
foreach (var nod in this.DocumentNode.SelectNodes("//" + tagName))
{
foreach (var attribut in nod.Attributes)
{
attribut.UseOriginalName = true;
}
}
}
#endif

public static string GetXmlName(string name, bool isAttribute, bool preserveXmlNamespaces)
{
string xmlname = string.Empty;
bool nameisok = true;
Expand Down Expand Up @@ -498,6 +512,19 @@ public HtmlTextNode CreateTextNode(string text)
/// <returns>The detected encoding.</returns>
public Encoding DetectEncoding(Stream stream)
{
return DetectEncoding(stream, false);
}

/// <summary>
/// Detects the encoding of an HTML stream.
/// </summary>
/// <param name="stream">The input stream. May not be null.</param>
/// <param name="checkHtml">The html is checked.</param>
/// <returns>The detected encoding.</returns>
public Encoding DetectEncoding(Stream stream, bool checkHtml)
{
_useHtmlEncodingForStream = checkHtml;

if (stream == null)
{
throw new ArgumentNullException("stream");
Expand Down Expand Up @@ -539,7 +566,7 @@ public Encoding DetectEncoding(TextReader reader)
}

StreamReader sr = reader as StreamReader;
if (sr != null)
if (sr != null && !_useHtmlEncodingForStream)
{
Text = sr.ReadToEnd();
_streamencoding = sr.CurrentEncoding;
Expand All @@ -565,7 +592,7 @@ public Encoding DetectEncoding(TextReader reader)
return _streamencoding;
}


/// <summary>
/// Detects the encoding of an HTML text.
/// </summary>
Expand Down Expand Up @@ -1719,15 +1746,24 @@ private void CloseParentImplicitExplicitNode()
{
hasNodeToClose = false;

bool forceExplicitEnd = false;

// CHECK if parent must be implicitely closed
if (IsParentImplicitEnd())
{
CloseParentImplicitEnd();
hasNodeToClose = true;
if (OptionOutputAsXml)
{
forceExplicitEnd = true;
}
else
{
CloseParentImplicitEnd();
hasNodeToClose = true;
}
}

// CHECK if parent must be explicitely closed
if (IsParentExplicitEnd())
if (forceExplicitEnd || IsParentExplicitEnd())
{
CloseParentExplicitEnd();
hasNodeToClose = true;
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlNode.cs
Expand Up @@ -1744,7 +1744,7 @@ public virtual void WriteTo(TextWriter outText, int level = 0)
else
WriteContentTo(outText, level);

if (!_isImplicitEnd)
if (_ownerdocument.OptionOutputAsXml || !_isImplicitEnd)
{
outText.Write("</" + name);
if (!_ownerdocument.OptionOutputAsXml)
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlNodeCollection.cs
Expand Up @@ -291,7 +291,7 @@ public static HtmlNode FindFirst(HtmlNodeCollection items, string name)
{
foreach (HtmlNode node in items)
{
if (node.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
if (node.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
return node;
if (!node.HasChildNodes) continue;
HtmlNode returnNode = FindFirst(node.ChildNodes, name);
Expand Down
18 changes: 14 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Expand Up @@ -1167,20 +1167,30 @@ public string GetCachePath(Uri uri)
throw new HtmlWebException("Cache is not enabled. Set UsingCache to true first.");
}

string cachePath;
string cachePath;
if (uri.AbsolutePath == "/")
{
cachePath = Path.Combine(_cachePath, ".htm");
}
else
{
if (uri.AbsolutePath[uri.AbsolutePath.Length - 1] == Path.AltDirectorySeparatorChar)

string absolutePathWithoutBadChar = uri.AbsolutePath;

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
absolutePathWithoutBadChar = absolutePathWithoutBadChar.Replace(c.ToString(), "");
}

if (uri.AbsolutePath[uri.AbsolutePath.Length - 1] == Path.AltDirectorySeparatorChar)
{
cachePath = Path.Combine(_cachePath, (uri.Host + uri.AbsolutePath.TrimEnd(Path.AltDirectorySeparatorChar)).Replace('/', '\\') + ".htm");
cachePath = Path.Combine(_cachePath, (uri.Host + absolutePathWithoutBadChar.TrimEnd(Path.AltDirectorySeparatorChar)).Replace('/', '\\') + ".htm");
}
else
{
cachePath = Path.Combine(_cachePath, (uri.Host + uri.AbsolutePath.Replace('/', '\\')));
cachePath = Path.Combine(_cachePath, (uri.Host + absolutePathWithoutBadChar.Replace('/', '\\')));
}
}

Expand Down

0 comments on commit 3a6f0d2

Please sign in to comment.