mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
cb37562f83
Summary: Goals are: 1. Catch errors in parsing during dev-mode in a way that is disruptive/grabs attention, but has enough information. 2. Use react_native_assert for hitting breakpoints (less useful for Android, more for iOS), and add LOGs for when this code is used in Android (more useful for Android, less useful for iOS). 3. Return sane defaults so that prod cases don't crash, and don't return totally garbage data. I also found a couple cases where parsing was incorrect before; see WritingDirection and TextAlignment. This could impact some layouts and RTL/LTR potentially. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D27540903 fbshipit-source-id: 99e6949d97e8ef5520d008c1df3cbe408b5a43a4
137 lines
4.0 KiB
C++
137 lines
4.0 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <better/map.h>
|
|
#include <folly/dynamic.h>
|
|
#include <glog/logging.h>
|
|
#include <react/debug/react_native_assert.h>
|
|
#include <react/renderer/graphics/conversions.h>
|
|
#include <react/renderer/imagemanager/primitives.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
inline void fromRawValue(const RawValue &value, ImageSource &result) {
|
|
if (value.hasType<std::string>()) {
|
|
result = {
|
|
/* .type = */ ImageSource::Type::Remote,
|
|
/* .uri = */ (std::string)value,
|
|
};
|
|
return;
|
|
}
|
|
|
|
if (value.hasType<better::map<std::string, RawValue>>()) {
|
|
auto items = (better::map<std::string, RawValue>)value;
|
|
result = {};
|
|
|
|
result.type = ImageSource::Type::Remote;
|
|
|
|
if (items.find("__packager_asset") != items.end()) {
|
|
result.type = ImageSource::Type::Local;
|
|
}
|
|
|
|
if (items.find("width") != items.end() &&
|
|
items.find("height") != items.end() &&
|
|
// The following checks have to be removed after codegen is shipped.
|
|
// See T45151459.
|
|
items.at("width").hasType<Float>() &&
|
|
items.at("height").hasType<Float>()) {
|
|
result.size = {(Float)items.at("width"), (Float)items.at("height")};
|
|
}
|
|
|
|
if (items.find("scale") != items.end() &&
|
|
// The following checks have to be removed after codegen is shipped.
|
|
// See T45151459.
|
|
items.at("scale").hasType<Float>()) {
|
|
result.scale = (Float)items.at("scale");
|
|
} else {
|
|
result.scale = items.find("deprecated") != items.end() ? 0.0f : 1.0f;
|
|
}
|
|
|
|
if (items.find("url") != items.end() &&
|
|
// The following should be removed after codegen is shipped.
|
|
// See T45151459.
|
|
items.at("url").hasType<std::string>()) {
|
|
result.uri = (std::string)items.at("url");
|
|
}
|
|
|
|
if (items.find("uri") != items.end() &&
|
|
// The following should be removed after codegen is shipped.
|
|
// See T45151459.
|
|
items.at("uri").hasType<std::string>()) {
|
|
result.uri = (std::string)items.at("uri");
|
|
}
|
|
|
|
if (items.find("bundle") != items.end() &&
|
|
// The following should be removed after codegen is shipped.
|
|
// See T45151459.
|
|
items.at("bundle").hasType<std::string>()) {
|
|
result.bundle = (std::string)items.at("bundle");
|
|
result.type = ImageSource::Type::Local;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
// The following should be removed after codegen is shipped.
|
|
// See T45151459.
|
|
result = {};
|
|
result.type = ImageSource::Type::Invalid;
|
|
}
|
|
|
|
inline std::string toString(const ImageSource &value) {
|
|
return "{uri: " + value.uri + "}";
|
|
}
|
|
|
|
inline void fromRawValue(const RawValue &value, ImageResizeMode &result) {
|
|
react_native_assert(value.hasType<std::string>());
|
|
if (!value.hasType<std::string>()) {
|
|
LOG(ERROR) << "Unsupported ImageResizeMode type";
|
|
// "cover" is default in non-Fabric web and iOS
|
|
result = ImageResizeMode::Cover;
|
|
return;
|
|
}
|
|
|
|
auto stringValue = (std::string)value;
|
|
if (stringValue == "cover") {
|
|
result = ImageResizeMode::Cover;
|
|
} else if (stringValue == "contain") {
|
|
result = ImageResizeMode::Contain;
|
|
} else if (stringValue == "stretch") {
|
|
result = ImageResizeMode::Stretch;
|
|
} else if (stringValue == "center") {
|
|
result = ImageResizeMode::Center;
|
|
} else if (stringValue == "repeat") {
|
|
result = ImageResizeMode::Repeat;
|
|
} else {
|
|
LOG(ERROR) << "Unsupported ImageResizeMode value: " << stringValue;
|
|
react_native_assert(false);
|
|
// "cover" is default in non-Fabric web and iOS
|
|
result = ImageResizeMode::Cover;
|
|
}
|
|
}
|
|
|
|
inline std::string toString(const ImageResizeMode &value) {
|
|
switch (value) {
|
|
case ImageResizeMode::Cover:
|
|
return "cover";
|
|
case ImageResizeMode::Contain:
|
|
return "contain";
|
|
case ImageResizeMode::Stretch:
|
|
return "stretch";
|
|
case ImageResizeMode::Center:
|
|
return "center";
|
|
case ImageResizeMode::Repeat:
|
|
return "repeat";
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|