setAttributes method

void setAttributes(
  1. DOMElement domElement,
  2. T element,
  3. DOMTreeMap<T> treeMap, {
  4. bool preserveClass = false,
  5. bool preserveStyle = false,
})

Implementation

void setAttributes(DOMElement domElement, T element, DOMTreeMap<T> treeMap,
    {bool preserveClass = false, bool preserveStyle = false}) {
  for (var attrName in domElement.attributesNames) {
    var attr = domElement.getAttribute(attrName)!;
    var attrVal = attr.getValue(_domContext, treeMap);

    if (preserveClass && attrName == 'class') {
      var prev = getAttribute(element, attrName);
      if (prev != null && prev.isNotEmpty) {
        attrVal =
            attrVal != null && attrVal.isNotEmpty ? '$prev $attrVal' : prev;
      }
    } else if (preserveStyle && attrName == 'style') {
      var prev = getAttribute(element, attrName);
      if (prev != null && prev.isNotEmpty) {
        if (!prev.endsWith(';')) prev += ';';
        attrVal =
            attrVal != null && attrVal.isNotEmpty ? '$prev $attrVal' : prev;
      }
    } else if ((attrName == 'src' || attrName == 'href') &&
        attrVal != null &&
        attrVal.isNotEmpty) {
      var attrVal2 = resolveSource(attrVal);

      if (attrVal != attrVal2) {
        setAttribute(element, '$attrName-original', attrVal);
        attrVal = attrVal2;
      }
    } else if (attr.isBoolean && DOMAttribute.isBooleanAttribute(attrName)) {
      if (attrVal != 'true') {
        continue;
      }
    }

    setAttribute(element, attrName, attrVal);
  }
}