From ea8743905e75b1f8b82fe1f7512d61ea67d616a8 Mon Sep 17 00:00:00 2001 From: Trond A Ekseth Date: Fri, 24 Jan 2025 19:17:26 +0100 Subject: [PATCH] Fix setting label from heading when processing Feature & Traits --- CHANGELOG.md | 4 +++ src/utils/web.js | 2 +- src/utils/web.test.js | 69 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5d9bc3..da5bdf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug fixes + + * Fixed header of features not being set as dice labels after D&D Beyond update. + ### Enhancements * Enabled right-click menu on monster pages. diff --git a/src/utils/web.js b/src/utils/web.js index 51f75ad..d6dd521 100644 --- a/src/utils/web.js +++ b/src/utils/web.js @@ -210,7 +210,7 @@ export const embedInText = (node, labelOrCallback, matchDicelessModifier) => { if (!label) { // Only fetch #text nodes directly under the parent. In most (all?) just // using node.firstChild.textContent would probably be enough. - const heading = getSiblingWithClass(node.parentElement, "__heading", 7); + const heading = getSiblingWithClass(node.parentElement, "_heading__", 7); if (heading) { label = Array.prototype.filter .call(heading.childNodes, (node) => node.nodeType === 3) diff --git a/src/utils/web.test.js b/src/utils/web.test.js index 7daf1e5..fc2d2ac 100644 --- a/src/utils/web.test.js +++ b/src/utils/web.test.js @@ -1,6 +1,11 @@ import { describe, expect, test } from "bun:test"; -import { getDiceRegex, isValidDice } from "~/utils/web"; +import { + embedInText, + getDiceRegex, + getTextNodes, + isValidDice, +} from "~/utils/web"; describe("diceRegex", () => { const regexGroups = (str) => @@ -219,3 +224,65 @@ describe("diceRegex", () => { }); } }); + +describe("getTextNodes + embedInText", () => { + // grom D&D Beyond Character v1.69.44 + describe("Feature & Traits", () => { + test("dice and modifier value inside tooltip", () => { + // Sneak Attack PHB + document.body.innerHTML = `
Sneak Attack4d6

PHB, pg. 96

Once per turn, you can deal an extra 4d6 damage to one creature you hit with an attack with a finesse or ranged weapon if you have advantage on the attack roll. You don’t need advantage on the attack roll if another enemy of the target is within 5 ft. of it, that enemy isn’t incapacitated, and you don’t have disadvantage on the attack roll.

Sneak Attack: (No Action)
`; + + const textNodes = getTextNodes(document.body); + expect(textNodes.length).toEqual(2); + + for (const node of textNodes) { + embedInText(node); + } + + const button = document.body.querySelector("button"); + expect(button.className).toEqual( + "integrated-dice__container tales-beyond-extension", + ); + expect(button.dataset.tsLabel).toEqual("Sneak Attack"); + expect(button.dataset.tsDice).toEqual("4d6"); + }); + + test("dice value inside tooltip and modifier outside", () => { + // Healing Hands PHB-2024 + document.body.innerHTML = `
Healing Hands

PHB-2024, pg. 186

Once per Long Rest as a Magic action, you touch a creature and they regain 2d4 HP.

Healing Hands: 1 Action
/
Long Rest
`; + + const textNodes = getTextNodes(document.body); + expect(textNodes.length).toEqual(2); + + for (const node of textNodes) { + embedInText(node); + } + + const button = document.querySelector("button"); + expect(button.className).toEqual( + "integrated-dice__container tales-beyond-extension", + ); + expect(button.dataset.tsLabel).toEqual("Healing Hands"); + expect(button.dataset.tsDice).toEqual("2d4"); + }); + + test("dice value outside tooltip and modifier inside", () => { + // Second Wind PHB + document.body.innerHTML = `
Second Wind

PHB, pg. 72

Once per short rest, you can use a bonus action to regain 1d10 + 3 HP.

Second Wind: 1 Bonus Action
/
Short Rest
`; + + const textNodes = getTextNodes(document.body); + expect(textNodes.length).toEqual(1); + + for (const node of textNodes) { + embedInText(node); + } + + const button = document.querySelector("button"); + expect(button.className).toEqual( + "integrated-dice__container tales-beyond-extension", + ); + expect(button.dataset.tsLabel).toEqual("Second Wind"); + expect(button.dataset.tsDice).toEqual("1d10+3"); + }); + }); +});