1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
/// <summary>
/// <para>Met à jour la propriété <see cref="LiteralControl.Text"/> de <paramref name="literal"/> en recherchant
/// une balise HTML <paramref name="tagName"/> afin de lui ajouter/modifier l'attribut <paramref name="attributeName"/>.</para>
/// <para>En cas d'echec de la modification du literal ou si ce dernier est <c>null</c>, retourne <c>false</c>.</para>
/// </summary>
/// <param name="literal">Contrôle literal à modifier</param>
/// <param name="tagName">Nom de la balise HTML à rechercher dans le literal</param>
/// <param name="attributeName">Nom de l'attribut HTML à mettre à jour</param>
/// <param name="attributeValue">Valeur de l'attribut HTML</param>
/// <param name="appendAttributeValue">Si <c>true</c> et que l'attribut existe déjà,
/// la nouvelle valeur est ajoutée à la valeur existante.
/// Sinon, aucune modification.</param>
/// <returns><c>true</c> si la méthode a réussi. <c>false</c> si le literal n'a pas été modifié.</returns>
private bool UpdateLiteralHtmlAttribute(
LiteralControl literal,
string tagName,
string attributeName,
string attributeValue,
bool appendAttributeValue)
{
bool success = false;
tagName = tagName.ToLower();
string attributeNamePart = " " + attributeName + "=";
if ((literal != null) && (literal.Text.ToLower().Contains(tagName)))
{
int indexTagName = literal.Text.IndexOf(tagName, StringComparison.OrdinalIgnoreCase);
Debug.Assert(indexTagName > -1, "Index de l'occurence '" + tagName + "' non trouvé dans le literal: " + literal.Text);
int indexAttribute = literal.Text.IndexOf(attributeNamePart, indexTagName, StringComparison.OrdinalIgnoreCase);
if ((indexAttribute > -1) && (appendAttributeValue))
{
// l'atttribut existe déjà
int indexQuote = literal.Text.IndexOf("\"", indexAttribute);
if (indexQuote - (indexAttribute + attributeNamePart.Length) <= 1)
indexQuote = literal.Text.IndexOf("\"", indexQuote + 1);
int indexExistingValue = literal.Text.IndexOf(attributeValue, indexAttribute + 1, StringComparison.OrdinalIgnoreCase);
if (indexExistingValue == -1)
{
string literalPart1 = literal.Text.Substring(0, indexQuote);
string literalPart2 = literal.Text.Substring(indexQuote, literal.Text.Length - literalPart1.Length);
literal.Text = literalPart1 + attributeValue + literalPart2;
success = true;
}
}
else
{
// l'atttribut n'existe pas encore
int indexBracket = literal.Text.IndexOf(">", indexTagName);
string literalPart1 = literal.Text.Substring(0, indexBracket);
string literalPart2 = literal.Text.Substring(indexBracket, literal.Text.Length - literalPart1.Length);
literal.Text = literalPart1 + attributeNamePart + "\"" + attributeValue + "\"" + literalPart2;
success = true;
}
}
return success;
} |
Partager