Skip to content

Commit 0a65777

Browse files
committed
performance optimizations and refactoring
1 parent 0ab16a0 commit 0a65777

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

lib/core/utils/dq-element.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ function getOuterHtml(element) {
2222
return source || '';
2323
}
2424

25-
function createTruncatedAttributeOuterHtml(element, truncatedAttributeSet) {
26-
if (!element) {return;}
25+
function truncateAttributes(element, truncatedAttributeSet) {
26+
if (!element) {
27+
return;
28+
}
2729

2830
const copyNode = element.cloneNode(true);
2931

3032
let str = getOuterHtml(copyNode);
31-
if (!truncatedAttributeSet || truncatedAttributeSet.size === 0) {return str;}
33+
if (!truncatedAttributeSet || truncatedAttributeSet.size === 0) {
34+
return str;
35+
}
3236

3337
let intermediateAttrString = '';
34-
const tagNameEnd = str.indexOf('>');
38+
const openingTagEndIndex = str.indexOf('>');
3539

3640
for (const truncatedAttr of truncatedAttributeSet) {
3741
intermediateAttrString += `${truncatedAttr.substring(0, 20)}... `;
3842
}
3943

4044
str =
41-
str.substring(0, tagNameEnd) +
45+
str.substring(0, openingTagEndIndex) +
4246
' ' +
4347
intermediateAttrString +
4448
' ' +
45-
str.substring(tagNameEnd);
49+
str.substring(openingTagEndIndex);
4650
return str;
4751
}
4852

@@ -55,41 +59,31 @@ function truncateElement(element, maxLength, maxAttributeLength) {
5559

5660
const elementAttributes = element.attributes;
5761
const lastIndex = elementAttributes.length - 1;
62+
let currElementStr = truncateAttributes(copyNode, attributesToTruncate);
5863

5964
for (let i = lastIndex; i >= 0; i--) {
60-
const attribute = elementAttributes[i];
65+
if (currElementStr.indexOf('>') < maxLength) {break;}
6166

67+
const attribute = elementAttributes[i];
6268
const attributeLength = attribute.value.length + attribute.name.length;
63-
if (
64-
createTruncatedAttributeOuterHtml(copyNode, attributesToTruncate).indexOf(
65-
'>'
66-
) < maxLength
67-
)
68-
{break;}
6969

7070
if (attributeLength > maxAttributeLength) {
7171
attributesToTruncate.add(attribute.name);
7272
copyNode.attributes.removeNamedItem(attribute.name);
73+
currElementStr = truncateAttributes(copyNode, attributesToTruncate);
7374
}
7475
}
7576

76-
let modifiedCurrentStr = createTruncatedAttributeOuterHtml(
77-
copyNode,
78-
attributesToTruncate
79-
);
80-
81-
if (modifiedCurrentStr.indexOf('>') > maxLength) {
82-
modifiedCurrentStr = modifiedCurrentStr.substring(0, maxLength) + '...>';
77+
if (currElementStr.indexOf('>') > maxLength) {
78+
currElementStr = currElementStr.substring(0, maxLength) + '...>';
8379
}
8480

85-
if (modifiedCurrentStr.length > maxLength) {
86-
modifiedCurrentStr = modifiedCurrentStr.substring(
87-
0,
88-
modifiedCurrentStr.indexOf('>') + 1
89-
);
81+
if (currElementStr.length > maxLength) {
82+
const endIndex = currElementStr.indexOf('>');
83+
currElementStr = currElementStr.substring(0, endIndex + 1);
9084
}
9185

92-
return modifiedCurrentStr;
86+
return currElementStr;
9387
}
9488

9589
function getSource(element) {

0 commit comments

Comments
 (0)