Skip to content

Commit

Permalink
Optimize with isChildOfNode function
Browse files Browse the repository at this point in the history
  • Loading branch information
yakupafsin committed Nov 8, 2023
1 parent 2f4fe78 commit e149093
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function AnchorRenderer(props) {
Link.openExternalLink(attrHref);
};

if (!HTMLEngineUtils.isInsideComment(props.tnode)) {
if (!HTMLEngineUtils.isChildOfComment(props.tnode)) {
// This is not a comment from a chat, the AnchorForCommentsOnly uses a Pressable to create a context menu on right click.
// We don't have this behaviour in other links in NewDot
// TODO: We should use TextLink, but I'm leaving it as Text for now because TextLink breaks the alignment in Android.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function CodeRenderer(props) {
});

// Determine the font size for the code based on whether it's inside an H1 element.
const isInsideH1 = HTMLEngineUtils.isInsideH1(props.tnode);
const isInsideH1 = HTMLEngineUtils.isChildOfH1(props.tnode);

const fontSize = StyleUtils.getCodeFontSize(isInsideH1);

Expand Down
36 changes: 20 additions & 16 deletions src/components/HTMLEngineProvider/htmlEngineUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,41 @@ function isCommentTag(tagName) {
}

/**
* Check if there is an ancestor node with name 'comment'.
* Finding node with name 'comment' flags that we are rendering a comment.
* Check if there is an ancestor node for which the predicate returns true.
*
* @param {TNode} tnode
* @param {Function} predicate
* @returns {Boolean}
*/
function isInsideComment(tnode) {
let currentNode = tnode;
while (currentNode.parent) {
if (isCommentTag(currentNode.domNode.name)) {
function isChildOfNode(tnode, predicate) {
let currentNode = tnode.parent;
while (currentNode) {
if (predicate(currentNode)) {
return true;
}
currentNode = currentNode.parent;
}
return false;
}

/**
* Check if there is an ancestor node with name 'comment'.
* Finding node with name 'comment' flags that we are rendering a comment.
* @param {TNode} tnode
* @returns {Boolean}
*/
function isChildOfComment(tnode) {
return isChildOfNode(tnode, (node) => isCommentTag(node.domNode.name));
}

/**
* Check if there is an ancestor node with the name 'h1'.
* Finding a node with the name 'h1' flags that we are rendering inside an h1 element.
* @param {TNode} tnode
* @returns {Boolean}
*/
function isInsideH1(tnode) {
let currentNode = tnode.parent;
while (currentNode) {
if (currentNode.domNode.name === 'h1') {
return true;
}
currentNode = currentNode.parent;
}
return false;
function isChildOfH1(tnode) {
return isChildOfNode(tnode, (node) => node.domNode.name.toLowerCase() === 'h1');
}

export {computeEmbeddedMaxWidth, isInsideComment, isCommentTag, isInsideH1};
export {computeEmbeddedMaxWidth, isChildOfComment, isCommentTag, isChildOfH1};

0 comments on commit e149093

Please sign in to comment.