org.jsoup.Jsoup Scala Examples
The following examples show how to use org.jsoup.Jsoup.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Markdown.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.libs.scala.domain import com.vladsch.flexmark.ext.emoji.{EmojiExtension, EmojiImageType} import com.vladsch.flexmark.html.HtmlRenderer import com.vladsch.flexmark.parser.Parser import com.vladsch.flexmark.util.data.MutableDataSet import com.vladsch.flexmark.util.misc.Extension import org.jsoup.Jsoup import scala.collection.JavaConverters._ final case class Markdown(value: String) extends AnyVal { def isEmpty: Boolean = value.trim.isEmpty def nonEmpty: Boolean = !isEmpty def toHtml: Html = Markdown.toHtml(this) def toText: String = Markdown.toText(this) } object Markdown { private val options = new MutableDataSet() // https://github.com/vsch/flexmark-java/wiki/Extensions#emoji .set(Parser.EXTENSIONS, Seq(EmojiExtension.create(): Extension).asJava) .set(EmojiExtension.USE_IMAGE_TYPE, EmojiImageType.UNICODE_ONLY) private val parser = Parser.builder(options).build private val renderer = HtmlRenderer.builder(options).escapeHtml(true).build def toHtml(md: Markdown): Html = { val parsed = parser.parse(md.value) val content = renderer.render(parsed).trim Html(s"""<div class="markdown">$content</div>""") } def toText(md: Markdown): String = { val parsed = parser.parse(md.value) val html = renderer.render(parsed).trim Jsoup.parse(html).text() } }
Example 2
Source File: HttpUtils.scala From reactive-programming with Apache License 2.0 | 5 votes |
package com.github.dnvriend import java.util.concurrent.Executor import com.ning.http.client.AsyncHttpClient import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.select.Elements import scala.collection.JavaConverters._ import scala.concurrent.{ Promise, ExecutionContext, Future } import scala.util.Try object HttpClient { def apply(): AsyncHttpClient = new AsyncHttpClient implicit class HttpClientToScala(client: AsyncHttpClient) { def get(url: String)(implicit ec: Executor): Future[String] = { val f = client.prepareGet(url).execute val p = Promise[String]() f.addListener(new Runnable { override def run(): Unit = { val response = f.get if (response.getStatusCode < 400) p.success(response.getResponseBodyExcerpt(131072)) else p.failure(new RuntimeException(s"BadStatus: ${response.getStatusCode}")) } }, ec) p.future } } } object HttpUtils { implicit class FindLinksFuture(self: Future[String])(implicit ec: ExecutionContext) { def links: Future[Option[Iterator[String]]] = self.map(body ⇒ findLinks(body)) } def findLinks(body: String): Option[Iterator[String]] = Try(Jsoup.parse(body)).map { (document: Document) ⇒ val links: Elements = document.select("a[href]") for (link ← links.iterator().asScala; if link.absUrl("href").startsWith("http://")) yield link.absUrl("href") }.toOption }
Example 3
Source File: HtmlLifterSpec.scala From diffy with GNU Affero General Public License v3.0 | 5 votes |
package ai.diffy.lifter import ai.diffy.ParentSpec import ai.diffy.compare.{Difference, PrimitiveDifference} import org.jsoup.Jsoup import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) class HtmlLifterSpec extends ParentSpec { describe("HtmlLifter"){ val simpleActualHtml = """<html><head><title>Sample HTML</title></head><body><div class="header"><h1 class="box">Hello World</h1></div><p>Lorem ipsum dolor sit amet.</p></body></html>""" val simpleExpectedHtml = """<html><head><title>Sample HTML</title></head><body><div class="header"><h1 class="round">Hello World</h1></div><p>Lorem ipsum dolor sit amet.</p></body></html>""" val simpleActualDoc = Jsoup.parse(simpleActualHtml) val simpleExpectedDoc = Jsoup.parse(simpleExpectedHtml) it("should return a FieldMap") { HtmlLifter.lift(simpleActualDoc) mustBe a [FieldMap[_]] } it("should return a Primitive Difference") { Difference(HtmlLifter.lift(simpleActualDoc), HtmlLifter.lift(simpleExpectedDoc)).flattened must be (FieldMap(Map("body.children.children.attributes.class.PrimitiveDifference" -> PrimitiveDifference("box","round")))) } } }
Example 4
Source File: HtmlLifter.scala From diffy with GNU Affero General Public License v3.0 | 5 votes |
package ai.diffy.lifter import org.jsoup.Jsoup import org.jsoup.nodes.{Document, Element} import org.jsoup.select.Elements import scala.collection.JavaConversions._ object HtmlLifter { def lift(node: Element): FieldMap[Any] = node match { case doc: Document => FieldMap( Map( "head" -> lift(doc.head), "body" -> lift(doc.body) ) ) case doc: Element => { val children: Elements = doc.children val attributes = FieldMap[String]( doc.attributes.asList map { attribute => attribute.getKey -> attribute.getValue } toMap ) FieldMap( Map( "tag" -> doc.tagName, "text" -> doc.ownText, "attributes" -> attributes, "children" -> children.map(element => lift(element)) ) ) } } def decode(html: String): Document = Jsoup.parse(html) }
Example 5
Source File: SocksProxyNet.scala From ProxyCrawler with Apache License 2.0 | 5 votes |
package org.crowdcrawler.proxycrawler.crawler.plugins import java.net.URI import org.crowdcrawler.proxycrawler.ProxyInfo import org.jsoup.Jsoup import scala.collection.mutable import scala.collection.JavaConversions._ class SocksProxyNet extends AbstractPlugin { val seeds: List[URI] = List(new URI("http://www.socks-proxy.net/")) def extract(html: String): List[ProxyInfo] = { val result = mutable.ListBuffer.empty[ProxyInfo] val doc = Jsoup.parse(html) val rows = doc.select("table#proxylisttable > tbody > tr") for (row <- rows) { val tds = row.select("td") val host = tds.get(0).text val port = tds.get(1).text.toInt val location = tds.get(3).text val schema= tds.get(4).text.toUpperCase result += ProxyInfo(host, port, schema, 0, location, null) } result.toList } def next(html: String): List[URI] = List() }
Example 6
Source File: ProxyListOrg.scala From ProxyCrawler with Apache License 2.0 | 5 votes |
package org.crowdcrawler.proxycrawler.crawler.plugins import java.net.URI import org.crowdcrawler.proxycrawler.ProxyInfo import org.jsoup.Jsoup import scala.collection.mutable import scala.collection.JavaConversions._ class ProxyListOrg extends AbstractPlugin { val seeds: List[URI] = List(new URI("https://proxy-list.org/english/index.php?p=1")) def extract(html: String): List[ProxyInfo] = { val result = mutable.ListBuffer.empty[ProxyInfo] val doc = Jsoup.parse(html) val rows = doc.select("div.table-wrap > div > ul") for (row <- rows) { val hostPort = row.select("li.proxy").text() val host = hostPort.split(":")(0) val port = hostPort.split(":")(1).toInt val schema = { val tmp = row.select("li.https").text() if (tmp == "-") "HTTP" else tmp.toUpperCase } val speed = { val tmp = row.select("li.speed").text() if (tmp.contains("kbit")) { (tmp.dropRight(4).toDouble * 1024).toInt } else { 0 } } val location = row.select("li.country-city > div > span.country").first().attr("title") result += ProxyInfo(host, port, schema, speed, location, null) } result.toList } def next(html: String): List[URI] = { val result = mutable.ListBuffer.empty[URI] val rootURL = "https://proxy-list.org/english" val doc = Jsoup.parse(html) val rows = doc.select("div.table-menu > a.item[href]") for (row <- rows) { val href = row.attr("href") result += new URI(rootURL + href.substring(1)) } result.toList } }
Example 7
Source File: CnProxyComPlugin.scala From ProxyCrawler with Apache License 2.0 | 5 votes |
package org.crowdcrawler.proxycrawler.crawler.plugins import org.crowdcrawler.proxycrawler.ProxyInfo import org.jsoup.Jsoup import java.net.URI import java.nio.charset.Charset import scala.collection.{immutable,mutable} import util.control.Breaks._ private val charNum = immutable.Map( "v" -> "3", "m" -> "4", "a" -> "2", "l" -> "9", "q" -> "0", "b" -> "5", "i" -> "7", "w" -> "6", "r" -> "8", "c" -> "1" ) val seeds: List[URI] = { List( new URI("http://www.cnproxy.com/proxy1.html"), new URI("http://www.cnproxy.com/proxy2.html"), new URI("http://www.cnproxy.com/proxy3.html"), new URI("http://www.cnproxy.com/proxy4.html"), new URI("http://www.cnproxy.com/proxy5.html"), new URI("http://www.cnproxy.com/proxy6.html"), new URI("http://www.cnproxy.com/proxy7.html"), new URI("http://www.cnproxy.com/proxy8.html"), new URI("http://www.cnproxy.com/proxy9.html"), new URI("http://www.cnproxy.com/proxy10.html"), new URI("http://www.cnproxy.com/proxyedu1.html"), new URI("http://www.cnproxy.com/proxyedu2.html") ) } private def decryptPort(encrypted: String): Int = encrypted.split("\\+").map(str => charNum(str)).mkString.toInt def extract(html: String): List[ProxyInfo] = { val result = mutable.ListBuffer.empty[ProxyInfo] val doc = Jsoup.parse(html) val rows = doc.select("#proxylisttb > table").get(2).select("tr") for (i <- 1 until rows.size()) { breakable { // skip the first row val row = rows.get(i) val tds = row.select("td") val host = tds.get(0).text val port = { val pattern = "document.write(\":\"+" val original = tds.get(0).html() val pos1 = original.indexOf(pattern) if (pos1 == -1) break val pos2 = original.indexOf(")</script>", pos1) if (pos2 == -1) break val portStr = original.substring(pos1 + pattern.length, pos2) decryptPort(portStr) } val schema = tds.get(1).text val speeds = tds.get(2).text val speed = { val splitted = speeds.split(",") var sum = 0 for (str <- splitted) { val tmp = str.toInt sum += tmp } sum / splitted.length } val country = tds.get(3).text val proxyInfo = ProxyInfo(host, port, schema, speed, country, null) result += proxyInfo } } result.toList } def next(html: String): List[URI] = List() override val responseCharset: Charset = Charset.forName("GB2312") }
Example 8
Source File: CoolProxyNetPlugin.scala From ProxyCrawler with Apache License 2.0 | 5 votes |
package org.crowdcrawler.proxycrawler.crawler.plugins import org.crowdcrawler.proxycrawler.ProxyInfo import org.jsoup.Jsoup import java.net.URI import java.nio.charset.StandardCharsets import sun.misc.BASE64Decoder import scala.collection.mutable import scala.collection.JavaConversions._ import util.control.Breaks._ class CoolProxyNetPlugin extends AbstractPlugin { private final val decoder: BASE64Decoder = new BASE64Decoder val seeds: List[URI] = List(new URI("http://www.cool-proxy.net/proxies/http_proxy_list/page:1")) private def decryptIP(ip: String): String = { val base64Encoded = new StringBuilder for (ch <- ip) { val newChar = if (Character.isAlphabetic(ch)) { if (ch.toLower < 'n') (ch + 13).toChar else (ch - 13).toChar } else { ch } base64Encoded += newChar } val bytes = decoder.decodeBuffer(base64Encoded.toString()) new String(bytes, StandardCharsets.UTF_8) } def extract(html: String): List[ProxyInfo] = { val result = mutable.ListBuffer.empty[ProxyInfo] val doc = Jsoup.parse(html) val rows = doc.select("table > tbody > tr") for (row <- rows) { breakable { val tds = row.select("td") if (tds.isEmpty) break val host = { val hostTmp = tds.get(0).html val startWith = "Base64.decode(str_rot13(\"" val start = hostTmp.indexOf(startWith) if (start == -1) break val end = hostTmp.indexOf("\")))", start) if (end == -1) break val hostEncrypted = hostTmp.substring(start + startWith.length, end) decryptIP(hostEncrypted) } val port = tds.get(1).text.toInt val location = tds.get(3).text val speed = tds.get(8).text.toInt result.add(ProxyInfo(host, port, "HTTP", speed, location, null)) } } result.toList } def next(html: String): List[URI] = { val result = mutable.ListBuffer.empty[URI] val doc = Jsoup.parse(html) val rows = doc.select(".pagination > span > a[href]") for (row <- rows) { val href = row.attr("href") result += new URI("http://www.cool-proxy.net" + href) } result.toList } }
Example 9
Source File: IpcnOrgPlugin.scala From ProxyCrawler with Apache License 2.0 | 5 votes |
package org.crowdcrawler.proxycrawler.crawler.plugins import org.crowdcrawler.proxycrawler.ProxyInfo import org.jsoup.Jsoup import java.net.URI import java.nio.charset.Charset import scala.collection.mutable final class IpcnOrgPlugin extends AbstractPlugin { val seeds: List[URI] = List( new URI("http://proxy.ipcn.org/proxylist.html"), new URI("http://proxy.ipcn.org/proxylist2.html") ) def extract(html: String): List[ProxyInfo] = { val result = mutable.ListBuffer.empty[ProxyInfo] val doc = Jsoup.parse(html) val preText = doc.select("tr > td > pre").text val rows = preText.split("\n") for (row <- rows) { if (row.matches("[0-9]+(?:\\.[0-9]+){3}:[0-9]+")) { val splitted = row.split(":") val host = splitted(0) val port = splitted(1).toInt result += ProxyInfo(host, port, "HTTP", 0, null, null) } } result.toList } def next(html: String): List[URI] = List() override val responseCharset: Charset = Charset.forName("GB2312") }
Example 10
Source File: ZeppelinRDisplay.scala From uberdata with Apache License 2.0 | 5 votes |
package org.apache.zeppelin.spark import org.apache.zeppelin.interpreter.InterpreterResult.Code import org.apache.zeppelin.interpreter.InterpreterResult.Code.{SUCCESS} import org.apache.zeppelin.interpreter.InterpreterResult.Type import org.apache.zeppelin.interpreter.InterpreterResult.Type.{TEXT, HTML, TABLE, IMG} import org.jsoup.Jsoup import org.jsoup.nodes.Element import org.jsoup.nodes.Document.OutputSettings import org.jsoup.safety.Whitelist import scala.collection.JavaConversions._ import scala.util.matching.Regex case class RDisplay(content: String, `type`: Type, code: Code) object ZeppelinRDisplay { val pattern = new Regex("""^ *\[\d*\] """) def render(html: String, imageWidth: String): RDisplay = { val document = Jsoup.parse(html) document.outputSettings().prettyPrint(false) val body = document.body() if (body.getElementsByTag("p").isEmpty) return RDisplay(body.html(), HTML, SUCCESS) val bodyHtml = body.html() if (! bodyHtml.contains("<img") && ! bodyHtml.contains("<script") && ! bodyHtml.contains("%html ") && ! bodyHtml.contains("%table ") && ! bodyHtml.contains("%img ") ) { return textDisplay(body) } if (bodyHtml.contains("%table")) { return tableDisplay(body) } if (bodyHtml.contains("%img")) { return imgDisplay(body) } return htmlDisplay(body, imageWidth) } private def textDisplay(body: Element): RDisplay = { // remove HTML tag while preserving whitespaces and newlines val text = Jsoup.clean(body.html(), "", Whitelist.none(), new OutputSettings().prettyPrint(false)) RDisplay(text, TEXT, SUCCESS) } private def tableDisplay(body: Element): RDisplay = { val p = body.getElementsByTag("p").first().html.replace("“%table " , "").replace("”", "") val r = (pattern findFirstIn p).getOrElse("") val table = p.replace(r, "").replace("\\t", "\t").replace("\\n", "\n") RDisplay(table, TABLE, SUCCESS) } private def imgDisplay(body: Element): RDisplay = { val p = body.getElementsByTag("p").first().html.replace("“%img " , "").replace("”", "") val r = (pattern findFirstIn p).getOrElse("") val img = p.replace(r, "") RDisplay(img, IMG, SUCCESS) } private def htmlDisplay(body: Element, imageWidth: String): RDisplay = { var div = new String() for (element <- body.children) { val eHtml = element.html() var eOuterHtml = element.outerHtml() eOuterHtml = eOuterHtml.replace("“%html " , "").replace("”", "") val r = (pattern findFirstIn eHtml).getOrElse("") div = div + eOuterHtml.replace(r, "") } val content = div .replaceAll("src=\"//", "src=\"http://") .replaceAll("href=\"//", "href=\"http://") body.html(content) for (image <- body.getElementsByTag("img")) { image.attr("width", imageWidth) } RDisplay(body.html, HTML, SUCCESS) } }
Example 11
Source File: GithubReadme.scala From scaladex with BSD 3-Clause "New" or "Revised" License | 5 votes |
package ch.epfl.scala.index package data package github import model.misc.GithubRepo private[github] object GithubReadme { def absoluteUrl(readmeHtml: String, githubRepo: GithubRepo, defaultBranch: String): String = { val GithubRepo(user, repo) = githubRepo import org.jsoup.Jsoup // github returns absolute url we need a "safe way" to replace the val someUrl = "http://NTU1ZTAwY2U2YTljZGZjOTYyYjg5NGZh.com" val doc = Jsoup.parse(readmeHtml, someUrl) val root = s"https://github.com/$user/$repo" def base(v: String) = s"$root/$v/$defaultBranch" val raw = base("raw") val blob = base("blob") doc .select("a, img") .toArray .map(_.asInstanceOf[org.jsoup.nodes.Element]) .foreach { e => val (at, replace) = if (e.tagName == "a") { val attr = "href" val href = if (e.attr(attr).startsWith("#")) root else blob e.attr("target", "_blank") (attr, href) } else ("src", raw) e.attr(at, e.absUrl(at).replaceAllLiterally(someUrl, replace)) } doc.body.childNodes.toArray.mkString("") } }
Example 12
Source File: HtmlUtils.scala From hepek with Apache License 2.0 | 5 votes |
package ba.sake.hepek.html import org.jsoup.Jsoup import org.jsoup.nodes._ object HtmlUtils { def process(htmlText: String, pretty: Boolean, xhtml: Boolean): String = if (xhtml || pretty) { val document = Jsoup.parse(htmlText) document .outputSettings() .prettyPrint(pretty) .outline(true) if (xhtml) { document .outputSettings() .escapeMode(Entities.EscapeMode.xhtml) .syntax(Document.OutputSettings.Syntax.xml) } document.html } else { // if nothing to do... htmlText } }
Example 13
Source File: FooterLinksSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.layouts import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.i18n.Lang import play.api.test.Helpers._ import play.twirl.api.Html import uk.gov.hmrc.play.views.html.layouts.FooterLinks import play.api.i18n.Messages.Implicits._ import play.api.inject.guice.GuiceApplicationBuilder import java.util.{List => JavaList} import scala.collection.JavaConverters._ import scala.collection.immutable.List class FooterLinksSpec extends WordSpec with Matchers { implicit val application = new GuiceApplicationBuilder() .configure(Map("play.i18n.langs" -> List("en", "cy"))) .build() val englishLinkTextEntries: JavaList[String] = List( "Cookies", "Privacy policy", "Terms and conditions", "Help using GOV.UK" ).asJava val welshLinkTextEntries: JavaList[String] = List( "Cwcis", "Polisi preifatrwydd", "Telerau ac Amodau", "Help wrth ddefnyddio GOV.UK" ).asJava "The footerLinks in English" should { val footerLinks = new FooterLinks() implicit val lang = Lang("en") val content = contentAsString(footerLinks()) val document = Jsoup.parse(content) val links = document.getElementsByTag("a") "include visually hidden h2 text in English" in { implicit val lang = Lang("en") val content = contentAsString(footerLinks()) content should include("<h2 class=\"visually-hidden\">Support links</h2>") } "Have the correct link text in English" in { links.eachText() should be(englishLinkTextEntries) } } "The footerLinks in Welsh" should { val footerLinks = new FooterLinks() implicit val lang = Lang("cy") val content = contentAsString(footerLinks()) val document = Jsoup.parse(content) val links = document.getElementsByTag("a") "include visually hidden h2 text in Welsh" in { content should include("<h2 class=\"visually-hidden\">Cysylltiadau cymorth</h2>") } "Have the correct link text in Welsh" in { links.eachText() should be(welshLinkTextEntries) } } }
Example 14
Source File: EuExitLinksSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.layouts import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.i18n.Lang import play.api.i18n.Messages.Implicits._ import play.api.inject.guice.GuiceApplicationBuilder import play.api.test.Helpers.{contentAsString, defaultAwaitTimeout} import java.util.{List => JavaList} import uk.gov.hmrc.play.views.html.layouts.eu_exit_links import scala.collection.JavaConverters._ import scala.collection.immutable.List class EuExitLinksSpec extends WordSpec with Matchers { val englishLinkTextEntries: JavaList[String] = List( "Prepare your business for the UK leaving the EU", "Prepare for EU Exit if you live in the UK", "Living in Europe after the UK leaves the EU", "Continue to live in the UK after it leaves the EU" ).asJava val welshLinkTextEntries: JavaList[String] = List( "Paratoi eich busnes ar gyfer y DU yn gadael yr UE (Saesneg yn unig)", "Paratoi ar gyfer Ymadael â’r UE os ydych yn byw yn y DU (Saesneg yn unig)", "Byw yn Ewrop ar ôl i’r DU adael yr UE (Saesneg yn unig)", "Parhau i fyw yn y DU ar ôl iddi adael yr UE (Saesneg yn unig)" ).asJava implicit val application = new GuiceApplicationBuilder() .configure(Map("play.i18n.langs" -> List("en", "cy"))) .build() "Eu Exit Links on an English Language Page" should { implicit val lang = Lang("en") val markup = contentAsString(eu_exit_links()) val document = Jsoup.parse(markup) val links = document.getElementsByTag("a") "Include the section header" in { markup should include("<h2 class=\"heading-medium\">Prepare for EU Exit</h2>") } "Include four links" in { links.size should be(4) } "Have the correct link text in English" in { links.eachText() should be(englishLinkTextEntries) } } "Eu Exit Links on a Welsh Language Page" should { implicit val lang = Lang("cy") val markup = contentAsString(eu_exit_links()) val document = Jsoup.parse(markup) val links = document.getElementsByTag("a") "Include the section header" in { markup should include("<h2 class=\"heading-medium\">Paratoi ar gyfer Ymadael â’r UE (Saesneg yn unig)</h2>") } "Include four links" in { links.size should be(4) } "Have the correct link text in Welsh" in { links.eachText() should be(welshLinkTextEntries) } } }
Example 15
Source File: BetaBannerSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.layouts import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.test.Helpers._ import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.layouts.BetaBanner class BetaBannerSpec extends WordSpec with Matchers with MessagesSupport { "The BetaBanner" should { "include correct banner text" in { val sResult = new BetaBanner()(true, "", "", true, false) val content = contentAsString(sResult) val document = Jsoup.parse(content) document .getElementsByClass("beta-banner") .text shouldBe "BETA This is a new service – your feedback will help us to improve it." } } }
Example 16
Source File: GTMSnippetSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.layouts import org.jsoup.Jsoup import org.scalatest.prop.PropertyChecks import org.scalatest.{Matchers, WordSpec} import play.api.Configuration import play.api.test.Helpers._ import play.twirl.api.Html import uk.gov.hmrc.play.config.GTMConfig import uk.gov.hmrc.play.views.html.layouts.GTMSnippet import scala.collection.JavaConverters._ class GTMSnippetSpec extends WordSpec with Matchers with PropertyChecks { "gtmSnippet" should { "include script tag with appropriate URL for container if gtm.container is defined" in { val transitionalUrl = "http://s3bucket/transitional/gtm.js" val mainUrl = "http://s3bucket/main/gtm.js" val dataLayerUrl = "http://s3bucket/include/gtm_dl.js" val containersAndUrls = Table( ("container", "url"), ("transitional", transitionalUrl), ("main", mainUrl) ) forAll(containersAndUrls) { (container: String, url: String) => val snippet = createSnippet( container = Some(container), mainUrl = Some(mainUrl), transitionalUrl = Some(transitionalUrl), dataLayerUrl = Some(dataLayerUrl)) script(snippet()) should contain(s"$url") script(snippet()) should contain(s"$dataLayerUrl") } } "not include script tag if gtm.container is not defined" in { val snippet = createSnippet(None, None, None, None) script(snippet()) shouldBe empty } } private def createSnippet( container: Option[String], mainUrl: Option[String], transitionalUrl: Option[String], dataLayerUrl: Option[String]): GTMSnippet = { val config = new GTMConfig( Configuration( Seq( container.map("gtm.container" -> _), mainUrl.map("gtm.main.url" -> _), transitionalUrl.map("gtm.transitional.url" -> _), dataLayerUrl.map("gtm.data.layer.url" -> _) ).flatten: _*)) new GTMSnippet(config) } private def script(html: Html): List[String] = { val content = contentAsString(html) val document = Jsoup.parse(content) document .head() .select("script") .iterator() .asScala .toList .map(_.attr("src")) } }
Example 17
Source File: OptimizelySnippetSpecs.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.layouts import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.Configuration import play.api.test.Helpers._ import uk.gov.hmrc.play.config.OptimizelyConfig import uk.gov.hmrc.play.views.html.layouts.OptimizelySnippet import scala.collection.JavaConverters._ class OptimizelySnippetSpecs extends WordSpec with Matchers { "optimizelySnippet" should { "include script tag if both project id and baseUrl are defined" in { val optimizelyBaseUrl = "http://optimizely.com/" val optimizelyProjectId = "1234567" val snippet = createSnippet(Some(optimizelyBaseUrl), Some(optimizelyProjectId)) scripts(snippet) should contain(s"$optimizelyBaseUrl$optimizelyProjectId.js") } "not include script tag if project id is not defined" in { val snippet = createSnippet(baseUrl = Some("base-url")) scripts(snippet) shouldBe empty } "not include script tag if baseUrl is not defined" in { val snippet = createSnippet(projectId = Some("id")) scripts(snippet) shouldBe empty } } private def createSnippet(baseUrl: Option[String] = None, projectId: Option[String] = None): OptimizelySnippet = new OptimizelySnippet( new OptimizelyConfig( Configuration( Seq( baseUrl.map("optimizely.url" -> _), projectId.map("optimizely.projectId" -> _) ).flatten: _*)) ) private def scripts(snippet: OptimizelySnippet): List[String] = { val content = contentAsString(snippet()) val document = Jsoup.parse(content) document.head().select("script").iterator().asScala.toList.map(_.attr("src")) } }
Example 18
Source File: SingleCheckboxSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.helpers import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.data.Form import play.api.data.Forms.{mapping, _} import play.api.test.Helpers._ import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.helpers.SingleCheckbox class SingleCheckboxSpec extends WordSpec with Matchers with MessagesSupport { case class DummyFormData(exampleCheckbox: Option[Boolean]) def dummyForm = Form( mapping( "exampleCheckbox" -> optional(boolean) )(DummyFormData.apply)(DummyFormData.unapply)) val singleCheckbox = new SingleCheckbox() "Have the checked attribute when value is 'true'" in { val WithTrueCheckboxValueForm = dummyForm.fill(DummyFormData(Some(true))) val doc = Jsoup.parse( contentAsString( singleCheckbox( WithTrueCheckboxValueForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe true checkboxElement.attr("checked") shouldBe "checked" checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe true } "The Single CheckBox" should { "Not have the checked attribute when value is 'None'" in { val doc = Jsoup.parse( contentAsString( singleCheckbox( dummyForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe false checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe false } "Not have the checked attribute when value is 'false'" in { val WithFalseCheckboxValueForm = dummyForm.fill(DummyFormData(Some(false))) val doc = Jsoup.parse( contentAsString( singleCheckbox( WithFalseCheckboxValueForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe false checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe false } } }