Code coverage report for htmlcs/lib/element.js

Statements: 97.56% (40 / 41)      Branches: 90.63% (29 / 32)      Functions: 63.16% (12 / 19)      Lines: 97.56% (40 / 41)      Ignored: none     

All files » htmlcs/lib/ » element.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131          1   1 1   1             1 387   387       1 387       387   387   387     1 387   387   387   387 387 577 207         387 387 505 207           387                                               1         523       526 526               1       2   2 2 4 2 1     3           198     22         1  
/**
 * @file class Element
 * @author nighca<nighca@live.cn>
 */
 
var cssSelect = require('CSSselect');
 
var util = require('./util');
var Node = require('./node');
 
var Namespaces = {
    HTML: 'http://www.w3.org/1999/xhtml',
    XML: 'http://www.w3.org/XML/1998/namespace',
    XMLNS: 'http://www.w3.org/2000/xmlns/'
};
 
// http://www.w3.org/TR/dom/#interface-element
function Element() {
    this.constructor = Element;
    /* eslint-disable no-proto */
    this.__proto__ = Element.prototype;
    /* eslint-enable no-proto */
}
 
Element.init = function (element) {
    Iif (!element || (element instanceof Element)) {
        return element;
    }
 
    Element.apply(element);
 
    Element.extend(element);
 
    return element;
};
 
Element.extend = function (element) {
    var tag = element.name.toUpperCase();
 
    var attributes = element.attribs || {};
 
    var children = element.childNodes.filter(util.isElement);
 
    var previousElementSibling = (function (element) {
        while (element = element.prev) {
            if (util.isElement(element)) {
                return element;
            }
        }
    })(element) || null;
 
    var nextElementSibling = (function (element) {
        while (element = element.next) {
            if (util.isElement(element)) {
                return element;
            }
        }
    })(element) || null;
 
    // https://dom.spec.whatwg.org/#interface-element
    return util.extend(element, {
        // as element
        namespaceURI: Namespaces.HTML,
        prefix: '',                                     // TODO
        localName: element.name.toLowerCase(),
        tagName: tag,
 
        id: attributes.id || '',
        className: attributes['class'] || '',
        classList: attributes['class'] ? attributes['class'].split(' ') : [],
        attributes: attributes,
 
        // as parent node
        children: children,
        firstElementChild: children[0],
        lastElementChild: children[children.length - 1],
        childElementCount: children.length,
        previousElementSibling: previousElementSibling,
        nextElementSibling: nextElementSibling
 
    });
};
 
// only read ops
Element.prototype = util.extend(new Node(), {
    constructor: Element,
 
    // as element
    getAttribute: function (name) {
        return this.hasAttribute(name) ? this.attributes[name] : null;
    },
    getAttributeNS: function () { /*TODO*/ },
    hasAttribute: function (name) {
        name = name && (this.namespaceURI === Namespaces.HTML ? name.toLowerCase() : name);
        return this.attributes.hasOwnProperty(name);
    },
    hasAttributeNS: function () { /*TODO*/ },
    getAttributeNode: function () { /*TODO*/ },
    getAttributeNodeNS: function () { /*TODO*/ },
    closest: function () { /*TODO*/ },
    matches: function () { /*TODO*/ },
    getElementsByTagName: function (tagName) {
        return this.querySelectorAll(tagName.toLowerCase());
    },
    getElementsByTagNameNS: function () { /*TODO*/ },
    getElementsByClassName: function (classNameList) {
        classNameList = classNameList.split(' ');
 
        var l = classNameList.length;
        return this.querySelectorAll('.' + classNameList[0]).filter(function (element) {
            for (var i = 1; i < l; i++) {
                if (classNameList[i] && element.classList.indexOf(classNameList[i]) < 0) {
                    return false;
                }
            }
            return true;
        });
    },
 
    // as parent node
    querySelector: function (selector) {
        return cssSelect.selectOne(selector, this.children) || null;
    },
    querySelectorAll: function (selector) {
        return cssSelect(selector, this.children);
    }
});
 
 
module.exports = Element;