Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatthes committed Mar 26, 2024
1 parent 2a46001 commit 958fb12
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leuffen/jodastyle",
"version": "3.0.9",
"version": "3.1.0",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.module.js",
Expand Down
49 changes: 40 additions & 9 deletions src/helper/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {DefaultLayout} from "../types/DefaultLayout";
import {JodaElementException} from "./JodaElementException";
import {QTemplate, template_parse} from "./QTemplate";
import {Joda} from "../joda";
import {JodaErrorElement} from "./JodaErrorElement";



Expand Down Expand Up @@ -133,6 +134,41 @@ function copyDataChildAttributes(source : HTMLElement, target : HTMLElement) {
}


/**
* Allow multiple Queries separated by || statement. Returns first element found
*
* @param selector
* @param element
* @param limit
* @returns Element found
*/
function queryMulti (selector : string, element : HTMLElement, limit : number = null) : Element[] {

let selectors = selector.split("||");
for (let sel of selectors) {
sel = sel.trim();
if (sel === "")
return [element];
try {
let found = element.querySelectorAll(sel);
if (found.length === 0)
continue;
if (limit === null) {
return Array.from(found);
}

return Array.from(found).slice(0, limit);
} catch (e) {
console.warn("Invalid selector: ", sel, "on element", element);
return [new JodaErrorElement("Invalid selector: " + sel + " on element " + element)];
continue;
}


}
return [];
}


let slotIndex = 0;
export async function getTemplateFilledWithContent(templateSelector : string, content : HTMLElement, origElement : HTMLElement) : Promise<DocumentFragment> {
Expand Down Expand Up @@ -191,9 +227,9 @@ export async function getTemplateFilledWithContent(templateSelector : string, co

let selected : any;
if (slot.getAttribute("data-limit") === "1") {
selected = Array.from([content.querySelector(select)]).map((element) => element.cloneNode(true));
selected = queryMulti(select, content, 1).map((element) => element.cloneNode(true));
} else {
selected = Array.from(content.querySelectorAll(select)).map((element) => element.cloneNode(true));
selected = queryMulti(select, content).map((element) => element.cloneNode(true));
}

selected.forEach((element) => {
Expand Down Expand Up @@ -223,14 +259,9 @@ export async function getTemplateFilledWithContent(templateSelector : string, co

let selected: any;
if (slot.getAttribute("data-limit") === "1") {
let curElements = content.querySelector(select);
if (curElements === null) {
selected = [];
} else {
selected = Array.from([content.querySelector(select)]);
}
selected = queryMulti(select, content, 1)
} else {
selected = Array.from(content.querySelectorAll(select));
selected = queryMulti(select, content)
}
if (selected.length === 0) {
console.warn("No element found for selector: " + select + " in template: " + templateSelector + " for slot: ", slot);
Expand Down
13 changes: 12 additions & 1 deletion src/processor/jodasplit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ interface JodaSplitConfig {
* @param target
*/
function copySectionAttributes(source : HTMLElement, target : HTMLElement) {

for(let className of source.getAttribute("class")?.split(" ") || []){
// if className starts with sec- it is a section class. Copy the name after sec- and add it to the target
// remove the sec- prefix

if (className.startsWith("sec-")) {
target.classList.add(className.substr(4));
source.classList.remove(className);
}
}

source.getAttributeNames().forEach((name : string) => {

if ( ! name.startsWith("data-section-")) {
Expand Down Expand Up @@ -105,7 +116,7 @@ export class Jodasplit {
let layer = 1;
let tag = "div";

if (child.matches("h1,h2,.section-h2")) {
if (child.matches("h1,h2,.section-h2") && !child.matches(".section-h3, .section-h4")) {
layer = lastLayer = 1;
tag = "section";
} else if (child.matches("h3, h4, h5, h6, h7, h8, h9, .section-h3, .section-h4")) {
Expand Down

0 comments on commit 958fb12

Please sign in to comment.