2013-05-17 09:12:25 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/fakedom.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: global
|
|
|
|
|
|
|
|
A barebones implementation of DOM interfaces needed by the rendering mechanism.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
// Sequence number used to enable us to track objects for testing
|
|
|
|
var sequenceNumber = null;
|
|
|
|
|
|
|
|
var bumpSequenceNumber = function(object) {
|
|
|
|
if(sequenceNumber !== null) {
|
|
|
|
object.sequenceNumber = sequenceNumber++;
|
|
|
|
}
|
2014-08-30 19:44:26 +00:00
|
|
|
};
|
2013-10-12 16:05:13 +00:00
|
|
|
|
2020-07-31 07:25:15 +00:00
|
|
|
var TW_Node = function (){
|
|
|
|
throw TypeError("Illegal constructor");
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperty(TW_Node.prototype, 'ELEMENT_NODE', {
|
|
|
|
get: function() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(TW_Node.prototype, 'TEXT_NODE', {
|
|
|
|
get: function() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
var TW_TextNode = function(text) {
|
2013-10-12 16:05:13 +00:00
|
|
|
bumpSequenceNumber(this);
|
2016-11-22 20:24:59 +00:00
|
|
|
this.textContent = text + "";
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
2013-05-17 09:12:25 +00:00
|
|
|
|
2024-05-21 21:02:37 +00:00
|
|
|
Object.setPrototypeOf(TW_TextNode.prototype,TW_Node.prototype);
|
2020-07-31 07:25:15 +00:00
|
|
|
|
2015-09-22 10:57:53 +00:00
|
|
|
Object.defineProperty(TW_TextNode.prototype, "nodeType", {
|
|
|
|
get: function() {
|
2020-07-31 07:25:15 +00:00
|
|
|
return this.TEXT_NODE;
|
2015-09-22 10:57:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-25 14:49:07 +00:00
|
|
|
Object.defineProperty(TW_TextNode.prototype, "formattedTextContent", {
|
|
|
|
get: function() {
|
|
|
|
return this.textContent.replace(/(\r?\n)/g,"");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-29 11:59:04 +00:00
|
|
|
var TW_Style = function(el) {
|
|
|
|
// Define the internal style object
|
|
|
|
var styleObject = {
|
|
|
|
// Method to get the entire style object
|
|
|
|
get: function() {
|
|
|
|
return el._style;
|
|
|
|
},
|
|
|
|
// Method to set styles using a string (e.g. "color:red; background-color:blue;")
|
|
|
|
set: function(str) {
|
|
|
|
var self = this;
|
|
|
|
str = str || "";
|
|
|
|
$tw.utils.each(str.split(";"),function(declaration) {
|
|
|
|
var parts = declaration.split(":"),
|
|
|
|
name = $tw.utils.trim(parts[0]),
|
|
|
|
value = $tw.utils.trim(parts[1]);
|
|
|
|
if(name && value) {
|
|
|
|
el._style[$tw.utils.convertStyleNameToPropertyName(name)] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// Method to set a specific property without transforming the property name, such as a custom property
|
|
|
|
setProperty: function(name, value) {
|
|
|
|
el._style[name] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return a Proxy to handle direct access to individual style properties
|
|
|
|
return new Proxy(styleObject, {
|
|
|
|
get: function(target, property) {
|
|
|
|
// If the property exists on styleObject, return it (get, set, setProperty methods)
|
|
|
|
if (property in target) {
|
|
|
|
return target[property];
|
|
|
|
}
|
|
|
|
// Otherwise, return the corresponding property from _style
|
|
|
|
return el._style[$tw.utils.convertStyleNameToPropertyName(property)] || "";
|
|
|
|
},
|
|
|
|
set: function(target, property, value) {
|
|
|
|
// Set the property in _style
|
|
|
|
el._style[$tw.utils.convertStyleNameToPropertyName(property)] = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var TW_Element = function(tag, namespace) {
|
2013-10-12 16:05:13 +00:00
|
|
|
bumpSequenceNumber(this);
|
2013-10-14 22:32:01 +00:00
|
|
|
this.isTiddlyWikiFakeDom = true;
|
2013-05-17 09:12:25 +00:00
|
|
|
this.tag = tag;
|
|
|
|
this.attributes = {};
|
|
|
|
this.isRaw = false;
|
|
|
|
this.children = [];
|
2024-10-29 11:59:04 +00:00
|
|
|
this._style = {}; // Internal style object
|
|
|
|
this.style = new TW_Style(this); // Proxy for style management
|
2013-10-13 19:14:31 +00:00
|
|
|
this.namespaceURI = namespace || "http://www.w3.org/1999/xhtml";
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
2013-05-17 09:12:25 +00:00
|
|
|
|
2020-07-31 07:25:15 +00:00
|
|
|
|
2024-10-29 11:59:04 +00:00
|
|
|
Object.setPrototypeOf(TW_Element.prototype,TW_Node.prototype);
|
2019-01-28 18:21:24 +00:00
|
|
|
|
2015-08-10 10:41:23 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "nodeType", {
|
|
|
|
get: function() {
|
2020-07-31 07:25:15 +00:00
|
|
|
return this.ELEMENT_NODE;
|
2015-08-10 10:41:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-10 19:32:13 +00:00
|
|
|
TW_Element.prototype.getAttribute = function(name) {
|
|
|
|
if(this.isRaw) {
|
|
|
|
throw "Cannot getAttribute on a raw TW_Element";
|
|
|
|
}
|
|
|
|
return this.attributes[name];
|
|
|
|
};
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
TW_Element.prototype.setAttribute = function(name,value) {
|
|
|
|
if(this.isRaw) {
|
|
|
|
throw "Cannot setAttribute on a raw TW_Element";
|
|
|
|
}
|
2023-11-22 20:05:40 +00:00
|
|
|
if(name === "style") {
|
2024-10-29 11:59:04 +00:00
|
|
|
this.style.set(value);
|
2023-11-22 20:05:40 +00:00
|
|
|
} else {
|
|
|
|
this.attributes[name] = value + "";
|
|
|
|
}
|
2013-05-17 09:12:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TW_Element.prototype.setAttributeNS = function(namespace,name,value) {
|
|
|
|
this.setAttribute(name,value);
|
|
|
|
};
|
|
|
|
|
2013-11-01 17:23:08 +00:00
|
|
|
TW_Element.prototype.removeAttribute = function(name) {
|
|
|
|
if(this.isRaw) {
|
|
|
|
throw "Cannot removeAttribute on a raw TW_Element";
|
|
|
|
}
|
|
|
|
if($tw.utils.hop(this.attributes,name)) {
|
|
|
|
delete this.attributes[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
TW_Element.prototype.appendChild = function(node) {
|
|
|
|
this.children.push(node);
|
|
|
|
node.parentNode = this;
|
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
TW_Element.prototype.insertBefore = function(node,nextSibling) {
|
|
|
|
if(nextSibling) {
|
|
|
|
var p = this.children.indexOf(nextSibling);
|
|
|
|
if(p !== -1) {
|
|
|
|
this.children.splice(p,0,node);
|
2013-12-20 15:29:01 +00:00
|
|
|
node.parentNode = this;
|
2013-10-12 16:05:13 +00:00
|
|
|
} else {
|
|
|
|
this.appendChild(node);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.appendChild(node);
|
|
|
|
}
|
2014-08-30 19:44:26 +00:00
|
|
|
};
|
2013-10-12 16:05:13 +00:00
|
|
|
|
2013-07-05 09:47:08 +00:00
|
|
|
TW_Element.prototype.removeChild = function(node) {
|
|
|
|
var p = this.children.indexOf(node);
|
|
|
|
if(p !== -1) {
|
|
|
|
this.children.splice(p,1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TW_Element.prototype.hasChildNodes = function() {
|
|
|
|
return !!this.children.length;
|
|
|
|
};
|
|
|
|
|
2015-08-10 10:41:23 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "childNodes", {
|
|
|
|
get: function() {
|
|
|
|
return this.children;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-05 09:47:08 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "firstChild", {
|
2014-08-30 19:44:26 +00:00
|
|
|
get: function() {
|
|
|
|
return this.children[0];
|
|
|
|
}
|
2013-07-05 09:47:08 +00:00
|
|
|
});
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
TW_Element.prototype.addEventListener = function(type,listener,useCapture) {
|
|
|
|
// Do nothing
|
|
|
|
};
|
|
|
|
|
2015-08-10 10:41:23 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "tagName", {
|
|
|
|
get: function() {
|
|
|
|
return this.tag || "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "className", {
|
|
|
|
get: function() {
|
|
|
|
return this.attributes["class"] || "";
|
|
|
|
},
|
2014-08-30 19:44:26 +00:00
|
|
|
set: function(value) {
|
2016-11-22 20:24:59 +00:00
|
|
|
this.attributes["class"] = value + "";
|
2014-08-30 19:44:26 +00:00
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
});
|
|
|
|
|
2014-02-21 15:43:25 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "value", {
|
|
|
|
get: function() {
|
2014-08-30 19:44:26 +00:00
|
|
|
return this.attributes.value || "";
|
2014-02-21 15:43:25 +00:00
|
|
|
},
|
2014-08-30 19:44:26 +00:00
|
|
|
set: function(value) {
|
2016-11-22 20:24:59 +00:00
|
|
|
this.attributes.value = value + "";
|
2014-08-30 19:44:26 +00:00
|
|
|
}
|
2014-02-21 15:43:25 +00:00
|
|
|
});
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "outerHTML", {
|
2014-08-30 19:44:26 +00:00
|
|
|
get: function() {
|
2013-05-17 09:12:25 +00:00
|
|
|
var output = [],attr,a,v;
|
|
|
|
output.push("<",this.tag);
|
|
|
|
if(this.attributes) {
|
|
|
|
attr = [];
|
|
|
|
for(a in this.attributes) {
|
|
|
|
attr.push(a);
|
|
|
|
}
|
|
|
|
attr.sort();
|
|
|
|
for(a=0; a<attr.length; a++) {
|
|
|
|
v = this.attributes[attr[a]];
|
|
|
|
if(v !== undefined) {
|
2015-03-21 14:17:42 +00:00
|
|
|
output.push(" ",attr[a],"=\"",$tw.utils.htmlEncode(v),"\"");
|
2013-05-17 09:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 18:21:24 +00:00
|
|
|
if(this._style) {
|
2014-10-06 20:10:35 +00:00
|
|
|
var style = [];
|
2019-01-28 18:21:24 +00:00
|
|
|
for(var s in this._style) {
|
|
|
|
style.push($tw.utils.convertPropertyNameToStyleName(s) + ":" + this._style[s] + ";");
|
2014-10-06 20:10:35 +00:00
|
|
|
}
|
2014-10-06 20:20:19 +00:00
|
|
|
if(style.length > 0) {
|
2019-01-28 18:21:24 +00:00
|
|
|
output.push(" style=\"",style.join(""),"\"");
|
2014-10-06 20:20:19 +00:00
|
|
|
}
|
2014-10-06 20:10:35 +00:00
|
|
|
}
|
2013-10-29 14:52:12 +00:00
|
|
|
output.push(">");
|
2013-05-17 09:12:25 +00:00
|
|
|
if($tw.config.htmlVoidElements.indexOf(this.tag) === -1) {
|
|
|
|
output.push(this.innerHTML);
|
|
|
|
output.push("</",this.tag,">");
|
|
|
|
}
|
|
|
|
return output.join("");
|
2014-08-30 19:44:26 +00:00
|
|
|
}
|
2013-05-17 09:12:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(TW_Element.prototype, "innerHTML", {
|
|
|
|
get: function() {
|
|
|
|
if(this.isRaw) {
|
|
|
|
return this.rawHTML;
|
|
|
|
} else {
|
|
|
|
var b = [];
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node instanceof TW_Element) {
|
|
|
|
b.push(node.outerHTML);
|
|
|
|
} else if(node instanceof TW_TextNode) {
|
2021-08-12 15:46:11 +00:00
|
|
|
b.push($tw.utils.htmlTextEncode(node.textContent));
|
2013-05-17 09:12:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return b.join("");
|
|
|
|
}
|
|
|
|
},
|
2014-08-30 19:44:26 +00:00
|
|
|
set: function(value) {
|
|
|
|
this.isRaw = true;
|
|
|
|
this.rawHTML = value;
|
2017-02-21 13:09:32 +00:00
|
|
|
this.rawTextContent = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(TW_Element.prototype, "textInnerHTML", {
|
|
|
|
set: function(value) {
|
|
|
|
if(this.isRaw) {
|
|
|
|
this.rawTextContent = value;
|
|
|
|
} else {
|
|
|
|
throw "Cannot set textInnerHTML of a non-raw TW_Element";
|
|
|
|
}
|
2014-08-30 19:44:26 +00:00
|
|
|
}
|
2013-05-17 09:12:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(TW_Element.prototype, "textContent", {
|
|
|
|
get: function() {
|
|
|
|
if(this.isRaw) {
|
2017-02-21 13:09:32 +00:00
|
|
|
if(this.rawTextContent === null) {
|
2017-06-05 15:12:43 +00:00
|
|
|
return "";
|
2017-02-21 13:09:32 +00:00
|
|
|
} else {
|
|
|
|
return this.rawTextContent;
|
|
|
|
}
|
2013-05-17 09:12:25 +00:00
|
|
|
} else {
|
|
|
|
var b = [];
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
b.push(node.textContent);
|
|
|
|
});
|
|
|
|
return b.join("");
|
|
|
|
}
|
2014-10-06 20:10:35 +00:00
|
|
|
},
|
|
|
|
set: function(value) {
|
|
|
|
this.children = [new TW_TextNode(value)];
|
2013-05-17 09:12:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-25 14:49:07 +00:00
|
|
|
Object.defineProperty(TW_Element.prototype, "formattedTextContent", {
|
|
|
|
get: function() {
|
|
|
|
if(this.isRaw) {
|
2017-06-05 15:12:43 +00:00
|
|
|
return "";
|
2014-02-25 14:49:07 +00:00
|
|
|
} else {
|
|
|
|
var b = [],
|
|
|
|
isBlock = $tw.config.htmlBlockElements.indexOf(this.tag) !== -1;
|
|
|
|
if(isBlock) {
|
|
|
|
b.push("\n");
|
|
|
|
}
|
|
|
|
if(this.tag === "li") {
|
2014-08-30 19:44:26 +00:00
|
|
|
b.push("* ");
|
2014-02-25 14:49:07 +00:00
|
|
|
}
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
b.push(node.formattedTextContent);
|
|
|
|
});
|
|
|
|
if(isBlock) {
|
|
|
|
b.push("\n");
|
|
|
|
}
|
|
|
|
return b.join("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-17 09:12:25 +00:00
|
|
|
var document = {
|
2013-10-12 16:05:13 +00:00
|
|
|
setSequenceNumber: function(value) {
|
|
|
|
sequenceNumber = value;
|
|
|
|
},
|
2013-05-17 09:12:25 +00:00
|
|
|
createElementNS: function(namespace,tag) {
|
2013-10-13 19:14:31 +00:00
|
|
|
return new TW_Element(tag,namespace);
|
2013-05-17 09:12:25 +00:00
|
|
|
},
|
|
|
|
createElement: function(tag) {
|
|
|
|
return new TW_Element(tag);
|
|
|
|
},
|
|
|
|
createTextNode: function(text) {
|
|
|
|
return new TW_TextNode(text);
|
|
|
|
},
|
2014-10-17 19:10:38 +00:00
|
|
|
compatMode: "CSS1Compat", // For KaTeX to know that we're not a browser in quirks mode
|
2014-03-10 18:41:11 +00:00
|
|
|
isTiddlyWikiFakeDom: true
|
2013-05-17 09:12:25 +00:00
|
|
|
};
|
|
|
|
|
2014-01-15 14:57:35 +00:00
|
|
|
exports.fakeDocument = document;
|
2013-05-17 09:12:25 +00:00
|
|
|
|
|
|
|
})();
|