JSFiddle - React, Tailwind, and code Playground
by smnh
CSS
div {
border: 1px dotted red;
padding: 10px;
}
JavaScript
function createDiv() {
var div = document.createElement("div");
div.style.border = "1px dotted red";
div.onmouseover=function(){div.style.color = "red"};
div.innerHTML = "I'm the div";
div.creationString = getFunctionContnet(createDiv); // noprint
return div; // noprint
}
var div = createDiv();
var str = div.innerHTML; //Writes the textual content: "I'm the div"
var str2 = div.outerHTML; //Writes the HTML code without the function: "<div>I'm the div</div>"
var str3 = div.creationString;
document.body.appendChild(div);
document.body.appendChild(document.createTextNode(str));
document.body.appendChild(document.createElement("p"));
document.body.appendChild(document.createTextNode(str2));
var pre = document.createElement("pre");
pre.appendChild(document.createTextNode(div.creationString));
document.body.appendChild(document.createElement("p"));
document.body.appendChild(pre);
function getFunctionContnet(func) {
var regExp = /function[^(]*\([^)]*\)[^{]*{(?:\s*\n)?(\s*)([\s\S]*)(?:\n\s*)}/,
match,
indention, indentionRE,
noprintRE = /\/\/.*noprint.*/,
content = null,
lines, i;
match = regExp.exec(func.toString());
if (match !== null) {
indention = match[1];
content = match[2];
lines = content.split("\n");
// if first line of the function is indented,
// remove that indention from all function lines.
if (typeof indention !== "undefined") {
indentionRE = new RegExp("^" + indention);
for (i = 0; i < lines.length; i++) {
if (indentionRE.test(lines[i])) {
lines[i] = lines[i].substr(indention.length);
}
}
}
// don't print lines with "// noprint"
for (i = lines.length - 1; i >= 0; i--) {
if (noprintRE.test(lines[i])) {
lines.splice(i, 1);
}
}
...