Skip to content

Commit

Permalink
Add Clay/htmlEncode extension (#1126)
Browse files Browse the repository at this point in the history
Co-authored-by: ClaytonTDM <[email protected]>
  • Loading branch information
GarboMuffin and ClaytonTDM authored Nov 6, 2023
1 parent 182580b commit 057d5aa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions extensions/Clay/htmlEncode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Name: HTML Encode
// ID: clayhtmlencode
// Description: Escape untrusted text to safely include in HTML.
// By: ClaytonTDM

(function (Scratch) {
"use strict";

class HtmlEncode {
getInfo() {
return {
id: "claytonhtmlencode",
name: "HTML Encode",
blocks: [
{
opcode: "encode",
blockType: Scratch.BlockType.REPORTER,
text: "encode [text] as HTML-safe",
arguments: {
text: {
type: Scratch.ArgumentType.STRING,
// don't use a script tag as the example here as the closing script
// tag might break things when this extension gets inlined in packed
// projects
defaultValue: "<h1>Hello!</h1>",
},
},
},
],
};
}

encode({ text }) {
return Scratch.Cast.toString(text).replace(/["'&<>]/g, (a) => {
switch (a) {
case "&":
return "&amp;";
case '"':
return "&apos;";
case "'":
return "&quot;";
case ">":
return "&gt;";
case "<":
return "&lt;";
}
// this should never happen...
return "";
});
}
}

Scratch.extensions.register(new HtmlEncode());
})(Scratch);
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"sound",
"Lily/Video",
"iframe",
"Clay/htmlEncode",
"Xeltalliv/clippingblending",
"clipboard",
"obviousAlexC/penPlus",
Expand Down

0 comments on commit 057d5aa

Please sign in to comment.