RuleSetAddTagProcessingRule Method |
Adds a rule for changing attributes of HTML tags.
Namespace: MailBee.HtmlAssembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax public void AddTagProcessingRule(
string tagName,
TagAttributeCollection tagAttrs,
TagAttributeCollection attrsToAdd,
TagAttributeCollection attrsToRemove,
bool replaceMode
)
Public Sub AddTagProcessingRule (
tagName As String,
tagAttrs As TagAttributeCollection,
attrsToAdd As TagAttributeCollection,
attrsToRemove As TagAttributeCollection,
replaceMode As Boolean
)
Parameters
- tagName
- Type: SystemString
The name of the tag for which to change the attributes. Case-insensitive. - tagAttrs
- Type: MailBee.HtmlTagAttributeCollection
The list of attributes any of which must exist in the definition of tagName tags
in order to trigger the rule's action, or a null reference (Nothing in Visual Basic) to apply the rule for any tags with tagName name
(regardless of which attributes they have). - attrsToAdd
- Type: MailBee.HtmlTagAttributeCollection
The list of attributes to be added to the
tagName tag. Can be a null reference if replaceMode is false. - attrsToRemove
- Type: MailBee.HtmlTagAttributeCollection
The list of attributes to be removed from the
tagName tag. Can be a null reference if replaceMode is false. - replaceMode
- Type: SystemBoolean
If true, attrsToRemove attributes will be replaced with attrsToAdd ones; otherwise,
attrsToRemove attributes will simply be removed and attrsToAdd ones will be added.
Exceptions Exception | Condition |
---|
MailBeeInvalidArgumentException | tagName is a null reference (Nothing in Visual Basic) or an empty string, or replaceMode is true and attrsToAdd does not match attrsToRemove.
|
Remarks The condition when this rule satisfies is the same as for ProcessingCondition rule
(created with AddTagProcessingCondition(String, TagAttributeCollection) method). See AddTagProcessingCondition(String, TagAttributeCollection) topic for the details
which apply to the HTML processing rules of all types.
Once the matching tag was found and the delegate (if any) returned true, the processing continues as follows
(depending on replaceMode value):
- If replaceMode is false, MailBee searches for any attrsToRemove attributes
in the tag definition and removes them. attrsToAdd attributes are added. Any of these actions are optional (and thus
attrsToAdd or attrsToRemove can be empty).
- If replaceMode is true, MailBee compares lengths of attrsToAdd and attrsToRemove
collections. If they are equal, MailBee will then iterate through every attribute of the tag and if it finds such attribute in attrsToRemove
(let's say, at index i), it replaces i-th attribute in attrsToRemove with
i-th attribute in attrsToAdd. In other words, attributes A, B and C will be replaced
with X, Y and Z correspondingly.
If lengths of attrsToAdd and attrsToRemove are not equal but
attrsToAdd length is 1, any attributes contained in attrsToRemove will be replaced with
the attribute in attrsToAdd. In other words, attributes A, B and C will be replaced
with X, X and X correspondingly.
Otherwise (attrsToAdd and/or attrsToRemove is a null reference or their lengths do not match),
MailBee will throw an exception.
Note |
---|
tagAttrs collection is not related with attrsToAdd or attrsToRemove in any way.
tagAttrs only specifies attributes any of which the tagName tag must have in order to satisfy the rule condition.
However, tagAttrs is not used later when the rule action begins. |
Examples
This sample change the background color of the HTML page to black.
using System;
using System.IO;
using System.Net;
using MailBee;
using MailBee.Html;
class Sample
{
static void Main(string[] args)
{
WebRequest myWebRequest = WebRequest.Create("http://www.afterlogic.com");
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream receivingStream = myWebResponse.GetResponseStream();
Processor htmlProcessor = new Processor();
htmlProcessor.LoadFromStream(receivingStream, Global.DefaultEncoding);
RuleSet rules = new RuleSet();
TagAttributeCollection attributes = new TagAttributeCollection();
TagAttribute attribute = new TagAttribute();
attribute.Definition = "bgcolor = 'black'";
attributes.Add(attribute);
rules.AddTagRemovalRule("link", null);
rules.AddTagProcessingRule("body", null, attributes, null, false);
Console.WriteLine(htmlProcessor.Dom.ProcessToString(rules, null));
}
}
Imports System
Imports System.IO
Imports System.Net
Imports MailBee
Imports MailBee.Html
Module Sample
Sub Main()
Dim myWebRequest As WebRequest = WebRequest.Create("http://www.afterlogic.com")
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
Dim receivingStream As Stream = myWebResponse.GetResponseStream()
Dim htmlProcessor As New Processor
htmlProcessor.LoadFromStream(receivingStream, Global.DefaultEncoding)
Dim rules As New RuleSet
Dim attributes As TagAttributeCollection = New TagAttributeCollection
Dim attribute As TagAttribute = New TagAttribute
attribute.Definition =
attributes.Add(attribute)
rules.AddTagRemovalRule("link", Nothing)
rules.AddTagProcessingRule("body", Nothing, attributes, Nothing, False)
Console.WriteLine(htmlProcessor.Dom.ProcessToString(rules, Nothing))
End Sub
End Module
See Also