Skip to content

Adds an excluded attributes list #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export default {
}
});

let skipAttributes = {};

settings.excluded_attributes.split("|").forEach((attr) => {
attr = attr.trim().toLowerCase();
if (attr !== "") {
skipAttributes[attr] = 1;
}
});

let createLink = function (text, url) {
let link = document.createElement("a");
link.innerHTML = text;
Expand All @@ -52,7 +61,7 @@ export default {
(element) => {
actions.forEach((action) => {
if (Object.keys(action.inputs).length > 0) {
traverseNodes(element, action, skipTags, skipClasses);
traverseNodes(element, action, skipTags, skipClasses, skipAttributes);
}
});
},
Expand Down
17 changes: 14 additions & 3 deletions javascripts/discourse-linkify/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,26 @@ const isSkippedClass = function (classes, skipClasses) {
return classes && classes.split(" ").some((cls) => cls in skipClasses);
};

const traverseNodes = function (elem, action, skipTags, skipClasses) {
const isSkippedAttribute = function (attributes, skipAttributes) {
// Return true if at least one of the attributes should be skipped
for(var i = attributes.length - 1; i >= 0; i--) {
if (attributes[i].name in skipAttributes) {
return true;
}
}
return false;
};

const traverseNodes = function (elem, action, skipTags, skipClasses, skipAttributes) {
// work backwards so changes do not break iteration
for (let i = elem.childNodes.length - 1; i >= 0; i--) {
let child = elem.childNodes[i];
if (child.nodeType === 1) {
let tag = child.nodeName.toLowerCase();
let cls = child.getAttribute("class");
if (!(tag in skipTags) && !isSkippedClass(cls, skipClasses)) {
traverseNodes(child, action, skipTags, skipClasses);
let attr = child.attributes;
if (!(tag in skipTags) && !isSkippedClass(cls, skipClasses) && !isSkippedAttribute(attr, skipAttributes)) {
traverseNodes(child, action, skipTags, skipClasses, skipAttributes);
}
} else if (child.nodeType === 3) {
modifyText(child, action);
Expand Down
4 changes: 4 additions & 0 deletions settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ excluded_classes:
type: list
list_type: compact
default: "onebox"
excluded_attributes:
type: list
list_type: compact
default: "data-nolinkify"