Coverage Report

Created: 2023-03-26 06:13

/src/libxslt/libexslt/common.c
Line
Count
Source (jump to first uncovered line)
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <libxml/tree.h>
5
#include <libxml/xpath.h>
6
#include <libxml/xpathInternals.h>
7
8
#include <libxslt/xsltutils.h>
9
#include <libxslt/xsltInternals.h>
10
#include <libxslt/extensions.h>
11
#include <libxslt/transform.h>
12
#include <libxslt/extra.h>
13
#include <libxslt/preproc.h>
14
15
#include "exslt.h"
16
17
static void
18
16.7k
exsltNodeSetFunction (xmlXPathParserContextPtr ctxt, int nargs) {
19
16.7k
    if (nargs != 1) {
20
67
  xmlXPathSetArityError(ctxt);
21
67
  return;
22
67
    }
23
16.7k
    if (xmlXPathStackIsNodeSet (ctxt)) {
24
15.4k
  xsltFunctionNodeSet (ctxt, nargs);
25
15.4k
  return;
26
15.4k
    } else {
27
1.30k
  xmlDocPtr fragment;
28
1.30k
  xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
29
1.30k
  xmlNodePtr txt;
30
1.30k
  xmlChar *strval;
31
1.30k
  xmlXPathObjectPtr obj;
32
  /*
33
  * SPEC EXSLT:
34
  * "You can also use this function to turn a string into a text
35
  * node, which is helpful if you want to pass a string to a
36
  * function that only accepts a node-set."
37
  */
38
1.30k
  fragment = xsltCreateRVT(tctxt);
39
1.30k
  if (fragment == NULL) {
40
3
      xsltTransformError(tctxt, NULL, tctxt->inst,
41
3
    "exsltNodeSetFunction: Failed to create a tree fragment.\n");
42
3
      tctxt->state = XSLT_STATE_STOPPED;
43
3
      return;
44
3
  }
45
1.30k
  xsltRegisterLocalRVT(tctxt, fragment);
46
47
1.30k
  strval = xmlXPathPopString (ctxt);
48
49
1.30k
  txt = xmlNewDocText (fragment, strval);
50
1.30k
  xmlAddChild((xmlNodePtr) fragment, txt);
51
1.30k
  obj = xmlXPathNewNodeSet(txt);
52
1.30k
  if (obj == NULL) {
53
2
      xsltTransformError(tctxt, NULL, tctxt->inst,
54
2
    "exsltNodeSetFunction: Failed to create a node set object.\n");
55
2
      tctxt->state = XSLT_STATE_STOPPED;
56
2
  }
57
1.30k
  if (strval != NULL)
58
1.29k
      xmlFree (strval);
59
60
1.30k
  valuePush (ctxt, obj);
61
1.30k
    }
62
16.7k
}
63
64
static void
65
27
exsltObjectTypeFunction (xmlXPathParserContextPtr ctxt, int nargs) {
66
27
    xmlXPathObjectPtr obj, ret;
67
68
27
    if (nargs != 1) {
69
1
  xmlXPathSetArityError(ctxt);
70
1
  return;
71
1
    }
72
73
26
    obj = valuePop(ctxt);
74
75
26
    switch (obj->type) {
76
8
    case XPATH_STRING:
77
8
  ret = xmlXPathNewCString("string");
78
8
  break;
79
3
    case XPATH_NUMBER:
80
3
  ret = xmlXPathNewCString("number");
81
3
  break;
82
2
    case XPATH_BOOLEAN:
83
2
  ret = xmlXPathNewCString("boolean");
84
2
  break;
85
5
    case XPATH_NODESET:
86
5
  ret = xmlXPathNewCString("node-set");
87
5
  break;
88
5
    case XPATH_XSLT_TREE:
89
5
  ret = xmlXPathNewCString("RTF");
90
5
  break;
91
3
    case XPATH_USERS:
92
3
  ret = xmlXPathNewCString("external");
93
3
  break;
94
0
    default:
95
0
  xsltGenericError(xsltGenericErrorContext,
96
0
    "object-type() invalid arg\n");
97
0
  ctxt->error = XPATH_INVALID_TYPE;
98
0
  xmlXPathFreeObject(obj);
99
0
  return;
100
26
    }
101
26
    xmlXPathFreeObject(obj);
102
26
    valuePush(ctxt, ret);
103
26
}
104
105
106
/**
107
 * exsltCommonRegister:
108
 *
109
 * Registers the EXSLT - Common module
110
 */
111
112
void
113
2
exsltCommonRegister (void) {
114
2
    xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
115
2
          EXSLT_COMMON_NAMESPACE,
116
2
          exsltNodeSetFunction);
117
2
    xsltRegisterExtModuleFunction((const xmlChar *) "object-type",
118
2
          EXSLT_COMMON_NAMESPACE,
119
2
          exsltObjectTypeFunction);
120
2
    xsltRegisterExtModuleElement((const xmlChar *) "document",
121
2
         EXSLT_COMMON_NAMESPACE,
122
2
         xsltDocumentComp,
123
2
         xsltDocumentElem);
124
2
}