| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/xml/XPathFunctions.h" | 5 #include "core/xml/XPathFunctions.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/xml/XPathExpressionNode.h" // EvaluationContext | 8 #include "core/xml/XPathExpressionNode.h" // EvaluationContext |
| 9 #include "core/xml/XPathPredicate.h" // Number, StringExpression | 9 #include "core/xml/XPathPredicate.h" // Number, StringExpression |
| 10 #include "core/xml/XPathValue.h" | 10 #include "core/xml/XPathValue.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 static String substring(XPathArguments& args) { | 38 static String substring(XPathArguments& args) { |
| 39 XPathContext xpath; | 39 XPathContext xpath; |
| 40 XPath::Expression* call = XPath::createFunction("substring", args); | 40 XPath::Expression* call = XPath::createFunction("substring", args); |
| 41 XPath::Value result = call->evaluate(xpath.context()); | 41 XPath::Value result = call->evaluate(xpath.context()); |
| 42 return result.toString(); | 42 return result.toString(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 static String substring(const char* string, double pos) { | 45 static String substring(const char* string, double pos) { |
| 46 XPathArguments args; | 46 XPathArguments args; |
| 47 args.append(new XPath::StringExpression(string)); | 47 args.push_back(new XPath::StringExpression(string)); |
| 48 args.append(new XPath::Number(pos)); | 48 args.push_back(new XPath::Number(pos)); |
| 49 return substring(args); | 49 return substring(args); |
| 50 } | 50 } |
| 51 | 51 |
| 52 static String substring(const char* string, double pos, double len) { | 52 static String substring(const char* string, double pos, double len) { |
| 53 XPathArguments args; | 53 XPathArguments args; |
| 54 args.append(new XPath::StringExpression(string)); | 54 args.push_back(new XPath::StringExpression(string)); |
| 55 args.append(new XPath::Number(pos)); | 55 args.push_back(new XPath::Number(pos)); |
| 56 args.append(new XPath::Number(len)); | 56 args.push_back(new XPath::Number(len)); |
| 57 return substring(args); | 57 return substring(args); |
| 58 } | 58 } |
| 59 | 59 |
| 60 } // namespace | 60 } // namespace |
| 61 | 61 |
| 62 TEST(XPathFunctionsTest, substring_specExamples) { | 62 TEST(XPathFunctionsTest, substring_specExamples) { |
| 63 EXPECT_EQ(" car", substring("motor car", 6.0)) | 63 EXPECT_EQ(" car", substring("motor car", 6.0)) |
| 64 << "should select characters staring at position 6 to the end"; | 64 << "should select characters staring at position 6 to the end"; |
| 65 EXPECT_EQ("ada", substring("metadata", 4.0, 3.0)) | 65 EXPECT_EQ("ada", substring("metadata", 4.0, 3.0)) |
| 66 << "should select characters at 4 <= position < 7"; | 66 << "should select characters at 4 <= position < 7"; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 TEST(XPathFunctionsTest, substring_extremePositionLength) { | 117 TEST(XPathFunctionsTest, substring_extremePositionLength) { |
| 118 EXPECT_EQ("", substring("no way", 1e100, 7.0)) | 118 EXPECT_EQ("", substring("no way", 1e100, 7.0)) |
| 119 << "extremely large positions should result in the empty string"; | 119 << "extremely large positions should result in the empty string"; |
| 120 | 120 |
| 121 EXPECT_EQ("no way", substring("no way", -1e200, 1e300)) | 121 EXPECT_EQ("no way", substring("no way", -1e200, 1e300)) |
| 122 << "although these indices are not representable as long, this should " | 122 << "although these indices are not representable as long, this should " |
| 123 << "produce the string because indices are computed as doubles"; | 123 << "produce the string because indices are computed as doubles"; |
| 124 } | 124 } |
| 125 | 125 |
| 126 } // namespace blink | 126 } // namespace blink |
| OLD | NEW |