Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/extensions/default/HTMLCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,60 @@
}
}

/**
* to validate whether a lorem abbreviation is safe to expand without crashing the app
* so there are many problematic patters like lorem1000000, li*10>lorem50000 etc
* we put a max constraint of 100000, above which we don't show the code hint at all
*
* @param {String} word - the abbreviation to validate
* @returns {Boolean} - true if safe to expand otherwise false
*/
function isLoremSafeToExpand(word) {

Check warning on line 460 in src/extensions/default/HTMLCodeHints/main.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move function 'isLoremSafeToExpand' to the outer scope.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AZsg8UgB0TmTzWRXABzm&open=AZsg8UgB0TmTzWRXABzm&pullRequest=2502
const MAX_LOREM_WORDS = 100000;

// extract all lorem word counts (handles lorem123, loremru123, lorem10-200, etc.)
const loremPattern = /lorem[a-z]*(\d+)(?:-(\d+))?/gi;
const loremMatches = [...word.matchAll(loremPattern)];
// if no lorem is there, it means its safe
if (loremMatches.length === 0) {
return true;
}

// find the maximum lorem word count
let maxLoremCount = 0;
for (const match of loremMatches) {
const count1 = parseInt(match[1]) || 0;

Check warning on line 474 in src/extensions/default/HTMLCodeHints/main.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `Number.parseInt` over `parseInt`.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AZsg8UgB0TmTzWRXABzn&open=AZsg8UgB0TmTzWRXABzn&pullRequest=2502
// for ranges like lorem10-200, take the second (max) number
const count2 = match[2] ? parseInt(match[2]) : count1;

Check warning on line 476 in src/extensions/default/HTMLCodeHints/main.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `Number.parseInt` over `parseInt`.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AZsg8UgB0TmTzWRXABzo&open=AZsg8UgB0TmTzWRXABzo&pullRequest=2502
const maxCount = Math.max(count1, count2);
maxLoremCount = Math.max(maxLoremCount, maxCount);
}

// if any single lorem exceeds the limit, for ex: `lorem1000000` or `lorem5000000`
if (maxLoremCount > MAX_LOREM_WORDS) {
return false;
}

// handle the multiplication cases, for ex: `li*10>lorem500000`
const multiplierPattern = /\*(\d+)/g;
const multipliers = [...word.matchAll(multiplierPattern)].map(m => parseInt(m[1]));

Check warning on line 488 in src/extensions/default/HTMLCodeHints/main.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `Number.parseInt` over `parseInt`.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AZsg8UgB0TmTzWRXABzp&open=AZsg8UgB0TmTzWRXABzp&pullRequest=2502

// if no multipliers, just check the max lorem count
if (multipliers.length === 0) {
return true;
}

// calc total multiplication factor (capped at 400 due to maxRepeat)
const totalMultiplier = Math.min(
multipliers.reduce((product, num) => product * num, 1),
400
);

// worst case: max lorem count × total multiplier
const estimatedTotal = maxLoremCount * totalMultiplier;

return estimatedTotal <= MAX_LOREM_WORDS;
}

/**
* This function checks whether the abbreviation can be expanded or not.
Expand Down Expand Up @@ -496,6 +550,12 @@
positiveSymbols.some(symbol => word.includes(symbol)) ||
word.toLowerCase().includes('lorem')
) {
// we need to check if this is safe to expand because cases like
// `lorem100000000` will crash phoenix
// read functions jsdoc for more details
if (word.toLowerCase().includes('lorem') && !isLoremSafeToExpand(word)) {
return null;
}

try {
return expandAbbr(word, { syntax: "html", type: "markup", maxRepeat: 400 }); // expanded
Expand Down
Loading