Skip to content

Commit

Permalink
Throw an explicit error for passing nodes crossing document barriers #9
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjdew committed Apr 25, 2014
1 parent fd81d93 commit 891bd74
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions memory-operations-functional.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ as map:map
let $modified-node-ids as element()* := mem-op:id-wrapper($nodes-to-modify) (: This line uses function mapping :)
return
(
mem-op:all-nodes-from-same-doc($nodes-to-modify,map:get($transaction-map,"copy")),
map:new((
$transaction-map,
map:entry(
Expand Down
10 changes: 10 additions & 0 deletions memory-operations.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ as empty-sequence()
let $modified-node-ids as element()* := mem-op:id-wrapper($nodes-to-modify) (: This line uses function mapping :)
return
(
mem-op:all-nodes-from-same-doc($nodes-to-modify,map:get($transaction-map,"copy")),
map:put(
$transaction-map,
"operation",
Expand Down Expand Up @@ -379,6 +380,14 @@ as empty-sequence()
else ()
};

declare function mem-op:all-nodes-from-same-doc($nodes as node()*,$parent-node as node()) as empty-sequence() {
(: NOTE: must use every in satisfies to account for multiple outermost nodes :)
if (every $n in node-op:outermost(($parent-node,$nodes)) satisfies $n is $parent-node)
then ()
else
error(xs:QName("mem-op:MIXEDSOURCES"), "The nodes to change are comming from multiple sources",$nodes)
};

(: The process functions handle the core logic for handling forked paths that
need to be altered :)
declare
Expand All @@ -388,6 +397,7 @@ function mem-op:process(
$operation)
as node()*
{
mem-op:all-nodes-from-same-doc($nodes-to-modify,root($nodes-to-modify[1])),
mem-op:process((), $nodes-to-modify, $new-nodes, $operation, ())
};

Expand Down
14 changes: 14 additions & 0 deletions test/memory-operations-test.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,18 @@ as item()*
assert:equal(fn:string($div/@class), "added-class")
)
)
};

declare %test:case function throws-error-on-mixed-sources()
as item()*
{
let $other-doc := <doc><p>my paragraph</p></doc>
return
assert:true(
try {
mem:replace(($test-xml,$other-doc)//p, <PARA/>)
} catch mem:MIXEDSOURCES {
fn:true()
}
)
};

0 comments on commit 891bd74

Please sign in to comment.