2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/element.js
|
2013-10-12 16:05:13 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
Element widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
var ElementWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
ElementWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
ElementWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
2014-03-19 10:05:44 +00:00
|
|
|
// Neuter blacklisted elements
|
2022-12-20 17:31:51 +00:00
|
|
|
this.tag = this.parseTreeNode.tag;
|
2020-07-02 12:13:55 +00:00
|
|
|
if($tw.config.htmlUnsafeElements.indexOf(this.tag) !== -1) {
|
|
|
|
this.tag = "safe-" + this.tag;
|
2014-03-19 10:05:44 +00:00
|
|
|
}
|
2022-12-20 17:31:51 +00:00
|
|
|
// Restrict tag name to digits, letts and dashes
|
|
|
|
this.tag = this.tag.replace(/[^0-9a-zA-Z\-]/mg,"");
|
|
|
|
// Default to a span
|
|
|
|
this.tag = this.tag || "span";
|
2018-06-10 14:54:23 +00:00
|
|
|
// Adjust headings by the current base level
|
2020-07-02 12:13:55 +00:00
|
|
|
var headingLevel = ["h1","h2","h3","h4","h5","h6"].indexOf(this.tag);
|
2018-06-10 14:54:23 +00:00
|
|
|
if(headingLevel !== -1) {
|
|
|
|
var baseLevel = parseInt(this.getVariable("tv-adjust-heading-level","0"),10) || 0;
|
|
|
|
headingLevel = Math.min(Math.max(headingLevel + 1 + baseLevel,1),6);
|
2020-07-02 12:13:55 +00:00
|
|
|
this.tag = "h" + headingLevel;
|
2018-06-10 14:54:23 +00:00
|
|
|
}
|
2013-10-13 19:14:31 +00:00
|
|
|
// Select the namespace for the tag
|
2022-07-05 16:47:57 +00:00
|
|
|
var XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml",
|
|
|
|
tagNamespaces = {
|
2013-10-13 19:14:31 +00:00
|
|
|
svg: "http://www.w3.org/2000/svg",
|
2013-10-28 15:05:19 +00:00
|
|
|
math: "http://www.w3.org/1998/Math/MathML",
|
2022-07-05 16:46:31 +00:00
|
|
|
body: XHTML_NAMESPACE
|
2013-10-13 19:14:31 +00:00
|
|
|
};
|
2020-07-02 12:13:55 +00:00
|
|
|
this.namespace = tagNamespaces[this.tag];
|
2013-10-13 19:14:31 +00:00
|
|
|
if(this.namespace) {
|
|
|
|
this.setVariable("namespace",this.namespace);
|
|
|
|
} else {
|
2022-07-05 16:47:57 +00:00
|
|
|
if(this.hasAttribute("xmlns")) {
|
2022-07-05 16:46:31 +00:00
|
|
|
this.namespace = this.getAttribute("xmlns");
|
|
|
|
this.setVariable("namespace",this.namespace);
|
|
|
|
} else {
|
|
|
|
this.namespace = this.getVariable("namespace",{defaultValue: XHTML_NAMESPACE});
|
|
|
|
}
|
2013-10-13 19:14:31 +00:00
|
|
|
}
|
2020-07-02 12:13:55 +00:00
|
|
|
// Invoke the th-rendering-element hook
|
|
|
|
var parseTreeNodes = $tw.hooks.invokeHook("th-rendering-element",null,this);
|
|
|
|
this.isReplaced = !!parseTreeNodes;
|
|
|
|
if(parseTreeNodes) {
|
|
|
|
// Use the parse tree nodes provided by the hook
|
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
|
|
|
this.renderChildren(this.parentDomNode,null);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-13 19:14:31 +00:00
|
|
|
// Make the child widgets
|
2013-10-12 16:05:13 +00:00
|
|
|
this.makeChildWidgets();
|
2020-07-02 12:13:55 +00:00
|
|
|
// Create the DOM node and render children
|
|
|
|
var domNode = this.document.createElementNS(this.namespace,this.tag);
|
|
|
|
this.assignAttributes(domNode,{excludeEventAttributes: true});
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
ElementWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes(),
|
|
|
|
hasChangedAttributes = $tw.utils.count(changedAttributes) > 0;
|
|
|
|
if(hasChangedAttributes) {
|
2020-07-02 12:13:55 +00:00
|
|
|
if(!this.isReplaced) {
|
|
|
|
// Update our attributes
|
2021-05-30 18:20:17 +00:00
|
|
|
this.assignAttributes(this.domNodes[0],{excludeEventAttributes: true});
|
2020-07-02 12:13:55 +00:00
|
|
|
} else {
|
|
|
|
// If we were replaced then completely refresh ourselves
|
|
|
|
return this.refreshSelf();
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
2013-11-01 17:22:51 +00:00
|
|
|
return this.refreshChildren(changedTiddlers) || hasChangedAttributes;
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.element = ElementWidget;
|
|
|
|
|
|
|
|
})();
|