Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Add aria‑label attributes to navigation links lacking descriptive text for screen readers.

using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
try
{
string html = "<!DOCTYPE html><html><head><title>Test</title></head><body><nav><ul><li><a href=\"home.html\"></a></li><li><a href=\"about.html\">About</a></li><li><a href=\"contact.html\"></a></li></ul></nav></body></html>";
string baseUri = "about:blank";

using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, baseUri))
{
// Add aria-label to links without descriptive text
Aspose.Html.Collections.HTMLCollection anchors = document.GetElementsByTagName("a");
for (int i = 0; i < anchors.Length; i++)
{
Aspose.Html.Dom.Element element = anchors[i] as Aspose.Html.Dom.Element;
if (element != null)
{
Aspose.Html.HTMLAnchorElement anchor = element as Aspose.Html.HTMLAnchorElement;
if (anchor != null)
{
string text = anchor.TextContent != null ? anchor.TextContent.Trim() : string.Empty;
if (string.IsNullOrEmpty(text))
{
string href = anchor.GetAttribute("href");
if (!string.IsNullOrEmpty(href))
{
anchor.SetAttribute("aria-label", href);
}
}
}
}
}

// Validate accessibility
Aspose.Html.Accessibility.WebAccessibility webAccessibility = new Aspose.Html.Accessibility.WebAccessibility();
Aspose.Html.Accessibility.AccessibilityValidator validator = webAccessibility.CreateValidator(Aspose.Html.Accessibility.ValidationBuilder.All);
Aspose.Html.Accessibility.Results.ValidationResult validationResult = validator.Validate(document);
if (!validationResult.Success)
{
foreach (Aspose.Html.Accessibility.Results.RuleValidationResult ruleResult in validationResult.Details)
{
if (!ruleResult.Success)
{
foreach (Aspose.Html.Accessibility.ITechniqueResult techResult in ruleResult.Errors)
{
if (techResult.Error.Target.TargetType == Aspose.Html.Accessibility.TargetTypes.HTMLElement)
{
Aspose.Html.HTMLElement element = (Aspose.Html.HTMLElement)techResult.Error.Target.Item;
string tagName = element.TagName;
string attributeValue = element.GetAttribute("aria-label");
Console.WriteLine($"Tag: {tagName}, Attribute: {attributeValue}, Message: {techResult.Error.ErrorMessage}");
}
}
}
}
}

// Save modified HTML
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "output.html");
document.Save(outputPath);
Console.WriteLine($"Modified HTML saved to {outputPath}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Add a crossorigin attribute to external script tags to enable CORS handling.

using System;

class Program
{
static void Main()
{
try
{
string htmlContent = "<html><head><script src=\"https://example.com/lib.js\"></script></head><body></body></html>";
string htmlContent = "<!DOCTYPE html><html><head><script src='https://example.com/lib.js'></script></head><body></body></html>";
string tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "sample.html");
System.IO.File.WriteAllText(tempFile, htmlContent);

Aspose.Html.Configuration configuration = new Aspose.Html.Configuration();
configuration.Security |= Aspose.Html.Sandbox.Scripts;

using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(tempFile, configuration))
{
Aspose.Html.Collections.HTMLCollection scriptElements = document.GetElementsByTagName("script");
Expand All @@ -23,13 +27,14 @@ static void Main()
scriptElement.SetAttribute("crossorigin", "anonymous");
}
}

string output = document.DocumentElement != null ? document.DocumentElement.OuterHTML : string.Empty;
System.Console.WriteLine(output);
}
}
catch (System.Exception ex)
{
System.Console.WriteLine("Error: " + ex.Message);
System.Console.Error.WriteLine("Error: " + ex.Message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Add a custom data‑attribute to all heading elements for JavaScript interaction later.

using System;

class Program
{
static void Main()
{
try
{
var document = new Aspose.Html.HTMLDocument();
var body = document.Body;

var h1 = (Aspose.Html.HTMLHeadingElement)document.CreateElement("h1");
h1.SetAttribute("data-custom", "heading1");
h1.AppendChild(document.CreateTextNode("Heading 1"));
body.AppendChild(h1);

var h2 = (Aspose.Html.HTMLHeadingElement)document.CreateElement("h2");
h2.SetAttribute("data-custom", "heading2");
h2.AppendChild(document.CreateTextNode("Heading 2"));
body.AppendChild(h2);

string outputPath = "output.html";
document.Save(outputPath);
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Add a custom data‑tracking attribute to all outbound links for analytics purposes.

using System;

class Program
{
static void Main(string[] args)
{
try
{
// Sample HTML content
string htmlContent = "<html><body>" +
"<a href=\"https://example.com\">External Link</a>" +
"<a href=\"/local\">Local Link</a>" +
"</body></html>";

// Load the document from the HTML string
using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"))
{
// Select all anchor elements with href attribute
Aspose.Html.Collections.NodeList anchors = document.QuerySelectorAll("a[href]");

foreach (Aspose.Html.Dom.Node node in anchors)
{
Aspose.Html.HTMLElement element = node as Aspose.Html.HTMLElement;
if (element != null)
{
string href = element.GetAttribute("href");
if (!string.IsNullOrEmpty(href) &&
(href.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
href.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
{
// Add custom data-tracking attribute
element.SetAttribute("data-tracking", Guid.NewGuid().ToString());
}
}
}

// Save the modified HTML to a file
string outputPath = "output.html";
document.Save(outputPath);
Console.WriteLine($"Modified HTML saved to: {outputPath}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Add a language attribute to the html tag based on detected content language.

class Program
{
static void Main()
{
try
{
string htmlContent = "<!DOCTYPE html><html><head><title>Sample</title></head><body>Hello world!</body></html>";
// Simple language detection based on content
string languageCode = htmlContent.Contains("Bonjour") ? "fr" : "en";

Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank");

Aspose.Html.HTMLElement htmlElement = (Aspose.Html.HTMLElement)document.DocumentElement;
htmlElement.SetAttribute("lang", languageCode);

string outputPath = "output.html";
document.Save(outputPath);

System.Console.WriteLine("HTML saved to " + outputPath + " with lang='" + languageCode + "'.");
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
// Add missing alt attributes to img tags using a placeholder description for accessibility.

using System;
using System.IO;
using System.Text;

class Program
{
static void Main()
{
try
{
// Prepare sample HTML content
string htmlContent = "<html><body>" +
"<img src='image1.png'>" +
"<img src='image2.png' alt='Existing alt'>" +
"</body></html>";

// Define input and output file paths
string inputPath = "sample.html";
string outputPath = "output.html";

// Write the sample HTML to a file
File.WriteAllText(inputPath, htmlContent, Encoding.UTF8);

// Load the HTML document
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath);

// Get all <img> elements
string htmlContent = "<!DOCTYPE html><html><head><title>Sample</title></head><body><img src='image1.png'><p>Text</p><img src='image2.jpg' alt='Existing alt'></body></html>";
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank");
Aspose.Html.Collections.HTMLCollection images = document.GetElementsByTagName("img");

// Iterate over each image element
foreach (Aspose.Html.Dom.Element node in images)
{
Aspose.Html.HTMLImageElement img = node as Aspose.Html.HTMLImageElement;
Expand All @@ -43,9 +24,7 @@ static void Main()
}
}
}

// Save the modified document
document.Save(outputPath);
document.Save("output.html");
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Add a missing charset meta tag based on detected file encoding to improve compatibility.

using System;
using System.IO;
using System.Text;

class Program
{
static void Main()
{
try
{
// Define input and output paths
string sourcePath = "input.html";
string outputPath = "output.html";

// Create a minimal sample HTML file if it does not exist
if (!File.Exists(sourcePath))
{
string sampleHtml = "<!DOCTYPE html><html><head><title>Sample</title></head><body><p>Hello World</p></body></html>";
File.WriteAllText(sourcePath, sampleHtml, Encoding.UTF8);
}

// Detect encoding (for this example we assume UTF-8)
string encodingName = "utf-8";
string htmlContent = File.ReadAllText(sourcePath, Encoding.GetEncoding(encodingName));

// Add charset meta tag if missing
if (!htmlContent.Contains("<meta charset=", StringComparison.OrdinalIgnoreCase))
{
if (htmlContent.Contains("<head>", StringComparison.OrdinalIgnoreCase))
{
htmlContent = htmlContent.Replace("<head>", "<head><meta charset=\"utf-8\">", StringComparison.OrdinalIgnoreCase);
}
else if (htmlContent.Contains("<html>", StringComparison.OrdinalIgnoreCase))
{
htmlContent = htmlContent.Replace("<html>", "<html><head><meta charset=\"utf-8\"></head>", StringComparison.OrdinalIgnoreCase);
}
else
{
htmlContent = "<meta charset=\"utf-8\">" + htmlContent;
}
}

// Load HTML content into Aspose.HTML document
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank");

// Save the modified document
document.Save(outputPath);

Console.WriteLine($"HTML saved to: {outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System;
using System.IO;
using Aspose.Html;
using System.Text;

class Program
{
Expand All @@ -13,33 +13,31 @@ static void Main()
string inputPath = "input.html";
string outputPath = "output.html";

// Create a minimal sample HTML file without DOCTYPE if it doesn't exist
// Create a minimal sample HTML file if it does not exist
if (!File.Exists(inputPath))
{
string sampleHtml = "<html><head><title>Sample</title></head><body><p>Hello, World!</p></body></html>";
File.WriteAllText(inputPath, sampleHtml);
File.WriteAllText(inputPath, sampleHtml, Encoding.UTF8);
}

// Load the HTML document using Aspose.Html
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath);

// Get the outer HTML of the document
string html = document.DocumentElement.OuterHTML;
// Read the HTML content
string html = File.ReadAllText(inputPath, Encoding.UTF8);

// Add DOCTYPE if missing
if (!html.TrimStart().StartsWith("<!DOCTYPE", StringComparison.OrdinalIgnoreCase))
{
html = "<!DOCTYPE html>\n" + html;
}

// Save the modified HTML to the output file
File.WriteAllText(outputPath, html);
// Load the HTML into Aspose.HTML document
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, "about:blank");

Console.WriteLine("HTML file processed successfully. Output saved to: " + outputPath);
// Save the updated document
document.Save(outputPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine(ex.Message);
}
}
}
Loading
Loading