Skip to content

Commit

Permalink
Make node: HTMLElement available as an implicit
Browse files Browse the repository at this point in the history
This makes it possible to implement helper methods that edit
`innerHTML`. Fixes scalameta#538.
  • Loading branch information
olafurpg committed Jul 26, 2021
1 parent 5eaa114 commit aa5cdc9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.scalajs.linker.MemOutputFile
import java.util.concurrent.Executor
import scala.concurrent.ExecutionContext.Implicits.global
import org.scalajs.linker.interface.ClearableLinker
import scala.util.Random

class JsModifier extends mdoc.PreModifier {
override val name = "js"
Expand Down Expand Up @@ -140,6 +141,7 @@ class JsModifier extends mdoc.PreModifier {
val code = new CodeBuilder()
val wrapped = code
.println("object mdocjs {")
.println(s"""class HTMLElementImplicit(val value: _root_.org.scalajs.dom.raw.HTMLElement)""")
.foreach(runs)(code.println)
.println("}")
.toString
Expand Down Expand Up @@ -254,6 +256,7 @@ class JsModifier extends mdoc.PreModifier {
.println(
s"""def $run($mountNodeParam: _root_.org.scalajs.dom.raw.HTMLElement): Unit = {"""
)
.println(s"implicit val ${mountNodeParam}Implicit = new HTMLElementImplicit($mountNodeParam);")
.println(input.text)
.println("}")
.toString
Expand Down
28 changes: 26 additions & 2 deletions tests/unit-js/src/test/scala/tests/js/JsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,14 @@ class JsSuite extends BaseMarkdownSuite {
|println(jsdocs.ExampleJS.greeting)
|```
""".stripMargin,
"""|error: no-dom.md:4 (mdoc generated code) object scalajs is not a member of package org
"""|error: no-dom.md:3 (mdoc generated code) object scalajs is not a member of package org
|class HTMLElementImplicit(val value: _root_.org.scalajs.dom.raw.HTMLElement)
| ^
|
|error: no-dom.md:5 (mdoc generated code) object scalajs is not a member of package org
|def run0(node: _root_.org.scalajs.dom.raw.HTMLElement): Unit = {
| ^
""".stripMargin,
|""".stripMargin,
settings = {
val noScalajsDom = Classpath(baseSettings.site("js-classpath")).entries
.filterNot(_.toNIO.getFileName.toString.contains("scalajs-dom"))
Expand Down Expand Up @@ -377,4 +381,24 @@ class JsSuite extends BaseMarkdownSuite {
)
}
)

check(
"implicit-node",
"""
|```scala mdoc:js:shared:invisible
|def div(msg: String)(implicit node: HTMLElementImplicit) =
| node.value.innerHTML = "msg"
|```
|```scala mdoc:js
|div("hello world")
|```
|""".stripMargin,
"""|```scala
|div("hello world")
|```
|<div id="mdoc-html-run1" data-mdoc-js></div>
|<script type="text/javascript" src="implicit-node.md.js" defer></script>
|<script type="text/javascript" src="mdoc.js" defer></script>
|""".stripMargin
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite {
original: String,
settings: Settings = baseSettings,
onOutput: String => Unit = _ => ()
): Unit = {
)(implicit loc: munit.Location): Unit = {
test(name) {
val reporter = newReporter()
val context = newContext(settings, reporter)
Expand Down

0 comments on commit aa5cdc9

Please sign in to comment.