1 /**
  2  * Olives http://flams.github.com/olives
  3  * The MIT License (MIT)
  4  * Copyright (c) 2012-2014 Olivier Scherrer <pode.fr@gmail.com> - Olivier Wietrich <olivier.wietrich@gmail.com>
  5  */
  6 "use strict";
  7 
  8 var Tools = require("emily").Tools;
  9 
 10 module.exports = {
 11     /**
 12      * Returns a NodeList including the given dom node,
 13      * its childNodes and its siblingNodes
 14      * @param {HTMLElement|SVGElement} dom the dom node to start with
 15      * @param {String} query an optional CSS selector to narrow down the query
 16      * @returns the list of nodes
 17      */
 18     getNodes: function getNodes(dom, query) {
 19         if (this.isAcceptedType(dom)) {
 20             if (!dom.parentNode) {
 21                 document.createDocumentFragment().appendChild(dom);
 22             }
 23 
 24             return dom.parentNode.querySelectorAll(query || "*");
 25         } else {
 26             return false;
 27         }
 28     },
 29 
 30     /**
 31      * Get a domNode's dataset attribute. If dataset doesn't exist (IE)
 32      * then the domNode is looped through to collect them.
 33      * @param {HTMLElement|SVGElement} dom
 34      * @returns {Object} dataset
 35      */
 36     getDataset: function getDataset(dom) {
 37         var i=0,
 38             l,
 39             dataset={},
 40             split,
 41             join;
 42 
 43         if (this.isAcceptedType(dom)) {
 44             if (dom.hasOwnProperty("dataset")) {
 45                 return dom.dataset;
 46             } else {
 47                 for (l=dom.attributes.length;i<l;i++) {
 48                     split = dom.attributes[i].name.split("-");
 49                     if (split.shift() == "data") {
 50                         dataset[join = split.join("-")] = dom.getAttribute("data-"+join);
 51                     }
 52                 }
 53                 return dataset;
 54             }
 55 
 56         } else {
 57             return false;
 58         }
 59     },
 60 
 61     /**
 62      * Olives can manipulate HTMLElement and SVGElements
 63      * This function tells if an element is one of them
 64      * @param {Element} type
 65      * @returns true if HTMLElement or SVGElement
 66      */
 67     isAcceptedType: function isAcceptedType(type) {
 68         if (type instanceof HTMLElement ||
 69             type instanceof SVGElement) {
 70             return true;
 71         } else {
 72             return false;
 73         }
 74     },
 75 
 76     /**
 77      * Assign a new value to an Element's property. Works with HTMLElement and SVGElement.
 78      * @param {HTMLElement|SVGElement} node the node which property should be changed
 79      * @param {String} property the name of the property
 80      * @param {any} value the value to set
 81      * @returns true if assigned
 82      */
 83     setAttribute: function setAttribute(node, property, value) {
 84             if (node instanceof HTMLElement) {
 85                 node[property] = value;
 86                 return true;
 87             } else if (node instanceof SVGElement){
 88                 node.setAttribute(property, value);
 89                 return true;
 90             } else {
 91                 return false;
 92             }
 93     },
 94 
 95     /**
 96      * Determine if an element matches a certain CSS selector.
 97      * @param {Element} the parent node
 98      * @param {String} CSS selector
 99      * @param {Element} the node to check out
100      * @param true if matches
101      */
102     matches : function matches(parent, selector, node){
103         return Tools.toArray(this.getNodes(parent, selector)).indexOf(node) > -1;
104     }
105 };