Skip to content

Commit

Permalink
Need QSA to be forgiving
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeaun committed Nov 20, 2024
1 parent deaddce commit 6c3c158
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 136 deletions.
24 changes: 20 additions & 4 deletions src/components/status.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,27 @@ function getPostText(status) {
);
}

function forgivingQSA(selectors = [], dom = document) {
// Run QSA for list of selectors
// If a selector return invalid selector error, try the next one
for (const selector of selectors) {
try {
return dom.querySelectorAll(selector);
} catch (e) {}
}
return [];
}

function isTranslateble(content) {
if (!content) return false;
content = content.trim();
if (!content) return false;
const text = getHTMLText(content, {
preProcess: (dom) => {
// Remove .mention, pre, code, a:has(.invisible)
for (const a of dom.querySelectorAll(
'.mention, pre, code, a:has(.invisible)',
for (const a of forgivingQSA(
['.mention, pre, code, a:has(.invisible)', '.mention, pre, code'],
dom,
)) {
a.remove();
}
Expand All @@ -148,8 +160,12 @@ function getHTMLTextForDetectLang(content) {
// Remove anything that can skew the language detection

// Remove .mention, .hashtag, pre, code, a:has(.invisible)
for (const a of dom.querySelectorAll(
'.mention, .hashtag, pre, code, a:has(.invisible)',
for (const a of forgivingQSA(
[
'.mention, .hashtag, pre, code, a:has(.invisible)',
'.mention, .hashtag, pre, code',
],
dom,
)) {
a.remove();
}
Expand Down
Loading

0 comments on commit 6c3c158

Please sign in to comment.