User:Proteins/unindent.js: Difference between revisions
Appearance
Content deleted Content added
m proper conditional |
m Maintenance: Replacing addPortletLink() with mw.util.addPortletLink() (mw:ResourceLoader/Migration_guide_(users)#addPortletLink) |
||
(35 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
//<pre> |
//<pre> |
||
// Unindent |
// Unindent discussions for accessibility |
||
// WARNING WARNING WARNING ----- DO NOT USE THIS SCRIPT |
|||
// OBSOLETE AND BUGGY ----- DO NOT USE THIS SCRIPT |
|||
// NOT DELETED ONLY BECAUSE I WANT TO REMEMBER SOME CODING IDEAS AND PITFALLS IN JAVASCRIPT |
|||
function unindent() { |
function unindent() { |
||
Line 8: | Line 11: | ||
var indent_level = 0; |
var indent_level = 0; |
||
var indent_level_string = ""; |
var indent_level_string = ""; |
||
var prev_indent_level_string = ""; |
|||
var prepended_string = ""; |
|||
var empty_prepended_string = " "; |
|||
var DL_elements; |
var DL_elements; |
||
Line 19: | Line 26: | ||
var num_DD_elements = 0; |
var num_DD_elements = 0; |
||
var DD_element_index = 0; |
var DD_element_index = 0; |
||
var num_unindented_DD_elements = 0; |
var num_unindented_DD_elements = 0; |
||
var num_higher_level_DD_elements = 0; |
|||
var top_node; |
var top_node; |
||
var child_node; |
var child_node; |
||
var parent_node; |
var parent_node; |
||
var top_DL_node; |
|||
var top_DD_node; |
var top_DD_node; |
||
var |
var top_DL_node; |
||
var |
var prev_top_DL_node; |
||
var temp_DD_text; |
|||
var untagged_text; |
|||
var unspaced_text; |
|||
var byte_count = 0; |
|||
var num_child_nodes = 0; |
|||
var num_sub_DL_elements = 0; |
|||
var sibling_node; |
|||
var sibling_node_list = new Array(); |
|||
// Colors to help sighted people after the unindenting |
// Colors to help sighted people after the unindenting |
||
var num_colors = 0; |
var num_colors = 0; |
||
var DD_background_colors = ["white", "yellow", "greenyellow", "gold", " |
var DD_background_colors = ["white", "yellow", "greenyellow", "gold", "lightskyblue", "orange", "lawngreen", "darkorange"]; |
||
Line 44: | Line 63: | ||
// First-pass loop colors the links and adds the level to the beginning |
// First-pass loop colors the links and adds the level to the beginning |
||
diagnostic_string = ""; |
diagnostic_string = ""; |
||
num_unindented_DD_elements = 0; |
|||
num_higher_level_DD_elements = 0; |
|||
top_DL_node = null; |
|||
prev_top_DL_node = null; |
|||
prepended_string = ""; |
|||
indent_level_string = ""; |
|||
prev_indent_level_string = ""; |
|||
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) { |
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) { |
||
temp_DD_element = DD_elements[DD_element_index]; |
temp_DD_element = DD_elements[DD_element_index]; |
||
child_node = temp_DD_element.firstChild; |
|||
if (child_node) { |
|||
if ((child_node.nodeType == 3) && (child_node.data.match(/^\(Indent\s\d+\)/))) { |
|||
continue; |
|||
} // add "(Indent #)" to beginning if it isn't there already |
|||
} |
|||
num_sub_DL_elements = temp_DD_element.getElementsByTagName("DL").length; |
|||
// Skip empty DD elements |
// Skip empty DD elements |
||
num_child_nodes = temp_DD_element.childNodes.length; |
num_child_nodes = temp_DD_element.childNodes.length; |
||
if (!num_child_nodes) { continue; } // ignore empty DD elements |
if (!num_child_nodes) { continue; } // ignore empty DD elements |
||
byte_count = temp_DD_element.replace(/(<([^>]+)>)/ig,"").replace(/\s/ig, "").length; // remove HTML tags |
|||
//Alternative approach: skip all DD elements that have a sub DL element within |
|||
temp_DD_text = temp_DD_element.innerHTML; |
|||
unspaced_text = temp_DD_text.replace(/\s/ig, ""); // remove whitespaces |
|||
untagged_text = unspaced_text.replace(/<dl>.*<\/dl>/ig, ""); // remove inner DL's |
|||
untagged_text = untagged_text.replace(/(<([^>]+)>)/ig,""); // remove other HTML tags |
|||
byte_count = untagged_text.length; // count characters |
|||
if (byte_count < 1) { continue; } |
if (byte_count < 1) { continue; } |
||
// Find the topmost DL element for this DD node |
// Find the topmost DL element for this DD node |
||
indent_level = 0; |
indent_level = 0; |
||
top_DL_node = null; |
top_DL_node = null; |
||
top_DD_node = |
top_DD_node = temp_DD_element; |
||
parent_node = temp_DD_element.parentNode; |
parent_node = temp_DD_element.parentNode; |
||
Line 79: | Line 125: | ||
parent_node = parent_node.parentNode; |
parent_node = parent_node.parentNode; |
||
} // closes loop climbing up the document tree |
} // closes loop climbing up the document tree |
||
if (top_DL_node != prev_top_DL_node) { |
|||
prev_indent_level_string = ""; |
|||
} |
|||
prev_top_DL_node = top_DL_node; |
|||
if (indent_level > 2) { num_higher_level_DD_elements++; } |
|||
if (indent_level > 0) { |
if (indent_level > 0) { |
||
Line 84: | Line 137: | ||
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors]; |
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors]; |
||
indent_level_string = "(Indent |
indent_level_string = "(Indent " + indent_level + ") "; |
||
// indent_level_string = "(Indent " + indent_level + " " + num_sub_DL_elements + ") "; |
|||
// if (num_sub_DL_elements > 0) { |
|||
// indent_level_string += "[ " + num_sub_DL_elements + " " + byte_count + " " + untagged_text + " ] "; |
|||
// } |
|||
if (indent_level_string == prev_indent_level_string) { |
|||
prepended_string = empty_prepended_string; |
|||
} else { |
|||
prepended_string = indent_level_string; |
|||
} |
|||
prev_indent_level_string = indent_level_string; |
|||
child_node = temp_DD_element.firstChild; |
child_node = temp_DD_element.firstChild; |
||
if (child_node) { |
if (child_node) { |
||
if ((child_node.nodeType != 3) || (!child_node.data.match(/^\(Indent\s\d+\)/))) { |
|||
temp_DD_element.insertBefore(document.createTextNode(indent_level_string), child_node); |
|||
temp_DD_element.insertBefore(document.createTextNode(prepended_string), child_node); |
|||
} // add "(Indent #)" to beginning if it isn't there already |
|||
} else { |
} else { |
||
indent_level_string += "No text in this DD element.\n"; |
indent_level_string += "No text in this DD element.\n"; |
||
temp_DD_element.appendChild(document.createTextNode( |
temp_DD_element.appendChild(document.createTextNode(prepended_string)); |
||
} |
|||
temp_DD_element.normalize(); |
|||
sibling_node_list[DD_element_index] = null; |
|||
if ((top_DD_node) && (top_DL_node)) { |
|||
sibling_node = top_DD_node.nextSibling; |
|||
while ((sibling_node) && (sibling_node.nodeType != 1)) { |
|||
sibling_node = sibling_node.nextSibling; |
|||
} |
|||
} // closes check that both top_DD_node and top_DL_node are defined |
|||
if (sibling_node_list[DD_element_index]) { |
|||
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n"; |
|||
} else { |
|||
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ". NULL SIBLING\n"; |
|||
} |
} |
||
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n"; |
|||
} // check for unindenting |
} // check for unindenting |
||
} // closes loop over the DD elements of the document |
} // closes loop over the DD elements of the document |
||
// window.alert(diagnostic_string); |
|||
// Prevents repeated applications of the script from changing the colors |
|||
if (num_higher_level_DD_elements == 0) { return; } |
|||
// Second-pass loop changes the document tree structure |
// Second-pass loop changes the document tree structure |
||
Line 130: | Line 215: | ||
} // closes loop climbing up the document tree |
} // closes loop climbing up the document tree |
||
if (indent_level > |
if (indent_level > 1) { |
||
if ((top_DD_node) && (top_DL_node)) { |
if ((top_DD_node) && (top_DL_node)) { |
||
// top_DL_node.insertBefore(temp_DD_element, top_DD_element); |
|||
sibling_node = top_DD_node.nextSibling; |
|||
while ((sibling_node) && (sibling_node.nodeType != 1)) { |
|||
sibling_node = sibling_node_list[DD_element_index]; |
|||
} |
|||
if (sibling_node) { |
if (sibling_node) { |
||
top_DL_node.insertBefore(temp_DD_element, sibling_code); |
top_DL_node.insertBefore(temp_DD_element, sibling_code); |
||
Line 145: | Line 228: | ||
} // check for unindenting |
} // check for unindenting |
||
} // closes loop over the DD elements of the document |
} // closes loop over the DD elements of the document |
||
// window.alert(diagnostic_string); |
|||
// Third-pass loop to uncreate empty discursive lists |
|||
//Acknowledgment |
//Acknowledgment |
||
Line 154: | Line 239: | ||
addOnloadHook(function () { |
addOnloadHook(function () { |
||
addPortletLink('p-cactions', 'javascript:unindent()', 'unindent', 'ca-unindent', 'Unindent discussions', '', ''); |
mw.util.addPortletLink('p-cactions', 'javascript:unindent()', 'unindent', 'ca-unindent', 'Unindent discussions', 'i', ''); |
||
}); |
}); |
||
Latest revision as of 07:46, 29 November 2021
//<pre>
// Unindent discussions for accessibility
// WARNING WARNING WARNING ----- DO NOT USE THIS SCRIPT
// OBSOLETE AND BUGGY ----- DO NOT USE THIS SCRIPT
// NOT DELETED ONLY BECAUSE I WANT TO REMEMBER SOME CODING IDEAS AND PITFALLS IN JAVASCRIPT
function unindent() {
var alert_string = "";
var diagnostic_string = "";
var indent_level = 0;
var indent_level_string = "";
var prev_indent_level_string = "";
var prepended_string = "";
var empty_prepended_string = " ";
var DL_elements;
var num_DL_elements = 0;
var DT_elements;
var num_DT_elements = 0;
var DD_elements;
var temp_DD_element;
var num_DD_elements = 0;
var DD_element_index = 0;
var num_unindented_DD_elements = 0;
var num_higher_level_DD_elements = 0;
var top_node;
var child_node;
var parent_node;
var top_DD_node;
var top_DL_node;
var prev_top_DL_node;
var temp_DD_text;
var untagged_text;
var unspaced_text;
var byte_count = 0;
var num_child_nodes = 0;
var num_sub_DL_elements = 0;
var sibling_node;
var sibling_node_list = new Array();
// Colors to help sighted people after the unindenting
var num_colors = 0;
var DD_background_colors = ["white", "yellow", "greenyellow", "gold", "lightskyblue", "orange", "lawngreen", "darkorange"];
// Initialization
num_colors = DD_background_colors.length;
top_node = document.getElementById('bodyContent');
DD_elements = top_node.getElementsByTagName("DD");
num_DD_elements = DD_elements.length;
// First-pass loop colors the links and adds the level to the beginning
diagnostic_string = "";
num_unindented_DD_elements = 0;
num_higher_level_DD_elements = 0;
top_DL_node = null;
prev_top_DL_node = null;
prepended_string = "";
indent_level_string = "";
prev_indent_level_string = "";
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
temp_DD_element = DD_elements[DD_element_index];
child_node = temp_DD_element.firstChild;
if (child_node) {
if ((child_node.nodeType == 3) && (child_node.data.match(/^\(Indent\s\d+\)/))) {
continue;
} // add "(Indent #)" to beginning if it isn't there already
}
num_sub_DL_elements = temp_DD_element.getElementsByTagName("DL").length;
// Skip empty DD elements
num_child_nodes = temp_DD_element.childNodes.length;
if (!num_child_nodes) { continue; } // ignore empty DD elements
//Alternative approach: skip all DD elements that have a sub DL element within
temp_DD_text = temp_DD_element.innerHTML;
unspaced_text = temp_DD_text.replace(/\s/ig, ""); // remove whitespaces
untagged_text = unspaced_text.replace(/<dl>.*<\/dl>/ig, ""); // remove inner DL's
untagged_text = untagged_text.replace(/(<([^>]+)>)/ig,""); // remove other HTML tags
byte_count = untagged_text.length; // count characters
if (byte_count < 1) { continue; }
// Find the topmost DL element for this DD node
indent_level = 0;
top_DL_node = null;
top_DD_node = temp_DD_element;
parent_node = temp_DD_element.parentNode;
while ((parent_node) && (parent_node != top_node)) {
if (parent_node.nodeType != 1) {
parent_node = parent_node.parentNode;
continue;
} // examine only Element nodes
if (parent_node.nodeName == "DL") {
// Check whether the parent DL element has any DT elements
num_DT_elements = parent_node.getElementsByTagName("DT").length;
if (num_DT_elements > 0) { break; } // if so, stop unindenting...
// ...else make this the new indent level
indent_level++;
top_DL_node = parent_node;
} // closes check for a parental DL element
if (parent_node.nodeName == "DD") { top_DD_node = parent_node; }
parent_node = parent_node.parentNode;
} // closes loop climbing up the document tree
if (top_DL_node != prev_top_DL_node) {
prev_indent_level_string = "";
}
prev_top_DL_node = top_DL_node;
if (indent_level > 2) { num_higher_level_DD_elements++; }
if (indent_level > 0) {
num_unindented_DD_elements++;
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors];
indent_level_string = "(Indent " + indent_level + ") ";
// indent_level_string = "(Indent " + indent_level + " " + num_sub_DL_elements + ") ";
// if (num_sub_DL_elements > 0) {
// indent_level_string += "[ " + num_sub_DL_elements + " " + byte_count + " " + untagged_text + " ] ";
// }
if (indent_level_string == prev_indent_level_string) {
prepended_string = empty_prepended_string;
} else {
prepended_string = indent_level_string;
}
prev_indent_level_string = indent_level_string;
child_node = temp_DD_element.firstChild;
if (child_node) {
if ((child_node.nodeType != 3) || (!child_node.data.match(/^\(Indent\s\d+\)/))) {
temp_DD_element.insertBefore(document.createTextNode(prepended_string), child_node);
} // add "(Indent #)" to beginning if it isn't there already
} else {
indent_level_string += "No text in this DD element.\n";
temp_DD_element.appendChild(document.createTextNode(prepended_string));
}
temp_DD_element.normalize();
sibling_node_list[DD_element_index] = null;
if ((top_DD_node) && (top_DL_node)) {
sibling_node = top_DD_node.nextSibling;
while ((sibling_node) && (sibling_node.nodeType != 1)) {
sibling_node = sibling_node.nextSibling;
}
} // closes check that both top_DD_node and top_DL_node are defined
if (sibling_node_list[DD_element_index]) {
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n";
} else {
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ". NULL SIBLING\n";
}
} // check for unindenting
} // closes loop over the DD elements of the document
// window.alert(diagnostic_string);
// Prevents repeated applications of the script from changing the colors
if (num_higher_level_DD_elements == 0) { return; }
// Second-pass loop changes the document tree structure
diagnostic_string = "";
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
temp_DD_element = DD_elements[DD_element_index];
indent_level = 0;
// Find the topmost DL element for this DD node
top_DL_node = null;
top_DD_node = null;
parent_node = temp_DD_element.parentNode;
while ((parent_node) && (parent_node != top_node)) {
if (parent_node.nodeType != 1) {
parent_node = parent_node.parentNode;
continue;
} // examine only Element nodes
if (parent_node.nodeName == "DL") {
// Check whether the parent DL element has any DT elements
num_DT_elements = parent_node.getElementsByTagName("DT").length;
if (num_DT_elements > 0) { break; } // if so, stop unindenting...
// ...else make this the new indent level
indent_level++;
top_DL_node = parent_node;
} // closes check for a parental DL element
if (parent_node.nodeName == "DD") { top_DD_node = parent_node; }
parent_node = parent_node.parentNode;
} // closes loop climbing up the document tree
if (indent_level > 1) {
if ((top_DD_node) && (top_DL_node)) {
// top_DL_node.insertBefore(temp_DD_element, top_DD_element);
sibling_node = sibling_node_list[DD_element_index];
if (sibling_node) {
top_DL_node.insertBefore(temp_DD_element, sibling_code);
} else {
top_DL_node.appendChild(temp_DD_element);
}
} // check that both the top_DD and top_DL elements are defined
} // check for unindenting
} // closes loop over the DD elements of the document
// window.alert(diagnostic_string);
// Third-pass loop to uncreate empty discursive lists
//Acknowledgment
alert_string = "Unindented " + num_unindented_DD_elements + " paragraphs of " + num_DD_elements + " possible.";
window.alert(alert_string);
} // closes unindent() function
addOnloadHook(function () {
mw.util.addPortletLink('p-cactions', 'javascript:unindent()', 'unindent', 'ca-unindent', 'Unindent discussions', 'i', '');
});
//</pre>