Changeset 29458

Show
Ignore:
Timestamp:
02/03/09 09:20:22 (4 years ago)
Author:
cho45
Message:

読みやすさのため E4X + DOMParser に

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userchrome/hatena-multi.uc.js

    r29457 r29458  
    2121(function () { with (D()) { 
    2222 
    23 function createElementFromStringNS (str, opts) { 
    24         if (!opts) opts = { data: {} }; 
    25         if (!opts.data) opts.data = { }; 
    26  
    27         var t, cur = opts.parent || document.createDocumentFragment(), root, stack = [cur]; 
    28         var namespaces = opts.ns || {}; 
    29         while (str.length) { 
    30                 if (str.indexOf("<") == 0) { 
    31                         if ((t = str.match(/^\s*<(\/?(?:([^\s>:]+?):)?([^\s>\/]+))([^>]+?)?(\/)?>/))) { 
    32                                 var tag = t[1], prefix = t[2], localname = t[3], attrs = t[4], isempty = !!t[5]; 
    33                                 if (tag.indexOf("/") == -1) { 
    34                                         child = document.createElementNS(namespaces[prefix || ""], tag); 
    35                                         if (attrs) attrs.replace(/(?:([^:\s]+?):)?([a-z\d]+)=(?:'([^']+)'|"([^"]+)")/gi, 
    36                                                 function (m, prefix, name, v1, v2) { 
    37                                                         var v = text(v1 || v2); 
    38                                                         if (name == "class") root && (root[v] = child); 
    39                                                         // ignore default namespace for attribute 
    40                                                         child.setAttributeNS(prefix == "" ? null : namespaces[prefix], name, v); 
    41                                                 } 
    42                                         ); 
    43                                         cur.appendChild(root ? child : (root = child)); 
    44                                         if (!isempty) { 
    45                                                 stack.push(cur); 
    46                                                 cur = child; 
    47                                         } 
    48                                 } else cur = stack.pop(); 
    49                         } else throw("Parse Error: " + str); 
    50                 } else { 
    51                         if ((t = str.match(/^([^<]+)/))) cur.appendChild(document.createTextNode(text(t[0]))); 
    52                 } 
    53                 str = str.substring(t[0].length); 
    54         } 
    55  
    56         function text (str) { 
    57                 return str 
    58                         .replace(/&(#(x)?)?([^;]+);/g, function (_, isNumRef, isHex, ref) { 
    59                                 return isNumRef ? String.fromCharCode(parseInt(ref, isHex ? 16 : 10)): 
    60                                                   {"lt":"<","gt":"<","amp":"&"}[ref]; 
    61                         }) 
    62                         .replace(/#\{([^}]+)\}/g, function (_, name) { 
    63                                 return (typeof(opts.data[name]) == "undefined") ? _ : opts.data[name]; 
    64                         }); 
    65         } 
    66  
    67         return root; 
     23function DOM (xmlns, xml) { 
     24        var doc = (new DOMParser()).parseFromString( 
     25                '<root xmlns="' + xmlns + '">' + xml.toXMLString() + "</root>", 
     26                "application/xml" 
     27        ); 
     28        var imported = document.importNode(doc.documentElement, true); 
     29        var range = document.createRange(); 
     30        range.selectNodeContents(imported); 
     31        var fragment = range.extractContents(); 
     32        range.detach(); 
     33        return fragment.childNodes.length > 1 ? fragment : fragment.firstChild; 
    6834} 
    6935 
     
    9157 
    9258                var statusbar = document.getElementById("status-bar"); 
    93                 self.panel    = createElementFromStringNS(<><![CDATA[ 
    94                         <statusbarpanel id='#{id}'> 
    95                                 <image src='#{iconimg}'/> 
    96                                 <label class='_label'/> 
    97                                 <menupopup class='_menupopup'/> 
     59                self.panel    = DOM(kXULNS, 
     60                        <statusbarpanel id={self.ID}> 
     61                                <image src={self.iconimg}/> 
     62                                <label/> 
     63                                <menupopup/> 
    9864                        </statusbarpanel> 
    99                 ]]></>.toString(), { 
    100                         parent : statusbar, 
    101                         data   : { 
    102                                 id : self.ID, 
    103                                 iconimg : self.iconimg 
    104                         }, 
    105                         ns     : { 
    106                                 "" : kXULNS 
    107                         } 
    108                 }); 
    109  
    110                 self.menu = self.panel._menupopup; 
    111                 self.lbl  = self.panel._label; 
     65                ); 
     66                statusbar.appendChild(self.panel); 
     67 
     68                self.menu = self.panel.querySelector("menupopup"); 
     69                self.lbl  = self.panel.querySelector("label"); 
    11270 
    11371                self.panel.addEventListener("click", function () { 
     
    143101                while (self.menu.firstChild) self.menu.removeChild(self.menu.firstChild); 
    144102                logins.forEach(function (l) { 
    145                         var mi = createElementFromStringNS("<menuitem label='#{label}'/>", { 
    146                                 parent : self.menu, 
    147                                 data   : { 
    148                                         label : l.username 
    149                                 }, 
    150                                 ns     : { 
    151                                         "" : kXULNS 
    152                                 } 
    153                         }); 
     103                        var mi = DOM(kXULNS, <menuitem label={l.username}/>); 
     104                        self.menu.appendChild(mi); 
    154105 
    155106                        mi.addEventListener("command", function (e) { 
     
    174125                ]; 
    175126 
    176                 var sep = document.createElementNS(kXULNS, "menuseparator"); 
    177                 self.menu.appendChild(sep); 
     127                self.menu.appendChild(DOM(kXULNS, <menuseparator/>)); 
    178128 
    179129                addtionalMenu.forEach(function (l) { 
    180                         createElementFromStringNS("<menuitem label='#{label}'/>", { 
    181                                 parent : self.menu, 
    182                                 data   : { 
    183                                         label : l.label 
    184                                 }, 
    185                                 ns     : { 
    186                                         "" : kXULNS 
    187                                 } 
    188                         }).addEventListener("command", l.command, false); 
     130                        var mi = DOM(kXULNS, <menuitem label={l.label}/>); 
     131                        self.menu.appendChild(mi); 
     132                        mi.addEventListener("command", l.command, false); 
    189133                }); 
    190134