Skip to content

Commit

Permalink
Add Xml parsing support to js platform (#3105)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdigman authored Apr 27, 2023
1 parent f178031 commit 62e5f1e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jetbrains.compose.resources.vector.xmldom

/**
* Error throw when parsed XML is malformed
*/
class MalformedXMLException(message: String?) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) ?: ""
}
Original file line number Diff line number Diff line change
@@ -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) ?: ""

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit 62e5f1e

Please sign in to comment.