Skip to content

Commit 99ab43c

Browse files
committed
🩹 Disable mouse events in text display text
1 parent 5f73449 commit 99ab43c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/outliner/textDisplay.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '../formats/blueprint'
66
import { registerAction } from '../util/moddingTools'
77
// import * as MinecraftFull from '../assets/MinecraftFull.json'
8-
import { JsonTextSyntaxError } from 'src/systems/jsonText/parser'
8+
import { JsonTextParser, JsonTextSyntaxError } from 'src/systems/jsonText/parser'
99
import { TextDisplayConfig } from '../nodeConfigs'
1010
import { JsonText, TextElement } from '../systems/jsonText'
1111
import { getVanillaFont, MinecraftFont } from '../systems/minecraft/fontManager'
@@ -214,9 +214,14 @@ export class TextDisplay extends ResizableOutlinerElement {
214214
updateTextMesh() {
215215
let result: JsonText | undefined
216216
try {
217-
result = JsonText.fromString(this.text, {
217+
const parser = new JsonTextParser({
218218
minecraftVersion: Project!.animated_java.target_minecraft_version,
219219
})
220+
parser.enabledFeatures &= ~(
221+
JsonTextParser.FEATURES.ALLOW_CLICK_EVENTS |
222+
JsonTextParser.FEATURES.ALLOW_HOVER_EVENTS
223+
)
224+
result = parser.parse(this.text)
220225
this.textError.set('')
221226
} catch (e: any) {
222227
console.error(e)

src/systems/jsonText/parser.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ enum FEATURES {
162162
* - `{ text: '', '#00aced' }` -> `{ text: '', color: '#00aced' }`
163163
*/
164164
TEXT_OBJECT_INFERRED_KEYS = 1 << 15,
165+
166+
ALLOW_CLICK_EVENTS = 1 << 16,
167+
ALLOW_HOVER_EVENTS = 1 << 17,
165168
}
166169

167170
export function compareVersions(a: string, b: string): number {
@@ -288,6 +291,9 @@ export class JsonTextParser {
288291
static maxNestingDepth = 512
289292
static maxArrayLength = 2 ** 31 - 9
290293

294+
// eslint-disable-next-line @typescript-eslint/naming-convention
295+
static FEATURES = FEATURES
296+
291297
static defaultFeatures =
292298
// Minecraft syntax sugar
293299
FEATURES.LITERAL_KEYS |
@@ -298,7 +304,10 @@ export class JsonTextParser {
298304
FEATURES.OPTIONAL_COMMAS |
299305
FEATURES.SHADOW_COLOR_ACCEPTS_STRING |
300306
FEATURES.TEXT_OBJECT_INFERRED_KEYS |
301-
FEATURES.IMPLICIT_TEXT_KEY
307+
FEATURES.IMPLICIT_TEXT_KEY |
308+
// Mouse events
309+
FEATURES.ALLOW_CLICK_EVENTS |
310+
FEATURES.ALLOW_HOVER_EVENTS
302311

303312
private s!: StringStream
304313
private currentNestingDepth = 0
@@ -835,6 +844,9 @@ export class JsonTextParser {
835844
break
836845

837846
case 'clickEvent': {
847+
if (!(this.enabledFeatures & FEATURES.ALLOW_CLICK_EVENTS)) {
848+
this.throwSyntax(`'clickEvent' field is not allowed`)
849+
}
838850
const event = this.parseLegacyClickEventObject()
839851
if (this.enabledFeatures & FEATURES.MODERN_EVENT_FORMAT) {
840852
if (obj.click_event !== undefined) {
@@ -851,6 +863,9 @@ export class JsonTextParser {
851863
}
852864

853865
case 'click_event': {
866+
if (!(this.enabledFeatures & FEATURES.ALLOW_CLICK_EVENTS)) {
867+
this.throwSyntax(`'click_event' field is not allowed`)
868+
}
854869
const event = this.parseModernClickEventObject()
855870
if (!(this.enabledFeatures & FEATURES.MODERN_EVENT_FORMAT)) {
856871
if (obj.clickEvent !== undefined) {
@@ -867,6 +882,9 @@ export class JsonTextParser {
867882
}
868883

869884
case 'hoverEvent': {
885+
if (!(this.enabledFeatures & FEATURES.ALLOW_HOVER_EVENTS)) {
886+
this.throwSyntax(`'hoverEvent' field is not allowed`)
887+
}
870888
const event = this.parseLegacyHoverEventObject()
871889
if (this.enabledFeatures & FEATURES.MODERN_EVENT_FORMAT) {
872890
if (obj.hover_event !== undefined) {
@@ -883,6 +901,9 @@ export class JsonTextParser {
883901
}
884902

885903
case 'hover_event':
904+
if (!(this.enabledFeatures & FEATURES.ALLOW_HOVER_EVENTS)) {
905+
this.throwSyntax(`'hover_event' field is not allowed`)
906+
}
886907
const event = this.parseModernHoverEventObject()
887908
if (!(this.enabledFeatures & FEATURES.MODERN_EVENT_FORMAT)) {
888909
if (obj.hoverEvent !== undefined) {

0 commit comments

Comments
 (0)