2 * xpointer.c : Code to handle XML Pointer
4 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
6 * http://www.w3.org/TR/2000/CR-xptr-20000607
8 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
10 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
12 * See Copyright for the status of this software.
21 * TODO: better handling of error cases, the full expression should
22 * be parsed beforehand instead of a progressive evaluation
23 * TODO: Access into entities references are not supported now ...
24 * need a start to be able to pop out of entities refs since
25 * parent is the endity declaration, not the ref.
29 #include <libxml/xpointer.h>
30 #include <libxml/xmlmemory.h>
31 #include <libxml/parserInternals.h>
32 #include <libxml/uri.h>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
35 #include <libxml/xmlerror.h>
36 #include <libxml/globals.h>
38 #ifdef LIBXML_XPTR_ENABLED
40 /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41 #define XPTR_XMLNS_SCHEME
43 /* #define DEBUG_RANGES */
45 #ifdef LIBXML_DEBUG_ENABLED
46 #include <libxml/debugXML.h>
51 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
56 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
60 /************************************************************************
62 * Some factorized error routines *
64 ************************************************************************/
68 * @extra: extra informations
70 * Handle a redefinition of attribute error
73 xmlXPtrErrMemory(const char *extra)
75 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
76 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
78 "Memory allocation failed : %s\n", extra);
83 * @ctxt: an XPTR evaluation context
84 * @extra: extra informations
86 * Handle a redefinition of attribute error
89 xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
90 const char * msg, const xmlChar *extra)
94 if ((ctxt == NULL) || (ctxt->context == NULL)) {
95 __xmlRaiseError(NULL, NULL, NULL,
96 NULL, NULL, XML_FROM_XPOINTER, error,
97 XML_ERR_ERROR, NULL, 0,
98 (const char *) extra, NULL, NULL, 0, 0,
102 ctxt->context->lastError.domain = XML_FROM_XPOINTER;
103 ctxt->context->lastError.code = error;
104 ctxt->context->lastError.level = XML_ERR_ERROR;
105 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
106 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
107 ctxt->context->lastError.node = ctxt->context->debugNode;
108 if (ctxt->context->error != NULL) {
109 ctxt->context->error(ctxt->context->userData,
110 &ctxt->context->lastError);
112 __xmlRaiseError(NULL, NULL, NULL,
113 NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
114 error, XML_ERR_ERROR, NULL, 0,
115 (const char *) extra, (const char *) ctxt->base, NULL,
116 ctxt->cur - ctxt->base, 0,
121 /************************************************************************
123 * A few helper functions for child sequences *
125 ************************************************************************/
126 /* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
127 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
132 * Returns the number of child for an element, -1 in case of error
135 xmlXPtrGetArity(xmlNodePtr cur) {
140 for (i = 0;cur != NULL;cur = cur->next) {
141 if ((cur->type == XML_ELEMENT_NODE) ||
142 (cur->type == XML_DOCUMENT_NODE) ||
143 (cur->type == XML_HTML_DOCUMENT_NODE)) {
154 * Returns the index of the node in its parent children list, -1
158 xmlXPtrGetIndex(xmlNodePtr cur) {
162 for (i = 1;cur != NULL;cur = cur->prev) {
163 if ((cur->type == XML_ELEMENT_NODE) ||
164 (cur->type == XML_DOCUMENT_NODE) ||
165 (cur->type == XML_HTML_DOCUMENT_NODE)) {
173 * xmlXPtrGetNthChild:
175 * @no: the child number
177 * Returns the @no'th element child of @cur or NULL
180 xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
185 for (i = 0;i <= no;cur = cur->next) {
188 if ((cur->type == XML_ELEMENT_NODE) ||
189 (cur->type == XML_DOCUMENT_NODE) ||
190 (cur->type == XML_HTML_DOCUMENT_NODE)) {
199 /************************************************************************
201 * Handling of XPointer specific types *
203 ************************************************************************/
207 * @node1: the first node
208 * @index1: the first index
209 * @node2: the second node
210 * @index2: the second index
212 * Compare two points w.r.t document order
214 * Returns -2 in case of error 1 if first point < second point, 0 if
215 * that's the same point, -1 otherwise
218 xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
219 if ((node1 == NULL) || (node2 == NULL))
222 * a couple of optimizations which will avoid computations in most cases
224 if (node1 == node2) {
231 return(xmlXPathCmpNodes(node1, node2));
236 * @node: the xmlNodePtr
237 * @indx: the indx within the node
239 * Create a new xmlXPathObjectPtr of type point
241 * Returns the newly created object.
243 static xmlXPathObjectPtr
244 xmlXPtrNewPoint(xmlNodePtr node, int indx) {
245 xmlXPathObjectPtr ret;
252 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
254 xmlXPtrErrMemory("allocating point");
257 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
258 ret->type = XPATH_POINT;
259 ret->user = (void *) node;
265 * xmlXPtrRangeCheckOrder:
266 * @range: an object range
268 * Make sure the points in the range are in the right order
271 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
276 if (range->type != XPATH_RANGE)
278 if (range->user2 == NULL)
280 tmp = xmlXPtrCmpPoints(range->user, range->index,
281 range->user2, range->index2);
284 range->user = range->user2;
287 range->index = range->index2;
293 * xmlXPtrRangesEqual:
294 * @range1: the first range
295 * @range2: the second range
299 * Returns 1 if equal, 0 otherwise
302 xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
303 if (range1 == range2)
305 if ((range1 == NULL) || (range2 == NULL))
307 if (range1->type != range2->type)
309 if (range1->type != XPATH_RANGE)
311 if (range1->user != range2->user)
313 if (range1->index != range2->index)
315 if (range1->user2 != range2->user2)
317 if (range1->index2 != range2->index2)
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
329 * Create a new xmlXPathObjectPtr of type range
331 * Returns the newly created object.
334 xmlXPtrNewRange(xmlNodePtr start, int startindex,
335 xmlNodePtr end, int endindex) {
336 xmlXPathObjectPtr ret;
347 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
349 xmlXPtrErrMemory("allocating range");
352 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
353 ret->type = XPATH_RANGE;
355 ret->index = startindex;
357 ret->index2 = endindex;
358 xmlXPtrRangeCheckOrder(ret);
363 * xmlXPtrNewRangePoints:
364 * @start: the starting point
365 * @end: the ending point
367 * Create a new xmlXPathObjectPtr of type range using 2 Points
369 * Returns the newly created object.
372 xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
373 xmlXPathObjectPtr ret;
379 if (start->type != XPATH_POINT)
381 if (end->type != XPATH_POINT)
384 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
386 xmlXPtrErrMemory("allocating range");
389 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
390 ret->type = XPATH_RANGE;
391 ret->user = start->user;
392 ret->index = start->index;
393 ret->user2 = end->user;
394 ret->index2 = end->index;
395 xmlXPtrRangeCheckOrder(ret);
400 * xmlXPtrNewRangePointNode:
401 * @start: the starting point
402 * @end: the ending node
404 * Create a new xmlXPathObjectPtr of type range from a point to a node
406 * Returns the newly created object.
409 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
410 xmlXPathObjectPtr ret;
416 if (start->type != XPATH_POINT)
419 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
421 xmlXPtrErrMemory("allocating range");
424 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
425 ret->type = XPATH_RANGE;
426 ret->user = start->user;
427 ret->index = start->index;
430 xmlXPtrRangeCheckOrder(ret);
435 * xmlXPtrNewRangeNodePoint:
436 * @start: the starting node
437 * @end: the ending point
439 * Create a new xmlXPathObjectPtr of type range from a node to a point
441 * Returns the newly created object.
444 xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
445 xmlXPathObjectPtr ret;
451 if (start->type != XPATH_POINT)
453 if (end->type != XPATH_POINT)
456 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
458 xmlXPtrErrMemory("allocating range");
461 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
462 ret->type = XPATH_RANGE;
465 ret->user2 = end->user;
466 ret->index2 = end->index;
467 xmlXPtrRangeCheckOrder(ret);
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
478 * Returns the newly created object.
481 xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
482 xmlXPathObjectPtr ret;
489 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
491 xmlXPtrErrMemory("allocating range");
494 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
495 ret->type = XPATH_RANGE;
500 xmlXPtrRangeCheckOrder(ret);
505 * xmlXPtrNewCollapsedRange:
506 * @start: the starting and ending node
508 * Create a new xmlXPathObjectPtr of type range using a single nodes
510 * Returns the newly created object.
513 xmlXPtrNewCollapsedRange(xmlNodePtr start) {
514 xmlXPathObjectPtr ret;
519 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
521 xmlXPtrErrMemory("allocating range");
524 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
525 ret->type = XPATH_RANGE;
534 * xmlXPtrNewRangeNodeObject:
535 * @start: the starting node
536 * @end: the ending object
538 * Create a new xmlXPathObjectPtr of type range from a not to an object
540 * Returns the newly created object.
543 xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
544 xmlXPathObjectPtr ret;
558 if (end->nodesetval->nodeNr <= 0)
566 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
568 xmlXPtrErrMemory("allocating range");
571 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
572 ret->type = XPATH_RANGE;
577 ret->user2 = end->user;
578 ret->index2 = end->index;
581 ret->user2 = end->user2;
582 ret->index2 = end->index2;
584 case XPATH_NODESET: {
585 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
593 xmlXPtrRangeCheckOrder(ret);
597 #define XML_RANGESET_DEFAULT 10
600 * xmlXPtrLocationSetCreate:
601 * @val: an initial xmlXPathObjectPtr, or NULL
603 * Create a new xmlLocationSetPtr of type double and of value @val
605 * Returns the newly created object.
608 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
609 xmlLocationSetPtr ret;
611 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
613 xmlXPtrErrMemory("allocating locationset");
616 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
618 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
619 sizeof(xmlXPathObjectPtr));
620 if (ret->locTab == NULL) {
621 xmlXPtrErrMemory("allocating locationset");
625 memset(ret->locTab, 0 ,
626 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
627 ret->locMax = XML_RANGESET_DEFAULT;
628 ret->locTab[ret->locNr++] = val;
634 * xmlXPtrLocationSetAdd:
635 * @cur: the initial range set
636 * @val: a new xmlXPathObjectPtr
638 * add a new xmlXPathObjectPtr to an existing LocationSet
639 * If the location already exist in the set @val is freed.
642 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
645 if ((cur == NULL) || (val == NULL)) return;
648 * check against doublons
650 for (i = 0;i < cur->locNr;i++) {
651 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
652 xmlXPathFreeObject(val);
658 * grow the locTab if needed
660 if (cur->locMax == 0) {
661 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
662 sizeof(xmlXPathObjectPtr));
663 if (cur->locTab == NULL) {
664 xmlXPtrErrMemory("adding location to set");
667 memset(cur->locTab, 0 ,
668 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
669 cur->locMax = XML_RANGESET_DEFAULT;
670 } else if (cur->locNr == cur->locMax) {
671 xmlXPathObjectPtr *temp;
674 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
675 sizeof(xmlXPathObjectPtr));
677 xmlXPtrErrMemory("adding location to set");
682 cur->locTab[cur->locNr++] = val;
686 * xmlXPtrLocationSetMerge:
687 * @val1: the first LocationSet
688 * @val2: the second LocationSet
690 * Merges two rangesets, all ranges from @val2 are added to @val1
692 * Returns val1 once extended or NULL in case of error.
695 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
698 if (val1 == NULL) return(NULL);
699 if (val2 == NULL) return(val1);
702 * !!!!! this can be optimized a lot, knowing that both
703 * val1 and val2 already have unicity of their values.
706 for (i = 0;i < val2->locNr;i++)
707 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
713 * xmlXPtrLocationSetDel:
714 * @cur: the initial range set
715 * @val: an xmlXPathObjectPtr
717 * Removes an xmlXPathObjectPtr from an existing LocationSet
720 xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
723 if (cur == NULL) return;
724 if (val == NULL) return;
727 * check against doublons
729 for (i = 0;i < cur->locNr;i++)
730 if (cur->locTab[i] == val) break;
732 if (i >= cur->locNr) {
734 xmlGenericError(xmlGenericErrorContext,
735 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
740 for (;i < cur->locNr;i++)
741 cur->locTab[i] = cur->locTab[i + 1];
742 cur->locTab[cur->locNr] = NULL;
746 * xmlXPtrLocationSetRemove:
747 * @cur: the initial range set
748 * @val: the index to remove
750 * Removes an entry from an existing LocationSet list.
753 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
754 if (cur == NULL) return;
755 if (val >= cur->locNr) return;
757 for (;val < cur->locNr;val++)
758 cur->locTab[val] = cur->locTab[val + 1];
759 cur->locTab[cur->locNr] = NULL;
763 * xmlXPtrFreeLocationSet:
764 * @obj: the xmlLocationSetPtr to free
766 * Free the LocationSet compound (not the actual ranges !).
769 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
772 if (obj == NULL) return;
773 if (obj->locTab != NULL) {
774 for (i = 0;i < obj->locNr; i++) {
775 xmlXPathFreeObject(obj->locTab[i]);
777 xmlFree(obj->locTab);
783 * xmlXPtrNewLocationSetNodes:
784 * @start: the start NodePtr value
785 * @end: the end NodePtr value or NULL
787 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
788 * it with the single range made of the two nodes @start and @end
790 * Returns the newly created object.
793 xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
794 xmlXPathObjectPtr ret;
796 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
798 xmlXPtrErrMemory("allocating locationset");
801 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
802 ret->type = XPATH_LOCATIONSET;
804 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
806 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
811 * xmlXPtrNewLocationSetNodeSet:
814 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
815 * it with all the nodes from @set
817 * Returns the newly created object.
820 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
821 xmlXPathObjectPtr ret;
823 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
825 xmlXPtrErrMemory("allocating locationset");
828 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
829 ret->type = XPATH_LOCATIONSET;
832 xmlLocationSetPtr newset;
834 newset = xmlXPtrLocationSetCreate(NULL);
838 for (i = 0;i < set->nodeNr;i++)
839 xmlXPtrLocationSetAdd(newset,
840 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
842 ret->user = (void *) newset;
848 * xmlXPtrWrapLocationSet:
849 * @val: the LocationSet value
851 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
853 * Returns the newly created object.
856 xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
857 xmlXPathObjectPtr ret;
859 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
861 xmlXPtrErrMemory("allocating locationset");
864 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
865 ret->type = XPATH_LOCATIONSET;
866 ret->user = (void *) val;
870 /************************************************************************
874 ************************************************************************/
876 static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
879 * Macros for accessing the content. Those should be used only by the parser,
882 * Dirty macros, i.e. one need to make assumption on the context to use them
884 * CUR_PTR return the current pointer to the xmlChar to be parsed.
885 * CUR returns the current xmlChar value, i.e. a 8 bit value
886 * in ISO-Latin or UTF-8.
887 * This should be used internally by the parser
888 * only to compare to ASCII values otherwise it would break when
889 * running with UTF-8 encoding.
890 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
891 * to compare on ASCII based substring.
892 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
893 * strings within the parser.
894 * CURRENT Returns the current char value, with the full decoding of
895 * UTF-8 if we are using this mode. It returns an int.
896 * NEXT Skip to the next character, this does the proper decoding
897 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
898 * It returns the pointer to the current xmlChar.
901 #define CUR (*ctxt->cur)
902 #define SKIP(val) ctxt->cur += (val)
903 #define NXT(val) ctxt->cur[(val)]
904 #define CUR_PTR ctxt->cur
906 #define SKIP_BLANKS \
907 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
909 #define CURRENT (*ctxt->cur)
910 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
914 * @ctxt: the XPointer Parser context
915 * @index: the child number
917 * Move the current node of the nodeset on the stack to the
918 * given child if found
921 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
922 xmlNodePtr cur = NULL;
923 xmlXPathObjectPtr obj;
924 xmlNodeSetPtr oldset;
926 CHECK_TYPE(XPATH_NODESET);
927 obj = valuePop(ctxt);
928 oldset = obj->nodesetval;
929 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
930 xmlXPathFreeObject(obj);
931 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
934 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
936 xmlXPathFreeObject(obj);
937 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
940 oldset->nodeTab[0] = cur;
941 valuePush(ctxt, obj);
945 * xmlXPtrEvalXPtrPart:
946 * @ctxt: the XPointer Parser context
947 * @name: the preparsed Scheme for the XPtrPart
949 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
950 * | Scheme '(' SchemeSpecificExpr ')'
952 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
954 * SchemeSpecificExpr ::= StringWithBalancedParens
956 * StringWithBalancedParens ::=
957 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
958 * [VC: Parenthesis escaping]
960 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
962 * VC: Parenthesis escaping:
963 * The end of an XPointer part is signaled by the right parenthesis ")"
964 * character that is balanced with the left parenthesis "(" character
965 * that began the part. Any unbalanced parenthesis character inside the
966 * expression, even within literals, must be escaped with a circumflex (^)
967 * character preceding it. If the expression contains any literal
968 * occurrences of the circumflex, each must be escaped with an additional
969 * circumflex (that is, ^^). If the unescaped parentheses in the expression
970 * are not balanced, a syntax error results.
972 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
973 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
975 * TODO: there is no new scheme registration mechanism
979 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
980 xmlChar *buffer, *cur;
985 name = xmlXPathParseName(ctxt);
987 XP_ERROR(XPATH_EXPR_ERROR);
990 XP_ERROR(XPATH_EXPR_ERROR);
994 len = xmlStrlen(ctxt->cur);
996 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
997 if (buffer == NULL) {
998 xmlXPtrErrMemory("allocating buffer");
1011 } else if (CUR == '(') {
1014 } else if (CUR == '^') {
1016 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
1029 if ((level != 0) && (CUR == 0)) {
1031 XP_ERROR(XPTR_SYNTAX_ERROR);
1034 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1035 const xmlChar *left = CUR_PTR;
1039 * To evaluate an xpointer scheme element (4.3) we need:
1040 * context initialized to the root
1041 * context position initalized to 1
1042 * context size initialized to 1
1044 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1045 ctxt->context->proximityPosition = 1;
1046 ctxt->context->contextSize = 1;
1047 xmlXPathEvalExpr(ctxt);
1049 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1050 const xmlChar *left = CUR_PTR;
1054 if (buffer[0] == '/') {
1056 xmlXPtrEvalChildSeq(ctxt, NULL);
1058 name2 = xmlXPathParseName(ctxt);
1059 if (name2 == NULL) {
1062 XP_ERROR(XPATH_EXPR_ERROR);
1064 xmlXPtrEvalChildSeq(ctxt, name2);
1067 #ifdef XPTR_XMLNS_SCHEME
1068 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1069 const xmlChar *left = CUR_PTR;
1075 prefix = xmlXPathParseNCName(ctxt);
1076 if (prefix == NULL) {
1079 XP_ERROR(XPTR_SYNTAX_ERROR);
1086 XP_ERROR(XPTR_SYNTAX_ERROR);
1090 /* @@ check escaping in the XPointer WD */
1092 value = xmlParseURI((const char *)ctxt->cur);
1093 if (value == NULL) {
1097 XP_ERROR(XPTR_SYNTAX_ERROR);
1099 URI = xmlSaveUri(value);
1105 XP_ERROR(XPATH_MEMORY_ERROR);
1108 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1112 #endif /* XPTR_XMLNS_SCHEME */
1114 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1115 "unsupported scheme '%s'\n", name);
1122 * xmlXPtrEvalFullXPtr:
1123 * @ctxt: the XPointer Parser context
1124 * @name: the preparsed Scheme for the first XPtrPart
1126 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1128 * As the specs says:
1130 * When multiple XPtrParts are provided, they must be evaluated in
1131 * left-to-right order. If evaluation of one part fails, the nexti
1132 * is evaluated. The following conditions cause XPointer part failure:
1134 * - An unknown scheme
1135 * - A scheme that does not locate any sub-resource present in the resource
1136 * - A scheme that is not applicable to the media type of the resource
1138 * The XPointer application must consume a failed XPointer part and
1139 * attempt to evaluate the next one, if any. The result of the first
1140 * XPointer part whose evaluation succeeds is taken to be the fragment
1141 * located by the XPointer as a whole. If all the parts fail, the result
1142 * for the XPointer as a whole is a sub-resource error.
1145 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1146 * expressions or other schemes.
1149 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1151 name = xmlXPathParseName(ctxt);
1153 XP_ERROR(XPATH_EXPR_ERROR);
1154 while (name != NULL) {
1155 ctxt->error = XPATH_EXPRESSION_OK;
1156 xmlXPtrEvalXPtrPart(ctxt, name);
1158 /* in case of syntax error, break here */
1159 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1160 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
1164 * If the returned value is a non-empty nodeset
1165 * or location set, return here.
1167 if (ctxt->value != NULL) {
1168 xmlXPathObjectPtr obj = ctxt->value;
1170 switch (obj->type) {
1171 case XPATH_LOCATIONSET: {
1172 xmlLocationSetPtr loc = ctxt->value->user;
1173 if ((loc != NULL) && (loc->locNr > 0))
1177 case XPATH_NODESET: {
1178 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1179 if ((loc != NULL) && (loc->nodeNr > 0))
1188 * Evaluating to improper values is equivalent to
1189 * a sub-resource error, clean-up the stack
1192 obj = valuePop(ctxt);
1194 xmlXPathFreeObject(obj);
1196 } while (obj != NULL);
1200 * Is there another XPointer part.
1203 name = xmlXPathParseName(ctxt);
1208 * xmlXPtrEvalChildSeq:
1209 * @ctxt: the XPointer Parser context
1210 * @name: a possible ID name of the child sequence
1212 * ChildSeq ::= '/1' ('/' [0-9]*)*
1213 * | Name ('/' [0-9]*)+
1215 * Parse and evaluate a Child Sequence. This routine also handle the
1216 * case of a Bare Name used to get a document ID.
1219 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1221 * XPointer don't allow by syntax to address in mutirooted trees
1222 * this might prove useful in some cases, warn about it.
1224 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1225 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1226 "warning: ChildSeq not starting by /1\n", NULL);
1230 valuePush(ctxt, xmlXPathNewString(name));
1232 xmlXPathIdFunction(ctxt, 1);
1236 while (CUR == '/') {
1240 while ((CUR >= '0') && (CUR <= '9')) {
1241 child = child * 10 + (CUR - '0');
1244 xmlXPtrGetChildNo(ctxt, child);
1250 * xmlXPtrEvalXPointer:
1251 * @ctxt: the XPointer Parser context
1257 * Parse and evaluate an XPointer
1260 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1261 if (ctxt->valueTab == NULL) {
1262 /* Allocate the value stack */
1263 ctxt->valueTab = (xmlXPathObjectPtr *)
1264 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1265 if (ctxt->valueTab == NULL) {
1266 xmlXPtrErrMemory("allocating evaluation context");
1270 ctxt->valueMax = 10;
1276 xmlXPtrEvalChildSeq(ctxt, NULL);
1280 name = xmlXPathParseName(ctxt);
1282 XP_ERROR(XPATH_EXPR_ERROR);
1284 xmlXPtrEvalFullXPtr(ctxt, name);
1285 /* Short evaluation */
1288 /* this handle both Bare Names and Child Sequences */
1289 xmlXPtrEvalChildSeq(ctxt, name);
1294 XP_ERROR(XPATH_EXPR_ERROR);
1298 /************************************************************************
1300 * General routines *
1302 ************************************************************************/
1305 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1307 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1309 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1311 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1313 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1315 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1317 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1320 * xmlXPtrNewContext:
1321 * @doc: the XML document
1322 * @here: the node that directly contains the XPointer being evaluated or NULL
1323 * @origin: the element from which a user or program initiated traversal of
1324 * the link, or NULL.
1326 * Create a new XPointer context
1328 * Returns the xmlXPathContext just allocated.
1331 xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1332 xmlXPathContextPtr ret;
1334 ret = xmlXPathNewContext(doc);
1339 ret->origin = origin;
1341 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1342 xmlXPtrRangeToFunction);
1343 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1344 xmlXPtrRangeFunction);
1345 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1346 xmlXPtrRangeInsideFunction);
1347 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1348 xmlXPtrStringRangeFunction);
1349 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1350 xmlXPtrStartPointFunction);
1351 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1352 xmlXPtrEndPointFunction);
1353 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1354 xmlXPtrHereFunction);
1355 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1356 xmlXPtrOriginFunction);
1363 * @str: the XPointer expression
1364 * @ctx: the XPointer context
1366 * Evaluate the XPath Location Path in the given context.
1368 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1369 * the caller has to free the object.
1372 xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1373 xmlXPathParserContextPtr ctxt;
1374 xmlXPathObjectPtr res = NULL, tmp;
1375 xmlXPathObjectPtr init = NULL;
1380 if ((ctx == NULL) || (str == NULL))
1383 ctxt = xmlXPathNewParserContext(str, ctx);
1385 xmlXPtrEvalXPointer(ctxt);
1387 if ((ctxt->value != NULL) &&
1388 (ctxt->value->type != XPATH_NODESET) &&
1389 (ctxt->value->type != XPATH_LOCATIONSET)) {
1390 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1391 "xmlXPtrEval: evaluation failed to return a node set\n",
1394 res = valuePop(ctxt);
1398 tmp = valuePop(ctxt);
1401 if (tmp->type == XPATH_NODESET) {
1403 * Evaluation may push a root nodeset which is unused
1406 set = tmp->nodesetval;
1407 if ((set->nodeNr != 1) ||
1408 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1413 xmlXPathFreeObject(tmp);
1415 } while (tmp != NULL);
1417 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1418 "xmlXPtrEval: object(s) left on the eval stack\n",
1421 if (ctxt->error != XPATH_EXPRESSION_OK) {
1422 xmlXPathFreeObject(res);
1426 xmlXPathFreeParserContext(ctxt);
1431 * xmlXPtrBuildRangeNodeList:
1432 * @range: a range object
1434 * Build a node list tree copy of the range
1436 * Returns an xmlNodePtr list or NULL.
1437 * the caller has to free the node tree.
1440 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1441 /* pointers to generated nodes */
1442 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1443 /* pointers to traversal nodes */
1444 xmlNodePtr start, cur, end;
1449 if (range->type != XPATH_RANGE)
1451 start = (xmlNodePtr) range->user;
1457 return(xmlCopyNode(start, 1));
1460 index1 = range->index;
1461 index2 = range->index2;
1462 while (cur != NULL) {
1464 if (cur->type == XML_TEXT_NODE) {
1465 const xmlChar *content = cur->content;
1468 if (content == NULL) {
1469 tmp = xmlNewTextLen(NULL, 0);
1472 if ((cur == start) && (index1 > 1)) {
1473 content += (index1 - 1);
1474 len -= (index1 - 1);
1479 tmp = xmlNewTextLen(content, len);
1481 /* single sub text node selection */
1484 /* prune and return full set */
1486 xmlAddNextSibling(last, tmp);
1488 xmlAddChild(parent, tmp);
1491 tmp = xmlCopyNode(cur, 0);
1496 xmlAddNextSibling(last, tmp);
1498 xmlAddChild(parent, tmp);
1504 end = xmlXPtrGetNthChild(cur, index2 - 1);
1507 if ((cur == start) && (index1 > 1)) {
1508 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1511 cur = cur->children;
1514 * Now gather the remaining nodes from cur to end
1516 continue; /* while */
1518 } else if ((cur == start) &&
1519 (list == NULL) /* looks superfluous but ... */ ) {
1520 if ((cur->type == XML_TEXT_NODE) ||
1521 (cur->type == XML_CDATA_SECTION_NODE)) {
1522 const xmlChar *content = cur->content;
1524 if (content == NULL) {
1525 tmp = xmlNewTextLen(NULL, 0);
1528 content += (index1 - 1);
1530 tmp = xmlNewText(content);
1534 if ((cur == start) && (index1 > 1)) {
1535 tmp = xmlCopyNode(cur, 0);
1539 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1542 * Now gather the remaining nodes from cur to end
1544 continue; /* while */
1546 tmp = xmlCopyNode(cur, 1);
1553 switch (cur->type) {
1555 case XML_ELEMENT_DECL:
1556 case XML_ATTRIBUTE_DECL:
1557 case XML_ENTITY_NODE:
1558 /* Do not copy DTD informations */
1560 case XML_ENTITY_DECL:
1561 TODO /* handle crossing entities -> stack needed */
1563 case XML_XINCLUDE_START:
1564 case XML_XINCLUDE_END:
1565 /* don't consider it part of the tree content */
1567 case XML_ATTRIBUTE_NODE:
1568 /* Humm, should not happen ! */
1572 tmp = xmlCopyNode(cur, 1);
1576 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1581 xmlAddNextSibling(last, tmp);
1583 xmlAddChild(parent, tmp);
1589 * Skip to next node in document order
1591 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1595 cur = xmlXPtrAdvanceNode(cur, NULL);
1601 * xmlXPtrBuildNodeList:
1602 * @obj: the XPointer result from the evaluation.
1604 * Build a node list tree copy of the XPointer result.
1605 * This will drop Attributes and Namespace declarations.
1607 * Returns an xmlNodePtr list or NULL.
1608 * the caller has to free the node tree.
1611 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1612 xmlNodePtr list = NULL, last = NULL;
1617 switch (obj->type) {
1618 case XPATH_NODESET: {
1619 xmlNodeSetPtr set = obj->nodesetval;
1622 for (i = 0;i < set->nodeNr;i++) {
1623 if (set->nodeTab[i] == NULL)
1625 switch (set->nodeTab[i]->type) {
1627 case XML_CDATA_SECTION_NODE:
1628 case XML_ELEMENT_NODE:
1629 case XML_ENTITY_REF_NODE:
1630 case XML_ENTITY_NODE:
1632 case XML_COMMENT_NODE:
1633 case XML_DOCUMENT_NODE:
1634 case XML_HTML_DOCUMENT_NODE:
1635 #ifdef LIBXML_DOCB_ENABLED
1636 case XML_DOCB_DOCUMENT_NODE:
1638 case XML_XINCLUDE_START:
1639 case XML_XINCLUDE_END:
1641 case XML_ATTRIBUTE_NODE:
1642 case XML_NAMESPACE_DECL:
1643 case XML_DOCUMENT_TYPE_NODE:
1644 case XML_DOCUMENT_FRAG_NODE:
1645 case XML_NOTATION_NODE:
1647 case XML_ELEMENT_DECL:
1648 case XML_ATTRIBUTE_DECL:
1649 case XML_ENTITY_DECL:
1653 list = last = xmlCopyNode(set->nodeTab[i], 1);
1655 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1656 if (last->next != NULL)
1662 case XPATH_LOCATIONSET: {
1663 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1666 for (i = 0;i < set->locNr;i++) {
1668 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1670 xmlAddNextSibling(last,
1671 xmlXPtrBuildNodeList(set->locTab[i]));
1673 while (last->next != NULL)
1680 return(xmlXPtrBuildRangeNodeList(obj));
1682 return(xmlCopyNode(obj->user, 0));
1689 /************************************************************************
1691 * XPointer functions *
1693 ************************************************************************/
1696 * xmlXPtrNbLocChildren:
1697 * @node: an xmlNodePtr
1699 * Count the number of location children of @node or the length of the
1700 * string value in case of text/PI/Comments nodes
1702 * Returns the number of location children
1705 xmlXPtrNbLocChildren(xmlNodePtr node) {
1709 switch (node->type) {
1710 case XML_HTML_DOCUMENT_NODE:
1711 case XML_DOCUMENT_NODE:
1712 case XML_ELEMENT_NODE:
1713 node = node->children;
1714 while (node != NULL) {
1715 if (node->type == XML_ELEMENT_NODE)
1720 case XML_ATTRIBUTE_NODE:
1724 case XML_COMMENT_NODE:
1726 case XML_CDATA_SECTION_NODE:
1727 case XML_ENTITY_REF_NODE:
1728 ret = xmlStrlen(node->content);
1737 * xmlXPtrHereFunction:
1738 * @ctxt: the XPointer Parser context
1739 * @nargs: the number of args
1741 * Function implementing here() operation
1742 * as described in 5.4.3
1745 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1748 if (ctxt->context->here == NULL)
1749 XP_ERROR(XPTR_SYNTAX_ERROR);
1751 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1755 * xmlXPtrOriginFunction:
1756 * @ctxt: the XPointer Parser context
1757 * @nargs: the number of args
1759 * Function implementing origin() operation
1760 * as described in 5.4.3
1763 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1766 if (ctxt->context->origin == NULL)
1767 XP_ERROR(XPTR_SYNTAX_ERROR);
1769 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1773 * xmlXPtrStartPointFunction:
1774 * @ctxt: the XPointer Parser context
1775 * @nargs: the number of args
1777 * Function implementing start-point() operation
1778 * as described in 5.4.3
1780 * location-set start-point(location-set)
1782 * For each location x in the argument location-set, start-point adds a
1783 * location of type point to the result location-set. That point represents
1784 * the start point of location x and is determined by the following rules:
1786 * - If x is of type point, the start point is x.
1787 * - If x is of type range, the start point is the start point of x.
1788 * - If x is of type root, element, text, comment, or processing instruction,
1789 * - the container node of the start point is x and the index is 0.
1790 * - If x is of type attribute or namespace, the function must signal a
1796 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1797 xmlXPathObjectPtr tmp, obj, point;
1798 xmlLocationSetPtr newset = NULL;
1799 xmlLocationSetPtr oldset = NULL;
1802 if ((ctxt->value == NULL) ||
1803 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1804 (ctxt->value->type != XPATH_NODESET)))
1805 XP_ERROR(XPATH_INVALID_TYPE)
1807 obj = valuePop(ctxt);
1808 if (obj->type == XPATH_NODESET) {
1810 * First convert to a location set
1812 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1813 xmlXPathFreeObject(obj);
1817 newset = xmlXPtrLocationSetCreate(NULL);
1818 if (newset == NULL) {
1819 xmlXPathFreeObject(obj);
1820 XP_ERROR(XPATH_MEMORY_ERROR);
1822 oldset = (xmlLocationSetPtr) obj->user;
1823 if (oldset != NULL) {
1826 for (i = 0; i < oldset->locNr; i++) {
1827 tmp = oldset->locTab[i];
1831 switch (tmp->type) {
1833 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1836 xmlNodePtr node = tmp->user;
1838 if (node->type == XML_ATTRIBUTE_NODE) {
1839 /* TODO: Namespace Nodes ??? */
1840 xmlXPathFreeObject(obj);
1841 xmlXPtrFreeLocationSet(newset);
1842 XP_ERROR(XPTR_SYNTAX_ERROR);
1844 point = xmlXPtrNewPoint(node, tmp->index);
1849 /*** Should we raise an error ?
1850 xmlXPathFreeObject(obj);
1851 xmlXPathFreeObject(newset);
1852 XP_ERROR(XPATH_INVALID_TYPE)
1857 xmlXPtrLocationSetAdd(newset, point);
1860 xmlXPathFreeObject(obj);
1861 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1865 * xmlXPtrEndPointFunction:
1866 * @ctxt: the XPointer Parser context
1867 * @nargs: the number of args
1869 * Function implementing end-point() operation
1870 * as described in 5.4.3
1871 * ----------------------------
1872 * location-set end-point(location-set)
1874 * For each location x in the argument location-set, end-point adds a
1875 * location of type point to the result location-set. That point represents
1876 * the end point of location x and is determined by the following rules:
1878 * - If x is of type point, the resulting point is x.
1879 * - If x is of type range, the resulting point is the end point of x.
1880 * - If x is of type root or element, the container node of the resulting
1881 * point is x and the index is the number of location children of x.
1882 * - If x is of type text, comment, or processing instruction, the container
1883 * node of the resulting point is x and the index is the length of the
1884 * string-value of x.
1885 * - If x is of type attribute or namespace, the function must signal a
1887 * ----------------------------
1890 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1891 xmlXPathObjectPtr tmp, obj, point;
1892 xmlLocationSetPtr newset = NULL;
1893 xmlLocationSetPtr oldset = NULL;
1896 if ((ctxt->value == NULL) ||
1897 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1898 (ctxt->value->type != XPATH_NODESET)))
1899 XP_ERROR(XPATH_INVALID_TYPE)
1901 obj = valuePop(ctxt);
1902 if (obj->type == XPATH_NODESET) {
1904 * First convert to a location set
1906 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1907 xmlXPathFreeObject(obj);
1911 newset = xmlXPtrLocationSetCreate(NULL);
1912 oldset = (xmlLocationSetPtr) obj->user;
1913 if (oldset != NULL) {
1916 for (i = 0; i < oldset->locNr; i++) {
1917 tmp = oldset->locTab[i];
1921 switch (tmp->type) {
1923 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1926 xmlNodePtr node = tmp->user2;
1928 if (node->type == XML_ATTRIBUTE_NODE) {
1929 /* TODO: Namespace Nodes ??? */
1930 xmlXPathFreeObject(obj);
1931 xmlXPtrFreeLocationSet(newset);
1932 XP_ERROR(XPTR_SYNTAX_ERROR);
1934 point = xmlXPtrNewPoint(node, tmp->index2);
1935 } else if (tmp->user == NULL) {
1936 point = xmlXPtrNewPoint(node,
1937 xmlXPtrNbLocChildren(node));
1942 /*** Should we raise an error ?
1943 xmlXPathFreeObject(obj);
1944 xmlXPathFreeObject(newset);
1945 XP_ERROR(XPATH_INVALID_TYPE)
1950 xmlXPtrLocationSetAdd(newset, point);
1953 xmlXPathFreeObject(obj);
1954 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1959 * xmlXPtrCoveringRange:
1960 * @ctxt: the XPointer Parser context
1961 * @loc: the location for which the covering range must be computed
1963 * A covering range is a range that wholly encompasses a location
1964 * Section 5.3.3. Covering Ranges for All Location Types
1965 * http://www.w3.org/TR/xptr#N2267
1967 * Returns a new location or NULL in case of error
1969 static xmlXPathObjectPtr
1970 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1973 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1974 (ctxt->context->doc == NULL))
1976 switch (loc->type) {
1978 return(xmlXPtrNewRange(loc->user, loc->index,
1979 loc->user, loc->index));
1981 if (loc->user2 != NULL) {
1982 return(xmlXPtrNewRange(loc->user, loc->index,
1983 loc->user2, loc->index2));
1985 xmlNodePtr node = (xmlNodePtr) loc->user;
1986 if (node == (xmlNodePtr) ctxt->context->doc) {
1987 return(xmlXPtrNewRange(node, 0, node,
1988 xmlXPtrGetArity(node)));
1990 switch (node->type) {
1991 case XML_ATTRIBUTE_NODE:
1992 /* !!! our model is slightly different than XPath */
1993 return(xmlXPtrNewRange(node, 0, node,
1994 xmlXPtrGetArity(node)));
1995 case XML_ELEMENT_NODE:
1997 case XML_CDATA_SECTION_NODE:
1998 case XML_ENTITY_REF_NODE:
2000 case XML_COMMENT_NODE:
2001 case XML_DOCUMENT_NODE:
2002 case XML_NOTATION_NODE:
2003 case XML_HTML_DOCUMENT_NODE: {
2004 int indx = xmlXPtrGetIndex(node);
2006 node = node->parent;
2007 return(xmlXPtrNewRange(node, indx - 1,
2016 TODO /* missed one case ??? */
2022 * xmlXPtrRangeFunction:
2023 * @ctxt: the XPointer Parser context
2024 * @nargs: the number of args
2026 * Function implementing the range() function 5.4.3
2027 * location-set range(location-set )
2029 * The range function returns ranges covering the locations in
2030 * the argument location-set. For each location x in the argument
2031 * location-set, a range location representing the covering range of
2032 * x is added to the result location-set.
2035 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2037 xmlXPathObjectPtr set;
2038 xmlLocationSetPtr oldset;
2039 xmlLocationSetPtr newset;
2042 if ((ctxt->value == NULL) ||
2043 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2044 (ctxt->value->type != XPATH_NODESET)))
2045 XP_ERROR(XPATH_INVALID_TYPE)
2047 set = valuePop(ctxt);
2048 if (set->type == XPATH_NODESET) {
2049 xmlXPathObjectPtr tmp;
2052 * First convert to a location set
2054 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2055 xmlXPathFreeObject(set);
2058 oldset = (xmlLocationSetPtr) set->user;
2061 * The loop is to compute the covering range for each item and add it
2063 newset = xmlXPtrLocationSetCreate(NULL);
2064 for (i = 0;i < oldset->locNr;i++) {
2065 xmlXPtrLocationSetAdd(newset,
2066 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2070 * Save the new value and cleanup
2072 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2073 xmlXPathFreeObject(set);
2077 * xmlXPtrInsideRange:
2078 * @ctxt: the XPointer Parser context
2079 * @loc: the location for which the inside range must be computed
2081 * A inside range is a range described in the range-inside() description
2083 * Returns a new location or NULL in case of error
2085 static xmlXPathObjectPtr
2086 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2089 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2090 (ctxt->context->doc == NULL))
2092 switch (loc->type) {
2094 xmlNodePtr node = (xmlNodePtr) loc->user;
2095 switch (node->type) {
2097 case XML_COMMENT_NODE:
2099 case XML_CDATA_SECTION_NODE: {
2100 if (node->content == NULL) {
2101 return(xmlXPtrNewRange(node, 0, node, 0));
2103 return(xmlXPtrNewRange(node, 0, node,
2104 xmlStrlen(node->content)));
2107 case XML_ATTRIBUTE_NODE:
2108 case XML_ELEMENT_NODE:
2109 case XML_ENTITY_REF_NODE:
2110 case XML_DOCUMENT_NODE:
2111 case XML_NOTATION_NODE:
2112 case XML_HTML_DOCUMENT_NODE: {
2113 return(xmlXPtrNewRange(node, 0, node,
2114 xmlXPtrGetArity(node)));
2122 xmlNodePtr node = (xmlNodePtr) loc->user;
2123 if (loc->user2 != NULL) {
2124 return(xmlXPtrNewRange(node, loc->index,
2125 loc->user2, loc->index2));
2127 switch (node->type) {
2129 case XML_COMMENT_NODE:
2131 case XML_CDATA_SECTION_NODE: {
2132 if (node->content == NULL) {
2133 return(xmlXPtrNewRange(node, 0, node, 0));
2135 return(xmlXPtrNewRange(node, 0, node,
2136 xmlStrlen(node->content)));
2139 case XML_ATTRIBUTE_NODE:
2140 case XML_ELEMENT_NODE:
2141 case XML_ENTITY_REF_NODE:
2142 case XML_DOCUMENT_NODE:
2143 case XML_NOTATION_NODE:
2144 case XML_HTML_DOCUMENT_NODE: {
2145 return(xmlXPtrNewRange(node, 0, node,
2146 xmlXPtrGetArity(node)));
2155 TODO /* missed one case ??? */
2161 * xmlXPtrRangeInsideFunction:
2162 * @ctxt: the XPointer Parser context
2163 * @nargs: the number of args
2165 * Function implementing the range-inside() function 5.4.3
2166 * location-set range-inside(location-set )
2168 * The range-inside function returns ranges covering the contents of
2169 * the locations in the argument location-set. For each location x in
2170 * the argument location-set, a range location is added to the result
2171 * location-set. If x is a range location, then x is added to the
2172 * result location-set. If x is not a range location, then x is used
2173 * as the container location of the start and end points of the range
2174 * location to be added; the index of the start point of the range is
2175 * zero; if the end point is a character point then its index is the
2176 * length of the string-value of x, and otherwise is the number of
2177 * location children of x.
2181 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2183 xmlXPathObjectPtr set;
2184 xmlLocationSetPtr oldset;
2185 xmlLocationSetPtr newset;
2188 if ((ctxt->value == NULL) ||
2189 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2190 (ctxt->value->type != XPATH_NODESET)))
2191 XP_ERROR(XPATH_INVALID_TYPE)
2193 set = valuePop(ctxt);
2194 if (set->type == XPATH_NODESET) {
2195 xmlXPathObjectPtr tmp;
2198 * First convert to a location set
2200 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2201 xmlXPathFreeObject(set);
2204 oldset = (xmlLocationSetPtr) set->user;
2207 * The loop is to compute the covering range for each item and add it
2209 newset = xmlXPtrLocationSetCreate(NULL);
2210 for (i = 0;i < oldset->locNr;i++) {
2211 xmlXPtrLocationSetAdd(newset,
2212 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2216 * Save the new value and cleanup
2218 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2219 xmlXPathFreeObject(set);
2223 * xmlXPtrRangeToFunction:
2224 * @ctxt: the XPointer Parser context
2225 * @nargs: the number of args
2227 * Implement the range-to() XPointer function
2230 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2231 xmlXPathObjectPtr range;
2233 xmlXPathObjectPtr res, obj;
2234 xmlXPathObjectPtr tmp;
2235 xmlLocationSetPtr newset = NULL;
2236 xmlNodeSetPtr oldset;
2239 if (ctxt == NULL) return;
2242 * Save the expression pointer since we will have to evaluate
2243 * it multiple times. Initialize the new set.
2245 CHECK_TYPE(XPATH_NODESET);
2246 obj = valuePop(ctxt);
2247 oldset = obj->nodesetval;
2248 ctxt->context->node = NULL;
2251 newset = xmlXPtrLocationSetCreate(NULL);
2253 for (i = 0; i < oldset->nodeNr; i++) {
2257 * Run the evaluation with a node list made of a single item
2260 ctxt->context->node = oldset->nodeTab[i];
2261 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2262 valuePush(ctxt, tmp);
2264 xmlXPathEvalExpr(ctxt);
2268 * The result of the evaluation need to be tested to
2269 * decided whether the filter succeeded or not
2271 res = valuePop(ctxt);
2272 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2273 if (range != NULL) {
2274 xmlXPtrLocationSetAdd(newset, range);
2281 xmlXPathFreeObject(res);
2282 if (ctxt->value == tmp) {
2283 res = valuePop(ctxt);
2284 xmlXPathFreeObject(res);
2287 ctxt->context->node = NULL;
2291 * The result is used as the new evaluation set.
2293 xmlXPathFreeObject(obj);
2294 ctxt->context->node = NULL;
2295 ctxt->context->contextSize = -1;
2296 ctxt->context->proximityPosition = -1;
2297 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2301 * xmlXPtrAdvanceNode:
2303 * @level: incremented/decremented to show level in tree
2305 * Advance to the next element or text node in document order
2306 * TODO: add a stack for entering/exiting entities
2308 * Returns -1 in case of failure, 0 otherwise
2311 xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
2315 if (cur->children != NULL) {
2316 cur = cur->children ;
2321 skip: /* This label should only be needed if something is wrong! */
2322 if (cur->next != NULL) {
2330 if (cur == NULL) return(NULL);
2331 if (cur->next != NULL) {
2335 } while (cur != NULL);
2338 if ((cur->type != XML_ELEMENT_NODE) &&
2339 (cur->type != XML_TEXT_NODE) &&
2340 (cur->type != XML_DOCUMENT_NODE) &&
2341 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2342 (cur->type != XML_CDATA_SECTION_NODE)) {
2343 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2353 * xmlXPtrAdvanceChar:
2356 * @bytes: the number of bytes
2358 * Advance a point of the associated number of bytes (not UTF8 chars)
2360 * Returns -1 in case of failure, 0 otherwise
2363 xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
2368 if ((node == NULL) || (indx == NULL))
2375 while (bytes >= 0) {
2377 * First position to the beginning of the first text node
2378 * corresponding to this point
2380 while ((cur != NULL) &&
2381 ((cur->type == XML_ELEMENT_NODE) ||
2382 (cur->type == XML_DOCUMENT_NODE) ||
2383 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2385 cur = xmlXPtrGetNthChild(cur, pos);
2388 cur = xmlXPtrAdvanceNode(cur, NULL);
2400 * if there is no move needed return the current value.
2402 if (pos == 0) pos = 1;
2409 * We should have a text (or cdata) node ...
2412 if ((cur->type != XML_ELEMENT_NODE) &&
2413 (cur->content != NULL)) {
2414 len = xmlStrlen(cur->content);
2417 /* Strange, the indx in the text node is greater than it's len */
2421 if (pos + bytes >= len) {
2422 bytes -= (len - pos);
2423 cur = xmlXPtrAdvanceNode(cur, NULL);
2425 } else if (pos + bytes < len) {
2436 * xmlXPtrMatchString:
2437 * @string: the string to search
2438 * @start: the start textnode
2439 * @startindex: the start index
2440 * @end: the end textnode IN/OUT
2441 * @endindex: the end index IN/OUT
2443 * Check whether the document contains @string at the position
2444 * (@start, @startindex) and limited by the (@end, @endindex) point
2446 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2447 * (@start, @startindex) will indicate the position of the beginning
2448 * of the range and (@end, @endindex) will indicate the end
2452 xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2453 xmlNodePtr *end, int *endindex) {
2455 int pos; /* 0 based */
2456 int len; /* in bytes */
2457 int stringlen; /* in bytes */
2464 if ((end == NULL) || (endindex == NULL))
2469 pos = startindex - 1;
2470 stringlen = xmlStrlen(string);
2472 while (stringlen > 0) {
2473 if ((cur == *end) && (pos + stringlen > *endindex))
2476 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2477 len = xmlStrlen(cur->content);
2478 if (len >= pos + stringlen) {
2479 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2482 xmlGenericError(xmlGenericErrorContext,
2483 "found range %d bytes at index %d of ->",
2484 stringlen, pos + 1);
2485 xmlDebugDumpString(stdout, cur->content);
2486 xmlGenericError(xmlGenericErrorContext, "\n");
2489 *endindex = pos + stringlen;
2495 int sub = len - pos;
2496 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2499 xmlGenericError(xmlGenericErrorContext,
2500 "found subrange %d bytes at index %d of ->",
2502 xmlDebugDumpString(stdout, cur->content);
2503 xmlGenericError(xmlGenericErrorContext, "\n");
2505 string = &string[sub];
2512 cur = xmlXPtrAdvanceNode(cur, NULL);
2521 * xmlXPtrSearchString:
2522 * @string: the string to search
2523 * @start: the start textnode IN/OUT
2524 * @startindex: the start index IN/OUT
2525 * @end: the end textnode
2526 * @endindex: the end index
2528 * Search the next occurrence of @string within the document content
2529 * until the (@end, @endindex) point is reached
2531 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2532 * (@start, @startindex) will indicate the position of the beginning
2533 * of the range and (@end, @endindex) will indicate the end
2537 xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2538 xmlNodePtr *end, int *endindex) {
2541 int pos; /* 0 based */
2542 int len; /* in bytes */
2547 if ((start == NULL) || (startindex == NULL))
2549 if ((end == NULL) || (endindex == NULL))
2554 pos = *startindex - 1;
2557 while (cur != NULL) {
2558 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2559 len = xmlStrlen(cur->content);
2560 while (pos <= len) {
2562 str = xmlStrchr(&cur->content[pos], first);
2564 pos = (str - (xmlChar *)(cur->content));
2566 xmlGenericError(xmlGenericErrorContext,
2567 "found '%c' at index %d of ->",
2569 xmlDebugDumpString(stdout, cur->content);
2570 xmlGenericError(xmlGenericErrorContext, "\n");
2572 if (xmlXPtrMatchString(string, cur, pos + 1,
2575 *startindex = pos + 1;
2584 * An empty string is considered to match before each
2585 * character of the string-value and after the final
2589 xmlGenericError(xmlGenericErrorContext,
2590 "found '' at index %d of ->",
2592 xmlDebugDumpString(stdout, cur->content);
2593 xmlGenericError(xmlGenericErrorContext, "\n");
2596 *startindex = pos + 1;
2598 *endindex = pos + 1;
2603 if ((cur == *end) && (pos >= *endindex))
2605 cur = xmlXPtrAdvanceNode(cur, NULL);
2614 * xmlXPtrGetLastChar:
2618 * Computes the point coordinates of the last char of this point
2620 * Returns -1 in case of failure, 0 otherwise
2623 xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
2627 if ((node == NULL) || (indx == NULL))
2635 if ((cur->type == XML_ELEMENT_NODE) ||
2636 (cur->type == XML_DOCUMENT_NODE) ||
2637 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2639 cur = xmlXPtrGetNthChild(cur, pos);
2642 while (cur != NULL) {
2643 if (cur->last != NULL)
2645 else if ((cur->type != XML_ELEMENT_NODE) &&
2646 (cur->content != NULL)) {
2647 len = xmlStrlen(cur->content);
2661 * xmlXPtrGetStartPoint:
2663 * @node: the resulting node
2664 * @indx: the resulting index
2666 * read the object and return the start point coordinates.
2668 * Returns -1 in case of failure, 0 otherwise
2671 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2672 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2675 switch (obj->type) {
2678 if (obj->index <= 0)
2685 if (obj->index <= 0)
2697 * xmlXPtrGetEndPoint:
2699 * @node: the resulting node
2700 * @indx: the resulting indx
2702 * read the object and return the end point coordinates.
2704 * Returns -1 in case of failure, 0 otherwise
2707 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2708 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2711 switch (obj->type) {
2714 if (obj->index <= 0)
2721 if (obj->index <= 0)
2733 * xmlXPtrStringRangeFunction:
2734 * @ctxt: the XPointer Parser context
2735 * @nargs: the number of args
2737 * Function implementing the string-range() function
2738 * range as described in 5.4.2
2740 * ------------------------------
2741 * [Definition: For each location in the location-set argument,
2742 * string-range returns a set of string ranges, a set of substrings in a
2743 * string. Specifically, the string-value of the location is searched for
2744 * substrings that match the string argument, and the resulting location-set
2745 * will contain a range location for each non-overlapping match.]
2746 * An empty string is considered to match before each character of the
2747 * string-value and after the final character. Whitespace in a string
2748 * is matched literally, with no normalization except that provided by
2749 * XML for line ends. The third argument gives the position of the first
2750 * character to be in the resulting range, relative to the start of the
2751 * match. The default value is 1, which makes the range start immediately
2752 * before the first character of the matched string. The fourth argument
2753 * gives the number of characters in the range; the default is that the
2754 * range extends to the end of the matched string.
2756 * Element boundaries, as well as entire embedded nodes such as processing
2757 * instructions and comments, are ignored as defined in [XPath].
2759 * If the string in the second argument is not found in the string-value
2760 * of the location, or if a value in the third or fourth argument indicates
2761 * a string that is beyond the beginning or end of the document, the
2764 * The points of the range-locations in the returned location-set will
2765 * all be character points.
2766 * ------------------------------
2769 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2770 int i, startindex, endindex = 0, fendindex;
2771 xmlNodePtr start, end = 0, fend;
2772 xmlXPathObjectPtr set;
2773 xmlLocationSetPtr oldset;
2774 xmlLocationSetPtr newset;
2775 xmlXPathObjectPtr string;
2776 xmlXPathObjectPtr position = NULL;
2777 xmlXPathObjectPtr number = NULL;
2778 int found, pos = 0, num = 0;
2781 * Grab the arguments
2783 if ((nargs < 2) || (nargs > 4))
2784 XP_ERROR(XPATH_INVALID_ARITY);
2787 CHECK_TYPE(XPATH_NUMBER);
2788 number = valuePop(ctxt);
2790 num = (int) number->floatval;
2793 CHECK_TYPE(XPATH_NUMBER);
2794 position = valuePop(ctxt);
2795 if (position != NULL)
2796 pos = (int) position->floatval;
2798 CHECK_TYPE(XPATH_STRING);
2799 string = valuePop(ctxt);
2800 if ((ctxt->value == NULL) ||
2801 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2802 (ctxt->value->type != XPATH_NODESET)))
2803 XP_ERROR(XPATH_INVALID_TYPE)
2805 set = valuePop(ctxt);
2806 newset = xmlXPtrLocationSetCreate(NULL);
2807 if (set->nodesetval == NULL) {
2810 if (set->type == XPATH_NODESET) {
2811 xmlXPathObjectPtr tmp;
2814 * First convert to a location set
2816 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2817 xmlXPathFreeObject(set);
2820 oldset = (xmlLocationSetPtr) set->user;
2823 * The loop is to search for each element in the location set
2824 * the list of location set corresponding to that search
2826 for (i = 0;i < oldset->locNr;i++) {
2828 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2831 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2832 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2833 xmlXPtrAdvanceChar(&start, &startindex, 0);
2834 xmlXPtrGetLastChar(&end, &endindex);
2837 xmlGenericError(xmlGenericErrorContext,
2838 "from index %d of ->", startindex);
2839 xmlDebugDumpString(stdout, start->content);
2840 xmlGenericError(xmlGenericErrorContext, "\n");
2841 xmlGenericError(xmlGenericErrorContext,
2842 "to index %d of ->", endindex);
2843 xmlDebugDumpString(stdout, end->content);
2844 xmlGenericError(xmlGenericErrorContext, "\n");
2848 fendindex = endindex;
2849 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2852 if (position == NULL) {
2853 xmlXPtrLocationSetAdd(newset,
2854 xmlXPtrNewRange(start, startindex, fend, fendindex));
2855 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2857 if ((number != NULL) && (num > 0)) {
2861 rindx = startindex - 1;
2862 if (xmlXPtrAdvanceChar(&rend, &rindx,
2864 xmlXPtrLocationSetAdd(newset,
2865 xmlXPtrNewRange(start, startindex,
2868 } else if ((number != NULL) && (num <= 0)) {
2869 xmlXPtrLocationSetAdd(newset,
2870 xmlXPtrNewRange(start, startindex,
2871 start, startindex));
2873 xmlXPtrLocationSetAdd(newset,
2874 xmlXPtrNewRange(start, startindex,
2879 startindex = fendindex;
2880 if (string->stringval[0] == 0)
2883 } while (found == 1);
2887 * Save the new value and cleanup
2890 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2891 xmlXPathFreeObject(set);
2892 xmlXPathFreeObject(string);
2893 if (position) xmlXPathFreeObject(position);
2894 if (number) xmlXPathFreeObject(number);
2898 * xmlXPtrEvalRangePredicate:
2899 * @ctxt: the XPointer Parser context
2901 * [8] Predicate ::= '[' PredicateExpr ']'
2902 * [9] PredicateExpr ::= Expr
2904 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2905 * a Location Set instead of a node set
2908 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2910 xmlXPathObjectPtr res;
2911 xmlXPathObjectPtr obj, tmp;
2912 xmlLocationSetPtr newset = NULL;
2913 xmlLocationSetPtr oldset;
2916 if (ctxt == NULL) return;
2920 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2926 * Extract the old set, and then evaluate the result of the
2927 * expression for all the element in the set. use it to grow
2930 CHECK_TYPE(XPATH_LOCATIONSET);
2931 obj = valuePop(ctxt);
2933 ctxt->context->node = NULL;
2935 if ((oldset == NULL) || (oldset->locNr == 0)) {
2936 ctxt->context->contextSize = 0;
2937 ctxt->context->proximityPosition = 0;
2938 xmlXPathEvalExpr(ctxt);
2939 res = valuePop(ctxt);
2941 xmlXPathFreeObject(res);
2942 valuePush(ctxt, obj);
2946 * Save the expression pointer since we will have to evaluate
2947 * it multiple times. Initialize the new set.
2950 newset = xmlXPtrLocationSetCreate(NULL);
2952 for (i = 0; i < oldset->locNr; i++) {
2956 * Run the evaluation with a node list made of a single item
2959 ctxt->context->node = oldset->locTab[i]->user;
2960 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2961 valuePush(ctxt, tmp);
2962 ctxt->context->contextSize = oldset->locNr;
2963 ctxt->context->proximityPosition = i + 1;
2965 xmlXPathEvalExpr(ctxt);
2969 * The result of the evaluation need to be tested to
2970 * decided whether the filter succeeded or not
2972 res = valuePop(ctxt);
2973 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2974 xmlXPtrLocationSetAdd(newset,
2975 xmlXPathObjectCopy(oldset->locTab[i]));
2982 xmlXPathFreeObject(res);
2983 if (ctxt->value == tmp) {
2984 res = valuePop(ctxt);
2985 xmlXPathFreeObject(res);
2988 ctxt->context->node = NULL;
2992 * The result is used as the new evaluation set.
2994 xmlXPathFreeObject(obj);
2995 ctxt->context->node = NULL;
2996 ctxt->context->contextSize = -1;
2997 ctxt->context->proximityPosition = -1;
2998 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
3001 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
3008 #define bottom_xpointer
3009 #include "elfgcchack.h"