Hello,
I am trying to modify the head information in an HTML file to include a CSS file. The problem is, the href= always comes out with path information. For example it can be something like,
href=“GUID-A7DC3723-8BD8-4453-9A0D-07DEDB7B30F5_files/commonltr-emc.css”
I want it to look like this only,
href=“commonltr-emc.css”
This is my code:
using (var document = new HTMLDocument(inputfile.ReadToEnd(), ""))
{
var head = document.GetElementsByTagName("head").FirstOrDefault();
var newheader = document.CreateElement(newHead);
var headerlink1 = document.CreateElement("link");
headerlink1.SetAttribute("rel", "stylesheet");
headerlink1.SetAttribute("type", "text/css");
headerlink1.SetAttribute("href", "commonltr-emc.css");
head.AppendChild(headerlink1);
document.Save(Path.Combine("newfiles", "GUID-A7DC3723-8BD8-4453-9A0D-07DEDB7B30F5.html"),
HTMLSaveFormat.Original);
}
What can I do to remove the path information?
@david.irons
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): HTMLNET-5248
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@david.irons
As a workaround, please try using the code like below:
var head = document.GetElementsByTagName("head").FirstOrDefault();
if (head != null)
{
var newheader = document.CreateElement("head");
var headerlink1 = document.CreateElement("link");
headerlink1.SetAttribute("rel", "stylesheet");
headerlink1.SetAttribute("type", "text/css");
// Use the src attribute to set the href without path information
headerlink1.SetAttribute("src", "commonltr-emc.css");
head.AppendChild(headerlink1);
}
document.Save(System.IO.Path.Combine(dataDir, "GUID-A7DC3723-8BD8-4453-9A0D-07DEDB7B30F5.html"),
HTMLSaveFormat.Original);
Hello,
I tried your workaround and unfortunately I had the same problem. HOWEVER, I did find if I deleted the CSS file from my Visual Studio solution folder and used your workaround, the path information was gone and it just left the CSS filename!
It would be nice if the save HTMLSaveOptions() had a setting that stopped the save from scanning for any linked or href files. Can you have this type of option created?
@david.irons
Your concerns have been recorded and ticket information has also been updated. We will surely inform you as soon as some progress is made towards ticket resolution. Please spare us some time.
Hello again. I discovered that using src= does not load the CSS. I have to use href= to load the CSS. Href creates a path to *_files. I cannot have this.
@david.irons
We have recorded your feedback and will surely let you know as soon as the earlier logged ticket is resolved. We apologize for the inconvenience.
@david.irons
In the next release (24.2) we will add option which will allow you to disable resources saving. As a result all the “path” in the saved document will be left as is.
That’s awesome! Thank you so much!
@david.irons
In version 24.2.0 we have added the functionality necessary to obtain the behavior requested by you. Now you can block the saving of referenced resources and save the original references to them.
- To disable saving referenced resources, you need to set the following options:
var options = new HTMLSaveOptions();
options.ResourceHandlingOptions.Default = ResourceHandling.Ignore;
options.ResourceHandlingOptions.JavaScript = ResourceHandling.Ignore;
- The default behavior when saving references of ignored resources is to specify the full path to the original file. Therefore, in order for ignored resources to retain their original references, you will need to use the implementation of the FileSystemResourceHandler class and save the document using it. To do this, you need to create a class inherited from FileSystemResourceHandler, override the method responsible for generating references and return the original reference for ignored resources. Here’s an example of how to do this:
public class CustomerResourceHandler : FileSystemResourceHandler
{
public CustomerResourceHandler(Url outputUrl) : base(outputUrl)
{
}
public CustomerResourceHandler(string outputPath) : base(outputPath)
{
}
public override string HandleResourceReference(Resource resource, ResourceHandlingContext context)
{
return resource.Status == ResourceStatus.Ignored
? resource.OriginalReference
: base.HandleResourceReference(resource, context);
}
}
After which you should save the document using created CustomerResourceHandler, as shown in the following code snippet:
document.Save(new CustomerResourceHandler(Path.Combine("newfiles", "GUID-A7DC3723-8BD8-4453-9A0D-07DEDB7B30F5.html")), options);
A complete example of the code for saving a document without referenced resources and with original references:
public void Run()
{
using (var document = new HTMLDocument(inputfile.ReadToEnd(), ""))
{
var head = document.GetElementsByTagName("head").FirstOrDefault();
var headerLink = document.CreateElement("link");
headerLink.SetAttribute("rel", "stylesheet");
headerLink.SetAttribute("type", "text/css");
headerLink.SetAttribute("href", "commonltr-emc.css");
head.AppendChild(headerLink);
var options = new HTMLSaveOptions();
options.ResourceHandlingOptions.Default = ResourceHandling.Ignore;
options.ResourceHandlingOptions.JavaScript = ResourceHandling.Ignore;
document.Save(new CustomerResourceHandler(Path.Combine("newfiles", "GUID-A7DC3723-8BD8-4453-9A0D-07DEDB7B30F5.html")), options);
}
}
public class CustomerResourceHandler : FileSystemResourceHandler
{
public CustomerResourceHandler(Url outputUrl) : base(outputUrl)
{
}
public CustomerResourceHandler(string outputPath) : base(outputPath)
{
}
public override string HandleResourceReference(Resource resource, ResourceHandlingContext context)
{
return resource.Status == ResourceStatus.Ignored
? resource.OriginalReference
: base.HandleResourceReference(resource, context);
}
}
Thank you. I will try out your suggestions.
This worked perfectly! Thank you all for your efforts.
@david.irons
Thanks for the kind feedback. Please feel free to create a new topic in case you need further assistance.