Idea: do things properly!

Suggestions and feedback for Linux Mint and the forums
Forum rules
Do not post support questions here. Before you post read: Where to post ideas & feature requests
Post Reply
User avatar
Drugwash
Level 5
Level 5
Posts: 734
Joined: Fri Jun 07, 2019 6:40 am
Location: Ploieşti, RO
Contact:

Idea: do things properly!

Post by Drugwash »

Needed a simple XML to JSON converter for an unimportant feature in the applet I'm working on. Thought it would be piece of cake. Well, nope. Wasted ten hours of this night trying to figure out why this simple regex won't work in Cinnamon while it works in the browser. Turned out the Cinnamon implementation is incomplete and faulty. Try it yourselves, see what it yields:

Code: Select all

let xml = "<ip><results><result><ip>111.222.333.444</ip><host>555.666.777.888</host><isp>Mobile Communications S.A.</isp><city>Some City</city><countrycode>WW</countrycode><countryname>Worldwebia</countryname><latitude>179.99</latitude><longitude>0.00</longitude></result></results></ip>";
global.log(fX2J(xml));

fX2J(xml) {
		const json = {};
		for (const res of xml.matchAll(/(?:<(\w*)(?:\s[^>]*)*>)((?:(?!<\1).)*)(?:<\/\1>)|<(\w*)(?:\s*)*\/>/gm)) {
			const key = res[1] || res[3];
			const value = res[2] && fX2J(res[2]);
			json[key] = ((value && Object.keys(value).length) ? value : res[2]) || null;
		}
		return json;
	}
The above comes (slightly modified) from here and it works just fine in the browser. But Cinnamon has no matchAll, neither in the String.prototype nor in RegExp[Symbol.xxxxx]. Using match won't do. How is one supposed to work when tools are broken? So here's an idea: if you're gonna implement something then do it all the way. :evil:

EDIT: nevermind, that's too new to be found in Cinnamon 4.2 anyway. Alternatives? None found so far.
Post Reply

Return to “Suggestions & Feedback”