Skip to content

Commit

Permalink
Merge pull request #858 from camille-hdl/feature/fix-ancestor-firefox
Browse files Browse the repository at this point in the history
Corrige la récupération du `did` car apparemment
  • Loading branch information
camille-hdl authored Oct 12, 2023
2 parents 0b32e26 + d56e3c1 commit ce0c069
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/lib/recipes/individual-recipes/deplacer-unitdate-unittitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { xpathFilter } from "../../xml.js";
import { each, getAttributesMap } from "../utils.js";
import { last, head } from "ramda";

function findParentNode(elem: Element, nodeName: string): ?Element {
const parent = elem.parentNode;
if (!parent) {
return null;
}
if (parent instanceof Element && parent.nodeName === nodeName) {
return parent;
}
return parent instanceof Element ? findParentNode(parent, nodeName) : null;
}

/**
* Si on trouve un unitdate dans le unittitle,
* on le déplace dans le did en laissant juste la valeur texte dans le unittitle,
Expand All @@ -14,7 +25,11 @@ export default () => (doc: Document): Document => {
each(unitdatesDansTitle, (elem) => {
const attributesMap = getAttributesMap(elem);
const textContent = (""+elem?.textContent).trim();
const did = head(xpathFilter(doc, elem, "ancestor::c/did"));
const did = findParentNode(elem, "did");
if (!did) {
console.log("Pas de did pour créer le unitdate.");
return;
}
console.log(elem, attributesMap, textContent);
/**
* Pour qu'on considère que 2 unitdates sont identiques, ils doivent avoir la même valeur texte, la même valeur normalisée
Expand All @@ -31,13 +46,8 @@ export default () => (doc: Document): Document => {
console.log("existing = ", existingDidUnitdate);
if (!existingDidUnitdate) {
// ajouter le unitdate au did
if (!did) {
console.log("Pas de did pour créer le unitdate.");
} else {
const newElem = elem.cloneNode(true);
did.appendChild(newElem);
console.log("append dans did", newElem);
}
const newElem = elem.cloneNode(true);
did.appendChild(newElem);
}
// remplacer elem dans son parent par textContent
const parent = elem.parentNode;
Expand Down

0 comments on commit ce0c069

Please sign in to comment.