andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 1 | /* |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Frerich Raabe <raabe@kde.org> |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 3 | * Copyright (C) 2006, 2009, 2013 Apple Inc. All rights reserved. |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 27 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 28 | #include "config.h" |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 29 | #include "XPathFunctions.h" |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 30 | |
darin | a51de91 | 2007-04-29 20:32:51 +0000 | [diff] [blame] | 31 | #include "Element.h" |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 32 | #include "ProcessingInstruction.h" |
rolandsteiner@chromium.org | 9d6b1e7 | 2011-05-03 17:17:44 +0000 | [diff] [blame] | 33 | #include "TreeScope.h" |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 34 | #include "XMLNames.h" |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 35 | #include "XPathUtil.h" |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 36 | #include <wtf/MathExtras.h> |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 37 | #include <wtf/NeverDestroyed.h> |
commit-queue@webkit.org | c28243e | 2011-09-07 05:43:09 +0000 | [diff] [blame] | 38 | #include <wtf/text/StringBuilder.h> |
sfalken | cd84600 | 2006-05-09 21:54:53 +0000 | [diff] [blame] | 39 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 40 | namespace WebCore { |
| 41 | namespace XPath { |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 42 | |
| 43 | static inline bool isWhitespace(UChar c) |
| 44 | { |
| 45 | return c == ' ' || c == '\n' || c == '\r' || c == '\t'; |
| 46 | } |
| 47 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 48 | #define DEFINE_FUNCTION_CREATOR(Suffix) static std::unique_ptr<Function> createFunction##Suffix() { return std::make_unique<Fun##Suffix>(); } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 49 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 50 | class Interval { |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 51 | public: |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 52 | static const int Inf = -1; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 53 | |
| 54 | Interval(); |
| 55 | Interval(int value); |
| 56 | Interval(int min, int max); |
| 57 | |
| 58 | bool contains(int value) const; |
| 59 | |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 60 | private: |
| 61 | int m_min; |
| 62 | int m_max; |
| 63 | }; |
| 64 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 65 | class FunLast final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 66 | virtual Value evaluate() const override; |
| 67 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 68 | public: |
| 69 | FunLast() { setIsContextSizeSensitive(true); } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 72 | class FunPosition final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 73 | virtual Value evaluate() const override; |
| 74 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 75 | public: |
| 76 | FunPosition() { setIsContextPositionSensitive(true); } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 79 | class FunCount final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 80 | virtual Value evaluate() const override; |
| 81 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 84 | class FunId final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 85 | virtual Value evaluate() const override; |
| 86 | virtual Value::Type resultType() const override { return Value::NodeSetValue; } |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 89 | class FunLocalName final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 90 | virtual Value evaluate() const override; |
| 91 | virtual Value::Type resultType() const override { return Value::StringValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 92 | public: |
| 93 | FunLocalName() { setIsContextNodeSensitive(true); } // local-name() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 96 | class FunNamespaceURI final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 97 | virtual Value evaluate() const override; |
| 98 | virtual Value::Type resultType() const override { return Value::StringValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 99 | public: |
| 100 | FunNamespaceURI() { setIsContextNodeSensitive(true); } // namespace-uri() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 103 | class FunName final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 104 | virtual Value evaluate() const override; |
| 105 | virtual Value::Type resultType() const override { return Value::StringValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 106 | public: |
| 107 | FunName() { setIsContextNodeSensitive(true); } // name() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 110 | class FunString final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 111 | virtual Value evaluate() const override; |
| 112 | virtual Value::Type resultType() const override { return Value::StringValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 113 | public: |
| 114 | FunString() { setIsContextNodeSensitive(true); } // string() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 117 | class FunConcat final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 118 | virtual Value evaluate() const override; |
| 119 | virtual Value::Type resultType() const override { return Value::StringValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 122 | class FunStartsWith final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 123 | virtual Value evaluate() const override; |
| 124 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 127 | class FunContains final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 128 | virtual Value evaluate() const override; |
| 129 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 132 | class FunSubstringBefore final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 133 | virtual Value evaluate() const override; |
| 134 | virtual Value::Type resultType() const override { return Value::StringValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 137 | class FunSubstringAfter final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 138 | virtual Value evaluate() const override; |
| 139 | virtual Value::Type resultType() const override { return Value::StringValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 142 | class FunSubstring final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 143 | virtual Value evaluate() const override; |
| 144 | virtual Value::Type resultType() const override { return Value::StringValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 147 | class FunStringLength final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 148 | virtual Value evaluate() const override; |
| 149 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 150 | public: |
| 151 | FunStringLength() { setIsContextNodeSensitive(true); } // string-length() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 154 | class FunNormalizeSpace final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 155 | virtual Value evaluate() const override; |
| 156 | virtual Value::Type resultType() const override { return Value::StringValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 157 | public: |
| 158 | FunNormalizeSpace() { setIsContextNodeSensitive(true); } // normalize-space() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 161 | class FunTranslate final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 162 | virtual Value evaluate() const override; |
| 163 | virtual Value::Type resultType() const override { return Value::StringValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 166 | class FunBoolean final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 167 | virtual Value evaluate() const override; |
| 168 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 171 | class FunNot : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 172 | virtual Value evaluate() const override; |
| 173 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 176 | class FunTrue final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 177 | virtual Value evaluate() const override; |
| 178 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 181 | class FunFalse final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 182 | virtual Value evaluate() const override; |
| 183 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 186 | class FunLang final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 187 | virtual Value evaluate() const override; |
| 188 | virtual Value::Type resultType() const override { return Value::BooleanValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 189 | public: |
| 190 | FunLang() { setIsContextNodeSensitive(true); } // lang() always works on context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 193 | class FunNumber final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 194 | virtual Value evaluate() const override; |
| 195 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 196 | public: |
| 197 | FunNumber() { setIsContextNodeSensitive(true); } // number() with no arguments uses context node. |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 200 | class FunSum final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 201 | virtual Value evaluate() const override; |
| 202 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 205 | class FunFloor final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 206 | virtual Value evaluate() const override; |
| 207 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 210 | class FunCeiling final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 211 | virtual Value evaluate() const override; |
| 212 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
andersca@apple.com | 16d2dd4 | 2014-01-16 23:08:24 +0000 | [diff] [blame] | 215 | class FunRound final : public Function { |
commit-queue@webkit.org | 2dfe6cb | 2014-01-16 19:42:50 +0000 | [diff] [blame] | 216 | virtual Value evaluate() const override; |
| 217 | virtual Value::Type resultType() const override { return Value::NumberValue; } |
ap | 15b514e | 2007-03-05 20:39:41 +0000 | [diff] [blame] | 218 | public: |
| 219 | static double round(double); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 222 | DEFINE_FUNCTION_CREATOR(Last) |
| 223 | DEFINE_FUNCTION_CREATOR(Position) |
| 224 | DEFINE_FUNCTION_CREATOR(Count) |
| 225 | DEFINE_FUNCTION_CREATOR(Id) |
| 226 | DEFINE_FUNCTION_CREATOR(LocalName) |
| 227 | DEFINE_FUNCTION_CREATOR(NamespaceURI) |
| 228 | DEFINE_FUNCTION_CREATOR(Name) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 229 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 230 | DEFINE_FUNCTION_CREATOR(String) |
| 231 | DEFINE_FUNCTION_CREATOR(Concat) |
| 232 | DEFINE_FUNCTION_CREATOR(StartsWith) |
| 233 | DEFINE_FUNCTION_CREATOR(Contains) |
| 234 | DEFINE_FUNCTION_CREATOR(SubstringBefore) |
| 235 | DEFINE_FUNCTION_CREATOR(SubstringAfter) |
| 236 | DEFINE_FUNCTION_CREATOR(Substring) |
| 237 | DEFINE_FUNCTION_CREATOR(StringLength) |
| 238 | DEFINE_FUNCTION_CREATOR(NormalizeSpace) |
| 239 | DEFINE_FUNCTION_CREATOR(Translate) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 240 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 241 | DEFINE_FUNCTION_CREATOR(Boolean) |
| 242 | DEFINE_FUNCTION_CREATOR(Not) |
| 243 | DEFINE_FUNCTION_CREATOR(True) |
| 244 | DEFINE_FUNCTION_CREATOR(False) |
| 245 | DEFINE_FUNCTION_CREATOR(Lang) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 246 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 247 | DEFINE_FUNCTION_CREATOR(Number) |
| 248 | DEFINE_FUNCTION_CREATOR(Sum) |
| 249 | DEFINE_FUNCTION_CREATOR(Floor) |
| 250 | DEFINE_FUNCTION_CREATOR(Ceiling) |
| 251 | DEFINE_FUNCTION_CREATOR(Round) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 252 | |
| 253 | #undef DEFINE_FUNCTION_CREATOR |
| 254 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 255 | inline Interval::Interval() |
| 256 | : m_min(Inf), m_max(Inf) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 257 | { |
| 258 | } |
| 259 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 260 | inline Interval::Interval(int value) |
| 261 | : m_min(value), m_max(value) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 262 | { |
| 263 | } |
| 264 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 265 | inline Interval::Interval(int min, int max) |
| 266 | : m_min(min), m_max(max) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 267 | { |
| 268 | } |
| 269 | |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 270 | inline bool Interval::contains(int value) const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 271 | { |
| 272 | if (m_min == Inf && m_max == Inf) |
| 273 | return true; |
| 274 | |
| 275 | if (m_min == Inf) |
| 276 | return value <= m_max; |
| 277 | |
| 278 | if (m_max == Inf) |
| 279 | return value >= m_min; |
| 280 | |
| 281 | return value >= m_min && value <= m_max; |
| 282 | } |
| 283 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 284 | void Function::setArguments(const String& name, Vector<std::unique_ptr<Expression>> arguments) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 285 | { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 286 | ASSERT(!subexpressionCount()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 287 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 288 | // Functions that use the context node as an implicit argument are context node sensitive when they |
| 289 | // have no arguments, but when explicit arguments are added, they are no longer context node sensitive. |
| 290 | // As of this writing, the only exception to this is the "lang" function. |
| 291 | if (name != "lang" && !arguments.isEmpty()) |
ap@webkit.org | 84b6819 | 2009-06-01 04:18:09 +0000 | [diff] [blame] | 292 | setIsContextNodeSensitive(false); |
| 293 | |
dbates@webkit.org | 0cefe4f | 2014-07-03 22:13:54 +0000 | [diff] [blame] | 294 | setSubexpressions(WTF::move(arguments)); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 295 | } |
| 296 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 297 | Value FunLast::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 298 | { |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 299 | return Expression::evaluationContext().size; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 300 | } |
| 301 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 302 | Value FunPosition::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 303 | { |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 304 | return Expression::evaluationContext().position; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 305 | } |
| 306 | |
darin@apple.com | ded18ec | 2014-03-22 18:52:22 +0000 | [diff] [blame] | 307 | static AtomicString atomicSubstring(StringBuilder& builder, unsigned start, unsigned length) |
| 308 | { |
| 309 | ASSERT(start <= builder.length()); |
| 310 | ASSERT(length <= builder.length() - start); |
| 311 | if (builder.is8Bit()) |
| 312 | return AtomicString(builder.characters8() + start, length); |
| 313 | return AtomicString(builder.characters16() + start, length); |
| 314 | } |
| 315 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 316 | Value FunId::evaluate() const |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 317 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 318 | Value a = argument(0).evaluate(); |
commit-queue@webkit.org | 585639b | 2014-01-13 06:26:50 +0000 | [diff] [blame] | 319 | StringBuilder idList; // A whitespace-separated list of IDs |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 320 | |
darin@apple.com | ded18ec | 2014-03-22 18:52:22 +0000 | [diff] [blame] | 321 | if (!a.isNodeSet()) |
| 322 | idList.append(a.toString()); |
| 323 | else { |
| 324 | for (auto& node : a.toNodeSet()) { |
| 325 | idList.append(stringValue(node.get())); |
commit-queue@webkit.org | 585639b | 2014-01-13 06:26:50 +0000 | [diff] [blame] | 326 | idList.append(' '); |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 327 | } |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 328 | } |
| 329 | |
akling@apple.com | 3eb4c41 | 2013-10-06 02:30:52 +0000 | [diff] [blame] | 330 | TreeScope& contextScope = evaluationContext().node->treeScope(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 331 | NodeSet result; |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 332 | HashSet<Node*> resultSet; |
| 333 | |
commit-queue@webkit.org | c28243e | 2011-09-07 05:43:09 +0000 | [diff] [blame] | 334 | unsigned startPos = 0; |
| 335 | unsigned length = idList.length(); |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 336 | while (true) { |
| 337 | while (startPos < length && isWhitespace(idList[startPos])) |
| 338 | ++startPos; |
| 339 | |
oliver | a0ee8b4 | 2007-10-12 15:16:31 +0000 | [diff] [blame] | 340 | if (startPos == length) |
| 341 | break; |
| 342 | |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 343 | size_t endPos = startPos; |
| 344 | while (endPos < length && !isWhitespace(idList[endPos])) |
| 345 | ++endPos; |
| 346 | |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 347 | // If there are several nodes with the same id, id() should return the first one. |
| 348 | // In WebKit, getElementById behaves so, too, although its behavior in this case is formally undefined. |
darin@apple.com | ded18ec | 2014-03-22 18:52:22 +0000 | [diff] [blame] | 349 | Node* node = contextScope.getElementById(atomicSubstring(idList, startPos, endPos - startPos)); |
caio.oliveira@openbossa.org | 4c11ee0 | 2012-03-29 18:48:23 +0000 | [diff] [blame] | 350 | if (node && resultSet.add(node).isNewEntry) |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 351 | result.append(node); |
| 352 | |
| 353 | startPos = endPos; |
| 354 | } |
| 355 | |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 356 | result.markSorted(false); |
| 357 | |
dbates@webkit.org | 0cefe4f | 2014-07-03 22:13:54 +0000 | [diff] [blame] | 358 | return Value(WTF::move(result)); |
ap | 2a65b4c | 2007-01-30 19:01:59 +0000 | [diff] [blame] | 359 | } |
| 360 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 361 | static inline String expandedNameLocalPart(Node* node) |
| 362 | { |
| 363 | // The local part of an XPath expanded-name matches DOM local name for most node types, except for namespace nodes and processing instruction nodes. |
| 364 | ASSERT(node->nodeType() != Node::XPATH_NAMESPACE_NODE); // Not supported yet. |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 365 | if (is<ProcessingInstruction>(*node)) |
cdumez@apple.com | 6992a60 | 2014-09-28 05:21:30 +0000 | [diff] [blame] | 366 | return downcast<ProcessingInstruction>(*node).target(); |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 367 | return node->localName().string(); |
| 368 | } |
| 369 | |
| 370 | static inline String expandedName(Node* node) |
| 371 | { |
| 372 | const AtomicString& prefix = node->prefix(); |
| 373 | return prefix.isEmpty() ? expandedNameLocalPart(node) : prefix + ":" + expandedNameLocalPart(node); |
| 374 | } |
| 375 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 376 | Value FunLocalName::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 377 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 378 | if (argumentCount() > 0) { |
| 379 | Value a = argument(0).evaluate(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 380 | if (!a.isNodeSet()) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 381 | return emptyString(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 382 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 383 | Node* node = a.toNodeSet().firstNode(); |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 384 | return node ? expandedNameLocalPart(node) : emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 385 | } |
| 386 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 387 | return expandedNameLocalPart(evaluationContext().node.get()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 388 | } |
| 389 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 390 | Value FunNamespaceURI::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 391 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 392 | if (argumentCount() > 0) { |
| 393 | Value a = argument(0).evaluate(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 394 | if (!a.isNodeSet()) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 395 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 396 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 397 | Node* node = a.toNodeSet().firstNode(); |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 398 | return node ? node->namespaceURI().string() : emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 399 | } |
| 400 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 401 | return evaluationContext().node->namespaceURI().string(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 402 | } |
| 403 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 404 | Value FunName::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 405 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 406 | if (argumentCount() > 0) { |
| 407 | Value a = argument(0).evaluate(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 408 | if (!a.isNodeSet()) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 409 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 410 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 411 | Node* node = a.toNodeSet().firstNode(); |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 412 | return node ? expandedName(node) : emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
ap@webkit.org | 68a5bc6 | 2009-06-02 17:59:09 +0000 | [diff] [blame] | 415 | return expandedName(evaluationContext().node.get()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 416 | } |
| 417 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 418 | Value FunCount::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 419 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 420 | Value a = argument(0).evaluate(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 421 | |
hausmann@webkit.org | a2f0b90 | 2008-01-18 09:46:07 +0000 | [diff] [blame] | 422 | return double(a.toNodeSet().size()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 423 | } |
| 424 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 425 | Value FunString::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 426 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 427 | if (!argumentCount()) |
ap | 142d046 | 2007-02-05 06:06:53 +0000 | [diff] [blame] | 428 | return Value(Expression::evaluationContext().node.get()).toString(); |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 429 | return argument(0).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 430 | } |
| 431 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 432 | Value FunConcat::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 433 | { |
commit-queue@webkit.org | c28243e | 2011-09-07 05:43:09 +0000 | [diff] [blame] | 434 | StringBuilder result; |
| 435 | result.reserveCapacity(1024); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 436 | |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 437 | for (unsigned i = 0, count = argumentCount(); i < count; ++i) { |
| 438 | String str(argument(i).evaluate().toString()); |
commit-queue@webkit.org | c28243e | 2011-09-07 05:43:09 +0000 | [diff] [blame] | 439 | result.append(str); |
ap | f0b81e0 | 2007-03-30 20:31:26 +0000 | [diff] [blame] | 440 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 441 | |
commit-queue@webkit.org | c28243e | 2011-09-07 05:43:09 +0000 | [diff] [blame] | 442 | return result.toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 443 | } |
| 444 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 445 | Value FunStartsWith::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 446 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 447 | String s1 = argument(0).evaluate().toString(); |
| 448 | String s2 = argument(1).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 449 | |
| 450 | if (s2.isEmpty()) |
| 451 | return true; |
| 452 | |
| 453 | return s1.startsWith(s2); |
| 454 | } |
| 455 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 456 | Value FunContains::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 457 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 458 | String s1 = argument(0).evaluate().toString(); |
| 459 | String s2 = argument(1).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 460 | |
| 461 | if (s2.isEmpty()) |
| 462 | return true; |
| 463 | |
| 464 | return s1.contains(s2) != 0; |
| 465 | } |
| 466 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 467 | Value FunSubstringBefore::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 468 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 469 | String s1 = argument(0).evaluate().toString(); |
| 470 | String s2 = argument(1).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 471 | |
| 472 | if (s2.isEmpty()) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 473 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 474 | |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 475 | size_t i = s1.find(s2); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 476 | |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 477 | if (i == notFound) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 478 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 479 | |
| 480 | return s1.left(i); |
| 481 | } |
| 482 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 483 | Value FunSubstringAfter::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 484 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 485 | String s1 = argument(0).evaluate().toString(); |
| 486 | String s2 = argument(1).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 487 | |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 488 | size_t i = s1.find(s2); |
| 489 | if (i == notFound) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 490 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 491 | |
oliver | 26d3e5b | 2007-10-12 15:36:14 +0000 | [diff] [blame] | 492 | return s1.substring(i + s2.length()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 495 | Value FunSubstring::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 496 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 497 | String s = argument(0).evaluate().toString(); |
| 498 | double doublePos = argument(1).evaluate().toNumber(); |
zandobersek@gmail.com | 9182d47 | 2013-02-13 23:01:21 +0000 | [diff] [blame] | 499 | if (std::isnan(doublePos)) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 500 | return emptyString(); |
benm@google.com | 0c112c0 | 2010-07-09 14:53:59 +0000 | [diff] [blame] | 501 | long pos = static_cast<long>(FunRound::round(doublePos)); |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 502 | bool haveLength = argumentCount() == 3; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 503 | long len = -1; |
ap | 15b514e | 2007-03-05 20:39:41 +0000 | [diff] [blame] | 504 | if (haveLength) { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 505 | double doubleLen = argument(2).evaluate().toNumber(); |
zandobersek@gmail.com | 9182d47 | 2013-02-13 23:01:21 +0000 | [diff] [blame] | 506 | if (std::isnan(doubleLen)) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 507 | return emptyString(); |
ap | 15b514e | 2007-03-05 20:39:41 +0000 | [diff] [blame] | 508 | len = static_cast<long>(FunRound::round(doubleLen)); |
| 509 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 510 | |
| 511 | if (pos > long(s.length())) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 512 | return emptyString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 513 | |
steveblock@google.com | fbe6b1a | 2010-07-12 10:42:38 +0000 | [diff] [blame] | 514 | if (pos < 1) { |
| 515 | if (haveLength) { |
| 516 | len -= 1 - pos; |
| 517 | if (len < 1) |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 518 | return emptyString(); |
steveblock@google.com | fbe6b1a | 2010-07-12 10:42:38 +0000 | [diff] [blame] | 519 | } |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 520 | pos = 1; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 521 | } |
| 522 | |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 523 | return s.substring(pos - 1, len); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 526 | Value FunStringLength::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 527 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 528 | if (!argumentCount()) |
ap | 142d046 | 2007-02-05 06:06:53 +0000 | [diff] [blame] | 529 | return Value(Expression::evaluationContext().node.get()).toString().length(); |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 530 | return argument(0).evaluate().toString().length(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 531 | } |
| 532 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 533 | Value FunNormalizeSpace::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 534 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 535 | if (!argumentCount()) { |
ap | 142d046 | 2007-02-05 06:06:53 +0000 | [diff] [blame] | 536 | String s = Value(Expression::evaluationContext().node.get()).toString(); |
ap | f0b81e0 | 2007-03-30 20:31:26 +0000 | [diff] [blame] | 537 | return s.simplifyWhiteSpace(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 540 | String s = argument(0).evaluate().toString(); |
ap | f0b81e0 | 2007-03-30 20:31:26 +0000 | [diff] [blame] | 541 | return s.simplifyWhiteSpace(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 544 | Value FunTranslate::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 545 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 546 | String s1 = argument(0).evaluate().toString(); |
| 547 | String s2 = argument(1).evaluate().toString(); |
| 548 | String s3 = argument(2).evaluate().toString(); |
abarth@webkit.org | 61a94cc | 2012-09-01 08:20:01 +0000 | [diff] [blame] | 549 | StringBuilder result; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 550 | |
| 551 | for (unsigned i1 = 0; i1 < s1.length(); ++i1) { |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 552 | UChar ch = s1[i1]; |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 553 | size_t i2 = s2.find(ch); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 554 | |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 555 | if (i2 == notFound) |
abarth@webkit.org | 61a94cc | 2012-09-01 08:20:01 +0000 | [diff] [blame] | 556 | result.append(ch); |
| 557 | else if (i2 < s3.length()) |
| 558 | result.append(s3[i2]); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 559 | } |
| 560 | |
abarth@webkit.org | 61a94cc | 2012-09-01 08:20:01 +0000 | [diff] [blame] | 561 | return result.toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 562 | } |
| 563 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 564 | Value FunBoolean::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 565 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 566 | return argument(0).evaluate().toBoolean(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 567 | } |
| 568 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 569 | Value FunNot::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 570 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 571 | return !argument(0).evaluate().toBoolean(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 572 | } |
| 573 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 574 | Value FunTrue::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 575 | { |
| 576 | return true; |
| 577 | } |
| 578 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 579 | Value FunLang::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 580 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 581 | String lang = argument(0).evaluate().toString(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 582 | |
cdumez@apple.com | a3bc43f | 2014-09-30 19:59:49 +0000 | [diff] [blame] | 583 | const Attribute* languageAttribute = nullptr; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 584 | Node* node = evaluationContext().node.get(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 585 | while (node) { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 586 | if (is<Element>(*node)) { |
cdumez@apple.com | a3bc43f | 2014-09-30 19:59:49 +0000 | [diff] [blame] | 587 | Element& element = downcast<Element>(*node); |
| 588 | if (element.hasAttributes()) |
| 589 | languageAttribute = element.findAttributeByName(XMLNames::langAttr); |
caio.oliveira@openbossa.org | 062edfb | 2012-02-02 00:20:36 +0000 | [diff] [blame] | 590 | } |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 591 | if (languageAttribute) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 592 | break; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 593 | node = node->parentNode(); |
| 594 | } |
| 595 | |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 596 | if (!languageAttribute) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 597 | return false; |
| 598 | |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 599 | String langValue = languageAttribute->value(); |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 600 | while (true) { |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 601 | if (equalIgnoringCase(langValue, lang)) |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 602 | return true; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 603 | |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 604 | // Remove suffixes one by one. |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 605 | size_t index = langValue.reverseFind('-'); |
| 606 | if (index == notFound) |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 607 | break; |
darin@apple.com | e8c3a2a | 2009-02-02 17:29:54 +0000 | [diff] [blame] | 608 | langValue = langValue.left(index); |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 609 | } |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 610 | |
ap | d50012d | 2007-03-05 05:34:10 +0000 | [diff] [blame] | 611 | return false; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 612 | } |
| 613 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 614 | Value FunFalse::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 615 | { |
| 616 | return false; |
| 617 | } |
| 618 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 619 | Value FunNumber::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 620 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 621 | if (!argumentCount()) |
ap | 335b4ab | 2007-02-07 18:58:04 +0000 | [diff] [blame] | 622 | return Value(Expression::evaluationContext().node.get()).toNumber(); |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 623 | return argument(0).evaluate().toNumber(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 624 | } |
| 625 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 626 | Value FunSum::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 627 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 628 | Value a = argument(0).evaluate(); |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 629 | if (!a.isNodeSet()) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 630 | return 0.0; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 631 | |
| 632 | double sum = 0.0; |
ap | ce79f36 | 2007-03-20 17:21:07 +0000 | [diff] [blame] | 633 | const NodeSet& nodes = a.toNodeSet(); |
| 634 | // To be really compliant, we should sort the node-set, as floating point addition is not associative. |
| 635 | // However, this is unlikely to ever become a practical issue, and sorting is slow. |
| 636 | |
commit-queue@webkit.org | 67f2be6 | 2015-05-19 16:29:37 +0000 | [diff] [blame] | 637 | for (auto& node : nodes) |
| 638 | sum += Value(stringValue(node.get())).toNumber(); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 639 | |
| 640 | return sum; |
| 641 | } |
| 642 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 643 | Value FunFloor::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 644 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 645 | return floor(argument(0).evaluate().toNumber()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 648 | Value FunCeiling::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 649 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 650 | return ceil(argument(0).evaluate().toNumber()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 651 | } |
| 652 | |
ap | 15b514e | 2007-03-05 20:39:41 +0000 | [diff] [blame] | 653 | double FunRound::round(double val) |
| 654 | { |
zandobersek@gmail.com | 9182d47 | 2013-02-13 23:01:21 +0000 | [diff] [blame] | 655 | if (!std::isnan(val) && !std::isinf(val)) { |
zandobersek@gmail.com | 8c24b7a | 2013-02-18 17:13:23 +0000 | [diff] [blame] | 656 | if (std::signbit(val) && val >= -0.5) |
ap | 15b514e | 2007-03-05 20:39:41 +0000 | [diff] [blame] | 657 | val *= 0; // negative zero |
| 658 | else |
| 659 | val = floor(val + 0.5); |
| 660 | } |
| 661 | return val; |
| 662 | } |
| 663 | |
weinig | 5112887 | 2007-03-08 03:47:34 +0000 | [diff] [blame] | 664 | Value FunRound::evaluate() const |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 665 | { |
andersca@apple.com | 9f50b2a | 2013-08-28 14:31:16 +0000 | [diff] [blame] | 666 | return round(argument(0).evaluate().toNumber()); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 667 | } |
| 668 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 669 | struct FunctionMapValue { |
| 670 | std::unique_ptr<Function> (*creationFunction)(); |
| 671 | Interval argumentCountInterval; |
eric@webkit.org | 4401549 | 2009-10-29 00:54:42 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 674 | static void populateFunctionMap(HashMap<String, FunctionMapValue>& functionMap) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 675 | { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 676 | struct FunctionMapping { |
| 677 | const char* name; |
| 678 | FunctionMapValue function; |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 679 | }; |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 680 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 681 | static const FunctionMapping functions[] = { |
| 682 | { "boolean", { createFunctionBoolean, 1 } }, |
| 683 | { "ceiling", { createFunctionCeiling, 1 } }, |
| 684 | { "concat", { createFunctionConcat, Interval(2, Interval::Inf) } }, |
| 685 | { "contains", { createFunctionContains, 2 } }, |
| 686 | { "count", { createFunctionCount, 1 } }, |
| 687 | { "false", { createFunctionFalse, 0 } }, |
| 688 | { "floor", { createFunctionFloor, 1 } }, |
| 689 | { "id", { createFunctionId, 1 } }, |
| 690 | { "lang", { createFunctionLang, 1 } }, |
| 691 | { "last", { createFunctionLast, 0 } }, |
| 692 | { "local-name", { createFunctionLocalName, Interval(0, 1) } }, |
| 693 | { "name", { createFunctionName, Interval(0, 1) } }, |
| 694 | { "namespace-uri", { createFunctionNamespaceURI, Interval(0, 1) } }, |
| 695 | { "normalize-space", { createFunctionNormalizeSpace, Interval(0, 1) } }, |
| 696 | { "not", { createFunctionNot, 1 } }, |
| 697 | { "number", { createFunctionNumber, Interval(0, 1) } }, |
| 698 | { "position", { createFunctionPosition, 0 } }, |
| 699 | { "round", { createFunctionRound, 1 } }, |
| 700 | { "starts-with", { createFunctionStartsWith, 2 } }, |
| 701 | { "string", { createFunctionString, Interval(0, 1) } }, |
| 702 | { "string-length", { createFunctionStringLength, Interval(0, 1) } }, |
| 703 | { "substring", { createFunctionSubstring, Interval(2, 3) } }, |
| 704 | { "substring-after", { createFunctionSubstringAfter, 2 } }, |
| 705 | { "substring-before", { createFunctionSubstringBefore, 2 } }, |
| 706 | { "sum", { createFunctionSum, 1 } }, |
| 707 | { "translate", { createFunctionTranslate, 3 } }, |
| 708 | { "true", { createFunctionTrue, 0 } }, |
| 709 | }; |
| 710 | |
commit-queue@webkit.org | 67f2be6 | 2015-05-19 16:29:37 +0000 | [diff] [blame] | 711 | for (auto& function : functions) |
| 712 | functionMap.add(function.name, function.function); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 713 | } |
| 714 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 715 | std::unique_ptr<Function> Function::create(const String& name, unsigned numArguments) |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 716 | { |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 717 | static NeverDestroyed<HashMap<String, FunctionMapValue>> functionMap; |
| 718 | if (functionMap.get().isEmpty()) |
| 719 | populateFunctionMap(functionMap); |
darin | a9406af | 2006-06-04 23:03:41 +0000 | [diff] [blame] | 720 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 721 | auto it = functionMap.get().find(name); |
| 722 | if (it == functionMap.get().end()) |
| 723 | return nullptr; |
ap | 335b4ab | 2007-02-07 18:58:04 +0000 | [diff] [blame] | 724 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 725 | if (!it->value.argumentCountInterval.contains(numArguments)) |
| 726 | return nullptr; |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 727 | |
darin@apple.com | 59755cf | 2013-10-10 02:46:27 +0000 | [diff] [blame] | 728 | return it->value.creationFunction(); |
| 729 | } |
| 730 | |
| 731 | std::unique_ptr<Function> Function::create(const String& name) |
| 732 | { |
| 733 | return create(name, 0); |
| 734 | } |
| 735 | |
| 736 | std::unique_ptr<Function> Function::create(const String& name, Vector<std::unique_ptr<Expression>> arguments) |
| 737 | { |
| 738 | std::unique_ptr<Function> function = create(name, arguments.size()); |
| 739 | if (function) |
dbates@webkit.org | 0cefe4f | 2014-07-03 22:13:54 +0000 | [diff] [blame] | 740 | function->setArguments(name, WTF::move(arguments)); |
andersca | 75fd42c | 2006-05-08 21:27:25 +0000 | [diff] [blame] | 741 | return function; |
| 742 | } |
| 743 | |
| 744 | } |
| 745 | } |