stripDownPage static method

dynamic stripDownPage(
  1. dynamic target, [
  2. HtmlElement? selected = null
])

Remove unwanted HTML Element tags from a HtmlDocument or ParentNode

strips out everything except:

Implementation

static stripDownPage(target, [HtmlElement? selected = null]) {
  log.info('Function : _stripDownPage, '
      'Parameters : {[target,$target][selected,$selected]}');

  Set<Element> elementsToBeDeleted = target.querySelectorAll('*').toSet();

  if (selected != null) {
    _whitelistElementAndParents(selected, elementsToBeDeleted);
  } else {
    //whitelist all elements of type input so that the user can still search
    //whitelist all elements of type object so the user can watch videos
    //whitelist all elements of type video so the user can watch videos
    //whitelist all elements of type iframe so that external content can remain
    target
        .querySelectorAll('input, object, iframe, video')
        .forEach((e) => _whitelistElementAndParents(e, elementsToBeDeleted));

    //whitelist all elements of type anchor that have text
    //so the user can click on links but not buttons
    target.querySelectorAll('a').forEach((Element e) {
      String txt = ifNull(e.text, '');
      if (txt != '') _whitelistElementAndParents(e, elementsToBeDeleted);
    });
  }

  //whitelist all scripts which are known to be useful
  //so that flash can be dynamically loaded
  target
      .querySelectorAll('script')
      .where((e) => _whitelistScripts(e))
      .forEach((Element e) {
    _whitelistElementAndParents(e, elementsToBeDeleted);
  });
  //destroy everything that remains
  elementsToBeDeleted.forEach((Element e) {
    log.finest('Function : _stripDownPage, remove : $e');
    e.remove();
  });
  if (target is HtmlDocument) {
    removeAllHandlers(target, selected);
  }
  log.fine('Function : _stripDownPage, Return : void');
}