alterAttribute static method

dynamic alterAttribute(
  1. Element node,
  2. String attribute,
  3. Object? alter(
    1. Object? a
    )
)

Alters a specific attribute of a HTML Element, node by applying function alter to the attribute.

  • node - any HTML Element.
  • attribute - the name of the attribute to be modifiied.
  • alter - a function that takes in
    • the old value
    • and returns the new value.

The following example uses alterAttribute to modify an attribute on an Element. Specifically it is altering the href attribute on a anchor element go to google the function removeUrlRedirect would be a declared to pass in a href String and return a modified href string

alterAttribute(element, 'href', (url) => removeUrlRedirect(url)));

Implementation

static alterAttribute(
    Element node, String attribute, Object? alter(Object? a)) {
  log.info('Function : alterAttribute, '
      'Parameters : {[node,$node][attribute,$attribute][alter,$alter]}');
  assert(alter is _MyHtml_Alter_Element);
  if (node.attributes.containsKey(attribute)) {
    log.finer(
        'Function : alterAttribute, old : ${node.attributes[attribute]}');
    node.attributes[attribute] = alter(node.attributes[attribute]) as String;
    log.finer(
        'Function : alterAttribute, new : ${node.attributes[attribute]}');
  }
  log.fine('Function : alterAttribute, Return : void');
}