-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4177 from preactjs/jsxssr
- Loading branch information
Showing
5 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const ENCODED_ENTITIES = /["&<]/; | ||
|
||
/** @param {string} str */ | ||
export function encodeEntities(str) { | ||
// Skip all work for strings with no entities needing encoding: | ||
if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) return str; | ||
|
||
let last = 0, | ||
i = 0, | ||
out = '', | ||
ch = ''; | ||
|
||
// Seek forward in str until the next entity char: | ||
for (; i < str.length; i++) { | ||
switch (str.charCodeAt(i)) { | ||
case 34: | ||
ch = '"'; | ||
break; | ||
case 38: | ||
ch = '&'; | ||
break; | ||
case 60: | ||
ch = '<'; | ||
break; | ||
default: | ||
continue; | ||
} | ||
// Append skipped/buffered characters and the encoded entity: | ||
if (i !== last) out += str.slice(last, i); | ||
out += ch; | ||
// Start the next seek/buffer after the entity's offset: | ||
last = i + 1; | ||
} | ||
if (i !== last) out += str.slice(last, i); | ||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters