Browse Source

Add Xml parsing support to js platform (#3105)

* First pass at adding Xml parsing support to js platform

Likely still need to add tests and samples

* Add JS to Parser Node and Element converter

Mirrors native impl by returning empty strings when content is unavailable

Moves MalformedXMLException out of native code, useful for common case.

* Remove unneeded null checks in Parser

Last commit handled null management

* Add newlines for code linter

* Fix namespaceUri bug

Undo accidental capture of localName

Also remove unneeded null check

* Undo grade.properies change

Had to disable webpack version number property to run sample

* Clean up NodeImpl comments

Corrected description

* Make NodeList impl lazy

Generates only when needed, serves cache after
compose-tooling-223 v1.5.0-dev1036
mdigman 2 years ago committed by GitHub
parent
commit
62e5f1edad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/MalformedXMLException.kt
  2. 8
      components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt
  3. 17
      components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/xmldom/ElementImpl.kt
  4. 28
      components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/xmldom/NodeImpl.kt
  5. 3
      components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt

6
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/MalformedXMLException.kt

@ -0,0 +1,6 @@
package org.jetbrains.compose.resources.vector.xmldom
/**
* Error throw when parsed XML is malformed
*/
class MalformedXMLException(message: String?) : Exception(message)

8
components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt

@ -6,8 +6,11 @@
package org.jetbrains.compose.resources
import org.jetbrains.compose.resources.vector.xmldom.Element
import org.jetbrains.compose.resources.vector.xmldom.ElementImpl
import org.jetbrains.compose.resources.vector.xmldom.MalformedXMLException
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Int8Array
import org.w3c.dom.parsing.DOMParser
import org.w3c.xhr.ARRAYBUFFER
import org.w3c.xhr.XMLHttpRequest
import org.w3c.xhr.XMLHttpRequestResponseType
@ -45,7 +48,10 @@ internal actual class MissingResourceException actual constructor(path: String)
Exception("Missing resource with path: $path")
internal actual fun parseXML(byteArray: ByteArray): Element {
throw UnsupportedOperationException("XML Vector Drawables are not supported for Web target")
val xmlString = byteArray.decodeToString()
val xmlDom = DOMParser().parseFromString(xmlString, "application/xml")
val domElement = xmlDom.documentElement ?: throw MalformedXMLException("missing documentElement")
return ElementImpl(domElement)
}
internal actual fun isSyncResourceLoadingSupported() = false

17
components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/xmldom/ElementImpl.kt

@ -0,0 +1,17 @@
package org.jetbrains.compose.resources.vector.xmldom
import org.w3c.dom.Element as DomElement
internal class ElementImpl(val element: DomElement): NodeImpl(element), Element {
override val localName: String
get() = element.localName
override val namespaceURI: String
get() = element.namespaceURI ?: ""
override fun getAttributeNS(nameSpaceURI: String, localName: String): String =
element.getAttributeNS(nameSpaceURI, localName) ?: ""
override fun getAttribute(name: String): String = element.getAttribute(name) ?: ""
}

28
components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/xmldom/NodeImpl.kt

@ -0,0 +1,28 @@
package org.jetbrains.compose.resources.vector.xmldom
import org.w3c.dom.Node as DomNode
import org.w3c.dom.Element as DomElement
internal open class NodeImpl(val n: DomNode): Node {
override val nodeName: String
get() = n.nodeName
override val localName = "" /* localName is not a Node property, only applies to Elements and Attrs */
override val namespaceURI = "" /* namespaceURI is not a Node property, only applies to Elements and Attrs */
override val childNodes: NodeList by lazy {
object: NodeList {
override fun item(i: Int): Node {
val child = n.childNodes.item(i)
?: throw IndexOutOfBoundsException("no child node accessible at index=$i")
return if (child is DomElement) ElementImpl(child) else NodeImpl(child)
}
override val length: Int = n.childNodes.length
}
}
override fun lookupPrefix(namespaceURI: String): String = n.lookupPrefix(namespaceURI) ?: ""
}

3
components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt

@ -4,6 +4,7 @@
*/
package org.jetbrains.compose.resources.vector.xmldom
import org.jetbrains.compose.resources.vector.xmldom.MalformedXMLException
import platform.Foundation.*
import platform.darwin.NSObject
@ -17,8 +18,6 @@ internal fun parse(xml: String): Element {
return parser.root!!
}
class MalformedXMLException(message: String?) : Exception(message)
private class ElementImpl(override val localName: String,
override val nodeName: String,
override val namespaceURI: String,

Loading…
Cancel
Save