Skip to content

Commit

Permalink
Fix computation of expand-text
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Oct 24, 2021
1 parent eccd853 commit 66f54c2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/main/scala/com/xmlcalabash/model/xml/Artifact.scala
Expand Up @@ -278,6 +278,27 @@ class Artifact(val config: XMLCalabash) {
}
}

def inScopeExpandText(node: XdmNode): Boolean = {
if (node.getNodeKind == XdmNodeKind.ELEMENT) {
val attr = if (node.getNodeName.getNamespaceURI == XProcConstants.ns_p) {
XProcConstants._expand_text
} else {
XProcConstants.p_expand_text
}
val expand = Option(node.getAttributeValue(attr))
if (expand.isDefined) {
// FIXME: what about AVT? what about invalid values?
return expand.get == "true"
}
}

if (Option(node.getParent).isDefined) {
inScopeExpandText(node.getParent)
} else {
false
}
}

protected[model] def parse(node: XdmNode): Unit = {
_synthetic = false
_tumbleId = tumbleId(node)
Expand Down
7 changes: 6 additions & 1 deletion src/main/scala/com/xmlcalabash/model/xml/Inline.scala
Expand Up @@ -23,9 +23,10 @@ class Inline(override val config: XMLCalabash, srcNode: XdmNode, implied: Boolea
private var _context_provided = false
private val nameBindings = mutable.HashSet.empty[QName]
private val _statics = mutable.HashMap.empty[String, Message]
private var _expandText = false

if (srcNode.getNodeKind != XdmNodeKind.DOCUMENT) {
throw new RuntimeException("inline document must be a document")
throw XProcException.xiThisCantHappen(s"Attempt to create p:inline from something that isn't a document node: ${srcNode}")
}

def this(config: XMLCalabash, srcNode: XdmNode) = {
Expand All @@ -44,6 +45,7 @@ class Inline(override val config: XMLCalabash, srcNode: XdmNode, implied: Boolea
nameBindings ++= copy.nameBindings
_inScopeDynamics = copy._inScopeDynamics
_inScopeStatics = copy._inScopeStatics
_expandText = copy._expandText
staticContext = copy.staticContext
}

Expand Down Expand Up @@ -107,9 +109,12 @@ class Inline(override val config: XMLCalabash, srcNode: XdmNode, implied: Boolea
if (_contentType.isEmpty) {
_contentType = Some(MediaType.XML)
}

_expandText = inScopeExpandText(node)
}
}

def expandText: Boolean = _expandText
def encoding: Option[String] = _encoding

def excludeURIs: Set[String] = {
Expand Down
Expand Up @@ -76,7 +76,7 @@ class XMLCalabashRuntime protected[xmlcalabash] (val decl: DeclareStep) extends
if (inline.documentProperties.isDefined) {
expander.documentProperties = inline.documentProperties.get
}
defaults += expander.loadDocument()
defaults += expander.loadDocument(inline.expandText)
case doc: Document =>
defaults += doc.loadDocument()
case _ =>
Expand Down
Expand Up @@ -31,7 +31,6 @@ protected[xmlcalabash] class InlineExpander(val config: XMLCalabash, val node: X
var encoding: Option[String] = None
var excludeURIs: Set[String] = Set()
var msgBindings: Map[String, XProcItemMessage] = Map()
var expandText: Boolean = true
var contextItem: Option[XProcItemMessage] = None

private var _documentProperties: Map[QName, XdmValue] = Map.empty[QName, XdmValue]
Expand Down Expand Up @@ -60,7 +59,7 @@ protected[xmlcalabash] class InlineExpander(val config: XMLCalabash, val node: X
_documentProperties = Map.empty[QName, XdmValue] ++ props
}

def loadDocument(): DocumentRequest = {
def loadDocument(expandText: Boolean): DocumentRequest = {
if (encoding.isDefined) {
if (contentType.xmlContentType || contentType.htmlContentType) {
throw XProcException.xdCannotEncodeMarkup(encoding.get, contentType, exprContext.location)
Expand Down
Expand Up @@ -129,11 +129,10 @@ class InlineLoader() extends AbstractLoader {
Set()
}
expander.msgBindings = msgBindings.toMap
expander.expandText = expandText
expander.contextItem = contextItem
expander.documentProperties = docProps

val req = expander.loadDocument()
val req = expander.loadDocument(expandText)
val resp = config.documentManager.parse(req)
if (resp.shadow.isDefined) {
consumer.receive("result", resp.shadow.get, new XProcMetadata(resp.contentType, resp.props))
Expand Down

0 comments on commit 66f54c2

Please sign in to comment.