Skip to content

Commit

Permalink
DRY code
Browse files Browse the repository at this point in the history
  • Loading branch information
aldo-expensify committed Jun 28, 2022
1 parent a732f4b commit de3301c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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' || currentNode.domNode.name === 'email-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' || dom.attribs[tagAttribute] === 'email-comment') {
if (isCommentTag(dom.attribs[tagAttribute])) {
dom.children.push(new Element('br', {}));
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/pages/home/report/ReportActionItemFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const propTypes = {
loading: PropTypes.bool,

/** The reportAction's source */
source: PropTypes.string,
source: PropTypes.oneOf(['Chronos', 'email', 'ios', 'android', 'web', 'email', '']),

/** Should this fragment be contained in a single line? */
isSingleLine: PropTypes.bool,
Expand Down Expand Up @@ -104,18 +104,13 @@ const ReportActionItemFragment = (props) => {

// Only render HTML if we have html in the fragment
if (html !== text) {
if (props.source === 'email') {
// Messages from email replies usually have complex HTML structure and they don't rely in white-space: pre to preserve spacing,
// instead, they normally use  
return (
<RenderHTML
html={`<email-comment>${html + (props.fragment.isEdited ? '<edited></edited>' : '')}</email-comment>`}
/>
);
}
const editedTag = props.fragment.isEdited ? '<edited></edited>' : '';
const htmlContent = html + editedTag;
return (
<RenderHTML
html={`<comment>${html + (props.fragment.isEdited ? '<edited></edited>' : '')}</comment>`}
html={props.source === 'email'
? `<email-comment>${htmlContent}</email-comment>`
: `<comment>${htmlContent}</comment>`}
/>
);
}
Expand Down

0 comments on commit de3301c

Please sign in to comment.