summaryrefslogtreecommitdiff
path: root/fxjs/cjs_color.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-10-23 21:38:28 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-10-23 21:38:28 +0000
commit5ff1ddeb54a425e1af25607646697e93a39bd51d (patch)
tree31266c1e22ab146883cc165bc7925a341ce28a9d /fxjs/cjs_color.cpp
parent401077e47c49c3b1cb865ee6f1f29a931a6ca45b (diff)
downloadpdfium-5ff1ddeb54a425e1af25607646697e93a39bd51d.tar.xz
Test color.convert() and equal() from JS (and fix comparison logic).
Currently, color.equal(a, b) may not give the same result as color.equal(b, a) since arg1 is converted to be the type of arg2, and some of these conversions lose information. Instead promote to the type with the most components in the hope of preserving the most information. Better error message when there are the right number of parameters but the types are wrong. Change-Id: I1d93fa29db4fb65e0f7c07c3ba7d9ca87ebf7bc9 Reviewed-on: https://pdfium-review.googlesource.com/c/44413 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fxjs/cjs_color.cpp')
-rw-r--r--fxjs/cjs_color.cpp20
1 files changed, 14 insertions, 6 deletions
diff --git a/fxjs/cjs_color.cpp b/fxjs/cjs_color.cpp
index 7ce5c2937c..0d4065eb6a 100644
--- a/fxjs/cjs_color.cpp
+++ b/fxjs/cjs_color.cpp
@@ -6,6 +6,7 @@
#include "fxjs/cjs_color.h"
+#include <algorithm>
#include <vector>
#include "core/fxge/cfx_color.h"
@@ -271,10 +272,12 @@ CJS_Result CJS_Color::SetPropertyHelper(CJS_Runtime* pRuntime,
CJS_Result CJS_Color::convert(CJS_Runtime* pRuntime,
const std::vector<v8::Local<v8::Value>>& params) {
- int iSize = params.size();
- if (iSize < 2 || params[0].IsEmpty() || !params[0]->IsArray())
+ if (params.size() < 2)
return CJS_Result::Failure(JSMessage::kParamError);
+ if (params[0].IsEmpty() || !params[0]->IsArray())
+ return CJS_Result::Failure(JSMessage::kTypeError);
+
WideString sDestSpace = pRuntime->ToWideString(params[1]);
int nColorType = CFX_Color::kTransparent;
if (sDestSpace == L"T")
@@ -298,9 +301,12 @@ CJS_Result CJS_Color::convert(CJS_Runtime* pRuntime,
CJS_Result CJS_Color::equal(CJS_Runtime* pRuntime,
const std::vector<v8::Local<v8::Value>>& params) {
- if (params.size() < 2 || params[0].IsEmpty() || !params[0]->IsArray() ||
- params[1].IsEmpty() || !params[1]->IsArray()) {
+ if (params.size() < 2)
return CJS_Result::Failure(JSMessage::kParamError);
+
+ if (params[0].IsEmpty() || !params[0]->IsArray() || params[1].IsEmpty() ||
+ !params[1]->IsArray()) {
+ return CJS_Result::Failure(JSMessage::kTypeError);
}
CFX_Color color1 =
@@ -308,6 +314,8 @@ CJS_Result CJS_Color::equal(CJS_Runtime* pRuntime,
CFX_Color color2 =
ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[1]));
- color1 = color1.ConvertColorType(color2.nColorType);
- return CJS_Result::Success(pRuntime->NewBoolean(color1 == color2));
+ // Relies on higher values having more components.
+ int32_t best = std::max(color1.nColorType, color2.nColorType);
+ return CJS_Result::Success(pRuntime->NewBoolean(
+ color1.ConvertColorType(best) == color2.ConvertColorType(best)));
}