File: functions.cpp

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (902 lines) | stat: -rw-r--r-- 20,051 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
 * functions.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "functions.h"

#include "xml/dom_docimpl.h"
#include "xml/dom_nodeimpl.h"
#include "xml/dom_nodelistimpl.h"
#include "xml/dom_elementimpl.h"
#include "kjs/operations.h"

#include <QtDebug>

#include <math.h>

using namespace DOM;

namespace khtml
{
namespace XPath
{

#define DEFINE_FUNCTION_CREATOR(Class) \
    static Function *create##Class() { return new Class; }

class Interval
{
public:
    static const int Inf = -1;

    Interval();
    Interval(int value);
    Interval(int min, int max);

    bool contains(int value) const;

    QString asString() const;

private:
    int m_min;
    int m_max;
};

class FunLast : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

class FunPosition : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

class FunCount : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

// Base for various node-property functions, that have
// the same node picking logic. It passes the proper node,
// if any, or otherwise returns an empty string by itself
class NodeFunction : public Function
{
private:
    Value doEvaluate() const override;
    virtual Value evaluateOnNode(DOM::NodeImpl *node) const = 0;
};

class FunLocalName : public NodeFunction
{
public:
    bool isConstant() const override;

private:
    Value evaluateOnNode(DOM::NodeImpl *node) const override;
};

class FunNamespaceURI : public NodeFunction
{
public:
    bool isConstant() const override;

private:
    Value evaluateOnNode(DOM::NodeImpl *node) const override;
};

class FunName : public NodeFunction
{
public:
    bool isConstant() const override;

private:
    Value evaluateOnNode(DOM::NodeImpl *node) const override;
};

class FunId : public Function
{
private:
    Value doEvaluate() const override;
};

class FunString : public Function
{
private:
    Value doEvaluate() const override;
};

class FunConcat : public Function
{
private:
    Value doEvaluate() const override;
};

class FunStartsWith : public Function
{
private:
    Value doEvaluate() const override;
};

class FunContains : public Function
{
private:
    Value doEvaluate() const override;
};

class FunSubstringBefore : public Function
{
private:
    Value doEvaluate() const override;
};

class FunSubstringAfter : public Function
{
private:
    Value doEvaluate() const override;
};

class FunSubstring : public Function
{
private:
    Value doEvaluate() const override;
};

class FunStringLength : public Function
{
private:
    Value doEvaluate() const override;
};

class FunNormalizeSpace : public Function
{
private:
    Value doEvaluate() const override;
};

class FunTranslate : public Function
{
private:
    Value doEvaluate() const override;
};

class FunBoolean : public Function
{
private:
    Value doEvaluate() const override;
};

class FunNot : public Function
{
private:
    Value doEvaluate() const override;
};

class FunTrue : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

class FunFalse : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

class FunLang : public Function
{
public:
    bool isConstant() const override;

private:
    Value doEvaluate() const override;
};

class FunNumber : public Function
{
private:
    Value doEvaluate() const override;
};

class FunSum : public Function
{
private:
    Value doEvaluate() const override;
};

class FunFloor : public Function
{
private:
    Value doEvaluate() const override;
};

class FunCeiling : public Function
{
private:
    Value doEvaluate() const override;
};

class FunRound : public Function
{
private:
    Value doEvaluate() const override;
};

DEFINE_FUNCTION_CREATOR(FunLast)
DEFINE_FUNCTION_CREATOR(FunPosition)
DEFINE_FUNCTION_CREATOR(FunCount)
DEFINE_FUNCTION_CREATOR(FunLocalName)
DEFINE_FUNCTION_CREATOR(FunNamespaceURI)
DEFINE_FUNCTION_CREATOR(FunName)
DEFINE_FUNCTION_CREATOR(FunId)

DEFINE_FUNCTION_CREATOR(FunString)
DEFINE_FUNCTION_CREATOR(FunConcat)
DEFINE_FUNCTION_CREATOR(FunStartsWith)
DEFINE_FUNCTION_CREATOR(FunContains)
DEFINE_FUNCTION_CREATOR(FunSubstringBefore)
DEFINE_FUNCTION_CREATOR(FunSubstringAfter)
DEFINE_FUNCTION_CREATOR(FunSubstring)
DEFINE_FUNCTION_CREATOR(FunStringLength)
DEFINE_FUNCTION_CREATOR(FunNormalizeSpace)
DEFINE_FUNCTION_CREATOR(FunTranslate)

DEFINE_FUNCTION_CREATOR(FunBoolean)
DEFINE_FUNCTION_CREATOR(FunNot)
DEFINE_FUNCTION_CREATOR(FunTrue)
DEFINE_FUNCTION_CREATOR(FunFalse)
DEFINE_FUNCTION_CREATOR(FunLang)

DEFINE_FUNCTION_CREATOR(FunNumber)
DEFINE_FUNCTION_CREATOR(FunSum)
DEFINE_FUNCTION_CREATOR(FunFloor)
DEFINE_FUNCTION_CREATOR(FunCeiling)
DEFINE_FUNCTION_CREATOR(FunRound)

#undef DEFINE_FUNCTION_CREATOR

Interval::Interval()
    : m_min(Inf),
      m_max(Inf)
{
}

Interval::Interval(int value)
    : m_min(value),
      m_max(value)
{
}

Interval::Interval(int min, int max)
    : m_min(min),
      m_max(max)
{
}

bool Interval::contains(int value) const
{
    if (m_min == Inf && m_max == Inf) {
        return true;
    }

    if (m_min == Inf) {
        return value <= m_max;
    }

    if (m_max == Inf) {
        return value >= m_min;
    }

    return value >= m_min && value <= m_max;
}

QString Interval::asString() const
{
    QString s = "[";

    if (m_min == Inf) {
        s += "-Infinity";
    } else {
        s += QString::number(m_min);
    }

    s += "..";

    if (m_max == Inf) {
        s += "Infinity";
    } else {
        s += QString::number(m_max);
    }

    s += "]";

    return s;
}

void Function::setArguments(const QList<Expression *> &args)
{
    foreach (Expression *arg, args) {
        addSubExpression(arg);
    }
}

void Function::setName(const DOM::DOMString &name)
{
    m_name = name;
}

QString Function::dump() const
{
    if (argCount() == 0) {
        return QString("<function name=\"%1\"/>").arg(name().string());
    }

    QString s = QString("<function name=\"%1\">").arg(name().string());
    for (unsigned int i = 0; i < argCount(); ++i) {
        s += "<operand>" + arg(i)->dump() + "</operand>";
    }
    s += "</function>";
    return s;
}

Expression *Function::arg(int i)
{
    return subExpr(i);
}

const Expression *Function::arg(int i) const
{
    return subExpr(i);
}

unsigned int Function::argCount() const
{
    return subExprCount();
}

DOM::DOMString Function::name() const
{
    return m_name;
}

Value FunLast::doEvaluate() const
{
    return Value(double(Expression::evaluationContext().size));
}

bool FunLast::isConstant() const
{
    return false;
}

Value FunPosition::doEvaluate() const
{
    return Value(double(Expression::evaluationContext().position));
}

bool FunPosition::isConstant() const
{
    return false;
}

Value NodeFunction::doEvaluate() const
{
    NodeImpl *node = nullptr;
    if (argCount() > 0) {
        Value a = arg(0)->evaluate();
        if (a.isNodeset() && a.toNodeset()->length()) {
            node = a.toNodeset()->first();
        }
    } else {
        // no argument -> default to context node
        node = evaluationContext().node;
    }

    if (!node) {
        return Value(DOMString());
    }

    return evaluateOnNode(node);
}

bool FunLocalName::isConstant() const
{
    return false;
}

Value FunLocalName::evaluateOnNode(DOM::NodeImpl *node) const
{
    DOM::DOMString n;
    switch (node->nodeType()) {
    case Node::PROCESSING_INSTRUCTION_NODE:
        n = node->nodeName(); // target name
        break;
    default:
        n = node->localName();
    }
    return Value(n);
}

bool FunNamespaceURI::isConstant() const
{
    return false;
}

Value FunNamespaceURI::evaluateOnNode(DOM::NodeImpl *node) const
{
    return Value(node->namespaceURI());
}

Value FunId::doEvaluate() const
{
    Value a = arg(0)->evaluate();

    WTF::Vector<DOM::DOMString> ids;

    QString queryString; // whitespace-separated IDs
    if (a.isNodeset()) {
        DomNodeList set = a.toNodeset();
        for (unsigned long i = 0; i < set->length(); ++i) {
            queryString += stringValue(set->item(i)).string() + QLatin1Char(' ');
        }
    } else {
        queryString = a.toString().string();
    }

    QStringList qids = queryString.simplified().split(' ');
    for (int i = 0; i < qids.size(); ++i) {
        ids.append(DOM::DOMString(qids[i]));
    }

    DomNodeList out = new StaticNodeListImpl();
    DOM::DocumentImpl *doc = Expression::evaluationContext().node->document();

    for (unsigned i = 0; i < ids.size(); ++i) {
        DOM::ElementImpl *e = doc->getElementById(ids[i]);

        if (e) {
            out->append(e);
        }
    }

    return Value(out);
}

bool FunName::isConstant() const
{
    return false;
}

Value FunName::evaluateOnNode(DOM::NodeImpl *node) const
{
    DOM::DOMString n;
    switch (node->nodeType()) {
    case Node::TEXT_NODE:
    case Node::CDATA_SECTION_NODE:
    case Node::COMMENT_NODE:
    case Node::DOCUMENT_NODE:
        // All of these have an empty XPath name
        break;
    case Node::ELEMENT_NODE: {
        n = static_cast<DOM::ElementImpl *>(node)->nonCaseFoldedTagName();
        break;
    }
    default:
        n = node->nodeName();
    }
    return Value(n);
}

Value FunCount::doEvaluate() const
{
    Value a = arg(0)->evaluate();
    if (!a.isNodeset()) {
        Expression::reportInvalidExpressionErr();
        qCWarning(KHTML_LOG) << "count() expects <nodeset>";
        return Value();
    }
    a.toNodeset()->normalizeUpto(StaticNodeListImpl::AxisOrder);

    return Value(double(a.toNodeset()->length()));
}

bool FunCount::isConstant() const
{
    return false;
}

Value FunString::doEvaluate() const
{
    if (argCount() == 0) {
        DOMString s = Value(Expression::evaluationContext().node).toString();
        return Value(s);
    }
    return Value(arg(0)->evaluate().toString());
}

Value FunConcat::doEvaluate() const
{
    QString str;
    for (unsigned int i = 0; i < argCount(); ++i) {
        str.append(arg(i)->evaluate().toString().string());
    }
    return Value(DOMString(str));
}

Value FunStartsWith::doEvaluate() const
{
    DOMString s1 = arg(0)->evaluate().toString();
    DOMString s2 = arg(1)->evaluate().toString();

    if (s2.isEmpty()) {
        return Value(true);
    }

    return Value(s1.startsWith(s2));
}

Value FunContains::doEvaluate() const
{
    QString s1 = arg(0)->evaluate().toString().string();
    QString s2 = arg(1)->evaluate().toString().string();

    if (s2.isEmpty()) {
        return Value(true);
    }

    return Value(s1.contains(s2));
}

Value FunSubstringBefore::doEvaluate() const
{
    QString s1 = arg(0)->evaluate().toString().string();
    QString s2 = arg(1)->evaluate().toString().string();

    if (s2.isEmpty()) {
        return Value(DOMString());
    }

    int i = s1.indexOf(s2);
    if (i == -1) {
        return Value(DOMString());
    }

    return Value(DOMString(s1.left(i)));
}

Value FunSubstringAfter::doEvaluate() const
{
    QString s1 = arg(0)->evaluate().toString().string();
    QString s2 = arg(1)->evaluate().toString().string();

    if (s2.isEmpty()) {
        return Value(s1);
    }

    int i = s1.indexOf(s2);
    if (i == -1) {
        return Value(DOMString());
    }

    return Value(DOMString(s1.mid(i + s2.length())));
}

Value FunSubstring::doEvaluate() const
{
    QString s = arg(0)->evaluate().toString().string();
    long pos = long(qRound(arg(1)->evaluate().toNumber()));
    bool haveLength = argCount() == 3;
    long len = -1;
    if (haveLength) {
        len = long(qRound(arg(2)->evaluate().toNumber()));
    }

    if (pos > long(s.length())) {
        return Value(DOMString());
    }

    if (haveLength && pos < 1) {
        len -= 1 - pos;
        pos = 1;
        if (len < 1) {
            return Value(DOMString());
        }
    }

    return Value(DOMString(s.mid(pos - 1, len)));
}

Value FunStringLength::doEvaluate() const
{
    if (argCount() == 0) {
        DOMString s = Value(Expression::evaluationContext().node).toString();
        return Value(double(s.length()));
    }

    return Value(double(arg(0)->evaluate().toString().length()));
}

Value FunNormalizeSpace::doEvaluate() const
{
    if (argCount() == 0) {
        DOMString s = Value(Expression::evaluationContext().node).toString();
        return Value(DOMString(s.string().simplified()));
    }

    QString s = arg(0)->evaluate().toString().string();
    s = s.simplified();
    return Value(DOMString(s));
}

Value FunTranslate::doEvaluate() const
{
    QString s1 = arg(0)->evaluate().toString().string();
    QString s2 = arg(1)->evaluate().toString().string();
    QString s3 = arg(2)->evaluate().toString().string();
    QString newString;

    for (int i1 = 0; i1 < s1.length(); ++i1) {
        QChar ch = s1[ i1 ];
        int i2 = s2.indexOf(ch);
        if (i2 == -1) {
            newString += ch;
        } else if (i2 < s3.length()) {
            newString += s3[ i2 ];
        }
    }

    return Value(DOMString(newString));
}

Value FunBoolean::doEvaluate() const
{
    return Value(arg(0)->evaluate().toBoolean());
}

Value FunNot::doEvaluate() const
{
    return Value(!arg(0)->evaluate().toBoolean());
}

Value FunTrue::doEvaluate() const
{
    return Value(true);
}

bool FunTrue::isConstant() const
{
    return true;
}

#ifdef __GNUC__
#warning "This looks bogus"
#endif

Value FunLang::doEvaluate() const
{
    QString lang = arg(0)->evaluate().toString().string();

    NodeImpl *node = evaluationContext().node;

    DOMString langNodeValue;

    while (node) {
        if (node->isElementNode()) {
            langNodeValue = static_cast<ElementImpl *>(node)->getAttribute("xml:lang");
            if (!langNodeValue.isNull()) {
                break;
            }
        }
        node = xpathParentNode(node);
    }

    if (langNodeValue.isNull()) {
        return Value(false);
    }

    // extract 'en' out of 'en-us'
    QString langNodeValueString = langNodeValue.string();
    QString langNodeBaseString = langNodeValueString.left(langNodeValueString.indexOf('-'));

    return Value(langNodeValueString.toLower() == lang.toLower() ||
                 langNodeBaseString.toLower()  == lang.toLower());
}

bool FunLang::isConstant() const
{
    return false;
}

Value FunFalse::doEvaluate() const
{
    return Value(false);
}

bool FunFalse::isConstant() const
{
    return true;
}

Value FunNumber::doEvaluate() const
{
    Value vi;
    if (argCount() == 0) {
        // Spec'd: convert context node to singleton nodeset, call
        // string on that --> that's just stringValue on that node.
        // then we call number on that string
        vi = Value(stringValue(evaluationContext().node));
    } else {
        vi = arg(0)->evaluate();
    }

    return Value(vi.toNumber());
}

Value FunSum::doEvaluate() const
{
    Value a = arg(0)->evaluate();
    if (!a.isNodeset()) {
        Expression::reportInvalidExpressionErr();
        qCWarning(KHTML_LOG) << "sum() expects <nodeset>";
        return Value(0.0);
    }

    double sum = 0.0;
    const DomNodeList nodes = a.toNodeset();
    for (unsigned long n = 0; n < nodes->length(); ++n) {
        NodeImpl *node = nodes->item(n);
        sum += Value(stringValue(node)).toNumber();
    }
    return Value(sum);
}

Value FunFloor::doEvaluate() const
{
    const double num = arg(0)->evaluate().toNumber();

    if (KJS::isNaN(num) || KJS::isInf(num)) {
        return Value(num);
    }

    return Value(floor(num));
}

Value FunCeiling::doEvaluate() const
{
    const double num = arg(0)->evaluate().toNumber();

    if (KJS::isNaN(num) || KJS::isInf(num)) {
        return Value(num);
    }

    return Value(ceil(num));
}

Value FunRound::doEvaluate() const
{
    return Value(double(qRound(arg(0)->evaluate().toNumber())));
}

struct FunctionLibrary::FunctionRec {
    typedef Function *(*FactoryFn)();

