override def clean()

in common/app/views/support/HtmlCleaner.scala [757:811]


  override def clean(document: Document): Document = {

    def hasFirstContainerThrasher(element: Element, index: Int): Boolean = {
      index == 0 && element.hasClass("fc-container--thrasher")
    }

    def hasAdjacentCommercialContainer(element: Element): Boolean = {
      val maybeNextEl: Option[Element] = Option(element.nextElementSibling())
      element.hasClass("fc-container--commercial") || maybeNextEl.exists(_.hasClass("fc-container--commercial"))
    }

    def hasAdjacentThrasher(element: Element): Boolean =
      Option(element.nextElementSibling()).exists(_.hasClass("fc-container--thrasher"))

    def isMostViewedContainer(element: Element): Boolean =
      Option(element.id()).exists(_.contains("most-viewed")) || Option(element.id()).exists(_.contains("popular-in"))

    val sliceSlot = views.html.fragments.items.facia_cards.sliceSlot

    val containers: List[Element] = document.getElementsByClass("fc-container").asScala.toList

    // On mobile, we remove the first container if it is a thrasher
    // and remove a container if it, or the next sibling, is a commercial container
    // we also exclude any containers that are directly before a thrasher
    // then we take every other container, up to a maximum of 10, for targeting MPU insertion
    val containersForCommercialMPUs = containers.zipWithIndex
      .collect {
        case (x, i)
            if !hasFirstContainerThrasher(x, i) && !hasAdjacentCommercialContainer(x) && !hasAdjacentThrasher(
              x,
            ) && !isMostViewedContainer(x) =>
          x
      }
      .zipWithIndex
      .collect {
        case (x, i) if i % 2 == 0 => x
      }
      .take(10)

    for (container <- containersForCommercialMPUs) {
      container.after(s"""<section class="fc-container__mpu--mobile">${sliceSlot(
          containersForCommercialMPUs.indexOf(container),
          isMobile = true,
        )}</section>""")
    }

    // On desktop, a MPU slot is simply inserted when there is a slice available
    val slices: List[Element] = document.getElementsByClass("fc-slice__item--mpu-candidate").asScala.toList

    for (slice <- slices) {
      slice.append(s"${sliceSlot(slices.indexOf(slice) + 1)}")
    }

    document
  }