Writing org.w3c.dom-related code in the common part of a jvm + js Multiplatform project

Hi all,

I would like to write some org.w3c.dom.Node-related code in the commonMain source set of a jvm + js Multiplatform project but the compiler is not happy:

import org.w3c.dom.Node // compiler error here: Unresolved reference: Node

fun sayHello(node: Node) {
    node.ownerDocument!!.createElement("div").textContent = "hello"

Is there any way to do so? My goal is to use this common code to process XML in the jvm part, and to process the browser DOM in the js part.

Please note that the same code is compiling properly in the jvmMain and jsMain source sets.

Below is a bit of information about my setup.

I have created a Kotlin Multiplatform Library project using IntelliJ IDEA 2021.1.
Then I removed the native source set to keep only common , jvm and js (browser target).
I changed the kotlin version to 1.5.20 in build.gradle.kts (just in case it may help, it was 1.4.32 before).

Hi there,

The DOM APIs can’t live in the common module because common code is supposed to work on ALL platforms, even K/N (which doesn’t have that DOM API, and probably never will). I think JetBrain wants to fix that by being able to define “common code” that works only for a subset of platform, thus unlocking APIs on project that don’t use all platforms, a bit like what they have on K/N to know which native API is available, but it’s not there yet as far as I know.

I am not even sure the two DOM APIs would be fully compatible though, and it might never be possible to share DOM code unless someone writes a multiplatform library for that.

@seekdasky Thank you for answering: much appreciated!

I see. In the meantime I will probably try to hack the gradle script to “generate” source code by copying jvmMain files into jsMain (or vice-versa).