Skip to content

Commit be42c1b

Browse files
authored
Merge pull request #107 from WebCoder49/special-chars-ignore-trailing-space
Remove final newline fix as no longer needed and causes problems with…
2 parents 25c152e + 524d2c1 commit be42c1b

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

code-input.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export namespace plugins {
183183
* Create a special characters plugin instance.
184184
* Default = covers many non-renderable ASCII characters.
185185
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code
186-
* @param {Boolean} inheritTextColor If `colorInSpecialChars` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base color of the `pre code` element is used to give contrast to the small characters.
186+
* @param {Boolean} inheritTextColor If `inheritTextColor` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base color of the `pre code` element is used to give contrast to the small characters.
187187
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
188188
*/
189189
constructor(colorInSpecialChars?: boolean, inheritTextColor?: boolean, specialCharRegExp?: RegExp);
@@ -286,7 +286,7 @@ export namespace templates {
286286
/**
287287
* Constructor to create a proof-of-concept template that shows text in a repeating series of colors.
288288
* @param {string[]} rainbowColors - An array of CSS colors, in the order each color will be shown
289-
* @param {string} delimiter - The character used to split up parts of text where each part is a different colour (e.g. "" = characters, " " = words)
289+
* @param {string} delimiter - The character used to split up parts of text where each part is a different color (e.g. "" = characters, " " = words)
290290
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
291291
* @returns template object
292292
*/

code-input.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,6 @@ var codeInput = {
511511
let resultElement = this.codeElement;
512512
let value = this.value;
513513

514-
// Handle final newlines
515-
if (value[value.length - 1] == "\n" || value.length == 0) {
516-
value += " ";
517-
}
518-
519514
// Update code
520515
resultElement.innerHTML = this.escapeHtml(value);
521516
this.pluginEvt("beforeHighlight");

plugins/special-chars.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
mask-image: var(--hex-0), var(--hex-1), var(--hex-2), var(--hex-3);
6464
mask-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
65-
mask-size: 40%, 40%, 40%, 40%;
65+
mask-size: min(40%, 0.25em), min(40%, 0.25em), min(40%, 0.25em), min(40%, 0.25em);
6666
mask-position: 10% 10%, 90% 10%, 10% 90%, 90% 90%;
6767

6868
-webkit-mask-image: var(--hex-0), var(--hex-1), var(--hex-2), var(--hex-3);

plugins/special-chars.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
1414
* Create a special characters plugin instance.
1515
* Default = covers many non-renderable ASCII characters.
1616
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code
17-
* @param {Boolean} inheritTextColor If `colorInSpecialChars` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base colour of the `pre code` element is used to give contrast to the small characters.
17+
* @param {Boolean} inheritTextColor If `inheritTextColor` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base color of the `pre code` element is used to give contrast to the small characters.
1818
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
1919
*/
2020
constructor(colorInSpecialChars = false, inheritTextColor = false, specialCharRegExp = /(?!\n)(?!\t)[\u{0000}-\u{001F}]|[\u{007F}-\u{009F}]|[\u{0200}-\u{FFFF}]/ug) { // By default, covers many non-renderable ASCII characters
@@ -71,6 +71,7 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
7171

7272
if(nextNode.textContent != "") {
7373
let replacementElement = this.getStylisedSpecialChar(codeInput, nextNode.textContent);
74+
// This next node will become the i+1th node so automatically iterated to
7475
nextNode.parentNode.insertBefore(replacementElement, nextNode);
7576
nextNode.textContent = "";
7677
}
@@ -120,14 +121,15 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
120121

121122
/* Get the colors a stylised representation of a given character must be shown in; lazy load and return [background color, text color] */
122123
getCharacterColors(asciiCode) {
123-
let backgroundColor;
124124
let textColor;
125125
if(!(asciiCode in this.cachedColors)) {
126-
// Get background color - arbitrary bit manipulation to get a good range of colours
127-
backgroundColor = asciiCode^(asciiCode << 3)^(asciiCode << 7)^(asciiCode << 14)^(asciiCode << 16); // Arbitrary
128-
backgroundColor = backgroundColor^0x1fc627; // Arbitrary
129-
backgroundColor = backgroundColor.toString(16);
130-
backgroundColor = ("000000" + backgroundColor).substring(backgroundColor.length); // So 6 chars with leading 0
126+
// Get background color
127+
let asciiHex = asciiCode.toString(16);
128+
let backgroundColor = "";
129+
for(let i = 0; i < asciiHex.length; i++) {
130+
backgroundColor += asciiHex[i] + asciiHex[i];
131+
}
132+
backgroundColor = ("000000" + backgroundColor).substring(backgroundColor.length); // So valid HEX color with 6 characters
131133

132134
// Get most suitable text color - white or black depending on background brightness
133135
let colorBrightness = 0;
@@ -148,7 +150,7 @@ codeInput.plugins.SpecialChars = class extends codeInput.Plugin {
148150
/* Get the width of a character in em (relative to font size), for use in creation of the stylised hexadecimal representation with the same width */
149151
getCharacterWidthEm(codeInput, char) {
150152
// Force zero-width characters
151-
if(new RegExp("\u00AD|\u02de|[\u0300-\u036F]|[\u0483-\u0489]|\u200b").test(char) ) { return 0 }
153+
if(new RegExp("\u00AD|\u02DE|[\u0300-\u036F]|[\u0483-\u0489]|[\u200B-\u200D]|\uFEFF").test(char) ) { return 0 }
152154
// Non-renderable ASCII characters should all be rendered at same size
153155
if(char != "\u0096" && new RegExp("[\u{0000}-\u{001F}]|[\u{007F}-\u{009F}]", "g").test(char)) {
154156
let fallbackWidth = this.getCharacterWidthEm(codeInput, "\u0096");

0 commit comments

Comments
 (0)