Do things properly, and set a data tag on the mapped element

This commit is contained in:
Jeremy Ruston
2026-01-23 23:02:06 +00:00
parent b27d102d55
commit a14cd291ac
2 changed files with 24 additions and 11 deletions

View File

@@ -25,7 +25,6 @@ Render this widget into the DOM
*/
ElementWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
// Neuter blacklisted elements
this.tag = this.parseTreeNode.tag;
if($tw.config.htmlUnsafeElements.indexOf(this.tag) !== -1) {
@@ -42,15 +41,8 @@ ElementWidget.prototype.render = function(parent,nextSibling) {
headingLevel = Math.min(Math.max(headingLevel + 1 + baseLevel,1),6);
this.tag = "h" + headingLevel;
}
// Check for element mapping
var mappedTag = this.getVariable("tv-map-" + this.tag);
if(mappedTag) {
this.tag = mappedTag.trim();
var mappedClass = this.getVariable("tv-map-" + this.tag + "-class");
if(mappedClass) {
this.setAttribute("class",mappedClass.trim() + (this.getAttribute("class") ? " " + this.getAttribute("class") : ""));
}
}
// Compute the attributes, mapping the element tag if needed
this.computeAttributes();
// Select the namespace for the tag
var XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml",
tagNamespaces = {
@@ -108,4 +100,25 @@ ElementWidget.prototype.refresh = function(changedTiddlers) {
return this.refreshChildren(changedTiddlers) || hasChangedAttributes;
};
/*
Override the base computeAttributes method
*/
ElementWidget.prototype.computeAttributes = function() {
// Call the base class to compute the initial values of the attributes
var changedAttributes = Widget.prototype.computeAttributes.call(this);
// Check for element mapping
var mappedTag = this.getVariable("tv-map-" + this.tag),
mappedClass = this.getVariable("tv-map-" + this.tag + "-class");
if(mappedTag) {
this.tag = mappedTag.trim();
// Add an attribute to indicate the original tag
this.attributes["data-element-mapping-from"] = this.parseTreeNode.tag;
// Check for a mapped class
if(mappedClass) {
this.attributes["class"] = mappedClass.trim() + (this.attributes["class"] ? " " + this.attributes["class"] : "");
}
}
return changedAttributes;
};
exports.element = ElementWidget;

View File

@@ -16,4 +16,4 @@ This is a paragraph
+
title: ExpectedResult
<div class="my-class">This is a paragraph</div>
<div class="my-class" data-element-mapping-from="p">This is a paragraph</div>