From 958fb12fb72f7efe1f3350bfed64e6af04d5ea3b Mon Sep 17 00:00:00 2001 From: Matthias Leuffen Date: Tue, 26 Mar 2024 15:25:08 +0100 Subject: [PATCH] sync --- package.json | 2 +- src/helper/functions.ts | 49 +++++++++++++++++++++++++++++++------- src/processor/jodasplit.ts | 13 +++++++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index b62fcf3..d84fa1a 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helper/functions.ts b/src/helper/functions.ts index c6c81f0..edbb68a 100755 --- a/src/helper/functions.ts +++ b/src/helper/functions.ts @@ -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"; @@ -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 { @@ -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) => { @@ -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); diff --git a/src/processor/jodasplit.ts b/src/processor/jodasplit.ts index 2c5adc1..a567519 100755 --- a/src/processor/jodasplit.ts +++ b/src/processor/jodasplit.ts @@ -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-")) { @@ -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")) {