replaceMedium function

Document replaceMedium(
  1. HtmlMediumType htmlMediumType,
  2. Document document,
  3. String htmlReferenceLabel,
  4. String htmlReference,
  5. String url,
)

Implementation

Document replaceMedium(HtmlMediumType htmlMediumType, Document document,
    String htmlReferenceLabel, String htmlReference, String url) {
  if (htmlMediumType == HtmlMediumType.img) {
    var images = document.getElementsByTagName("img");
    for (var image in images) {
      var attributes = image.attributes;
      var mediumAttribute = attributes[htmlReferenceLabel];
      if (mediumAttribute == htmlReference) {
        var newHtml =
            constructHtmlForImg(url, htmlReferenceLabel, htmlReference);
        var newElement = Element.html(newHtml);
        image.replaceWith(newElement);
      }
    }
    return document;
  } else if (htmlMediumType == HtmlMediumType.video) {
    var videos = document.getElementsByTagName("img");
    for (var video in videos) {
      var attributes = video.attributes;
      var mediumAttribute = attributes[htmlReferenceLabel];
      if (mediumAttribute == htmlReference) {
        var newHtml =
            constructHtmlForVideo(url, htmlReferenceLabel, htmlReference);
        var newElement = Element.html(newHtml);
        video.replaceWith(newElement);
      }
    }
    return document;
  } else {
    return document;
  }
}