Skip to content

Commit

Permalink
Fix crash when null colors are passed (#218)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nickwinder authored Apr 30, 2019
1 parent 44598d1 commit fec925d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-pspdfkit",
"version": "1.23.8",
"version": "1.23.9",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down
2 changes: 1 addition & 1 deletion samples/Catalog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Catalog",
"version": "1.23.8",
"version": "1.23.9",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
Expand Down
17 changes: 17 additions & 0 deletions windows/ReactNativePSPDFKit/ReactNativePSPDFKit/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using Windows.UI;
using Newtonsoft.Json.Linq;
using PSPDFKit.Search;
using PSPDFKitFoundation;
using PSPDFKitFoundation.Search;
using PSPDFKitNative;
using ReactNative.UIManager;

namespace ReactNativePSPDFKit
{
Expand Down Expand Up @@ -131,5 +133,20 @@ private static JArray LibraryQueryReultToJson(LibraryQueryResult libraryQueryRes

return pageNumbersJson;
}

internal static Color? ParserColor(JObject jObject, string propertyName)
{
if (TryGetNotNullValue(jObject, propertyName, out var jsonHighlightColor))
{
return ColorHelpers.Parse(jsonHighlightColor.Value<uint>());
}

return null;
}

internal static bool TryGetNotNullValue(JObject jObject, string propertyName, out JToken value)
{
return jObject.TryGetValue(propertyName, out value) && value.Type != JTokenType.Null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,23 @@ public void SetHideNavigationBar(PDFViewPage view, bool hideNavigationBar)
public async void SetCustomCss(PDFViewPage view, JObject styleJObject)
{
var colorString = string.Empty;
if (styleJObject.ContainsKey("highlightColor"))

var maybeHighlightColor = JsonUtils.ParserColor(styleJObject, "highlightColor");
if (maybeHighlightColor.HasValue)
{
var highlightColor = ColorHelpers.Parse(styleJObject["highlightColor"].Value<uint>());
colorString += $" --primary: {highlightColor.ToHexWithoutAlpha()};\r\n";
colorString += $" --primary: {maybeHighlightColor.Value.ToHexWithoutAlpha()};\r\n";
}

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

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

if (colorString.Length > 0)
Expand Down

0 comments on commit fec925d

Please sign in to comment.