Skip to content

Commit fec925d

Browse files
authored
Fix crash when null colors are passed (#218)
JObject throws when checking for a null or undefined value. Instead of throwing we check for this and do not include the value allow the application to continue.
1 parent 44598d1 commit fec925d

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-pspdfkit",
3-
"version": "1.23.8",
3+
"version": "1.23.9",
44
"description": "A React Native module for the PSPDFKit library.",
55
"keywords": [
66
"react native",

samples/Catalog/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Catalog",
3-
"version": "1.23.8",
3+
"version": "1.23.9",
44
"private": true,
55
"scripts": {
66
"start": "node node_modules/react-native/local-cli/cli.js start"

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/JsonUtils.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Collections.Generic;
2+
using Windows.UI;
23
using Newtonsoft.Json.Linq;
34
using PSPDFKit.Search;
45
using PSPDFKitFoundation;
56
using PSPDFKitFoundation.Search;
67
using PSPDFKitNative;
8+
using ReactNative.UIManager;
79

810
namespace ReactNativePSPDFKit
911
{
@@ -131,5 +133,20 @@ private static JArray LibraryQueryReultToJson(LibraryQueryResult libraryQueryRes
131133

132134
return pageNumbersJson;
133135
}
136+
137+
internal static Color? ParserColor(JObject jObject, string propertyName)
138+
{
139+
if (TryGetNotNullValue(jObject, propertyName, out var jsonHighlightColor))
140+
{
141+
return ColorHelpers.Parse(jsonHighlightColor.Value<uint>());
142+
}
143+
144+
return null;
145+
}
146+
147+
internal static bool TryGetNotNullValue(JObject jObject, string propertyName, out JToken value)
148+
{
149+
return jObject.TryGetValue(propertyName, out value) && value.Type != JTokenType.Null;
150+
}
134151
}
135152
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitViewManager.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,23 @@ public void SetHideNavigationBar(PDFViewPage view, bool hideNavigationBar)
6363
public async void SetCustomCss(PDFViewPage view, JObject styleJObject)
6464
{
6565
var colorString = string.Empty;
66-
if (styleJObject.ContainsKey("highlightColor"))
66+
67+
var maybeHighlightColor = JsonUtils.ParserColor(styleJObject, "highlightColor");
68+
if (maybeHighlightColor.HasValue)
6769
{
68-
var highlightColor = ColorHelpers.Parse(styleJObject["highlightColor"].Value<uint>());
69-
colorString += $" --primary: {highlightColor.ToHexWithoutAlpha()};\r\n";
70+
colorString += $" --primary: {maybeHighlightColor.Value.ToHexWithoutAlpha()};\r\n";
7071
}
7172

72-
if (styleJObject.ContainsKey("primaryColor"))
73+
var maybePrimaryColor = JsonUtils.ParserColor(styleJObject, "primaryColor");
74+
if (maybePrimaryColor.HasValue)
7375
{
74-
var primaryColor = ColorHelpers.Parse(styleJObject["primaryColor"].Value<uint>());
75-
colorString += $" --primary-dark-1: {primaryColor.ToHexWithoutAlpha()};\r\n";
76+
colorString += $" --primary-dark-1: {maybePrimaryColor.Value.ToHexWithoutAlpha()};\r\n";
7677
}
7778

78-
if (styleJObject.ContainsKey("primaryDarkColor"))
79+
var maybePrimaryDarkColor = JsonUtils.ParserColor(styleJObject, "primaryDarkColor");
80+
if (maybePrimaryDarkColor.HasValue)
7981
{
80-
var primaryDarkColor = ColorHelpers.Parse(styleJObject["primaryDarkColor"].Value<uint>());
81-
colorString += $" --primary-dark-2: {primaryDarkColor.ToHexWithoutAlpha()};\r\n";
82+
colorString += $" --primary-dark-2: {maybePrimaryDarkColor.Value.ToHexWithoutAlpha()};\r\n";
8283
}
8384

8485
if (colorString.Length > 0)

0 commit comments

Comments
 (0)