1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2026-01-24 03:44:41 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Jeremy Ruston
f29de0cb44 Split the tests for clarity 2026-01-23 23:05:05 +00:00
Jeremy Ruston
a14cd291ac Do things properly, and set a data tag on the mapped element 2026-01-23 23:02:06 +00:00
Jeremy Ruston
b27d102d55 Support a class to be automatically added to transformed elements 2026-01-23 22:26:51 +00:00
Jeremy Ruston
9aaab87b87 Initial Commit 2026-01-23 22:23:48 +00:00
3 changed files with 60 additions and 1 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,6 +41,8 @@ ElementWidget.prototype.render = function(parent,nextSibling) {
headingLevel = Math.min(Math.max(headingLevel + 1 + baseLevel,1),6);
this.tag = "h" + headingLevel;
}
// 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 = {
@@ -99,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

@@ -0,0 +1,18 @@
title: ElementMapping/Basic
description: Mapping one element to another
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
<!-- Map <p> to <div>, adding the class my-class -->
\define tv-map-p() div
This is a paragraph
+
title: ExpectedResult
<div data-element-mapping-from="p">This is a paragraph</div>

View File

@@ -0,0 +1,19 @@
title: ElementMapping/BasicWithClass
description: Mapping one element to another with an added class
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
<!-- Map <p> to <div>, adding the class my-class -->
\define tv-map-p() div
\define tv-map-p-class() my-class
This is a paragraph
+
title: ExpectedResult
<div class="my-class" data-element-mapping-from="p">This is a paragraph</div>