blob: 1413868ee8acc59b68ebc304272cc831c7dc51e6 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00004 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
Owen Taylor3473f882001-02-23 17:55:21 +00006 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00008 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
Daniel Veillard9a237c92003-02-13 15:52:58 +000011 *
Owen Taylor3473f882001-02-23 17:55:21 +000012 * See Copyright for the status of this software.
13 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000014 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Daniel Veillard34ce8be2002-03-18 19:37:11 +000017#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000020/*
Owen Taylor3473f882001-02-23 17:55:21 +000021 * 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.
26 */
27
Owen Taylor3473f882001-02-23 17:55:21 +000028#include <string.h>
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>
Owen Taylor3473f882001-02-23 17:55:21 +000035#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000037
38#ifdef LIBXML_XPTR_ENABLED
39
40/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41#define XPTR_XMLNS_SCHEME
42
43/* #define DEBUG_RANGES */
Daniel Veillard017b1082001-06-21 11:20:21 +000044#ifdef DEBUG_RANGES
45#ifdef LIBXML_DEBUG_ENABLED
46#include <libxml/debugXML.h>
47#endif
48#endif
Owen Taylor3473f882001-02-23 17:55:21 +000049
Daniel Veillardf8e3db02012-09-11 13:26:36 +080050#define TODO \
Owen Taylor3473f882001-02-23 17:55:21 +000051 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
53 __FILE__, __LINE__);
54
Daniel Veillardf8e3db02012-09-11 13:26:36 +080055#define STRANGE \
Owen Taylor3473f882001-02-23 17:55:21 +000056 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
58 __FILE__, __LINE__);
59
60/************************************************************************
61 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080062 * Some factorized error routines *
Daniel Veillardfcf719c2003-10-10 11:42:17 +000063 * *
64 ************************************************************************/
65
66/**
67 * xmlXPtrErrMemory:
68 * @extra: extra informations
69 *
70 * Handle a redefinition of attribute error
71 */
72static void
73xmlXPtrErrMemory(const char *extra)
74{
Daniel Veillard659e71e2003-10-10 14:10:40 +000075 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000076 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
77 NULL, NULL, 0, 0,
78 "Memory allocation failed : %s\n", extra);
79}
80
81/**
82 * xmlXPtrErr:
83 * @ctxt: an XPTR evaluation context
84 * @extra: extra informations
85 *
86 * Handle a redefinition of attribute error
87 */
David Kilzer4472c3a2016-05-13 15:13:17 +080088static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillardfcf719c2003-10-10 11:42:17 +000089xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
90 const char * msg, const xmlChar *extra)
91{
92 if (ctxt != NULL)
93 ctxt->error = error;
94 if ((ctxt == NULL) || (ctxt->context == NULL)) {
Daniel Veillard659e71e2003-10-10 14:10:40 +000095 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000096 NULL, NULL, XML_FROM_XPOINTER, error,
97 XML_ERR_ERROR, NULL, 0,
98 (const char *) extra, NULL, NULL, 0, 0,
99 msg, extra);
100 return;
101 }
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200102
103 /* cleanup current last error */
104 xmlResetError(&ctxt->context->lastError);
105
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000106 ctxt->context->lastError.domain = XML_FROM_XPOINTER;
107 ctxt->context->lastError.code = error;
108 ctxt->context->lastError.level = XML_ERR_ERROR;
109 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
110 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
111 ctxt->context->lastError.node = ctxt->context->debugNode;
112 if (ctxt->context->error != NULL) {
113 ctxt->context->error(ctxt->context->userData,
114 &ctxt->context->lastError);
115 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000116 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000117 NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
118 error, XML_ERR_ERROR, NULL, 0,
119 (const char *) extra, (const char *) ctxt->base, NULL,
120 ctxt->cur - ctxt->base, 0,
121 msg, extra);
122 }
123}
124
125/************************************************************************
126 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000127 * A few helper functions for child sequences *
128 * *
129 ************************************************************************/
William M. Brackf7eb7942003-12-31 07:59:17 +0000130/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
131xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
Owen Taylor3473f882001-02-23 17:55:21 +0000132/**
133 * xmlXPtrGetArity:
134 * @cur: the node
135 *
136 * Returns the number of child for an element, -1 in case of error
137 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000138static int
Owen Taylor3473f882001-02-23 17:55:21 +0000139xmlXPtrGetArity(xmlNodePtr cur) {
140 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800141 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000142 return(-1);
143 cur = cur->children;
144 for (i = 0;cur != NULL;cur = cur->next) {
145 if ((cur->type == XML_ELEMENT_NODE) ||
146 (cur->type == XML_DOCUMENT_NODE) ||
147 (cur->type == XML_HTML_DOCUMENT_NODE)) {
148 i++;
149 }
150 }
151 return(i);
152}
153
154/**
155 * xmlXPtrGetIndex:
156 * @cur: the node
157 *
158 * Returns the index of the node in its parent children list, -1
159 * in case of error
160 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000161static int
Owen Taylor3473f882001-02-23 17:55:21 +0000162xmlXPtrGetIndex(xmlNodePtr cur) {
163 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800164 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000165 return(-1);
166 for (i = 1;cur != NULL;cur = cur->prev) {
167 if ((cur->type == XML_ELEMENT_NODE) ||
168 (cur->type == XML_DOCUMENT_NODE) ||
169 (cur->type == XML_HTML_DOCUMENT_NODE)) {
170 i++;
171 }
172 }
173 return(i);
174}
175
176/**
177 * xmlXPtrGetNthChild:
178 * @cur: the node
179 * @no: the child number
180 *
181 * Returns the @no'th element child of @cur or NULL
182 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000183static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000184xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
185 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800186 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000187 return(cur);
188 cur = cur->children;
189 for (i = 0;i <= no;cur = cur->next) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800190 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000191 return(cur);
192 if ((cur->type == XML_ELEMENT_NODE) ||
193 (cur->type == XML_DOCUMENT_NODE) ||
194 (cur->type == XML_HTML_DOCUMENT_NODE)) {
195 i++;
196 if (i == no)
197 break;
198 }
199 }
200 return(cur);
201}
202
203/************************************************************************
204 * *
205 * Handling of XPointer specific types *
206 * *
207 ************************************************************************/
208
209/**
210 * xmlXPtrCmpPoints:
211 * @node1: the first node
212 * @index1: the first index
213 * @node2: the second node
214 * @index2: the second index
215 *
216 * Compare two points w.r.t document order
217 *
218 * Returns -2 in case of error 1 if first point < second point, 0 if
219 * that's the same point, -1 otherwise
220 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000221static int
Owen Taylor3473f882001-02-23 17:55:21 +0000222xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
223 if ((node1 == NULL) || (node2 == NULL))
224 return(-2);
225 /*
226 * a couple of optimizations which will avoid computations in most cases
227 */
228 if (node1 == node2) {
229 if (index1 < index2)
230 return(1);
231 if (index1 > index2)
232 return(-1);
233 return(0);
234 }
235 return(xmlXPathCmpNodes(node1, node2));
236}
237
238/**
239 * xmlXPtrNewPoint:
240 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000241 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000242 *
243 * Create a new xmlXPathObjectPtr of type point
244 *
245 * Returns the newly created object.
246 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000247static xmlXPathObjectPtr
248xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000249 xmlXPathObjectPtr ret;
250
251 if (node == NULL)
252 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000253 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000254 return(NULL);
255
256 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
257 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000258 xmlXPtrErrMemory("allocating point");
Owen Taylor3473f882001-02-23 17:55:21 +0000259 return(NULL);
260 }
261 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
262 ret->type = XPATH_POINT;
263 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000264 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000265 return(ret);
266}
267
268/**
269 * xmlXPtrRangeCheckOrder:
270 * @range: an object range
271 *
272 * Make sure the points in the range are in the right order
273 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000274static void
Owen Taylor3473f882001-02-23 17:55:21 +0000275xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
276 int tmp;
277 xmlNodePtr tmp2;
278 if (range == NULL)
279 return;
280 if (range->type != XPATH_RANGE)
281 return;
282 if (range->user2 == NULL)
283 return;
284 tmp = xmlXPtrCmpPoints(range->user, range->index,
285 range->user2, range->index2);
286 if (tmp == -1) {
287 tmp2 = range->user;
288 range->user = range->user2;
289 range->user2 = tmp2;
290 tmp = range->index;
291 range->index = range->index2;
292 range->index2 = tmp;
293 }
294}
295
296/**
297 * xmlXPtrRangesEqual:
298 * @range1: the first range
299 * @range2: the second range
300 *
301 * Compare two ranges
302 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000303 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000304 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000305static int
Owen Taylor3473f882001-02-23 17:55:21 +0000306xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
307 if (range1 == range2)
308 return(1);
309 if ((range1 == NULL) || (range2 == NULL))
310 return(0);
311 if (range1->type != range2->type)
312 return(0);
313 if (range1->type != XPATH_RANGE)
314 return(0);
315 if (range1->user != range2->user)
316 return(0);
317 if (range1->index != range2->index)
318 return(0);
319 if (range1->user2 != range2->user2)
320 return(0);
321 if (range1->index2 != range2->index2)
322 return(0);
323 return(1);
324}
325
326/**
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200327 * xmlXPtrNewRangeInternal:
328 * @start: the starting node
329 * @startindex: the start index
330 * @end: the ending point
331 * @endindex: the ending index
332 *
333 * Internal function to create a new xmlXPathObjectPtr of type range
334 *
335 * Returns the newly created object.
336 */
337static xmlXPathObjectPtr
338xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
339 xmlNodePtr end, int endindex) {
340 xmlXPathObjectPtr ret;
341
342 /*
343 * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
344 * Disallow them for now.
345 */
346 if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
347 return(NULL);
348 if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
349 return(NULL);
350
351 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
352 if (ret == NULL) {
353 xmlXPtrErrMemory("allocating range");
354 return(NULL);
355 }
356 memset(ret, 0, sizeof(xmlXPathObject));
357 ret->type = XPATH_RANGE;
358 ret->user = start;
359 ret->index = startindex;
360 ret->user2 = end;
361 ret->index2 = endindex;
362 return(ret);
363}
364
365/**
Owen Taylor3473f882001-02-23 17:55:21 +0000366 * xmlXPtrNewRange:
367 * @start: the starting node
368 * @startindex: the start index
369 * @end: the ending point
370 * @endindex: the ending index
371 *
372 * Create a new xmlXPathObjectPtr of type range
373 *
374 * Returns the newly created object.
375 */
376xmlXPathObjectPtr
377xmlXPtrNewRange(xmlNodePtr start, int startindex,
378 xmlNodePtr end, int endindex) {
379 xmlXPathObjectPtr ret;
380
381 if (start == NULL)
382 return(NULL);
383 if (end == NULL)
384 return(NULL);
385 if (startindex < 0)
386 return(NULL);
387 if (endindex < 0)
388 return(NULL);
389
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200390 ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
Owen Taylor3473f882001-02-23 17:55:21 +0000391 xmlXPtrRangeCheckOrder(ret);
392 return(ret);
393}
394
395/**
396 * xmlXPtrNewRangePoints:
397 * @start: the starting point
398 * @end: the ending point
399 *
400 * Create a new xmlXPathObjectPtr of type range using 2 Points
401 *
402 * Returns the newly created object.
403 */
404xmlXPathObjectPtr
405xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
406 xmlXPathObjectPtr ret;
407
408 if (start == NULL)
409 return(NULL);
410 if (end == NULL)
411 return(NULL);
412 if (start->type != XPATH_POINT)
413 return(NULL);
414 if (end->type != XPATH_POINT)
415 return(NULL);
416
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200417 ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
418 end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000419 xmlXPtrRangeCheckOrder(ret);
420 return(ret);
421}
422
423/**
424 * xmlXPtrNewRangePointNode:
425 * @start: the starting point
426 * @end: the ending node
427 *
428 * Create a new xmlXPathObjectPtr of type range from a point to a node
429 *
430 * Returns the newly created object.
431 */
432xmlXPathObjectPtr
433xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
434 xmlXPathObjectPtr ret;
435
436 if (start == NULL)
437 return(NULL);
438 if (end == NULL)
439 return(NULL);
440 if (start->type != XPATH_POINT)
441 return(NULL);
442
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200443 ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000444 xmlXPtrRangeCheckOrder(ret);
445 return(ret);
446}
447
448/**
449 * xmlXPtrNewRangeNodePoint:
450 * @start: the starting node
451 * @end: the ending point
452 *
453 * Create a new xmlXPathObjectPtr of type range from a node to a point
454 *
455 * Returns the newly created object.
456 */
457xmlXPathObjectPtr
458xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
459 xmlXPathObjectPtr ret;
460
461 if (start == NULL)
462 return(NULL);
463 if (end == NULL)
464 return(NULL);
465 if (start->type != XPATH_POINT)
466 return(NULL);
467 if (end->type != XPATH_POINT)
468 return(NULL);
469
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200470 ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000471 xmlXPtrRangeCheckOrder(ret);
472 return(ret);
473}
474
475/**
476 * xmlXPtrNewRangeNodes:
477 * @start: the starting node
478 * @end: the ending node
479 *
480 * Create a new xmlXPathObjectPtr of type range using 2 nodes
481 *
482 * Returns the newly created object.
483 */
484xmlXPathObjectPtr
485xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
486 xmlXPathObjectPtr ret;
487
488 if (start == NULL)
489 return(NULL);
490 if (end == NULL)
491 return(NULL);
492
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200493 ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000494 xmlXPtrRangeCheckOrder(ret);
495 return(ret);
496}
497
498/**
499 * xmlXPtrNewCollapsedRange:
500 * @start: the starting and ending node
501 *
502 * Create a new xmlXPathObjectPtr of type range using a single nodes
503 *
504 * Returns the newly created object.
505 */
506xmlXPathObjectPtr
507xmlXPtrNewCollapsedRange(xmlNodePtr start) {
508 xmlXPathObjectPtr ret;
509
510 if (start == NULL)
511 return(NULL);
512
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200513 ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000514 return(ret);
515}
516
517/**
518 * xmlXPtrNewRangeNodeObject:
519 * @start: the starting node
520 * @end: the ending object
521 *
522 * Create a new xmlXPathObjectPtr of type range from a not to an object
523 *
524 * Returns the newly created object.
525 */
526xmlXPathObjectPtr
527xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200528 xmlNodePtr endNode;
529 int endIndex;
Owen Taylor3473f882001-02-23 17:55:21 +0000530 xmlXPathObjectPtr ret;
531
532 if (start == NULL)
533 return(NULL);
534 if (end == NULL)
535 return(NULL);
536 switch (end->type) {
537 case XPATH_POINT:
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200538 endNode = end->user;
539 endIndex = end->index;
540 break;
William M. Brack72ee48d2003-12-30 08:30:19 +0000541 case XPATH_RANGE:
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200542 endNode = end->user2;
543 endIndex = end->index2;
Owen Taylor3473f882001-02-23 17:55:21 +0000544 break;
545 case XPATH_NODESET:
546 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800547 * Empty set ...
Owen Taylor3473f882001-02-23 17:55:21 +0000548 */
Nick Wellnhofere905f082016-06-26 12:38:28 +0200549 if ((end->nodesetval == NULL) || (end->nodesetval->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +0000550 return(NULL);
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200551 endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
552 endIndex = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000553 break;
554 default:
Daniel Veillard3d97e662004-11-04 10:49:00 +0000555 /* TODO */
Owen Taylor3473f882001-02-23 17:55:21 +0000556 return(NULL);
557 }
558
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200559 ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
Owen Taylor3473f882001-02-23 17:55:21 +0000560 xmlXPtrRangeCheckOrder(ret);
561 return(ret);
562}
563
564#define XML_RANGESET_DEFAULT 10
565
566/**
567 * xmlXPtrLocationSetCreate:
568 * @val: an initial xmlXPathObjectPtr, or NULL
569 *
570 * Create a new xmlLocationSetPtr of type double and of value @val
571 *
572 * Returns the newly created object.
573 */
574xmlLocationSetPtr
575xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
576 xmlLocationSetPtr ret;
577
578 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
579 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000580 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000581 return(NULL);
582 }
583 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
584 if (val != NULL) {
585 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
586 sizeof(xmlXPathObjectPtr));
587 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000588 xmlXPtrErrMemory("allocating locationset");
589 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000590 return(NULL);
591 }
592 memset(ret->locTab, 0 ,
593 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
594 ret->locMax = XML_RANGESET_DEFAULT;
595 ret->locTab[ret->locNr++] = val;
596 }
597 return(ret);
598}
599
600/**
601 * xmlXPtrLocationSetAdd:
602 * @cur: the initial range set
603 * @val: a new xmlXPathObjectPtr
604 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000605 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000606 * If the location already exist in the set @val is freed.
607 */
608void
609xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
610 int i;
611
Daniel Veillardce682bc2004-11-05 17:22:25 +0000612 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000613
614 /*
615 * check against doublons
616 */
617 for (i = 0;i < cur->locNr;i++) {
618 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
619 xmlXPathFreeObject(val);
620 return;
621 }
622 }
623
624 /*
625 * grow the locTab if needed
626 */
627 if (cur->locMax == 0) {
628 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
629 sizeof(xmlXPathObjectPtr));
630 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000631 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000632 return;
633 }
634 memset(cur->locTab, 0 ,
635 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
636 cur->locMax = XML_RANGESET_DEFAULT;
637 } else if (cur->locNr == cur->locMax) {
638 xmlXPathObjectPtr *temp;
639
640 cur->locMax *= 2;
641 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
642 sizeof(xmlXPathObjectPtr));
643 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000644 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000645 return;
646 }
647 cur->locTab = temp;
648 }
649 cur->locTab[cur->locNr++] = val;
650}
651
652/**
653 * xmlXPtrLocationSetMerge:
654 * @val1: the first LocationSet
655 * @val2: the second LocationSet
656 *
657 * Merges two rangesets, all ranges from @val2 are added to @val1
658 *
659 * Returns val1 once extended or NULL in case of error.
660 */
661xmlLocationSetPtr
662xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
663 int i;
664
665 if (val1 == NULL) return(NULL);
666 if (val2 == NULL) return(val1);
667
668 /*
669 * !!!!! this can be optimized a lot, knowing that both
670 * val1 and val2 already have unicity of their values.
671 */
672
673 for (i = 0;i < val2->locNr;i++)
674 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
675
676 return(val1);
677}
678
679/**
680 * xmlXPtrLocationSetDel:
681 * @cur: the initial range set
682 * @val: an xmlXPathObjectPtr
683 *
684 * Removes an xmlXPathObjectPtr from an existing LocationSet
685 */
686void
687xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
688 int i;
689
690 if (cur == NULL) return;
691 if (val == NULL) return;
692
693 /*
694 * check against doublons
695 */
696 for (i = 0;i < cur->locNr;i++)
697 if (cur->locTab[i] == val) break;
698
699 if (i >= cur->locNr) {
700#ifdef DEBUG
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800701 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000702 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000703#endif
704 return;
705 }
706 cur->locNr--;
707 for (;i < cur->locNr;i++)
708 cur->locTab[i] = cur->locTab[i + 1];
709 cur->locTab[cur->locNr] = NULL;
710}
711
712/**
713 * xmlXPtrLocationSetRemove:
714 * @cur: the initial range set
715 * @val: the index to remove
716 *
717 * Removes an entry from an existing LocationSet list.
718 */
719void
720xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
721 if (cur == NULL) return;
722 if (val >= cur->locNr) return;
723 cur->locNr--;
724 for (;val < cur->locNr;val++)
725 cur->locTab[val] = cur->locTab[val + 1];
726 cur->locTab[cur->locNr] = NULL;
727}
728
729/**
730 * xmlXPtrFreeLocationSet:
731 * @obj: the xmlLocationSetPtr to free
732 *
733 * Free the LocationSet compound (not the actual ranges !).
734 */
735void
736xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
737 int i;
738
739 if (obj == NULL) return;
740 if (obj->locTab != NULL) {
741 for (i = 0;i < obj->locNr; i++) {
742 xmlXPathFreeObject(obj->locTab[i]);
743 }
Owen Taylor3473f882001-02-23 17:55:21 +0000744 xmlFree(obj->locTab);
745 }
Owen Taylor3473f882001-02-23 17:55:21 +0000746 xmlFree(obj);
747}
748
749/**
750 * xmlXPtrNewLocationSetNodes:
751 * @start: the start NodePtr value
752 * @end: the end NodePtr value or NULL
753 *
754 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
755 * it with the single range made of the two nodes @start and @end
756 *
757 * Returns the newly created object.
758 */
759xmlXPathObjectPtr
760xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
761 xmlXPathObjectPtr ret;
762
763 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
764 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000765 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000766 return(NULL);
767 }
768 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
769 ret->type = XPATH_LOCATIONSET;
770 if (end == NULL)
771 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
772 else
773 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
774 return(ret);
775}
776
777/**
778 * xmlXPtrNewLocationSetNodeSet:
779 * @set: a node set
780 *
781 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
782 * it with all the nodes from @set
783 *
784 * Returns the newly created object.
785 */
786xmlXPathObjectPtr
787xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
788 xmlXPathObjectPtr ret;
789
790 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
791 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000792 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000793 return(NULL);
794 }
795 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
796 ret->type = XPATH_LOCATIONSET;
797 if (set != NULL) {
798 int i;
799 xmlLocationSetPtr newset;
800
801 newset = xmlXPtrLocationSetCreate(NULL);
802 if (newset == NULL)
803 return(ret);
804
805 for (i = 0;i < set->nodeNr;i++)
806 xmlXPtrLocationSetAdd(newset,
807 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
808
809 ret->user = (void *) newset;
810 }
811 return(ret);
812}
813
814/**
815 * xmlXPtrWrapLocationSet:
816 * @val: the LocationSet value
817 *
818 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
819 *
820 * Returns the newly created object.
821 */
822xmlXPathObjectPtr
823xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
824 xmlXPathObjectPtr ret;
825
826 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
827 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000828 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000829 return(NULL);
830 }
831 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
832 ret->type = XPATH_LOCATIONSET;
833 ret->user = (void *) val;
834 return(ret);
835}
836
837/************************************************************************
838 * *
839 * The parser *
840 * *
841 ************************************************************************/
842
Daniel Veillard9a237c92003-02-13 15:52:58 +0000843static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
844
Owen Taylor3473f882001-02-23 17:55:21 +0000845/*
846 * Macros for accessing the content. Those should be used only by the parser,
847 * and not exported.
848 *
849 * Dirty macros, i.e. one need to make assumption on the context to use them
850 *
851 * CUR_PTR return the current pointer to the xmlChar to be parsed.
852 * CUR returns the current xmlChar value, i.e. a 8 bit value
853 * in ISO-Latin or UTF-8.
854 * This should be used internally by the parser
855 * only to compare to ASCII values otherwise it would break when
856 * running with UTF-8 encoding.
857 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
858 * to compare on ASCII based substring.
859 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
860 * strings within the parser.
861 * CURRENT Returns the current char value, with the full decoding of
862 * UTF-8 if we are using this mode. It returns an int.
863 * NEXT Skip to the next character, this does the proper decoding
864 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
865 * It returns the pointer to the current xmlChar.
866 */
867
868#define CUR (*ctxt->cur)
869#define SKIP(val) ctxt->cur += (val)
870#define NXT(val) ctxt->cur[(val)]
871#define CUR_PTR ctxt->cur
872
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800873#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000874 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000875
876#define CURRENT (*ctxt->cur)
877#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
878
879/*
880 * xmlXPtrGetChildNo:
881 * @ctxt: the XPointer Parser context
882 * @index: the child number
883 *
884 * Move the current node of the nodeset on the stack to the
885 * given child if found
886 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000887static void
888xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000889 xmlNodePtr cur = NULL;
890 xmlXPathObjectPtr obj;
891 xmlNodeSetPtr oldset;
892
893 CHECK_TYPE(XPATH_NODESET);
894 obj = valuePop(ctxt);
895 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000896 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000897 xmlXPathFreeObject(obj);
898 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
899 return;
900 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000901 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000902 if (cur == NULL) {
903 xmlXPathFreeObject(obj);
904 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
905 return;
906 }
907 oldset->nodeTab[0] = cur;
908 valuePush(ctxt, obj);
909}
910
911/**
912 * xmlXPtrEvalXPtrPart:
913 * @ctxt: the XPointer Parser context
914 * @name: the preparsed Scheme for the XPtrPart
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800915 *
Owen Taylor3473f882001-02-23 17:55:21 +0000916 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
917 * | Scheme '(' SchemeSpecificExpr ')'
918 *
919 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
920 *
921 * SchemeSpecificExpr ::= StringWithBalancedParens
922 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800923 * StringWithBalancedParens ::=
Owen Taylor3473f882001-02-23 17:55:21 +0000924 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
925 * [VC: Parenthesis escaping]
926 *
927 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
928 *
929 * VC: Parenthesis escaping:
930 * The end of an XPointer part is signaled by the right parenthesis ")"
931 * character that is balanced with the left parenthesis "(" character
932 * that began the part. Any unbalanced parenthesis character inside the
933 * expression, even within literals, must be escaped with a circumflex (^)
934 * character preceding it. If the expression contains any literal
935 * occurrences of the circumflex, each must be escaped with an additional
936 * circumflex (that is, ^^). If the unescaped parentheses in the expression
937 * are not balanced, a syntax error results.
938 *
939 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000940 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800941 *
Owen Taylor3473f882001-02-23 17:55:21 +0000942 * TODO: there is no new scheme registration mechanism
943 */
944
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000945static void
Owen Taylor3473f882001-02-23 17:55:21 +0000946xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
947 xmlChar *buffer, *cur;
948 int len;
949 int level;
950
951 if (name == NULL)
952 name = xmlXPathParseName(ctxt);
953 if (name == NULL)
954 XP_ERROR(XPATH_EXPR_ERROR);
955
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200956 if (CUR != '(') {
957 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000958 XP_ERROR(XPATH_EXPR_ERROR);
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200959 }
Owen Taylor3473f882001-02-23 17:55:21 +0000960 NEXT;
961 level = 1;
962
963 len = xmlStrlen(ctxt->cur);
964 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000965 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000966 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000967 xmlXPtrErrMemory("allocating buffer");
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200968 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000969 return;
970 }
971
972 cur = buffer;
973 while (CUR != 0) {
974 if (CUR == ')') {
975 level--;
976 if (level == 0) {
977 NEXT;
978 break;
979 }
Owen Taylor3473f882001-02-23 17:55:21 +0000980 } else if (CUR == '(') {
981 level++;
Owen Taylor3473f882001-02-23 17:55:21 +0000982 } else if (CUR == '^') {
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800983 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
984 NEXT;
985 }
Owen Taylor3473f882001-02-23 17:55:21 +0000986 }
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800987 *cur++ = CUR;
Owen Taylor3473f882001-02-23 17:55:21 +0000988 NEXT;
989 }
990 *cur = 0;
991
992 if ((level != 0) && (CUR == 0)) {
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200993 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000994 xmlFree(buffer);
995 XP_ERROR(XPTR_SYNTAX_ERROR);
996 }
997
998 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
999 const xmlChar *left = CUR_PTR;
1000
1001 CUR_PTR = buffer;
William M. Brack72ee48d2003-12-30 08:30:19 +00001002 /*
1003 * To evaluate an xpointer scheme element (4.3) we need:
1004 * context initialized to the root
1005 * context position initalized to 1
1006 * context size initialized to 1
1007 */
1008 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1009 ctxt->context->proximityPosition = 1;
1010 ctxt->context->contextSize = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001011 xmlXPathEvalExpr(ctxt);
1012 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001013 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1014 const xmlChar *left = CUR_PTR;
1015 xmlChar *name2;
1016
1017 CUR_PTR = buffer;
1018 if (buffer[0] == '/') {
1019 xmlXPathRoot(ctxt);
1020 xmlXPtrEvalChildSeq(ctxt, NULL);
1021 } else {
1022 name2 = xmlXPathParseName(ctxt);
1023 if (name2 == NULL) {
1024 CUR_PTR = left;
1025 xmlFree(buffer);
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +02001026 xmlFree(name);
Daniel Veillard9a237c92003-02-13 15:52:58 +00001027 XP_ERROR(XPATH_EXPR_ERROR);
1028 }
1029 xmlXPtrEvalChildSeq(ctxt, name2);
1030 }
1031 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001032#ifdef XPTR_XMLNS_SCHEME
1033 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1034 const xmlChar *left = CUR_PTR;
1035 xmlChar *prefix;
1036 xmlChar *URI;
1037 xmlURIPtr value;
1038
1039 CUR_PTR = buffer;
1040 prefix = xmlXPathParseNCName(ctxt);
1041 if (prefix == NULL) {
1042 xmlFree(buffer);
1043 xmlFree(name);
1044 XP_ERROR(XPTR_SYNTAX_ERROR);
1045 }
1046 SKIP_BLANKS;
1047 if (CUR != '=') {
1048 xmlFree(prefix);
1049 xmlFree(buffer);
1050 xmlFree(name);
1051 XP_ERROR(XPTR_SYNTAX_ERROR);
1052 }
1053 NEXT;
1054 SKIP_BLANKS;
1055 /* @@ check escaping in the XPointer WD */
1056
1057 value = xmlParseURI((const char *)ctxt->cur);
1058 if (value == NULL) {
1059 xmlFree(prefix);
1060 xmlFree(buffer);
1061 xmlFree(name);
1062 XP_ERROR(XPTR_SYNTAX_ERROR);
1063 }
1064 URI = xmlSaveUri(value);
1065 xmlFreeURI(value);
1066 if (URI == NULL) {
1067 xmlFree(prefix);
1068 xmlFree(buffer);
1069 xmlFree(name);
1070 XP_ERROR(XPATH_MEMORY_ERROR);
1071 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001072
Owen Taylor3473f882001-02-23 17:55:21 +00001073 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1074 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001075 xmlFree(URI);
1076 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001077#endif /* XPTR_XMLNS_SCHEME */
1078 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001079 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1080 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001081 }
1082 xmlFree(buffer);
1083 xmlFree(name);
1084}
1085
1086/**
1087 * xmlXPtrEvalFullXPtr:
1088 * @ctxt: the XPointer Parser context
1089 * @name: the preparsed Scheme for the first XPtrPart
1090 *
1091 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1092 *
1093 * As the specs says:
1094 * -----------
1095 * When multiple XPtrParts are provided, they must be evaluated in
1096 * left-to-right order. If evaluation of one part fails, the nexti
1097 * is evaluated. The following conditions cause XPointer part failure:
1098 *
1099 * - An unknown scheme
1100 * - A scheme that does not locate any sub-resource present in the resource
1101 * - A scheme that is not applicable to the media type of the resource
1102 *
1103 * The XPointer application must consume a failed XPointer part and
1104 * attempt to evaluate the next one, if any. The result of the first
1105 * XPointer part whose evaluation succeeds is taken to be the fragment
1106 * located by the XPointer as a whole. If all the parts fail, the result
1107 * for the XPointer as a whole is a sub-resource error.
1108 * -----------
1109 *
1110 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001111 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001112 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001113static void
Owen Taylor3473f882001-02-23 17:55:21 +00001114xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1115 if (name == NULL)
1116 name = xmlXPathParseName(ctxt);
1117 if (name == NULL)
1118 XP_ERROR(XPATH_EXPR_ERROR);
1119 while (name != NULL) {
Jakub Wilk928d7032009-07-29 12:24:11 +02001120 ctxt->error = XPATH_EXPRESSION_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00001121 xmlXPtrEvalXPtrPart(ctxt, name);
1122
1123 /* in case of syntax error, break here */
Jakub Wilk928d7032009-07-29 12:24:11 +02001124 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1125 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
Owen Taylor3473f882001-02-23 17:55:21 +00001126 return;
1127
1128 /*
1129 * If the returned value is a non-empty nodeset
1130 * or location set, return here.
1131 */
1132 if (ctxt->value != NULL) {
1133 xmlXPathObjectPtr obj = ctxt->value;
1134
1135 switch (obj->type) {
1136 case XPATH_LOCATIONSET: {
1137 xmlLocationSetPtr loc = ctxt->value->user;
1138 if ((loc != NULL) && (loc->locNr > 0))
1139 return;
1140 break;
1141 }
1142 case XPATH_NODESET: {
1143 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1144 if ((loc != NULL) && (loc->nodeNr > 0))
1145 return;
1146 break;
1147 }
1148 default:
1149 break;
1150 }
1151
1152 /*
1153 * Evaluating to improper values is equivalent to
1154 * a sub-resource error, clean-up the stack
1155 */
1156 do {
1157 obj = valuePop(ctxt);
1158 if (obj != NULL) {
1159 xmlXPathFreeObject(obj);
1160 }
1161 } while (obj != NULL);
1162 }
1163
1164 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001165 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001166 */
1167 SKIP_BLANKS;
1168 name = xmlXPathParseName(ctxt);
1169 }
1170}
1171
1172/**
1173 * xmlXPtrEvalChildSeq:
1174 * @ctxt: the XPointer Parser context
1175 * @name: a possible ID name of the child sequence
1176 *
1177 * ChildSeq ::= '/1' ('/' [0-9]*)*
1178 * | Name ('/' [0-9]*)+
1179 *
1180 * Parse and evaluate a Child Sequence. This routine also handle the
1181 * case of a Bare Name used to get a document ID.
1182 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001183static void
Owen Taylor3473f882001-02-23 17:55:21 +00001184xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1185 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001186 * XPointer don't allow by syntax to address in mutirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001187 * this might prove useful in some cases, warn about it.
1188 */
1189 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001190 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1191 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001192 }
1193
1194 if (name != NULL) {
1195 valuePush(ctxt, xmlXPathNewString(name));
1196 xmlFree(name);
1197 xmlXPathIdFunction(ctxt, 1);
1198 CHECK_ERROR;
1199 }
1200
1201 while (CUR == '/') {
1202 int child = 0;
1203 NEXT;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001204
Owen Taylor3473f882001-02-23 17:55:21 +00001205 while ((CUR >= '0') && (CUR <= '9')) {
1206 child = child * 10 + (CUR - '0');
1207 NEXT;
1208 }
1209 xmlXPtrGetChildNo(ctxt, child);
1210 }
1211}
1212
1213
1214/**
1215 * xmlXPtrEvalXPointer:
1216 * @ctxt: the XPointer Parser context
1217 *
1218 * XPointer ::= Name
1219 * | ChildSeq
1220 * | FullXPtr
1221 *
1222 * Parse and evaluate an XPointer
1223 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001224static void
Owen Taylor3473f882001-02-23 17:55:21 +00001225xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001226 if (ctxt->valueTab == NULL) {
1227 /* Allocate the value stack */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001228 ctxt->valueTab = (xmlXPathObjectPtr *)
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001229 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1230 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001231 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001232 return;
1233 }
1234 ctxt->valueNr = 0;
1235 ctxt->valueMax = 10;
1236 ctxt->value = NULL;
Daniel Veillardf5048b32011-08-18 17:10:13 +08001237 ctxt->valueFrame = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001238 }
Owen Taylor3473f882001-02-23 17:55:21 +00001239 SKIP_BLANKS;
1240 if (CUR == '/') {
1241 xmlXPathRoot(ctxt);
1242 xmlXPtrEvalChildSeq(ctxt, NULL);
1243 } else {
1244 xmlChar *name;
1245
1246 name = xmlXPathParseName(ctxt);
1247 if (name == NULL)
1248 XP_ERROR(XPATH_EXPR_ERROR);
1249 if (CUR == '(') {
1250 xmlXPtrEvalFullXPtr(ctxt, name);
1251 /* Short evaluation */
1252 return;
1253 } else {
1254 /* this handle both Bare Names and Child Sequences */
1255 xmlXPtrEvalChildSeq(ctxt, name);
1256 }
1257 }
1258 SKIP_BLANKS;
1259 if (CUR != 0)
1260 XP_ERROR(XPATH_EXPR_ERROR);
1261}
1262
1263
1264/************************************************************************
1265 * *
1266 * General routines *
1267 * *
1268 ************************************************************************/
1269
Daniel Veillard8ed10722009-08-20 19:17:36 +02001270static
Owen Taylor3473f882001-02-23 17:55:21 +00001271void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001272static
Owen Taylor3473f882001-02-23 17:55:21 +00001273void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001274static
Owen Taylor3473f882001-02-23 17:55:21 +00001275void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001276static
Owen Taylor3473f882001-02-23 17:55:21 +00001277void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001278static
Owen Taylor3473f882001-02-23 17:55:21 +00001279void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001280static
Owen Taylor3473f882001-02-23 17:55:21 +00001281void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001282static
Owen Taylor3473f882001-02-23 17:55:21 +00001283void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1284
1285/**
1286 * xmlXPtrNewContext:
1287 * @doc: the XML document
1288 * @here: the node that directly contains the XPointer being evaluated or NULL
1289 * @origin: the element from which a user or program initiated traversal of
1290 * the link, or NULL.
1291 *
1292 * Create a new XPointer context
1293 *
1294 * Returns the xmlXPathContext just allocated.
1295 */
1296xmlXPathContextPtr
1297xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1298 xmlXPathContextPtr ret;
1299
1300 ret = xmlXPathNewContext(doc);
1301 if (ret == NULL)
1302 return(ret);
1303 ret->xptr = 1;
1304 ret->here = here;
1305 ret->origin = origin;
1306
Owen Taylor3473f882001-02-23 17:55:21 +00001307 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1308 xmlXPtrRangeFunction);
1309 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1310 xmlXPtrRangeInsideFunction);
1311 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1312 xmlXPtrStringRangeFunction);
1313 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1314 xmlXPtrStartPointFunction);
1315 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1316 xmlXPtrEndPointFunction);
1317 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1318 xmlXPtrHereFunction);
1319 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1320 xmlXPtrOriginFunction);
1321
1322 return(ret);
1323}
1324
1325/**
1326 * xmlXPtrEval:
1327 * @str: the XPointer expression
1328 * @ctx: the XPointer context
1329 *
1330 * Evaluate the XPath Location Path in the given context.
1331 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001332 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001333 * the caller has to free the object.
1334 */
1335xmlXPathObjectPtr
1336xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1337 xmlXPathParserContextPtr ctxt;
1338 xmlXPathObjectPtr res = NULL, tmp;
1339 xmlXPathObjectPtr init = NULL;
1340 int stack = 0;
1341
1342 xmlXPathInit();
1343
1344 if ((ctx == NULL) || (str == NULL))
1345 return(NULL);
1346
1347 ctxt = xmlXPathNewParserContext(str, ctx);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001348 if (ctxt == NULL)
1349 return(NULL);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001350 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001351 xmlXPtrEvalXPointer(ctxt);
1352
1353 if ((ctxt->value != NULL) &&
1354 (ctxt->value->type != XPATH_NODESET) &&
1355 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001356 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1357 "xmlXPtrEval: evaluation failed to return a node set\n",
1358 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001359 } else {
1360 res = valuePop(ctxt);
1361 }
1362
1363 do {
1364 tmp = valuePop(ctxt);
1365 if (tmp != NULL) {
1366 if (tmp != init) {
1367 if (tmp->type == XPATH_NODESET) {
1368 /*
1369 * Evaluation may push a root nodeset which is unused
1370 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001371 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001372 set = tmp->nodesetval;
Nick Wellnhofere905f082016-06-26 12:38:28 +02001373 if ((set == NULL) || (set->nodeNr != 1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00001374 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1375 stack++;
1376 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001377 stack++;
Owen Taylor3473f882001-02-23 17:55:21 +00001378 }
1379 xmlXPathFreeObject(tmp);
1380 }
1381 } while (tmp != NULL);
1382 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001383 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1384 "xmlXPtrEval: object(s) left on the eval stack\n",
1385 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001386 }
1387 if (ctxt->error != XPATH_EXPRESSION_OK) {
1388 xmlXPathFreeObject(res);
1389 res = NULL;
1390 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001391
Owen Taylor3473f882001-02-23 17:55:21 +00001392 xmlXPathFreeParserContext(ctxt);
1393 return(res);
1394}
1395
1396/**
1397 * xmlXPtrBuildRangeNodeList:
1398 * @range: a range object
1399 *
1400 * Build a node list tree copy of the range
1401 *
1402 * Returns an xmlNodePtr list or NULL.
1403 * the caller has to free the node tree.
1404 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001405static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001406xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1407 /* pointers to generated nodes */
1408 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1409 /* pointers to traversal nodes */
1410 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001411 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001412
1413 if (range == NULL)
1414 return(NULL);
1415 if (range->type != XPATH_RANGE)
1416 return(NULL);
1417 start = (xmlNodePtr) range->user;
1418
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001419 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00001420 return(NULL);
1421 end = range->user2;
1422 if (end == NULL)
1423 return(xmlCopyNode(start, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001424 if (end->type == XML_NAMESPACE_DECL)
1425 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001426
1427 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001428 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001429 index2 = range->index2;
1430 while (cur != NULL) {
1431 if (cur == end) {
1432 if (cur->type == XML_TEXT_NODE) {
1433 const xmlChar *content = cur->content;
1434 int len;
1435
1436 if (content == NULL) {
1437 tmp = xmlNewTextLen(NULL, 0);
1438 } else {
1439 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001440 if ((cur == start) && (index1 > 1)) {
1441 content += (index1 - 1);
1442 len -= (index1 - 1);
1443 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001444 } else {
1445 len = index2;
1446 }
1447 tmp = xmlNewTextLen(content, len);
1448 }
1449 /* single sub text node selection */
1450 if (list == NULL)
1451 return(tmp);
1452 /* prune and return full set */
1453 if (last != NULL)
1454 xmlAddNextSibling(last, tmp);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001455 else
Owen Taylor3473f882001-02-23 17:55:21 +00001456 xmlAddChild(parent, tmp);
1457 return(list);
1458 } else {
1459 tmp = xmlCopyNode(cur, 0);
1460 if (list == NULL)
1461 list = tmp;
1462 else {
1463 if (last != NULL)
1464 xmlAddNextSibling(last, tmp);
1465 else
1466 xmlAddChild(parent, tmp);
1467 }
1468 last = NULL;
1469 parent = tmp;
1470
1471 if (index2 > 1) {
1472 end = xmlXPtrGetNthChild(cur, index2 - 1);
1473 index2 = 0;
1474 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001475 if ((cur == start) && (index1 > 1)) {
1476 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1477 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001478 } else {
1479 cur = cur->children;
1480 }
1481 /*
1482 * Now gather the remaining nodes from cur to end
1483 */
1484 continue; /* while */
1485 }
1486 } else if ((cur == start) &&
1487 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001488 if ((cur->type == XML_TEXT_NODE) ||
1489 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001490 const xmlChar *content = cur->content;
1491
1492 if (content == NULL) {
1493 tmp = xmlNewTextLen(NULL, 0);
1494 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001495 if (index1 > 1) {
1496 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001497 }
1498 tmp = xmlNewText(content);
1499 }
1500 last = list = tmp;
1501 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001502 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001503 tmp = xmlCopyNode(cur, 0);
1504 list = tmp;
1505 parent = tmp;
1506 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001507 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1508 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001509 /*
1510 * Now gather the remaining nodes from cur to end
1511 */
1512 continue; /* while */
1513 }
1514 tmp = xmlCopyNode(cur, 1);
1515 list = tmp;
1516 parent = NULL;
1517 last = tmp;
1518 }
1519 } else {
1520 tmp = NULL;
1521 switch (cur->type) {
1522 case XML_DTD_NODE:
1523 case XML_ELEMENT_DECL:
1524 case XML_ATTRIBUTE_DECL:
1525 case XML_ENTITY_NODE:
1526 /* Do not copy DTD informations */
1527 break;
1528 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001529 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001530 break;
1531 case XML_XINCLUDE_START:
1532 case XML_XINCLUDE_END:
1533 /* don't consider it part of the tree content */
1534 break;
1535 case XML_ATTRIBUTE_NODE:
1536 /* Humm, should not happen ! */
1537 STRANGE
1538 break;
1539 default:
1540 tmp = xmlCopyNode(cur, 1);
1541 break;
1542 }
1543 if (tmp != NULL) {
1544 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1545 STRANGE
1546 return(NULL);
1547 }
1548 if (last != NULL)
1549 xmlAddNextSibling(last, tmp);
1550 else {
1551 xmlAddChild(parent, tmp);
1552 last = tmp;
1553 }
1554 }
1555 }
1556 /*
1557 * Skip to next node in document order
1558 */
1559 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1560 STRANGE
1561 return(NULL);
1562 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001563 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001564 }
1565 return(list);
1566}
1567
1568/**
1569 * xmlXPtrBuildNodeList:
1570 * @obj: the XPointer result from the evaluation.
1571 *
1572 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001573 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001574 *
1575 * Returns an xmlNodePtr list or NULL.
1576 * the caller has to free the node tree.
1577 */
1578xmlNodePtr
1579xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1580 xmlNodePtr list = NULL, last = NULL;
1581 int i;
1582
1583 if (obj == NULL)
1584 return(NULL);
1585 switch (obj->type) {
1586 case XPATH_NODESET: {
1587 xmlNodeSetPtr set = obj->nodesetval;
1588 if (set == NULL)
1589 return(NULL);
1590 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001591 if (set->nodeTab[i] == NULL)
1592 continue;
1593 switch (set->nodeTab[i]->type) {
1594 case XML_TEXT_NODE:
1595 case XML_CDATA_SECTION_NODE:
1596 case XML_ELEMENT_NODE:
1597 case XML_ENTITY_REF_NODE:
1598 case XML_ENTITY_NODE:
1599 case XML_PI_NODE:
1600 case XML_COMMENT_NODE:
1601 case XML_DOCUMENT_NODE:
1602 case XML_HTML_DOCUMENT_NODE:
1603#ifdef LIBXML_DOCB_ENABLED
1604 case XML_DOCB_DOCUMENT_NODE:
1605#endif
1606 case XML_XINCLUDE_START:
1607 case XML_XINCLUDE_END:
1608 break;
1609 case XML_ATTRIBUTE_NODE:
1610 case XML_NAMESPACE_DECL:
1611 case XML_DOCUMENT_TYPE_NODE:
1612 case XML_DOCUMENT_FRAG_NODE:
1613 case XML_NOTATION_NODE:
1614 case XML_DTD_NODE:
1615 case XML_ELEMENT_DECL:
1616 case XML_ATTRIBUTE_DECL:
1617 case XML_ENTITY_DECL:
1618 continue; /* for */
1619 }
Owen Taylor3473f882001-02-23 17:55:21 +00001620 if (last == NULL)
1621 list = last = xmlCopyNode(set->nodeTab[i], 1);
1622 else {
1623 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1624 if (last->next != NULL)
1625 last = last->next;
1626 }
1627 }
1628 break;
1629 }
1630 case XPATH_LOCATIONSET: {
1631 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1632 if (set == NULL)
1633 return(NULL);
1634 for (i = 0;i < set->locNr;i++) {
1635 if (last == NULL)
1636 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1637 else
1638 xmlAddNextSibling(last,
1639 xmlXPtrBuildNodeList(set->locTab[i]));
1640 if (last != NULL) {
1641 while (last->next != NULL)
1642 last = last->next;
1643 }
1644 }
1645 break;
1646 }
1647 case XPATH_RANGE:
1648 return(xmlXPtrBuildRangeNodeList(obj));
1649 case XPATH_POINT:
1650 return(xmlCopyNode(obj->user, 0));
1651 default:
1652 break;
1653 }
1654 return(list);
1655}
1656
1657/************************************************************************
1658 * *
1659 * XPointer functions *
1660 * *
1661 ************************************************************************/
1662
1663/**
1664 * xmlXPtrNbLocChildren:
1665 * @node: an xmlNodePtr
1666 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001667 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001668 * string value in case of text/PI/Comments nodes
1669 *
1670 * Returns the number of location children
1671 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001672static int
Owen Taylor3473f882001-02-23 17:55:21 +00001673xmlXPtrNbLocChildren(xmlNodePtr node) {
1674 int ret = 0;
1675 if (node == NULL)
1676 return(-1);
1677 switch (node->type) {
1678 case XML_HTML_DOCUMENT_NODE:
1679 case XML_DOCUMENT_NODE:
1680 case XML_ELEMENT_NODE:
1681 node = node->children;
1682 while (node != NULL) {
1683 if (node->type == XML_ELEMENT_NODE)
1684 ret++;
1685 node = node->next;
1686 }
1687 break;
1688 case XML_ATTRIBUTE_NODE:
1689 return(-1);
1690
1691 case XML_PI_NODE:
1692 case XML_COMMENT_NODE:
1693 case XML_TEXT_NODE:
1694 case XML_CDATA_SECTION_NODE:
1695 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001696 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001697 break;
1698 default:
1699 return(-1);
1700 }
1701 return(ret);
1702}
1703
1704/**
1705 * xmlXPtrHereFunction:
1706 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001707 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001708 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001709 * Function implementing here() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001710 * as described in 5.4.3
1711 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001712static void
Owen Taylor3473f882001-02-23 17:55:21 +00001713xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001714 CHECK_ARITY(0);
1715
Owen Taylor3473f882001-02-23 17:55:21 +00001716 if (ctxt->context->here == NULL)
1717 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001718
Owen Taylor3473f882001-02-23 17:55:21 +00001719 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1720}
1721
1722/**
1723 * xmlXPtrOriginFunction:
1724 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001725 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001726 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001727 * Function implementing origin() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001728 * as described in 5.4.3
1729 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001730static void
Owen Taylor3473f882001-02-23 17:55:21 +00001731xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001732 CHECK_ARITY(0);
1733
Owen Taylor3473f882001-02-23 17:55:21 +00001734 if (ctxt->context->origin == NULL)
1735 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001736
Owen Taylor3473f882001-02-23 17:55:21 +00001737 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1738}
1739
1740/**
1741 * xmlXPtrStartPointFunction:
1742 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001743 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001744 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001745 * Function implementing start-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001746 * as described in 5.4.3
1747 * ----------------
1748 * location-set start-point(location-set)
1749 *
1750 * For each location x in the argument location-set, start-point adds a
1751 * location of type point to the result location-set. That point represents
1752 * the start point of location x and is determined by the following rules:
1753 *
1754 * - If x is of type point, the start point is x.
1755 * - If x is of type range, the start point is the start point of x.
1756 * - If x is of type root, element, text, comment, or processing instruction,
1757 * - the container node of the start point is x and the index is 0.
1758 * - If x is of type attribute or namespace, the function must signal a
1759 * syntax error.
1760 * ----------------
1761 *
1762 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001763static void
Owen Taylor3473f882001-02-23 17:55:21 +00001764xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1765 xmlXPathObjectPtr tmp, obj, point;
1766 xmlLocationSetPtr newset = NULL;
1767 xmlLocationSetPtr oldset = NULL;
1768
1769 CHECK_ARITY(1);
1770 if ((ctxt->value == NULL) ||
1771 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1772 (ctxt->value->type != XPATH_NODESET)))
1773 XP_ERROR(XPATH_INVALID_TYPE)
1774
1775 obj = valuePop(ctxt);
1776 if (obj->type == XPATH_NODESET) {
1777 /*
1778 * First convert to a location set
1779 */
1780 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1781 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001782 if (tmp == NULL)
1783 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001784 obj = tmp;
1785 }
1786
1787 newset = xmlXPtrLocationSetCreate(NULL);
1788 if (newset == NULL) {
1789 xmlXPathFreeObject(obj);
1790 XP_ERROR(XPATH_MEMORY_ERROR);
1791 }
1792 oldset = (xmlLocationSetPtr) obj->user;
1793 if (oldset != NULL) {
1794 int i;
1795
1796 for (i = 0; i < oldset->locNr; i++) {
1797 tmp = oldset->locTab[i];
1798 if (tmp == NULL)
1799 continue;
1800 point = NULL;
1801 switch (tmp->type) {
1802 case XPATH_POINT:
1803 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1804 break;
1805 case XPATH_RANGE: {
1806 xmlNodePtr node = tmp->user;
1807 if (node != NULL) {
Nick Wellnhofer3f8a9102016-06-28 15:55:09 +02001808 if ((node->type == XML_ATTRIBUTE_NODE) ||
1809 (node->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001810 xmlXPathFreeObject(obj);
1811 xmlXPtrFreeLocationSet(newset);
1812 XP_ERROR(XPTR_SYNTAX_ERROR);
1813 }
1814 point = xmlXPtrNewPoint(node, tmp->index);
1815 }
1816 break;
1817 }
1818 default:
1819 /*** Should we raise an error ?
1820 xmlXPathFreeObject(obj);
1821 xmlXPathFreeObject(newset);
1822 XP_ERROR(XPATH_INVALID_TYPE)
1823 ***/
1824 break;
1825 }
1826 if (point != NULL)
1827 xmlXPtrLocationSetAdd(newset, point);
1828 }
1829 }
1830 xmlXPathFreeObject(obj);
1831 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1832}
1833
1834/**
1835 * xmlXPtrEndPointFunction:
1836 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001837 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001838 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001839 * Function implementing end-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001840 * as described in 5.4.3
1841 * ----------------------------
1842 * location-set end-point(location-set)
1843 *
1844 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001845 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001846 * the end point of location x and is determined by the following rules:
1847 *
1848 * - If x is of type point, the resulting point is x.
1849 * - If x is of type range, the resulting point is the end point of x.
1850 * - If x is of type root or element, the container node of the resulting
1851 * point is x and the index is the number of location children of x.
1852 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001853 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001854 * string-value of x.
1855 * - If x is of type attribute or namespace, the function must signal a
1856 * syntax error.
1857 * ----------------------------
1858 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001859static void
Owen Taylor3473f882001-02-23 17:55:21 +00001860xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1861 xmlXPathObjectPtr tmp, obj, point;
1862 xmlLocationSetPtr newset = NULL;
1863 xmlLocationSetPtr oldset = NULL;
1864
1865 CHECK_ARITY(1);
1866 if ((ctxt->value == NULL) ||
1867 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1868 (ctxt->value->type != XPATH_NODESET)))
1869 XP_ERROR(XPATH_INVALID_TYPE)
1870
1871 obj = valuePop(ctxt);
1872 if (obj->type == XPATH_NODESET) {
1873 /*
1874 * First convert to a location set
1875 */
1876 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1877 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001878 if (tmp == NULL)
1879 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001880 obj = tmp;
1881 }
1882
1883 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001884 if (newset == NULL) {
1885 xmlXPathFreeObject(obj);
1886 XP_ERROR(XPATH_MEMORY_ERROR);
1887 }
Owen Taylor3473f882001-02-23 17:55:21 +00001888 oldset = (xmlLocationSetPtr) obj->user;
1889 if (oldset != NULL) {
1890 int i;
1891
1892 for (i = 0; i < oldset->locNr; i++) {
1893 tmp = oldset->locTab[i];
1894 if (tmp == NULL)
1895 continue;
1896 point = NULL;
1897 switch (tmp->type) {
1898 case XPATH_POINT:
1899 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1900 break;
1901 case XPATH_RANGE: {
1902 xmlNodePtr node = tmp->user2;
1903 if (node != NULL) {
Nick Wellnhofer3f8a9102016-06-28 15:55:09 +02001904 if ((node->type == XML_ATTRIBUTE_NODE) ||
1905 (node->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001906 xmlXPathFreeObject(obj);
1907 xmlXPtrFreeLocationSet(newset);
1908 XP_ERROR(XPTR_SYNTAX_ERROR);
1909 }
1910 point = xmlXPtrNewPoint(node, tmp->index2);
1911 } else if (tmp->user == NULL) {
1912 point = xmlXPtrNewPoint(node,
1913 xmlXPtrNbLocChildren(node));
1914 }
1915 break;
1916 }
1917 default:
1918 /*** Should we raise an error ?
1919 xmlXPathFreeObject(obj);
1920 xmlXPathFreeObject(newset);
1921 XP_ERROR(XPATH_INVALID_TYPE)
1922 ***/
1923 break;
1924 }
1925 if (point != NULL)
1926 xmlXPtrLocationSetAdd(newset, point);
1927 }
1928 }
1929 xmlXPathFreeObject(obj);
1930 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1931}
1932
1933
1934/**
1935 * xmlXPtrCoveringRange:
1936 * @ctxt: the XPointer Parser context
1937 * @loc: the location for which the covering range must be computed
1938 *
1939 * A covering range is a range that wholly encompasses a location
1940 * Section 5.3.3. Covering Ranges for All Location Types
1941 * http://www.w3.org/TR/xptr#N2267
1942 *
1943 * Returns a new location or NULL in case of error
1944 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001945static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001946xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1947 if (loc == NULL)
1948 return(NULL);
1949 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1950 (ctxt->context->doc == NULL))
1951 return(NULL);
1952 switch (loc->type) {
1953 case XPATH_POINT:
1954 return(xmlXPtrNewRange(loc->user, loc->index,
1955 loc->user, loc->index));
1956 case XPATH_RANGE:
1957 if (loc->user2 != NULL) {
1958 return(xmlXPtrNewRange(loc->user, loc->index,
1959 loc->user2, loc->index2));
1960 } else {
1961 xmlNodePtr node = (xmlNodePtr) loc->user;
1962 if (node == (xmlNodePtr) ctxt->context->doc) {
1963 return(xmlXPtrNewRange(node, 0, node,
1964 xmlXPtrGetArity(node)));
1965 } else {
1966 switch (node->type) {
1967 case XML_ATTRIBUTE_NODE:
1968 /* !!! our model is slightly different than XPath */
1969 return(xmlXPtrNewRange(node, 0, node,
1970 xmlXPtrGetArity(node)));
1971 case XML_ELEMENT_NODE:
1972 case XML_TEXT_NODE:
1973 case XML_CDATA_SECTION_NODE:
1974 case XML_ENTITY_REF_NODE:
1975 case XML_PI_NODE:
1976 case XML_COMMENT_NODE:
1977 case XML_DOCUMENT_NODE:
1978 case XML_NOTATION_NODE:
1979 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001980 int indx = xmlXPtrGetIndex(node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001981
Owen Taylor3473f882001-02-23 17:55:21 +00001982 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001983 return(xmlXPtrNewRange(node, indx - 1,
1984 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001985 }
1986 default:
1987 return(NULL);
1988 }
1989 }
1990 }
1991 default:
1992 TODO /* missed one case ??? */
1993 }
1994 return(NULL);
1995}
1996
1997/**
1998 * xmlXPtrRangeFunction:
1999 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002000 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002001 *
2002 * Function implementing the range() function 5.4.3
2003 * location-set range(location-set )
2004 *
2005 * The range function returns ranges covering the locations in
2006 * the argument location-set. For each location x in the argument
2007 * location-set, a range location representing the covering range of
2008 * x is added to the result location-set.
2009 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002010static void
Owen Taylor3473f882001-02-23 17:55:21 +00002011xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2012 int i;
2013 xmlXPathObjectPtr set;
2014 xmlLocationSetPtr oldset;
2015 xmlLocationSetPtr newset;
2016
2017 CHECK_ARITY(1);
2018 if ((ctxt->value == NULL) ||
2019 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2020 (ctxt->value->type != XPATH_NODESET)))
2021 XP_ERROR(XPATH_INVALID_TYPE)
2022
2023 set = valuePop(ctxt);
2024 if (set->type == XPATH_NODESET) {
2025 xmlXPathObjectPtr tmp;
2026
2027 /*
2028 * First convert to a location set
2029 */
2030 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2031 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002032 if (tmp == NULL)
2033 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002034 set = tmp;
2035 }
2036 oldset = (xmlLocationSetPtr) set->user;
2037
2038 /*
2039 * The loop is to compute the covering range for each item and add it
2040 */
2041 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002042 if (newset == NULL) {
2043 xmlXPathFreeObject(set);
2044 XP_ERROR(XPATH_MEMORY_ERROR);
2045 }
Nick Wellnhofere905f082016-06-26 12:38:28 +02002046 if (oldset != NULL) {
2047 for (i = 0;i < oldset->locNr;i++) {
2048 xmlXPtrLocationSetAdd(newset,
2049 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2050 }
Owen Taylor3473f882001-02-23 17:55:21 +00002051 }
2052
2053 /*
2054 * Save the new value and cleanup
2055 */
2056 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2057 xmlXPathFreeObject(set);
2058}
2059
2060/**
2061 * xmlXPtrInsideRange:
2062 * @ctxt: the XPointer Parser context
2063 * @loc: the location for which the inside range must be computed
2064 *
2065 * A inside range is a range described in the range-inside() description
2066 *
2067 * Returns a new location or NULL in case of error
2068 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002069static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002070xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2071 if (loc == NULL)
2072 return(NULL);
2073 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2074 (ctxt->context->doc == NULL))
2075 return(NULL);
2076 switch (loc->type) {
2077 case XPATH_POINT: {
2078 xmlNodePtr node = (xmlNodePtr) loc->user;
2079 switch (node->type) {
2080 case XML_PI_NODE:
2081 case XML_COMMENT_NODE:
2082 case XML_TEXT_NODE:
2083 case XML_CDATA_SECTION_NODE: {
2084 if (node->content == NULL) {
2085 return(xmlXPtrNewRange(node, 0, node, 0));
2086 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002087 return(xmlXPtrNewRange(node, 0, node,
2088 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002089 }
2090 }
2091 case XML_ATTRIBUTE_NODE:
2092 case XML_ELEMENT_NODE:
2093 case XML_ENTITY_REF_NODE:
2094 case XML_DOCUMENT_NODE:
2095 case XML_NOTATION_NODE:
2096 case XML_HTML_DOCUMENT_NODE: {
2097 return(xmlXPtrNewRange(node, 0, node,
2098 xmlXPtrGetArity(node)));
2099 }
2100 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002101 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002102 }
2103 return(NULL);
2104 }
2105 case XPATH_RANGE: {
2106 xmlNodePtr node = (xmlNodePtr) loc->user;
2107 if (loc->user2 != NULL) {
2108 return(xmlXPtrNewRange(node, loc->index,
2109 loc->user2, loc->index2));
2110 } else {
2111 switch (node->type) {
2112 case XML_PI_NODE:
2113 case XML_COMMENT_NODE:
2114 case XML_TEXT_NODE:
2115 case XML_CDATA_SECTION_NODE: {
2116 if (node->content == NULL) {
2117 return(xmlXPtrNewRange(node, 0, node, 0));
2118 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002119 return(xmlXPtrNewRange(node, 0, node,
2120 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002121 }
2122 }
2123 case XML_ATTRIBUTE_NODE:
2124 case XML_ELEMENT_NODE:
2125 case XML_ENTITY_REF_NODE:
2126 case XML_DOCUMENT_NODE:
2127 case XML_NOTATION_NODE:
2128 case XML_HTML_DOCUMENT_NODE: {
2129 return(xmlXPtrNewRange(node, 0, node,
2130 xmlXPtrGetArity(node)));
2131 }
2132 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002133 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002134 }
2135 return(NULL);
2136 }
2137 }
2138 default:
2139 TODO /* missed one case ??? */
2140 }
2141 return(NULL);
2142}
2143
2144/**
2145 * xmlXPtrRangeInsideFunction:
2146 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002147 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002148 *
2149 * Function implementing the range-inside() function 5.4.3
2150 * location-set range-inside(location-set )
2151 *
2152 * The range-inside function returns ranges covering the contents of
2153 * the locations in the argument location-set. For each location x in
2154 * the argument location-set, a range location is added to the result
2155 * location-set. If x is a range location, then x is added to the
2156 * result location-set. If x is not a range location, then x is used
2157 * as the container location of the start and end points of the range
2158 * location to be added; the index of the start point of the range is
2159 * zero; if the end point is a character point then its index is the
2160 * length of the string-value of x, and otherwise is the number of
2161 * location children of x.
2162 *
2163 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002164static void
Owen Taylor3473f882001-02-23 17:55:21 +00002165xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2166 int i;
2167 xmlXPathObjectPtr set;
2168 xmlLocationSetPtr oldset;
2169 xmlLocationSetPtr newset;
2170
2171 CHECK_ARITY(1);
2172 if ((ctxt->value == NULL) ||
2173 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2174 (ctxt->value->type != XPATH_NODESET)))
2175 XP_ERROR(XPATH_INVALID_TYPE)
2176
2177 set = valuePop(ctxt);
2178 if (set->type == XPATH_NODESET) {
2179 xmlXPathObjectPtr tmp;
2180
2181 /*
2182 * First convert to a location set
2183 */
2184 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2185 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002186 if (tmp == NULL)
2187 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002188 set = tmp;
2189 }
2190 oldset = (xmlLocationSetPtr) set->user;
2191
2192 /*
2193 * The loop is to compute the covering range for each item and add it
2194 */
2195 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002196 if (newset == NULL) {
2197 xmlXPathFreeObject(set);
2198 XP_ERROR(XPATH_MEMORY_ERROR);
2199 }
Owen Taylor3473f882001-02-23 17:55:21 +00002200 for (i = 0;i < oldset->locNr;i++) {
2201 xmlXPtrLocationSetAdd(newset,
2202 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2203 }
2204
2205 /*
2206 * Save the new value and cleanup
2207 */
2208 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2209 xmlXPathFreeObject(set);
2210}
2211
2212/**
2213 * xmlXPtrRangeToFunction:
2214 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002215 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002216 *
2217 * Implement the range-to() XPointer function
Nick Wellnhofer9ab01a22016-06-28 14:22:23 +02002218 *
2219 * Obsolete. range-to is not a real function but a special type of location
2220 * step which is handled in xpath.c.
Owen Taylor3473f882001-02-23 17:55:21 +00002221 */
2222void
Nick Wellnhofer9ab01a22016-06-28 14:22:23 +02002223xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
2224 int nargs ATTRIBUTE_UNUSED) {
2225 XP_ERROR(XPATH_EXPR_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +00002226}
2227
2228/**
2229 * xmlXPtrAdvanceNode:
2230 * @cur: the node
William M. Brackce4fc562004-01-22 02:47:18 +00002231 * @level: incremented/decremented to show level in tree
Owen Taylor3473f882001-02-23 17:55:21 +00002232 *
2233 * Advance to the next element or text node in document order
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002234 * TODO: add a stack for entering/exiting entities
Owen Taylor3473f882001-02-23 17:55:21 +00002235 *
2236 * Returns -1 in case of failure, 0 otherwise
2237 */
2238xmlNodePtr
William M. Brackf7eb7942003-12-31 07:59:17 +00002239xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Owen Taylor3473f882001-02-23 17:55:21 +00002240next:
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002241 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002242 return(NULL);
2243 if (cur->children != NULL) {
2244 cur = cur->children ;
William M. Brackf7eb7942003-12-31 07:59:17 +00002245 if (level != NULL)
2246 (*level)++;
Owen Taylor3473f882001-02-23 17:55:21 +00002247 goto found;
2248 }
William M. Brack4d59e222004-03-08 14:42:31 +00002249skip: /* This label should only be needed if something is wrong! */
Owen Taylor3473f882001-02-23 17:55:21 +00002250 if (cur->next != NULL) {
2251 cur = cur->next;
2252 goto found;
2253 }
2254 do {
2255 cur = cur->parent;
William M. Brackf7eb7942003-12-31 07:59:17 +00002256 if (level != NULL)
2257 (*level)--;
Owen Taylor3473f882001-02-23 17:55:21 +00002258 if (cur == NULL) return(NULL);
2259 if (cur->next != NULL) {
2260 cur = cur->next;
2261 goto found;
2262 }
2263 } while (cur != NULL);
2264
2265found:
2266 if ((cur->type != XML_ELEMENT_NODE) &&
2267 (cur->type != XML_TEXT_NODE) &&
2268 (cur->type != XML_DOCUMENT_NODE) &&
2269 (cur->type != XML_HTML_DOCUMENT_NODE) &&
William M. Brack4d59e222004-03-08 14:42:31 +00002270 (cur->type != XML_CDATA_SECTION_NODE)) {
2271 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2272 TODO
2273 goto skip;
2274 }
2275 goto next;
2276 }
Owen Taylor3473f882001-02-23 17:55:21 +00002277 return(cur);
2278}
2279
2280/**
2281 * xmlXPtrAdvanceChar:
2282 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002283 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002284 * @bytes: the number of bytes
2285 *
2286 * Advance a point of the associated number of bytes (not UTF8 chars)
2287 *
2288 * Returns -1 in case of failure, 0 otherwise
2289 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002290static int
2291xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002292 xmlNodePtr cur;
2293 int pos;
2294 int len;
2295
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002296 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002297 return(-1);
2298 cur = *node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002299 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002300 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002301 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002302
2303 while (bytes >= 0) {
2304 /*
2305 * First position to the beginning of the first text node
2306 * corresponding to this point
2307 */
2308 while ((cur != NULL) &&
2309 ((cur->type == XML_ELEMENT_NODE) ||
2310 (cur->type == XML_DOCUMENT_NODE) ||
2311 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2312 if (pos > 0) {
2313 cur = xmlXPtrGetNthChild(cur, pos);
2314 pos = 0;
2315 } else {
William M. Brackf7eb7942003-12-31 07:59:17 +00002316 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002317 pos = 0;
2318 }
2319 }
2320
2321 if (cur == NULL) {
2322 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002323 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002324 return(-1);
2325 }
2326
2327 /*
2328 * if there is no move needed return the current value.
2329 */
2330 if (pos == 0) pos = 1;
2331 if (bytes == 0) {
2332 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002333 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002334 return(0);
2335 }
2336 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002337 * We should have a text (or cdata) node ...
Owen Taylor3473f882001-02-23 17:55:21 +00002338 */
2339 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002340 if ((cur->type != XML_ELEMENT_NODE) &&
2341 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002342 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002343 }
2344 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002345 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002346 STRANGE
2347 pos = len;
2348 }
2349 if (pos + bytes >= len) {
2350 bytes -= (len - pos);
William M. Brackf7eb7942003-12-31 07:59:17 +00002351 cur = xmlXPtrAdvanceNode(cur, NULL);
Daniel Veillard24505b02005-07-28 23:49:35 +00002352 pos = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002353 } else if (pos + bytes < len) {
2354 pos += bytes;
2355 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002356 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002357 return(0);
2358 }
2359 }
2360 return(-1);
2361}
2362
2363/**
2364 * xmlXPtrMatchString:
2365 * @string: the string to search
2366 * @start: the start textnode
2367 * @startindex: the start index
2368 * @end: the end textnode IN/OUT
2369 * @endindex: the end index IN/OUT
2370 *
2371 * Check whether the document contains @string at the position
2372 * (@start, @startindex) and limited by the (@end, @endindex) point
2373 *
2374 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2375 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002376 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002377 * of the range
2378 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002379static int
Owen Taylor3473f882001-02-23 17:55:21 +00002380xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2381 xmlNodePtr *end, int *endindex) {
2382 xmlNodePtr cur;
2383 int pos; /* 0 based */
2384 int len; /* in bytes */
2385 int stringlen; /* in bytes */
2386 int match;
2387
2388 if (string == NULL)
2389 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002390 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002391 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002392 if ((end == NULL) || (*end == NULL) ||
2393 ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002394 return(-1);
2395 cur = start;
Owen Taylor3473f882001-02-23 17:55:21 +00002396 pos = startindex - 1;
2397 stringlen = xmlStrlen(string);
2398
2399 while (stringlen > 0) {
2400 if ((cur == *end) && (pos + stringlen > *endindex))
2401 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002402
2403 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002404 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002405 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002406 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002407 if (match) {
2408#ifdef DEBUG_RANGES
2409 xmlGenericError(xmlGenericErrorContext,
2410 "found range %d bytes at index %d of ->",
2411 stringlen, pos + 1);
2412 xmlDebugDumpString(stdout, cur->content);
2413 xmlGenericError(xmlGenericErrorContext, "\n");
2414#endif
2415 *end = cur;
2416 *endindex = pos + stringlen;
2417 return(1);
2418 } else {
2419 return(0);
2420 }
2421 } else {
2422 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002423 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002424 if (match) {
2425#ifdef DEBUG_RANGES
2426 xmlGenericError(xmlGenericErrorContext,
2427 "found subrange %d bytes at index %d of ->",
2428 sub, pos + 1);
2429 xmlDebugDumpString(stdout, cur->content);
2430 xmlGenericError(xmlGenericErrorContext, "\n");
2431#endif
2432 string = &string[sub];
2433 stringlen -= sub;
2434 } else {
2435 return(0);
2436 }
2437 }
2438 }
William M. Brackf7eb7942003-12-31 07:59:17 +00002439 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002440 if (cur == NULL)
2441 return(0);
2442 pos = 0;
2443 }
2444 return(1);
2445}
2446
2447/**
2448 * xmlXPtrSearchString:
2449 * @string: the string to search
2450 * @start: the start textnode IN/OUT
2451 * @startindex: the start index IN/OUT
2452 * @end: the end textnode
2453 * @endindex: the end index
2454 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002455 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002456 * until the (@end, @endindex) point is reached
2457 *
2458 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2459 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002460 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002461 * of the range
2462 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002463static int
Owen Taylor3473f882001-02-23 17:55:21 +00002464xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2465 xmlNodePtr *end, int *endindex) {
2466 xmlNodePtr cur;
2467 const xmlChar *str;
2468 int pos; /* 0 based */
2469 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002470 xmlChar first;
2471
2472 if (string == NULL)
2473 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002474 if ((start == NULL) || (*start == NULL) ||
2475 ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002476 return(-1);
2477 if ((end == NULL) || (endindex == NULL))
2478 return(-1);
2479 cur = *start;
Owen Taylor3473f882001-02-23 17:55:21 +00002480 pos = *startindex - 1;
2481 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002482
2483 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002484 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002485 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002486 while (pos <= len) {
2487 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002488 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002489 if (str != NULL) {
2490 pos = (str - (xmlChar *)(cur->content));
2491#ifdef DEBUG_RANGES
2492 xmlGenericError(xmlGenericErrorContext,
2493 "found '%c' at index %d of ->",
2494 first, pos + 1);
2495 xmlDebugDumpString(stdout, cur->content);
2496 xmlGenericError(xmlGenericErrorContext, "\n");
2497#endif
2498 if (xmlXPtrMatchString(string, cur, pos + 1,
2499 end, endindex)) {
2500 *start = cur;
2501 *startindex = pos + 1;
2502 return(1);
2503 }
2504 pos++;
2505 } else {
2506 pos = len + 1;
2507 }
2508 } else {
2509 /*
2510 * An empty string is considered to match before each
2511 * character of the string-value and after the final
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002512 * character.
Owen Taylor3473f882001-02-23 17:55:21 +00002513 */
2514#ifdef DEBUG_RANGES
2515 xmlGenericError(xmlGenericErrorContext,
2516 "found '' at index %d of ->",
2517 pos + 1);
2518 xmlDebugDumpString(stdout, cur->content);
2519 xmlGenericError(xmlGenericErrorContext, "\n");
2520#endif
2521 *start = cur;
2522 *startindex = pos + 1;
2523 *end = cur;
2524 *endindex = pos + 1;
2525 return(1);
2526 }
2527 }
2528 }
2529 if ((cur == *end) && (pos >= *endindex))
2530 return(0);
William M. Brackf7eb7942003-12-31 07:59:17 +00002531 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002532 if (cur == NULL)
2533 return(0);
2534 pos = 1;
2535 }
2536 return(0);
2537}
2538
2539/**
2540 * xmlXPtrGetLastChar:
2541 * @node: the node
2542 * @index: the index
2543 *
2544 * Computes the point coordinates of the last char of this point
2545 *
2546 * Returns -1 in case of failure, 0 otherwise
2547 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002548static int
2549xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002550 xmlNodePtr cur;
2551 int pos, len = 0;
2552
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002553 if ((node == NULL) || (*node == NULL) ||
2554 ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002555 return(-1);
2556 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002557 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002558
Owen Taylor3473f882001-02-23 17:55:21 +00002559 if ((cur->type == XML_ELEMENT_NODE) ||
2560 (cur->type == XML_DOCUMENT_NODE) ||
2561 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2562 if (pos > 0) {
2563 cur = xmlXPtrGetNthChild(cur, pos);
Owen Taylor3473f882001-02-23 17:55:21 +00002564 }
2565 }
2566 while (cur != NULL) {
2567 if (cur->last != NULL)
2568 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002569 else if ((cur->type != XML_ELEMENT_NODE) &&
2570 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002571 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002572 break;
2573 } else {
2574 return(-1);
2575 }
2576 }
2577 if (cur == NULL)
2578 return(-1);
2579 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002580 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002581 return(0);
2582}
2583
2584/**
2585 * xmlXPtrGetStartPoint:
2586 * @obj: an range
2587 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002588 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002589 *
2590 * read the object and return the start point coordinates.
2591 *
2592 * Returns -1 in case of failure, 0 otherwise
2593 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002594static int
2595xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2596 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002597 return(-1);
2598
2599 switch (obj->type) {
2600 case XPATH_POINT:
2601 *node = obj->user;
2602 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002603 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002604 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002605 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002606 return(0);
2607 case XPATH_RANGE:
2608 *node = obj->user;
2609 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002610 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002611 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002612 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002613 return(0);
2614 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002615 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002616 }
2617 return(-1);
2618}
2619
2620/**
2621 * xmlXPtrGetEndPoint:
2622 * @obj: an range
2623 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002624 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002625 *
2626 * read the object and return the end point coordinates.
2627 *
2628 * Returns -1 in case of failure, 0 otherwise
2629 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002630static int
2631xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2632 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002633 return(-1);
2634
2635 switch (obj->type) {
2636 case XPATH_POINT:
2637 *node = obj->user;
2638 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002639 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002640 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002641 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002642 return(0);
2643 case XPATH_RANGE:
2644 *node = obj->user;
2645 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002646 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002647 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002648 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002649 return(0);
2650 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002651 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002652 }
2653 return(-1);
2654}
2655
2656/**
2657 * xmlXPtrStringRangeFunction:
2658 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002659 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002660 *
2661 * Function implementing the string-range() function
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002662 * range as described in 5.4.2
Owen Taylor3473f882001-02-23 17:55:21 +00002663 *
2664 * ------------------------------
2665 * [Definition: For each location in the location-set argument,
2666 * string-range returns a set of string ranges, a set of substrings in a
2667 * string. Specifically, the string-value of the location is searched for
2668 * substrings that match the string argument, and the resulting location-set
2669 * will contain a range location for each non-overlapping match.]
2670 * An empty string is considered to match before each character of the
2671 * string-value and after the final character. Whitespace in a string
2672 * is matched literally, with no normalization except that provided by
2673 * XML for line ends. The third argument gives the position of the first
2674 * character to be in the resulting range, relative to the start of the
2675 * match. The default value is 1, which makes the range start immediately
2676 * before the first character of the matched string. The fourth argument
2677 * gives the number of characters in the range; the default is that the
2678 * range extends to the end of the matched string.
2679 *
2680 * Element boundaries, as well as entire embedded nodes such as processing
2681 * instructions and comments, are ignored as defined in [XPath].
2682 *
2683 * If the string in the second argument is not found in the string-value
2684 * of the location, or if a value in the third or fourth argument indicates
2685 * a string that is beyond the beginning or end of the document, the
2686 * expression fails.
2687 *
2688 * The points of the range-locations in the returned location-set will
2689 * all be character points.
2690 * ------------------------------
2691 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002692static void
Owen Taylor3473f882001-02-23 17:55:21 +00002693xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardaac7c682006-03-10 13:40:16 +00002694 int i, startindex, endindex = 0, fendindex;
2695 xmlNodePtr start, end = 0, fend;
Owen Taylor3473f882001-02-23 17:55:21 +00002696 xmlXPathObjectPtr set;
2697 xmlLocationSetPtr oldset;
2698 xmlLocationSetPtr newset;
2699 xmlXPathObjectPtr string;
2700 xmlXPathObjectPtr position = NULL;
2701 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002702 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002703
2704 /*
2705 * Grab the arguments
2706 */
2707 if ((nargs < 2) || (nargs > 4))
2708 XP_ERROR(XPATH_INVALID_ARITY);
2709
2710 if (nargs >= 4) {
2711 CHECK_TYPE(XPATH_NUMBER);
2712 number = valuePop(ctxt);
2713 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002714 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002715 }
2716 if (nargs >= 3) {
2717 CHECK_TYPE(XPATH_NUMBER);
2718 position = valuePop(ctxt);
2719 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002720 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002721 }
2722 CHECK_TYPE(XPATH_STRING);
2723 string = valuePop(ctxt);
2724 if ((ctxt->value == NULL) ||
2725 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2726 (ctxt->value->type != XPATH_NODESET)))
2727 XP_ERROR(XPATH_INVALID_TYPE)
2728
2729 set = valuePop(ctxt);
William M. Brack08171912003-12-29 02:52:11 +00002730 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002731 if (newset == NULL) {
2732 xmlXPathFreeObject(set);
2733 XP_ERROR(XPATH_MEMORY_ERROR);
2734 }
William M. Brack08171912003-12-29 02:52:11 +00002735 if (set->nodesetval == NULL) {
2736 goto error;
2737 }
Owen Taylor3473f882001-02-23 17:55:21 +00002738 if (set->type == XPATH_NODESET) {
2739 xmlXPathObjectPtr tmp;
2740
2741 /*
2742 * First convert to a location set
2743 */
2744 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2745 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002746 if (tmp == NULL)
2747 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002748 set = tmp;
2749 }
2750 oldset = (xmlLocationSetPtr) set->user;
2751
2752 /*
2753 * The loop is to search for each element in the location set
2754 * the list of location set corresponding to that search
2755 */
Owen Taylor3473f882001-02-23 17:55:21 +00002756 for (i = 0;i < oldset->locNr;i++) {
2757#ifdef DEBUG_RANGES
2758 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2759#endif
2760
2761 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2762 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2763 xmlXPtrAdvanceChar(&start, &startindex, 0);
2764 xmlXPtrGetLastChar(&end, &endindex);
2765
2766#ifdef DEBUG_RANGES
2767 xmlGenericError(xmlGenericErrorContext,
2768 "from index %d of ->", startindex);
2769 xmlDebugDumpString(stdout, start->content);
2770 xmlGenericError(xmlGenericErrorContext, "\n");
2771 xmlGenericError(xmlGenericErrorContext,
2772 "to index %d of ->", endindex);
2773 xmlDebugDumpString(stdout, end->content);
2774 xmlGenericError(xmlGenericErrorContext, "\n");
2775#endif
2776 do {
2777 fend = end;
2778 fendindex = endindex;
2779 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2780 &fend, &fendindex);
2781 if (found == 1) {
2782 if (position == NULL) {
2783 xmlXPtrLocationSetAdd(newset,
2784 xmlXPtrNewRange(start, startindex, fend, fendindex));
2785 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2786 pos - 1) == 0) {
2787 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002788 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002789 xmlNodePtr rend;
2790 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002791 rindx = startindex - 1;
2792 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002793 num) == 0) {
2794 xmlXPtrLocationSetAdd(newset,
2795 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002796 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002797 }
2798 } else if ((number != NULL) && (num <= 0)) {
2799 xmlXPtrLocationSetAdd(newset,
2800 xmlXPtrNewRange(start, startindex,
2801 start, startindex));
2802 } else {
2803 xmlXPtrLocationSetAdd(newset,
2804 xmlXPtrNewRange(start, startindex,
2805 fend, fendindex));
2806 }
2807 }
2808 start = fend;
2809 startindex = fendindex;
2810 if (string->stringval[0] == 0)
2811 startindex++;
2812 }
2813 } while (found == 1);
2814 }
2815
2816 /*
2817 * Save the new value and cleanup
2818 */
William M. Brack08171912003-12-29 02:52:11 +00002819error:
Owen Taylor3473f882001-02-23 17:55:21 +00002820 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2821 xmlXPathFreeObject(set);
2822 xmlXPathFreeObject(string);
2823 if (position) xmlXPathFreeObject(position);
2824 if (number) xmlXPathFreeObject(number);
2825}
2826
2827/**
2828 * xmlXPtrEvalRangePredicate:
2829 * @ctxt: the XPointer Parser context
2830 *
2831 * [8] Predicate ::= '[' PredicateExpr ']'
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002832 * [9] PredicateExpr ::= Expr
Owen Taylor3473f882001-02-23 17:55:21 +00002833 *
2834 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2835 * a Location Set instead of a node set
2836 */
2837void
2838xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2839 const xmlChar *cur;
2840 xmlXPathObjectPtr res;
2841 xmlXPathObjectPtr obj, tmp;
2842 xmlLocationSetPtr newset = NULL;
2843 xmlLocationSetPtr oldset;
2844 int i;
2845
Daniel Veillardce682bc2004-11-05 17:22:25 +00002846 if (ctxt == NULL) return;
2847
Owen Taylor3473f882001-02-23 17:55:21 +00002848 SKIP_BLANKS;
2849 if (CUR != '[') {
2850 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2851 }
2852 NEXT;
2853 SKIP_BLANKS;
2854
2855 /*
2856 * Extract the old set, and then evaluate the result of the
2857 * expression for all the element in the set. use it to grow
2858 * up a new set.
2859 */
2860 CHECK_TYPE(XPATH_LOCATIONSET);
2861 obj = valuePop(ctxt);
2862 oldset = obj->user;
2863 ctxt->context->node = NULL;
2864
2865 if ((oldset == NULL) || (oldset->locNr == 0)) {
2866 ctxt->context->contextSize = 0;
2867 ctxt->context->proximityPosition = 0;
2868 xmlXPathEvalExpr(ctxt);
2869 res = valuePop(ctxt);
2870 if (res != NULL)
2871 xmlXPathFreeObject(res);
2872 valuePush(ctxt, obj);
2873 CHECK_ERROR;
2874 } else {
2875 /*
2876 * Save the expression pointer since we will have to evaluate
2877 * it multiple times. Initialize the new set.
2878 */
2879 cur = ctxt->cur;
2880 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002881
Owen Taylor3473f882001-02-23 17:55:21 +00002882 for (i = 0; i < oldset->locNr; i++) {
2883 ctxt->cur = cur;
2884
2885 /*
2886 * Run the evaluation with a node list made of a single item
2887 * in the nodeset.
2888 */
2889 ctxt->context->node = oldset->locTab[i]->user;
2890 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2891 valuePush(ctxt, tmp);
2892 ctxt->context->contextSize = oldset->locNr;
2893 ctxt->context->proximityPosition = i + 1;
2894
2895 xmlXPathEvalExpr(ctxt);
2896 CHECK_ERROR;
2897
2898 /*
2899 * The result of the evaluation need to be tested to
2900 * decided whether the filter succeeded or not
2901 */
2902 res = valuePop(ctxt);
2903 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2904 xmlXPtrLocationSetAdd(newset,
2905 xmlXPathObjectCopy(oldset->locTab[i]));
2906 }
2907
2908 /*
2909 * Cleanup
2910 */
2911 if (res != NULL)
2912 xmlXPathFreeObject(res);
2913 if (ctxt->value == tmp) {
2914 res = valuePop(ctxt);
2915 xmlXPathFreeObject(res);
2916 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002917
Owen Taylor3473f882001-02-23 17:55:21 +00002918 ctxt->context->node = NULL;
2919 }
2920
2921 /*
2922 * The result is used as the new evaluation set.
2923 */
2924 xmlXPathFreeObject(obj);
2925 ctxt->context->node = NULL;
2926 ctxt->context->contextSize = -1;
2927 ctxt->context->proximityPosition = -1;
2928 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2929 }
2930 if (CUR != ']') {
2931 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2932 }
2933
2934 NEXT;
2935 SKIP_BLANKS;
2936}
2937
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002938#define bottom_xpointer
2939#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00002940#endif
2941