1 /* 2 * Copyright 2005 Frerich Raabe <raabe (at) kde.org> 3 * Copyright (C) 2006 Apple Computer, Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "core/xml/XPathValue.h" 29 30 #include <limits> 31 #include "core/xml/XPathExpressionNode.h" 32 #include "core/xml/XPathUtil.h" 33 #include "wtf/MathExtras.h" 34 #include "wtf/StdLibExtras.h" 35 36 using std::numeric_limits; 37 38 namespace WebCore { 39 namespace XPath { 40 41 const Value::AdoptTag Value::adopt = {}; 42 43 const NodeSet& Value::toNodeSet() const 44 { 45 if (!isNodeSet()) 46 Expression::evaluationContext().hadTypeConversionError = true; 47 48 if (!m_data) { 49 DEFINE_STATIC_LOCAL(NodeSet, emptyNodeSet, ()); 50 return emptyNodeSet; 51 } 52 53 return m_data->m_nodeSet; 54 } 55 56 NodeSet& Value::modifiableNodeSet() 57 { 58 if (!isNodeSet()) 59 Expression::evaluationContext().hadTypeConversionError = true; 60 61 if (!m_data) 62 m_data = ValueData::create(); 63 64 m_type = NodeSetValue; 65 return m_data->m_nodeSet; 66 } 67 68 bool Value::toBoolean() const 69 { 70 switch (m_type) { 71 case NodeSetValue: 72 return !m_data->m_nodeSet.isEmpty(); 73 case BooleanValue: 74 return m_bool; 75 case NumberValue: 76 return m_number && !std::isnan(m_number); 77 case StringValue: 78 return !m_data->m_string.isEmpty(); 79 } 80 ASSERT_NOT_REACHED(); 81 return false; 82 } 83 84 double Value::toNumber() const 85 { 86 switch (m_type) { 87 case NodeSetValue: 88 return Value(toString()).toNumber(); 89 case NumberValue: 90 return m_number; 91 case StringValue: { 92 const String& str = m_data->m_string.simplifyWhiteSpace(); 93 94 // String::toDouble() supports exponential notation, which is not allowed in XPath. 95 unsigned len = str.length(); 96 for (unsigned i = 0; i < len; ++i) { 97 UChar c = str[i]; 98 if (!isASCIIDigit(c) && c != '.' && c != '-') 99 return numeric_limits<double>::quiet_NaN(); 100 } 101 102 bool canConvert; 103 double value = str.toDouble(&canConvert); 104 if (canConvert) 105 return value; 106 return numeric_limits<double>::quiet_NaN(); 107 } 108 case BooleanValue: 109 return m_bool; 110 } 111 ASSERT_NOT_REACHED(); 112 return 0.0; 113 } 114 115 String Value::toString() const 116 { 117 switch (m_type) { 118 case NodeSetValue: 119 if (m_data->m_nodeSet.isEmpty()) 120 return ""; 121 return stringValue(m_data->m_nodeSet.firstNode()); 122 case StringValue: 123 return m_data->m_string; 124 case NumberValue: 125 if (std::isnan(m_number)) 126 return "NaN"; 127 if (m_number == 0) 128 return "0"; 129 if (std::isinf(m_number)) 130 return std::signbit(m_number) ? "-Infinity" : "Infinity"; 131 return String::number(m_number); 132 case BooleanValue: 133 return m_bool ? "true" : "false"; 134 } 135 ASSERT_NOT_REACHED(); 136 return String(); 137 } 138 139 } 140 } 141