//=========================================================================================
// 実体のないファイルの場合、画像やリンクは相対アドレスは不可能なので、絶対アドレスへと置き換える。
//=========================================================================================
private string RelativeToAbsolute(string html)
{
try
{
string fn = strCurFileFullPath;
fn = fn.Replace("\\", "/");
string strBaseUrl = fn;
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
bool isExistBaseHref = false;
foreach (var nodeBase in doc.DocumentNode.Descendants("base"))
{
if (nodeBase.Attributes["href"].Value.Length > 0)
{
isExistBaseHref = true;
}
else
{
}
}
// base hrefの指定が無いのであれば、現在の開いているファイルのディレクトリをそこにしておくことで、相対ディレクトリをフォローする。
if (!isExistBaseHref)
{
string basedir = System.IO.Path.GetDirectoryName(strBaseUrl);
HtmlAgilityPack.HtmlNode baseNode = HtmlAgilityPack.HtmlNode.CreateNode("<base href=''>");
baseNode.Attributes["href"].Value = basedir + "\\";
// Headタグがあればそこにたす
HtmlAgilityPack.HtmlNode nodeHead = doc.DocumentNode.SelectSingleNode("/html/head");
HtmlAgilityPack.HtmlNode nodeHtml = doc.DocumentNode.SelectSingleNode("/html");
if (nodeHead != null)
{
nodeHead.PrependChild(baseNode);
}
else if (nodeHtml != null)
{
nodeHtml.PrependChild(baseNode);
}
// Headタグがないなら、トップにたさざるをえないだろう
else
{
doc.DocumentNode.PrependChild(baseNode);
}
}
StringWriter writer = new StringWriter();
doc.Save(writer);
string newHtml = writer.ToString();
return(newHtml);
}
catch (Exception)
{
}
return(html);
}