Skip to content

Support html entity character calculation when cutting text contents #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions html-substring.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ function html_substr(src, length, suffix){
tagTree.push(tagStrip(tag));
}
i = end; //Move the position to end of the tag

} else if (c == '&') {
// HTML Character Entities found - https://www.w3schools.com/HTML/html_entities.asp
end = src.slice(i).indexOf(";");
if(end == -1) { //Check for incomplete entity name
return str;
}
end += i + 1;
tag = src.slice(i,end); //Read entity name
// Append entity name and move the position
str += tag; //Append the entity name to final content
i = end; // Move the position to end of the entity name
cnt++;
} else {
//Append content character and move the position
str += c;
Expand All @@ -69,4 +80,4 @@ function html_substr(src, length, suffix){
}
if (module && module.exports) {
module.exports = html_substr;
}
}