Changeset 1158

Show
Ignore:
Timestamp:
10/09/06 11:55:56 (2 years ago)
Author:
therve@gmail.com
Message:

Arnar's patch for using currentDocument, with MockDOM enhancements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • mochikit/branches/selector/MochiKit/MockDOM.js

    r1144 r1158  
    3838 
    3939/** @id MochiKit.MockDOM.MockElement */ 
    40 MochiKit.MockDOM.MockElement = function (name, data) { 
     40MochiKit.MockDOM.MockElement = function (name, data, ownerDocument) { 
    4141    this.tagName = this.nodeName = name.toUpperCase(); 
    42     if (typeof(data) == "string") { 
     42    this.ownerDocument = ownerDocument || null; 
     43    if (name == "DOCUMENT") { 
     44        this.nodeType = 9; 
     45        this.childNodes = []; 
     46    } else if (typeof(data) == "string") { 
    4347        this.nodeValue = data; 
    4448        this.nodeType = 3; 
     
    5963    /** @id MochiKit.MockDOM.MockElement.prototype.createElement */ 
    6064    createElement: function (tagName) { 
    61         return new MochiKit.MockDOM.MockElement(tagName); 
     65        return new MochiKit.MockDOM.MockElement(tagName, null, this.nodeType == 9 ? this : this.ownerDocument); 
    6266    }, 
    6367    /** @id MochiKit.MockDOM.MockElement.prototype.createTextNode */ 
     
    8084    toString: function () { 
    8185        return "MockElement(" + this.tagName + ")"; 
     86    }, 
     87    /** @id MochiKit.MockDOM.MockElement.prototype.getElementsByTagName */ 
     88    getElementsByTagName: function (tagName) { 
     89        var foundElements = []; 
     90        MochiKit.Base.nodeWalk(this, function(node){ 
     91            if (tagName == '*' || tagName == node.tagName) { 
     92                foundElements.push(node); 
     93                return node.childNodes; 
     94            } 
     95        }); 
     96        return foundElements; 
    8297    } 
    8398}; 
  • mochikit/branches/selector/MochiKit/Selector.js

    r1157 r1158  
    127127                switch (pseudoClass) { 
    128128                    case 'root': 
    129                         conditions.push('element === document.documentElement'); break; 
     129                        conditions.push('element.nodeType == 9 || element === element.ownerDocument.documentElement'); break; 
    130130                    case 'nth-child': 
    131131                    case 'nth-last-child': 
     
    285285        } 
    286286 
    287         if (element = $(this.params.id)) { 
     287        if (element = MochiKit.DOM.getElement(this.params.id)) { 
    288288            if (this.match(element)) { 
    289289                if (!scope || inScope(element, scope)) { 
     
    302302 
    303303        if (axis == "") { 
    304             scope = (scope || document).getElementsByTagName(this.params.tagName || '*'); 
     304            scope = (scope || currentDocument()).getElementsByTagName(this.params.tagName || '*'); 
    305305        } else if (axis == ">") { 
    306306            if (!scope) { 
     
    371371 
    372372function $$() { 
    373     return MochiKit.Selector.findChildElements(document, arguments); 
    374 } 
    375  
     373    return MochiKit.Selector.findChildElements(currentDocument(), arguments); 
     374} 
     375 
  • mochikit/branches/selector/tests/test_MochiKit-Selector.html

    r1156 r1158  
    223223        is( results[i], expected[i+4], "'a[href$=outsidep] ~ *' selector" + ' (' + i + ')'); 
    224224    } 
     225     
     226    expected = [document.documentElement]; 
     227    results = $$(':root'); 
     228    for (var i=0; i < results.length; i ++) { 
     229        is( results[i], expected[i], "':root' selector" + ' (' + i + ')'); 
     230    } 
     231     
     232    var doc = MochiKit.MockDOM.createDocument(); 
     233    appendChildNodes(doc.body, A({"href": "http://www.example.com/insideAnotherDocument"}, "Inside a document")); 
     234    withDocument(doc, function(){ 
     235        is( $$(":root")[0], doc, ":root on a different document" ); 
     236        is( $$("a")[0], doc.body.firstChild, "a inside a different document" ); 
     237    }); 
    225238 
    226239    ok( true, "test suite finished!");