    FactoryFn factoryFn;
    Interval args;
};

struct FunctionMapping {
    const char *name;
    FunctionLibrary::FunctionRec function;
};

static FunctionMapping functions[] = {
    { "last", { &createFunLast, 0 } },
    { "last", { &createFunLast, 0 } },
    { "position", { &createFunPosition, 0 } },
    { "count", { &createFunCount, 1 } },
    { "sum", { &createFunSum, 1 } },
    { "local-name", { &createFunLocalName, Interval(0, 1) } },
    { "namespace-uri", { &createFunNamespaceURI, Interval(0, 1) } },
    { "id",   { &createFunId, 1 } },
    { "name", { &createFunName, Interval(0, 1) } },

    { "string", { &createFunString, Interval(0, 1) } },
    { "concat", { &createFunConcat, Interval(2, Interval::Inf) } },
    { "starts-with", { &createFunStartsWith, 2 } },
    { "contains", { &createFunContains, 2 } },
    { "substring-before", { &createFunSubstringBefore, 2 } },
    { "substring-after", { &createFunSubstringAfter, 2 } },
    { "substring", { &createFunSubstring, Interval(2, 3) } },
    { "string-length", { &createFunStringLength, Interval(0, 1) } },
    { "normalize-space", { &createFunNormalizeSpace, Interval(0, 1) } },
    { "translate", { &createFunTranslate, 3 } },

    { "boolean", { &createFunBoolean, 1 } },
    { "not", { &createFunNot, 1 } },
    { "true", { &createFunTrue, 0 } },
    { "false", { &createFunFalse, 0 } },
    { "lang", { &createFunLang, 1 } },

    { "number", { &createFunNumber, Interval(0, 1) } },
    { "floor", { &createFunFloor, 1 } },
    { "ceiling", { &createFunCeiling, 1 } },
    { "round", { &createFunRound, 1 } }
};
static const unsigned int numFunctions = sizeof(functions) / sizeof(functions[ 0 ]);

FunctionLibrary &FunctionLibrary::self()
{
    static FunctionLibrary instance;
    return instance;
}

FunctionLibrary::FunctionLibrary()
{
    for (unsigned int i = 0; i < numFunctions; ++i) {
        m_functionDict.insert(functions[ i ].name, functions[ i ].function);
    }
}

Function *FunctionLibrary::getFunction(const DOM::DOMString &name,
                                       const QList<Expression *> &args) const
{
    if (!m_functionDict.contains(name)) {
        qCWarning(KHTML_LOG) << "Function '" << name << "' not supported by this implementation.";

        return nullptr;
    }

    FunctionRec functionRec = m_functionDict[ name ];
    if (!functionRec.args.contains(args.count())) {
        qCWarning(KHTML_LOG) << "Function '" << name << "' requires " << functionRec.args.asString() << " arguments, but " << args.count() << " given.";
        return nullptr;
    }

    Function *function = functionRec.factoryFn();
    function->setArguments(args);
    function->setName(name);
    return function;
}

} //namespace XPath
} //namespace khtml