-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Clay/htmlEncode extension (#1126)
Co-authored-by: ClaytonTDM <[email protected]>
- Loading branch information
1 parent
182580b
commit 057d5aa
Showing
2 changed files
with
55 additions
and
0 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
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 "&"; | ||
case '"': | ||
return "'"; | ||
case "'": | ||
return """; | ||
case ">": | ||
return ">"; | ||
case "<": | ||
return "<"; | ||
} | ||
// this should never happen... | ||
return ""; | ||
}); | ||
} | ||
} | ||
|
||
Scratch.extensions.register(new HtmlEncode()); | ||
})(Scratch); |
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