Skip to content

Commit

Permalink
New data.stripHtml() to strip HTML from text.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoramadas committed Oct 30, 2019
1 parent 5e141bb commit 52ee71b
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 29 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for JAUL

1.2.0
=====
* New data.stripHtml() to remove HTML tags from text.
* Updated dependencies.

1.1.2
=====
* Updated dependencies.
Expand Down
7 changes: 7 additions & 0 deletions lib/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ declare class DataUtils {
* @returns The minified JSON as object or string, depending on asString.
*/
static minifyJson(source: string, asString?: boolean): any;
/**
* Strips all the HTML tags from the specified value and returns only the text.
* Tags will be replaced with an empty space by default.
* @param value The HTML string to be converted to only text.
* @param tagReplace Replace tags with that value
*/
static stripHtml(html: string, tagReplace?: string): string;
/**
* Generates a RFC4122-compliant unique ID using random numbers.
* @returns A unique ID.
Expand Down
108 changes: 108 additions & 0 deletions lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,114 @@ class DataUtils {
return JSON.parse(result);
}
}
/**
* Strips all the HTML tags from the specified value and returns only the text.
* Tags will be replaced with an empty space by default.
* @param value The HTML string to be converted to only text.
* @param tagReplace Replace tags with that value
*/
static stripHtml(html, tagReplace) {
if (!html || html == "") {
return "";
}
// Default tagReplace is an empty space.
if (!tagReplace && tagReplace !== "") {
tagReplace = " ";
}
// Output will be set here.
let output = "";
// Default symbols.
const PLAINTEXT = Symbol("plaintext");
const HTML = Symbol("html");
const COMMENT = Symbol("comment");
// Initial state variables.
let tagBuffer = "";
let qChar = "";
let depth = 0;
let state = PLAINTEXT;
// Iterate HTML content to replace tags.
for (let i = 0, length = html.length; i < length; i++) {
let char = html[i];
if (state === PLAINTEXT) {
switch (char) {
case "<":
state = HTML;
tagBuffer += char;
break;
default:
output += char;
break;
}
}
else if (state === HTML) {
switch (char) {
case "<":
if (qChar) {
break;
}
depth++;
break;
case ">":
if (qChar) {
break;
}
if (depth) {
depth--;
break;
}
qChar = "";
state = PLAINTEXT;
tagBuffer += ">";
output += tagReplace;
tagBuffer = "";
break;
case '"':
case "'":
if (char === qChar) {
qChar = "";
}
else {
qChar = qChar || char;
}
tagBuffer += char;
break;
case "-":
if (tagBuffer === "<!-") {
state = COMMENT;
}
tagBuffer += char;
break;
case " ":
case "\n":
if (tagBuffer === "<") {
state = PLAINTEXT;
output += "< ";
tagBuffer = "";
break;
}
tagBuffer += char;
break;
default:
tagBuffer += char;
break;
}
}
else if (state === COMMENT) {
switch (char) {
case ">":
if (tagBuffer.slice(-2) == "--") {
state = PLAINTEXT;
}
tagBuffer = "";
break;
default:
tagBuffer += char;
break;
}
}
}
return output;
}
/**
* Generates a RFC4122-compliant unique ID using random numbers.
* @returns A unique ID.
Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jaul",
"version": "1.1.2",
"version": "1.2.0",
"description": "Just another utilities library. But a good one.",
"keywords": [
"jaul",
Expand Down Expand Up @@ -32,13 +32,13 @@
"moment": "^2.24.0"
},
"devDependencies": {
"@types/node": "^12.7.12",
"@types/node": "^12.12.1",
"chai": "^4.2.0",
"coveralls": "^3.0.7",
"express": "^4.17.1",
"get-port": "^5.0.0",
"nyc": "^14.1.1",
"mocha": "^6.2.1",
"mocha": "^6.2.2",
"mocha-lcov-reporter": "^1.3.0",
"socket.io-client": "^2.3.0",
"supertest": "^4.0.2",
Expand Down
Loading

0 comments on commit 52ee71b

Please sign in to comment.