From 93549c451d65ca37eaaf30c205d231a9767fa816 Mon Sep 17 00:00:00 2001 From: jez Date: Thu, 18 Sep 2008 22:43:56 +0100 Subject: [PATCH] improved preceding and preceding-sibling in the face of consecutive text nodes --- include/XPath/impl/xpath_axis_enumerator.hpp | 23 +++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/include/XPath/impl/xpath_axis_enumerator.hpp b/include/XPath/impl/xpath_axis_enumerator.hpp index b99c928a..ba09a589 100644 --- a/include/XPath/impl/xpath_axis_enumerator.hpp +++ b/include/XPath/impl/xpath_axis_enumerator.hpp @@ -146,6 +146,23 @@ protected: return next; } // findNextSibling + static DOM::Node findPreviousSibling(const DOM::Node& node) + { + DOM::Node prev = node.getPreviousSibling(); + + if((prev == 0) || (!nodeIsText(prev))) + return prev; + + DOM::Node prev_again = prev.getPreviousSibling(); + while((prev_again != 0) && (nodeIsText(prev_again))) + { + prev = prev_again; + prev_again = prev.getPreviousSibling(); + } // while + + return prev; + } // findPreviousSibling + private: static bool nodeIsText(const DOM::Node& node) { @@ -525,7 +542,7 @@ private: DOM::Node previousInDocument(const DOM::Node& context) { - DOM::Node next = context.getPreviousSibling(); + DOM::Node next = AxisWalker::findPreviousSibling(context); if(next != 0) return getLastDescendant(next); @@ -567,13 +584,13 @@ public: PrecedingSiblingAxisWalker(const DOM::Node& context) : AxisWalker(false) { if(context != 0) - AxisWalker::set(context.getPreviousSibling()); + AxisWalker::set(AxisWalker::findPreviousSibling(context)); } // PrecedingSiblingAxisWalker virtual void advance() { if(AxisWalker::get() != 0) - AxisWalker::set(AxisWalker::get().getPreviousSibling()); + AxisWalker::set(AxisWalker::findPreviousSibling(AxisWalker::get())); } // advance virtual AxisWalker* clone() const { return new PrecedingSiblingAxisWalker(*this); }