Skip to content

Commit

Permalink
mbw/xml: fix bugs, add inner HTML blocks (#1647)
Browse files Browse the repository at this point in the history
Fixes type error in setTextContent
Makes querySelector related blocks also be able to get their parent - e.g. querying a on <a /> will return the expected result
Adds "inner elements of" and "set inner elements of" blocks for interacting with innerHTML
  • Loading branch information
mybearworld authored Aug 13, 2024
1 parent 5499be0 commit d228f60
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions extensions/mbw/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@
xmlToString(element) {
return element.outerHTML;
}
/**
* @param {Element} element
* @param {string} query
*/
resolveQuery(element, query) {
return element.matches(query) ? element : element.querySelector(query);
}
/**
* @param {Element} element
* @param {string} query
*/
resolveQueryAll(element, query) {
const response = [...element.querySelectorAll(query)];
if (element.matches(query)) {
response.unshift(element);
}
return response;
}

/** @returns {Scratch.Info} */
getInfo() {
Expand Down Expand Up @@ -100,6 +118,32 @@
},
},
},
{
opcode: "innerHTML",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("inner elements of [XML]"),
arguments: {
XML: {
type: Scratch.ArgumentType.STRING,
defaultValue: '<hello><planet name="world" /></hello>',
},
},
},
{
opcode: "setInnerHTML",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("set inner elements of [XML] to [VALUE]"),
arguments: {
XML: {
type: Scratch.ArgumentType.STRING,
defaultValue: '<hello><planet name="world" /></hello>',
},
VALUE: {
type: Scratch.ArgumentType.STRING,
defaultValue: '<planet name="mars" />',
},
},
},
"---",
{
opcode: "attributes",
Expand Down Expand Up @@ -367,7 +411,41 @@
if (xml === null) {
return "";
}
xml.textContent = VALUE;
xml.textContent = Scratch.Cast.toString(VALUE);
return this.xmlToString(xml);
}

/**
* @param {object} args
* @param {unknown} args.XML
*/
innerHTML({ XML }) {
const { xml } = this.stringToXml(Scratch.Cast.toString(XML));
if (xml === null) {
return "";
}
return xml.innerHTML;
}

/**
* @param {object} args
* @param {unknown} args.XML
* @param {unknown} args.VALUE
*/
setInnerHTML({ XML, VALUE }) {
const { xml } = this.stringToXml(Scratch.Cast.toString(XML));
if (xml === null) {
return "";
}
const value = Scratch.Cast.toString(VALUE);
// there needs to be exactly one parent element
const { xml: newXML } = this.stringToXml(
"<testElement>" + value + "</testElement>"
);
if (newXML === null) {
return "";
}
xml.innerHTML = Scratch.Cast.toString(value);
return this.xmlToString(xml);
}

Expand Down Expand Up @@ -544,7 +622,7 @@
if (xml === null) {
return "";
}
const child = xml.querySelector(Scratch.Cast.toString(QUERY));
const child = this.resolveQuery(xml, Scratch.Cast.toString(QUERY));
return child !== null;
}

Expand All @@ -558,7 +636,7 @@
if (xml === null) {
return "";
}
const child = xml.querySelector(Scratch.Cast.toString(QUERY));
const child = this.resolveQuery(xml, Scratch.Cast.toString(QUERY));
if (child === null) {
return "";
}
Expand All @@ -574,7 +652,7 @@
if (xml === null) {
return "";
}
const child = xml.querySelectorAll(Scratch.Cast.toString(QUERY));
const child = this.resolveQueryAll(xml, Scratch.Cast.toString(QUERY));
if (child.length === 0) {
return "";
}
Expand Down

0 comments on commit d228f60

Please sign in to comment.