Skip to content

Commit

Permalink
[issue #79] - head and body merging
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRaven2000 committed Sep 23, 2023
1 parent bf8cb68 commit 61b4a07
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions chrome/content/smartTemplate-compose.js
Expand Up @@ -1268,15 +1268,20 @@ SmartTemplate4.classSmartTemplate = function() {
}
}

///new extract <head> sections and inject into doc head.
// [issue 79]
// Extract <head> sections and inject into doc head.
// merge all <body> attributes into document body (body will be converted into an attributeless div)
try {
const isExtractHead = SmartTemplate4.Preferences.getMyBoolPref("header.inject");
if (isExtractHead) {
let testDiv = editor.document.createElement("div");
testDiv.id = "tempTemplate";
testDiv.hidden = true;
// replace <head> tags, because they will be removed on adding the HTML:
testDiv.innerHTML = template.replace("<head","<div class='smartTemplateHeader'").replace("</head","</div");
testDiv.innerHTML = template.replace("<head","<div class='smartTemplateHeader' ").replace("</head","</div")
.replace("<body","<div class='smartTemplateBody' " ).replace("</body","</div");

// ===== merge head contents
let heads = testDiv.querySelectorAll("div.smartTemplateHeader");
if (heads.length) {
let docHeader = editor.document.head || editor.document.getElementsByTagName('head')[0],
Expand All @@ -1295,8 +1300,41 @@ SmartTemplate4.classSmartTemplate = function() {
testDiv.removeChild(head);
}
template = testDiv.innerHTML; // extract the remaining markup
editor.document.removeElement(testDiv);
}

// ===== merge body attributes
let bodies = testDiv.querySelectorAll("div.smartTemplateBody");
if (bodies.length) { // gather all attributes.
let allAttributes = [];
for (let body of bodies) {
let atts = [...body.attributes];
allAttributes.push(...atts);
for (let a of atts) { // strip all attributes of the div, it shouldn't do anything hopefully
body.removeAttribute(a.name);
}
}
// all body attributes are dropped by composer, so there is no need to tidy up!
for (let a of allAttributes) {
let isClass = (a.name=="class");
if (isClass) {
a.value = a.value.replace("smartTemplateBody","").trim();
}
if (a.value && a.value.trim()) {
if (isClass) {
let clist = a.value.split(" ");
for (let cl of clist) {
if (cl) {
bodyEl.classList.add(cl);
}
}
} else { // note: this definitely overwrites previous attributes!
bodyEl.setAttribute(a.name, a.value);
}
}
}
template = testDiv.innerHTML; // extract the remaining markup again.
}
testDiv.remove();
}
} catch(ex) {
util.logException("Extract header from template failed", ex);
Expand Down

0 comments on commit 61b4a07

Please sign in to comment.