Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove whitespace: pre because of extra empty lines bug #9195

Merged
merged 10 commits into from
Jul 15, 2022
5 changes: 5 additions & 0 deletions src/components/HTMLEngineProvider/BaseHTMLEngineProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const customHTMLElementModels = {
}),
comment: defaultHTMLElementModels.div.extend({
tagName: 'comment',
mixedUAStyles: {whiteSpace: 'pre'},
}),
'email-comment': defaultHTMLElementModels.div.extend({
tagName: 'email-comment',
mixedUAStyles: {whiteSpace: 'normal'},
}),
};

Expand Down
13 changes: 12 additions & 1 deletion src/components/HTMLEngineProvider/htmlEngineUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ function computeEmbeddedMaxWidth(tagName, contentWidth) {
return contentWidth;
}

/**
* Check if tagName is equal to any of our custom tags wrapping chat comments.
*
* @param {string} tagName
* @returns {Boolean}
*/
function isCommentTag(tagName) {
return tagName === 'email-comment' || tagName === 'comment';
}

/**
* Check if there is an ancestor node with name 'comment'.
* Finding node with name 'comment' flags that we are rendering a comment.
Expand All @@ -26,7 +36,7 @@ function computeEmbeddedMaxWidth(tagName, contentWidth) {
function isInsideComment(tnode) {
let currentNode = tnode;
while (currentNode.parent) {
if (currentNode.domNode.name === 'comment') {
if (isCommentTag(currentNode.domNode.name)) {
return true;
}
currentNode = currentNode.parent;
Expand All @@ -37,4 +47,5 @@ function isInsideComment(tnode) {
export {
computeEmbeddedMaxWidth,
isInsideComment,
isCommentTag,
};
3 changes: 2 additions & 1 deletion src/libs/SelectionScraper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {parseDocument} from 'htmlparser2';
import {Element} from 'domhandler';
import _ from 'underscore';
import Str from 'expensify-common/lib/str';
import {isCommentTag} from '../../components/HTMLEngineProvider/htmlEngineUtils';

const elementsWillBeSkipped = ['html', 'body'];
const tagAttribute = 'data-testid';
Expand Down Expand Up @@ -103,7 +104,7 @@ const replaceNodes = (dom) => {
}

// Adding a new line after each comment here, because adding after each range is not working for chrome.
if (dom.attribs[tagAttribute] === 'comment') {
if (isCommentTag(dom.attribs[tagAttribute])) {
dom.children.push(new Element('br', {}));
}
}
Expand Down
51 changes: 31 additions & 20 deletions src/pages/home/report/ReportActionItemFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const propTypes = {
/** Does this fragment belong to a reportAction that has not yet loaded? */
loading: PropTypes.bool,

/** The reportAction's source */
source: PropTypes.oneOf(['Chronos', 'email', 'ios', 'android', 'web', 'email', '']),
roryabraham marked this conversation as resolved.
Show resolved Hide resolved

/** Should this fragment be contained in a single line? */
isSingleLine: PropTypes.bool,

Expand All @@ -64,6 +67,7 @@ const defaultProps = {
loading: false,
isSingleLine: false,
tooltipText: '',
source: '',
};

const ReportActionItemFragment = (props) => {
Expand All @@ -86,39 +90,46 @@ const ReportActionItemFragment = (props) => {
)
);
}
let {html, text} = props.fragment;
roryabraham marked this conversation as resolved.
Show resolved Hide resolved

// If the only difference between fragment.text and fragment.html is <br /> tags
// we replace them with line breaks and render it as text, not as html.
// This is done to render emojis with line breaks between them as text.
const differByLineBreaksOnly = Str.replaceAll(props.fragment.html, '<br />', ' ') === props.fragment.text;
if (differByLineBreaksOnly) {
const textWithLineBreaks = Str.replaceAll(props.fragment.html, '<br />', '\n');
// eslint-disable-next-line no-param-reassign
props.fragment = {...props.fragment, text: textWithLineBreaks, html: textWithLineBreaks};
html = textWithLineBreaks;
text = textWithLineBreaks;
}

// Only render HTML if we have html in the fragment
return props.fragment.html !== props.fragment.text
? (
if (html !== text) {
const editedTag = props.fragment.isEdited ? '<edited></edited>' : '';
const htmlContent = html + editedTag;
return (
<RenderHTML
html={`<comment>${props.fragment.html + (props.fragment.isEdited ? '<edited></edited>' : '')}</comment>`}
html={props.source === 'email'
? `<email-comment>${htmlContent}</email-comment>`
: `<comment>${htmlContent}</comment>`}
/>
) : (
<Text
selectable={!canUseTouchScreen() || !props.isSmallScreenWidth}
style={EmojiUtils.containsOnlyEmojis(props.fragment.text) ? styles.onlyEmojisText : undefined}
>
{Str.htmlDecode(props.fragment.text)}
{props.fragment.isEdited && (
<Text
fontSize={variables.fontSizeSmall}
color={themeColors.textSupporting}
>
{` ${props.translate('reportActionCompose.edited')}`}
</Text>
)}
</Text>
);
}
return (
<Text
selectable={!canUseTouchScreen() || !props.isSmallScreenWidth}
style={EmojiUtils.containsOnlyEmojis(text) ? styles.onlyEmojisText : undefined}
>
{Str.htmlDecode(text)}
{props.fragment.isEdited && (
<Text
fontSize={variables.fontSizeSmall}
color={themeColors.textSupporting}
>
{` ${props.translate('reportActionCompose.edited')}`}
</Text>
)}
</Text>
);
}
case 'TEXT':
return (
Expand Down
2 changes: 2 additions & 0 deletions src/pages/home/report/ReportActionItemMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import styles from '../../../styles/styles';
import ReportActionItemFragment from './ReportActionItemFragment';
import reportActionPropTypes from './reportActionPropTypes';
Expand Down Expand Up @@ -33,6 +34,7 @@ const ReportActionItemMessage = (props) => {
isAttachment={props.action.isAttachment}
attachmentInfo={props.action.attachmentInfo}
loading={props.action.loading}
source={lodashGet(props.action, 'originalMessage.source')}
/>
))}
</View>
Expand Down
1 change: 0 additions & 1 deletion src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ const webViewStyles = {
fontSize: variables.fontSizeNormal,
fontFamily: fontFamily.GTA,
flex: 1,
whiteSpace: 'pre',
},
};

Expand Down