<commit_before>/*
 * 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.
 */

#include "LayoutableShadowNode.h"

#include <react/core/LayoutConstraints.h>
#include <react/core/LayoutContext.h>
#include <react/core/LayoutMetrics.h>
#include <react/core/ShadowNode.h>
#include <react/debug/DebugStringConvertibleItem.h>
#include <react/graphics/conversions.h>

namespace facebook {
namespace react {

LayoutMetrics LayoutableShadowNode::getLayoutMetrics() const {
  return layoutMetrics_;
}

bool LayoutableShadowNode::setLayoutMetrics(LayoutMetrics layoutMetrics) {
  ensureUnsealed();

  if (layoutMetrics_ == layoutMetrics) {
    return false;
  }

  layoutMetrics_ = layoutMetrics;
  return true;
}

bool LayoutableShadowNode::LayoutableShadowNode::isLayoutOnly() const {
  return false;
}

Transform LayoutableShadowNode::getTransform() const {
  return Transform::Identity();
}

LayoutMetrics LayoutableShadowNode::getRelativeLayoutMetrics(
    LayoutableShadowNode const &ancestorLayoutableShadowNode,
    LayoutInspectingPolicy policy) const {
  auto &ancestorShadowNode =
      dynamic_cast<ShadowNode const &>(ancestorLayoutableShadowNode);
  auto &shadowNode = dynamic_cast<ShadowNode const &>(*this);

  auto ancestors = shadowNode.getAncestors(ancestorShadowNode);

  if (ancestors.size() == 0) {
    return EmptyLayoutMetrics;
  }

  auto layoutMetrics = getLayoutMetrics();

  // `AncestorList` starts from the given ancestor node and ends with the parent
  // node. We iterate from parent node (reverse iteration) and stop before the
  // given ancestor (rend() - 1).<commit_msg>Fix wrong layoutMetrics being returned by `measure` call<commit_after>/*
 * 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.
 */

#include "LayoutableShadowNode.h"

#include <react/core/LayoutConstraints.h>
#include <react/core/LayoutContext.h>
#include <react/core/LayoutMetrics.h>
#include <react/core/ShadowNode.h>
#include <react/debug/DebugStringConvertibleItem.h>
#include <react/graphics/conversions.h>

namespace facebook {
namespace react {

/*
 * `shadowNode` might not be the newest revision of `ShadowNodeFamily`.
 * This function looks at `parentNode`'s children and finds one that belongs
 * to the same family as `shadowNode`.
 */
static ShadowNode const *findNewestChildInParent(
    ShadowNode const &parentNode,
    ShadowNode const &shadowNode) {
  for (auto const &child : parentNode.getChildren()) {
    if (ShadowNode::sameFamily(*child, shadowNode)) {
      return child.get();
    }
  }
  return nullptr;
}

LayoutMetrics LayoutableShadowNode::getLayoutMetrics() const {
  return layoutMetrics_;
}

bool LayoutableShadowNode::setLayoutMetrics(LayoutMetrics layoutMetrics) {
  ensureUnsealed();

  if (layoutMetrics_ == layoutMetrics) {
    return false;
  }

  layoutMetrics_ = layoutMetrics;
  return true;
}

bool LayoutableShadowNode::LayoutableShadowNode::isLayoutOnly() const {
  return false;
}

Transform LayoutableShadowNode::getTransform() const {
  return Transform::Identity();
}

LayoutMetrics LayoutableShadowNode::getRelativeLayoutMetrics(
    LayoutableShadowNode const &ancestorLayoutableShadowNode,
    LayoutInspectingPolicy policy) const {
  auto &ancestorShadowNode =
      dynamic_cast<ShadowNode const &>(ancestorLayoutableShadowNode);
  auto &shadowNode = dynamic_cast<ShadowNode const &>(*this);

  auto ancestors = shadowNode.getAncestors(ancestorShadowNode);

  if (ancestors.size() == 0) {
    return EmptyLayoutMetrics;
  }

  auto newestChild =
      findNewestChildInParent(ancestors.rbegin()->first.get(), shadowNode);

  if (!newestChild) {
    return EmptyLayoutMetrics;
  }

  auto layoutMetrics = dynamic_cast<LayoutableShadowNode const *>(newestChild)
                           ->getLayoutMetrics();

  // `AncestorList` starts from the given ancestor node and ends with the parent
  // node. We iterate from parent node (reverse iteration) and stop before the
  // given ancestor (rend() - 1).
{'fraction_non_alphanumeric': 0.09707351891506068, 'fraction_numerical': 0.0009517011658339282, 'mean_word_length': 4.871508379888268, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3518047', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1177, 'n_words': 331}