1 /**
2  * Copyright (C) 2001-2002 Thomas Broyer, Charlie Bozeman and Daniel Veillard.
3  * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is fur-
10  * nished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
17  * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
20  * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Except as contained in this notice, the name of the authors shall not
23  * be used in advertising or otherwise to promote the sale, use or other deal-
24  * ings in this Software without prior written authorization from him.
25  */
26 
27 #include "config.h"
28 
29 #if ENABLE(XSLT)
30 #include "XSLTExtensions.h"
31 
32 #include <libxml/xpathInternals.h>
33 
34 #include <libxslt/xsltutils.h>
35 #include <libxslt/extensions.h>
36 #include <libxslt/extra.h>
37 
38 #if PLATFORM(MAC)
39 #include "SoftLinking.h"
40 #endif
41 
42 #if PLATFORM(MAC)
43 SOFT_LINK_LIBRARY(libxslt)
44 SOFT_LINK(libxslt, xsltRegisterExtFunction, int, (xsltTransformContextPtr ctxt, const xmlChar *name, const xmlChar *URI, xmlXPathFunction function), (ctxt, name, URI, function))
45 SOFT_LINK(libxslt, xsltFunctionNodeSet, void, (xmlXPathParserContextPtr ctxt, int nargs), (ctxt, nargs))
46 #endif
47 
48 namespace WebCore {
49 
50 // FIXME: This code is taken from libexslt 1.1.11; should sync with newer versions.
exsltNodeSetFunction(xmlXPathParserContextPtr ctxt,int nargs)51 static void exsltNodeSetFunction(xmlXPathParserContextPtr ctxt, int nargs)
52 {
53     xmlChar *strval;
54     xmlNodePtr retNode;
55     xmlXPathObjectPtr ret;
56 
57     if (nargs != 1) {
58         xmlXPathSetArityError(ctxt);
59         return;
60     }
61 
62     if (xmlXPathStackIsNodeSet(ctxt)) {
63         xsltFunctionNodeSet(ctxt, nargs);
64         return;
65     }
66 
67     strval = xmlXPathPopString(ctxt);
68     retNode = xmlNewDocText(NULL, strval);
69     ret = xmlXPathNewValueTree(retNode);
70 
71     // FIXME: It might be helpful to push any errors from xmlXPathNewValueTree
72     // up to the Javascript Console.
73     if (ret != NULL)
74         ret->type = XPATH_NODESET;
75 
76     if (strval != NULL)
77         xmlFree(strval);
78 
79     valuePush(ctxt, ret);
80 }
81 
registerXSLTExtensions(xsltTransformContextPtr ctxt)82 void registerXSLTExtensions(xsltTransformContextPtr ctxt)
83 {
84     xsltRegisterExtFunction(ctxt, (const xmlChar*)"node-set", (const xmlChar*)"http://exslt.org/common", exsltNodeSetFunction);
85 }
86 
87 }
88 
89 #endif
90