maths / moodle-qtype_stack

Stack question type for Moodle
GNU General Public License v3.0
138 stars 147 forks source link

STACK-JS switch_content produces body tags in content #1144

Closed bgailer closed 3 months ago

bgailer commented 3 months ago

Hello,

we have been using a lot of the new STACK-JS features recently and the functions to interact with the question content from inside the iframes are really helpful. One thing I noticed, when using the function stack_js.switch_content to change the content of an HTML element with an id like for example "target", then the content "hello" shows up enclosed by a HTML body tag.

For example, if I have

<span id="source">hello</span>
[[javascript]]
stack_js.get_content("source").then((content) => {
stack_js.switch_content("target", content);
});
[[/javascript]]
<span id="target">content should go here</span>

then the target span is

<span id="target"><body>hello</body></span>

which results in a different layout (block-level) and style of the target span than before. Instead, it should be just <span id="target">hello</span> in my opinion. Maybe this comes from parsing the content in the stackjsvle script.

aharjula commented 3 months ago

Makes sense. This will be fixed to not use the whole body from the sanitiser parsing logic and only the contents.

aharjula commented 3 months ago

Side note: the new [[quid/]]-block may be handy if you need to keep your identifiers unique:

<span id="[[quid id='source'/]]">hello</span>
[[javascript]]
stack_js.get_content("[[quid id='source'/]]").then((content) => {
stack_js.switch_content("[[quid id='target'/]]", content);
});
[[/javascript]]
<span id="[[quid id='target'/]]">content should go here</span>

Basically, if you find yourself copying the same pattern everywhere and have the risk of multiple questions on the same page using the same identifiers, then this might be useful.

georgekinnear commented 1 month ago

I've got a side issue: the new [[quid/]] block sounds really useful, but it's not working for me. When I try to use it on the stack-demo site, I get an error message when saving the question: "Unknown block of type quid requested". Searching on github doesn't show up anything in the code defining [[quid/]], so am I right in thinking this feature is still a work in progress?

aharjula commented 1 month ago

It will come out with 4.6, there has not been a release since this was created.

It does exist in the dev branch, though... Simply dropping that single file into any 4.5+ level install should work.

georgekinnear commented 1 month ago

Great, thanks!