Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML elements with data-interactive="false" can be safely copied #1126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion runtime/scripts/jme-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ newBuiltin('html',[TString],THTML,null, {
container.innerHTML = args[0].value;
var subber = new jme.variables.DOMcontentsubber(scope);
subber.subvars(container);
return new THTML(Array.from(container.childNodes));
var nodes = Array.from(container.childNodes);
nodes.forEach(node => node.setAttribute('data-interactive', 'false'));
return new THTML(nodes);
}
});
newBuiltin('isnonemptyhtml',[TString],TBool,function(html) {
Expand All @@ -351,6 +353,7 @@ newBuiltin('image',[TString, '[number]', '[number]'],THTML,null, {
}
var subber = new jme.variables.DOMcontentsubber(scope);
var element = subber.subvars(img);
element.setAttribute('data-interactive', 'false');
return new THTML(element);
}
});
Expand Down Expand Up @@ -826,6 +829,7 @@ newBuiltin('scientificnumberhtml', [TDecimal], THTML, function(n) {
var bits = math.parseScientific(n.re.toExponential());
var s = document.createElement('span');
s.innerHTML = math.niceRealNumber(bits.significand)+' × 10<sup>'+bits.exponent+'</sup>';
s.setAttribute('data-interactive', 'false');
return s;
});
newBuiltin('scientificnumberhtml', [TNum], THTML, function(n) {
Expand All @@ -835,6 +839,7 @@ newBuiltin('scientificnumberhtml', [TNum], THTML, function(n) {
var bits = math.parseScientific(math.niceRealNumber(n,{style:'scientific', scientificStyle:'plain'}));
var s = document.createElement('span');
s.innerHTML = math.niceRealNumber(bits.significand)+' × 10<sup>'+bits.exponent+'</sup>';
s.setAttribute('data-interactive', 'false');
return s;
});

Expand Down Expand Up @@ -2394,6 +2399,7 @@ newBuiltin('table',[TList,TList],THTML, null, {
row.appendChild(td);
}
}
table.setAttribute('data-interactive','false');
return new THTML(table);
}
});
Expand All @@ -2412,6 +2418,7 @@ newBuiltin('table',[TList],THTML, null, {
row.appendChild(td);
}
}
table.setAttribute('data-interactive','false');
return new THTML(table);
}
});
Expand Down
3 changes: 3 additions & 0 deletions runtime/scripts/jme-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ jme.variables = /** @lends Numbas.jme.variables */ {
function doToken(token) {
if(jme.isType(token,'html')) {
token = jme.castToType(token,'html');
if(token.value.every(e => e.getAttribute('data-interactive') === 'false')) {
return token.value.map(e => e.cloneNode(true));
}
if(token.value.numbas_embedded) {
throw(new Numbas.Error('jme.subvars.html inserted twice'))
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/scripts/jme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3229,6 +3229,9 @@ var TBool = types.TBool = function(b) {
jme.registerType(TBool,'boolean');

/** HTML DOM element.
*
* If the element has the attribute `data-interactive="false"` then it can be safely copied and embedded multiple times.
* If the attribute is not present or has any other value, then it's assumed that it can't be safely copied.
*
* @memberof Numbas.jme.types
* @augments Numbas.jme.token
Expand Down
Loading