From a315e4cd30e4b8da841f587650146a62c868f67d Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Fri, 11 Sep 2020 11:35:34 -0700 Subject: [PATCH] Prevent an infinite loop in Text.onTextLayout Summary: Changelog: [internal] # Problem onTextLayout was called even if there was no change to text layout. This can cause an infinite loop with onTextLayout triggering commit which triggers onTextLayout. # Fix Do not call onTextLayout if there is nothing to layout. Reviewed By: yungsters Differential Revision: D23648430 fbshipit-source-id: 055dc34a9aca0edf2c78a5812b35b80df32c9e3e --- .../components/text/ParagraphShadowNode.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp b/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp index 993cc65b883..b4fc65923a6 100644 --- a/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp +++ b/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp @@ -164,6 +164,18 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) { updateStateIfNeeded(content); + if (content.attachments.empty()) { +#ifndef ANDROID + if (getConcreteProps().onTextLayout) { + // `onTextLayout` needs to be called even if text is empty + // to be compatible with Paper. + getConcreteEventEmitter().onTextLayout({}); + } +#endif + // No attachments, nothing to layout. + return; + } + auto measurement = textLayoutManager_->measure( AttributedStringBox{content.attributedString}, content.paragraphAttributes, @@ -179,11 +191,6 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) { } #endif - if (content.attachments.empty()) { - // No attachments, nothing to layout. - return; - } - // Iterating on attachments, we clone shadow nodes and moving // `paragraphShadowNode` that represents clones of `this` object. auto paragraphShadowNode = static_cast(this);