UNPKG

flipper-plugin

Version:

Flipper Desktop plugin SDK and components

60 lines 1.87 kB
"use strict"; /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ Object.defineProperty(exports, "__esModule", { value: true }); exports.textContent = void 0; function isReactElement(object) { return (typeof object === 'object' && object !== null && object.$$typeof === Symbol.for('react.element')); } function multilineTextContent(text) { // Replace any existing double quotes with two double quotes const escapedText = text.replace(/"/g, '""'); if (escapedText.includes('\n')) { return `"${escapedText}"`; } else { return escapedText; } } /** * Recursively walks through all children of a React element and returns * the string representation of the leafs concatenated. */ function textContent(node) { let res = ''; const traverse = (node) => { if (typeof node === 'string' || typeof node === 'number') { // this is a leaf, add it to the result string res += node; } else if (Array.isArray(node)) { // traverse all array members and recursively stringify them node.forEach(traverse); } else if (isReactElement(node)) { // node is a react element access its children an recursively stringify them const { children, value } = node.props; if (value) { res += multilineTextContent(value); } else if (Array.isArray(children)) { children.forEach(traverse); } else { traverse(children); } } }; traverse(node); return res; } exports.textContent = textContent; //# sourceMappingURL=textContent.js.map