FIX: Restore replacement functionality for a[href] attributes (#42)
Followup to 948634fe31
This commit is contained in:
parent
83d5f2f914
commit
a37e4bf266
|
@ -64,24 +64,7 @@ function buildSelect(key, placeholder) {
|
|||
return select;
|
||||
}
|
||||
|
||||
function performReplacements(cooked, placeholders) {
|
||||
cooked.querySelectorAll(VALID_TAGS).forEach((elem) => {
|
||||
const textNodeWalker = document.createTreeWalker(
|
||||
elem,
|
||||
NodeFilter.SHOW_TEXT
|
||||
);
|
||||
|
||||
while (textNodeWalker.nextNode()) {
|
||||
const node = textNodeWalker.currentNode;
|
||||
|
||||
if (!originalContentMap.has(node)) {
|
||||
// Haven't seen this node before. Get the text, and store it for future transformations
|
||||
originalContentMap.set(node, node.data);
|
||||
}
|
||||
|
||||
const originalText = originalContentMap.get(node);
|
||||
let text = originalText;
|
||||
|
||||
function replaceInText(text, placeholders) {
|
||||
for (const [key, { delimiter, value }] of Object.entries(placeholders)) {
|
||||
const placeholderWithDelimiter = `${delimiter}${key}${delimiter}`;
|
||||
|
||||
|
@ -92,11 +75,48 @@ function performReplacements(cooked, placeholders) {
|
|||
|
||||
text = text.replaceAll(placeholderWithDelimiter, substitution);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function performReplacements(cooked, placeholders) {
|
||||
cooked.querySelectorAll(VALID_TAGS).forEach((elem) => {
|
||||
const textNodeWalker = document.createTreeWalker(
|
||||
elem,
|
||||
NodeFilter.SHOW_TEXT
|
||||
);
|
||||
|
||||
// Handle text nodes
|
||||
while (textNodeWalker.nextNode()) {
|
||||
const node = textNodeWalker.currentNode;
|
||||
|
||||
if (!originalContentMap.has(node)) {
|
||||
// Haven't seen this node before. Get the text, and store it for future transformations
|
||||
originalContentMap.set(node, node.data);
|
||||
}
|
||||
|
||||
const originalText = originalContentMap.get(node);
|
||||
const text = replaceInText(originalText, placeholders);
|
||||
|
||||
if (node.data !== text) {
|
||||
node.data = text;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle a[href] attributes
|
||||
document.querySelectorAll("a[href]").forEach((link) => {
|
||||
const hrefAttr = link.attributes.getNamedItem("href");
|
||||
|
||||
if (!originalContentMap.has(hrefAttr)) {
|
||||
// Haven't seen this attr before. Get the text, and store it for future transformations
|
||||
originalContentMap.set(hrefAttr, hrefAttr.value);
|
||||
}
|
||||
const originalUrl = originalContentMap.get(hrefAttr);
|
||||
const newUrl = replaceInText(originalUrl, placeholders);
|
||||
|
||||
if (hrefAttr.value !== newUrl) {
|
||||
hrefAttr.value = newUrl;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -47,4 +47,30 @@ RSpec.describe "Placeholder", system: true do
|
|||
expect(page).to have_content("BEFORE foo bar AFTER")
|
||||
end
|
||||
end
|
||||
|
||||
context "when placeholder is used in a[href]" do
|
||||
fab!(:post) do
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: <<~MD
|
||||
[wrap=placeholder key=\"TEST1\"][/wrap]
|
||||
[Some link](https://example.com/=TEST1=)
|
||||
MD
|
||||
)
|
||||
end
|
||||
|
||||
it "replaces string in href" do
|
||||
topic_page.visit_topic(post.topic)
|
||||
|
||||
expect(page).to have_link(href: "https://example.com/=TEST1=")
|
||||
|
||||
page.find('.discourse-placeholder-value[data-key="TEST1"]').fill_in(with: "foo")
|
||||
|
||||
expect(page).to have_link(href: "https://example.com/foo")
|
||||
|
||||
page.find('.discourse-placeholder-value[data-key="TEST1"]').fill_in(with: "bar")
|
||||
|
||||
expect(page).to have_link(href: "https://example.com/bar")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue