Changeset View
Changeset View
Standalone View
Standalone View
core/script/kjs_util.cpp
Show All 9 Lines | |||||
10 | 10 | | |||
11 | #include "kjs_util_p.h" | 11 | #include "kjs_util_p.h" | ||
12 | 12 | | |||
13 | #include <kjs/kjsobject.h> | 13 | #include <kjs/kjsobject.h> | ||
14 | #include <kjs/kjsprototype.h> | 14 | #include <kjs/kjsprototype.h> | ||
15 | #include <kjs/kjsarguments.h> | 15 | #include <kjs/kjsarguments.h> | ||
16 | 16 | | |||
17 | #include <QUrl> | 17 | #include <QUrl> | ||
18 | #include <QLocale> | ||||
19 | | ||||
20 | #include <cmath> | ||||
18 | 21 | | |||
19 | using namespace Okular; | 22 | using namespace Okular; | ||
20 | 23 | | |||
21 | static KJSPrototype *g_utilProto; | 24 | static KJSPrototype *g_utilProto; | ||
22 | 25 | | |||
23 | static KJSObject crackURL( KJSContext *context, void *, | 26 | static KJSObject crackURL( KJSContext *context, void *, | ||
24 | const KJSArguments &arguments ) | 27 | const KJSArguments &arguments ) | ||
25 | { | 28 | { | ||
Show All 25 Lines | |||||
51 | // TODO cPath (Optional) The path portion of the URL. | 54 | // TODO cPath (Optional) The path portion of the URL. | ||
52 | // TODO cParameters (Optional) The parameter string portion of the URL. | 55 | // TODO cParameters (Optional) The parameter string portion of the URL. | ||
53 | if ( url.hasFragment() ) | 56 | if ( url.hasFragment() ) | ||
54 | obj.setProperty( context, QStringLiteral("cFragments"), url.fragment(QUrl::FullyDecoded) ); | 57 | obj.setProperty( context, QStringLiteral("cFragments"), url.fragment(QUrl::FullyDecoded) ); | ||
55 | 58 | | |||
56 | return obj; | 59 | return obj; | ||
57 | } | 60 | } | ||
58 | 61 | | |||
62 | /** Converts a Number to a String using l10n | ||||
63 | * | ||||
64 | * String numberToString( Number number, String format = 'g', int precision = 6, | ||||
65 | * String LocaleName = system ) | ||||
66 | */ | ||||
67 | static KJSObject numberToString ( KJSContext *context, void *, | ||||
68 | const KJSArguments &arguments ) | ||||
69 | { | ||||
70 | if ( arguments.count() < 1 ) | ||||
71 | { | ||||
72 | return context->throwException( QStringLiteral( "Invalid arguments" ) ); | ||||
73 | } | ||||
74 | | ||||
75 | double number = arguments.at( 0 ).toNumber( context ); | ||||
76 | if ( std::isnan( number ) ) | ||||
77 | { | ||||
78 | return KJSString( "NaN" ); | ||||
79 | } | ||||
80 | | ||||
81 | QChar format = QLatin1Char( 'g' ); | ||||
82 | if ( arguments.count() >= 2 ) | ||||
83 | { | ||||
84 | const QString fmt = arguments.at( 1 ).toString( context ); | ||||
85 | if ( fmt.size() ) | ||||
86 | { | ||||
87 | format = fmt[0]; | ||||
88 | } | ||||
89 | } | ||||
90 | | ||||
91 | int precision = 6; | ||||
92 | if ( arguments.count() >= 3 ) | ||||
93 | { | ||||
94 | precision = arguments.at( 2 ).toInt32( context ); | ||||
95 | } | ||||
96 | | ||||
97 | QLocale locale; | ||||
98 | if ( arguments.count() == 4 ) | ||||
99 | { | ||||
100 | locale = QLocale( arguments.at( 3 ).toString( context ) ); | ||||
101 | } | ||||
102 | | ||||
103 | return KJSString( locale.toString( number, format.toLatin1(), precision ) ); | ||||
104 | } | ||||
105 | | ||||
106 | /** Converts a String to a Number using l10n. | ||||
107 | * | ||||
108 | * Number stringToNumber( String number, String LocaleName = system ) */ | ||||
109 | static KJSObject stringToNumber ( KJSContext *context, void *, | ||||
110 | const KJSArguments &arguments ) | ||||
111 | { | ||||
112 | if ( arguments.count() < 1 ) | ||||
113 | { | ||||
114 | return context->throwException( QStringLiteral( "Invalid arguments" ) ); | ||||
115 | } | ||||
116 | | ||||
117 | QString number = arguments.at( 0 ).toString( context ); | ||||
118 | if ( number.isEmpty() ) | ||||
119 | { | ||||
120 | return KJSNumber( 0 ); | ||||
121 | } | ||||
122 | | ||||
123 | QLocale locale; | ||||
124 | if ( arguments.count() == 2 ) | ||||
125 | { | ||||
126 | locale = QLocale( arguments.at( 1 ).toString( context ) ); | ||||
127 | } | ||||
128 | | ||||
129 | bool ok; | ||||
130 | double converted = locale.toDouble( number, &ok ); | ||||
131 | | ||||
132 | if ( !ok ) | ||||
133 | { | ||||
134 | return KJSNumber( std::nan( "" ) ); | ||||
135 | } | ||||
136 | | ||||
137 | return KJSNumber( converted ); | ||||
138 | } | ||||
139 | | ||||
59 | void JSUtil::initType( KJSContext *ctx ) | 140 | void JSUtil::initType( KJSContext *ctx ) | ||
60 | { | 141 | { | ||
61 | static bool initialized = false; | 142 | static bool initialized = false; | ||
62 | if ( initialized ) | 143 | if ( initialized ) | ||
63 | return; | 144 | return; | ||
64 | initialized = true; | 145 | initialized = true; | ||
65 | 146 | | |||
66 | g_utilProto = new KJSPrototype(); | 147 | g_utilProto = new KJSPrototype(); | ||
67 | g_utilProto->defineFunction( ctx, QStringLiteral("crackURL"), crackURL ); | 148 | g_utilProto->defineFunction( ctx, QStringLiteral( "crackURL" ), crackURL ); | ||
149 | g_utilProto->defineFunction( ctx, QStringLiteral( "stringToNumber" ), stringToNumber ); | ||||
150 | g_utilProto->defineFunction( ctx, QStringLiteral( "numberToString" ), numberToString ); | ||||
68 | } | 151 | } | ||
69 | 152 | | |||
70 | KJSObject JSUtil::object( KJSContext *ctx ) | 153 | KJSObject JSUtil::object( KJSContext *ctx ) | ||
71 | { | 154 | { | ||
72 | return g_utilProto->constructObject( ctx, nullptr ); | 155 | return g_utilProto->constructObject( ctx, nullptr ); | ||
73 | } | 156 | } | ||
74 | 157 | |