This is an old revision of this page, as edited by Proteins(talk | contribs) at 19:38, 23 October 2008(refine the conditional to eliminate DD elements used only for formatting). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 19:38, 23 October 2008 by Proteins(talk | contribs)(refine the conditional to eliminate DD elements used only for formatting)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>// Unindent discusions for accessibilityfunctionunindent(){varalert_string="";vardiagnostic_string="";varindent_level=0;varindent_level_string="";varDL_elements;varnum_DL_elements=0;varDT_elements;varnum_DT_elements=0;varDD_elements;vartemp_DD_element;varnum_DD_elements=0;varDD_element_index=0;varnum_unindented_DD_elements=0;vartop_node;varchild_node;varparent_node;vartop_DL_node;vartop_DD_node;varuntagged_text;varbyte_count=0;varnum_child_nodes=0;// Colors to help sighted people after the unindentingvarnum_colors=0;varDD_background_colors=["white","yellow","greenyellow","gold","lawngreen","orange","lightskyblue","darkorange"];// Initializationnum_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 beginningdiagnostic_string="";for(DD_element_index=0;DD_element_index<num_DD_elements;DD_element_index++){temp_DD_element=DD_elements[DD_element_index];// Skip empty DD elementsnum_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 withinuntagged_text=temp_DD_element.innerHTML.replace(/(<dl([^>]*)>)(.*<\/dl>)/ig,"");// remove inner DL'suntagged_text=untagged_text.replace(/(<([^>]+)>)/ig,"");// remove other HTML tagsbyte_count=untagged_text.replace(/\s/ig,"").length;// remove whitespacesif(byte_count<1){continue;}// Find the topmost DL element for this DD nodeindent_level=0;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 nodesif(parent_node.nodeName=="DL"){// Check whether the parent DL element has any DT elementsnum_DT_elements=parent_node.getElementsByTagName("DT").length;if(num_DT_elements>0){break;}// if so, stop unindenting...// ...else make this the new indent levelindent_level++;top_DL_node=parent_node;}// closes check for a parental DL elementif(parent_node.nodeName=="DD"){top_DD_node=parent_node;}parent_node=parent_node.parentNode;}// closes loop climbing up the document treeif(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 level "+indent_level+") ";child_node=temp_DD_element.firstChild;if(child_node){temp_DD_element.insertBefore(document.createTextNode(indent_level_string),child_node);}else{indent_level_string+="No text in this DD element.\n";temp_DD_element.appendChild(document.createTextNode(indent_level_string));}temp_DD_element.normalize();diagnostic_string+="DD element "+DD_element_index+" is indented to level "+indent_level+".\n";}// check for unindenting}// closes loop over the DD elements of the document// window.alert(diagnostic_string);// Second-pass loop changes the document tree structurediagnostic_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 nodetop_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 nodesif(parent_node.nodeName=="DL"){// Check whether the parent DL element has any DT elementsnum_DT_elements=parent_node.getElementsByTagName("DT").length;if(num_DT_elements>0){break;}// if so, stop unindenting...// ...else make this the new indent levelindent_level++;top_DL_node=parent_node;}// closes check for a parental DL elementif(parent_node.nodeName=="DD"){top_DD_node=parent_node;}parent_node=parent_node.parentNode;}// closes loop climbing up the document treeif(indent_level>0){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.nextSibling;}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//Acknowledgmentalert_string="Unindented "+num_unindented_DD_elements+" paragraphs of "+num_DD_elements+" possible.";window.alert(alert_string);}// closes unindent() functionaddOnloadHook(function(){addPortletLink('p-cactions','javascript:unindent()','unindent','ca-unindent','Unindent discussions','','');});//</pre>