├── LICENSE ├── README.md └── getNodesByType.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Austin Cheney 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # getNodesByType 2 | a custom DOM method to get nodes by node type 3 | 4 | When the code is run a method `getNodesByType` will be attached to all element node types in the current DOM tree and the document object. 5 | 6 | ##Examples 7 | ###getNodesByType 8 | Execute the method with either a numeric value or a node type name. 9 | 10 | document.getNodesByType(1); // get all element nodes in the document 11 | document.getNodesByType("TEXT_NODE"); // get all text nodes in the document 12 | 13 | var item = document.getElementById("item"); 14 | item.getNodesByType("COMMENT_NODE"); // get all child comment node types 15 | item.getNodesByType(1); // get all child element node types 16 | 17 | ###getElementsByAttribute 18 | Updated with a new method: `getElementsByAttribute(name, value)`. Both the name and value arguments of `getElementsByAttribute` are optional. 19 | 20 | document.getElementsByAttribute("class", "segment"); // get all elements with a class attribute of value segment 21 | document.getElementsByAttribute("src", ""); // get all elements with a src attribute of any value 22 | 23 | var item = document.getElementById("item"); 24 | item.getElementsByAttribute("id"); // get all child elements with an id attribute of any value 25 | item.getElementsByAttribute("", "purple_alien"); // get all child elements with any attribute whose value is purple_alien 26 | 27 | ##Node Types 28 | Here are the supported node types and their corresponding numeric values: 29 | 30 | * 0 - all nodes (non standard) 31 | * 1 - "ELEMENT_NODE" 32 | * 2 - "ATTRIBUTE_NODE" 33 | * 3 - "TEXT_NODE" 34 | * 4 - "CDATA_SECTION_NODE" 35 | * 5 - "ENTITY_REFERENCE_NODE" 36 | * 6 - "ENTITY_NODE" 37 | * 7 - "PROCESSING_INSTRUCTION_NODE" 38 | * 8 - "COMMENT_NODE" 39 | * 9 - "DOCUMENT_NODE" 40 | * 10 - "DOCUMENT_TYPE_NODE" 41 | * 11 - "DOCUMENT_FRAGMENT_NODE" 42 | * 12 - "NOTATION_NODE" 43 | 44 | All of the above node types remain valid types in the W3C specifications, but the WHATWG HTML5 group has deprecated these types: 45 | 46 | * 2 - "ATTRIBUTE_NODE" 47 | * 4 - "CDATA_SECTION_NODE" 48 | * 5 - "ENTITY_REFERENCE_NODE" 49 | * 6 - "ENTITY_NODE" 50 | * 12 - "NOTATION_NODE" 51 | -------------------------------------------------------------------------------- /getNodesByType.js: -------------------------------------------------------------------------------- 1 | /*global document*/ 2 | //a function to get DOM nodes by nodeType property. 3 | //If you do not supply a value I will give you every DOM node. 4 | // 5 | //global examples: 6 | // var allComments = document.getNodesByType(8); 7 | // var allComments = document.getNodesByType("COMMENT_NODE"); 8 | // 9 | //or use locally: 10 | // var a = document.getElementById("item"); 11 | // item.getNodesByType(2); 12 | // 13 | //The accepted string values are the actual node type names, so that the 14 | //typeValue argument can be supplied dynamically from other code. 15 | // 16 | // 17 | // Please try http://prettydiff.com/ for all your web development needs! 18 | // 19 | // 20 | //Keep in mind that the following node types are valid in the W3C DOM 21 | //standards, but have been deprecated in the WHATWG DOM specification. 22 | // 23 | // 2 - ATTRIBUTE_NODE 24 | // 4 - CDATA_SECTION_NODE 25 | // 5 - ENTITY_REFERENCE_NODE 26 | // 6 - ENTITY_NODE 27 | // 12 - NOTATION_NODE 28 | // 29 | //This means all node types are still valid in the standard, but the 30 | //deprecated types may not be retrievable from certain DOM 31 | //implementations. 32 | // 33 | (function wrapper() { 34 | var getNodesByType = function getNodesByType(typeValue) { 35 | "use strict"; 36 | var types = 0, 37 | valueTest = (typeof typeValue === "string") ? typeValue.toUpperCase() : "", 38 | root = this; 39 | 40 | // Normalize string input for case insensitivity. 41 | if (typeof typeValue === "string") { 42 | typeValue = typeValue.toLowerCase(); 43 | } 44 | 45 | // If input is a string and supported standard value 46 | // associate to the standard numeric type 47 | if (typeValue === "all") { 48 | types = 0; 49 | } else if (typeValue === "element_node") { 50 | types = 1; 51 | } else if (typeValue === "attribute_node") { 52 | types = 2; 53 | } else if (typeValue === "text_node") { 54 | types = 3; 55 | } else if (typeValue === "cdata_section_node") { 56 | types = 4; 57 | } else if (typeValue === "entity_reference_node") { 58 | types = 5; 59 | } else if (typeValue === "entity_node") { 60 | types = 6; 61 | } else if (typeValue === "processing_instruction_node") { 62 | types = 7; 63 | } else if (typeValue === "comment_node") { 64 | types = 8; 65 | } else if (typeValue === "document_node") { 66 | types = 9; 67 | } else if (typeValue === "document_type_node") { 68 | types = 10; 69 | } else if (typeValue === "document_fragment_node") { 70 | types = 11; 71 | } else if (typeValue === "notation_node") { 72 | types = 12; 73 | } 74 | 75 | // If input is type string but the value is a supported number 76 | if (isNaN(valueTest) === false && (valueTest.length === 1 || valueTest === "10" || valueTest === "11" || valueTest === "12")) { 77 | types = Number(valueTest); 78 | } 79 | 80 | // If input is a supported number 81 | if (valueTest === "" && (typeValue === 0 || typeValue === 1 || typeValue === 2 || typeValue === 3 || typeValue === 4 || typeValue === 5 || typeValue === 6 || typeValue === 7 || typeValue === 8 || typeValue === 9 || typeValue === 10 || typeValue === 11 || typeValue === 12)) { 82 | types = typeValue; 83 | } 84 | 85 | // Identify the starting point. When used globally the root 86 | // element is the document's root, which is typically . 87 | // When used locally this is the node on which the method is 88 | // executed against. 89 | if (root === document) { 90 | root = document.documentElement; 91 | } 92 | 93 | // A handy dandy function to trap all the DOM walking 94 | return (function getNodesByType_walking() { 95 | var output = [], 96 | child = function getNodesByType_walking_child(x) { 97 | var atty = [], 98 | a = x.attributes, 99 | b = a.length, 100 | c = 0; 101 | // Special functionality for attribute types. 102 | if (b > 0 && (types === 2 || types === 0)) { 103 | do { 104 | output.push(a[c]); 105 | c += 1; 106 | } while (c < b); 107 | } 108 | a = x.childNodes; 109 | b = a.length; 110 | c = 0; 111 | if (b > 0) { 112 | do { 113 | if (a[c].nodeType === types || types === 0) { 114 | output.push(a[c]); 115 | } 116 | if (a[c].nodeType === 1) { 117 | //recursion magic 118 | getNodesByType_walking_child(a[c]); 119 | } 120 | c += 1; 121 | } while (c < b); 122 | } 123 | }; 124 | child(root); 125 | return output; 126 | }()); 127 | }, 128 | getElementsByAttribute = function getElementsByAttribute(name, value) { 129 | var attrs = this.getNodesByType(2), 130 | out = []; 131 | if (typeof name !== "string") { 132 | name = ""; 133 | } 134 | if (typeof value !== "string") { 135 | value = ""; 136 | } 137 | attrs.forEach(function getElementsByAttribute_loop(item) { 138 | if (item.name === name || name === "") { 139 | if (item.value === value || value === "") { 140 | out.push(item.ownerElement); 141 | } 142 | } 143 | }); 144 | return out; 145 | }; 146 | 147 | // Create a document method 148 | document.getNodesByType = getNodesByType; 149 | document.getElementsByAttribute = getElementsByAttribute; 150 | 151 | (function addToExistingElements() { 152 | var el = document.getNodesByType(1); 153 | el.forEach(function addToExistingElements_loop(item) { 154 | item.getNodesByType = getNodesByType; 155 | item.getElementsByAttribute = getElementsByAttribute; 156 | }); 157 | }()); 158 | // Add this code as a method onto each DOM element 159 | 160 | // Ensure dynamically created elements get this method too 161 | Element.prototype.getNodesByType = getNodesByType; 162 | Element.prototype.getElementsByAttribute = getElementsByAttribute; 163 | 164 | }()); 165 | --------------------------------------------------------------------------------