2014-12-15 1 views
0

Firefox를 복제 한 후 Firefox가 deep37 플래그를 true으로 설정 한 후 느슨해 보이는 이상한 시나리오를 발견했습니다. 이것은 Firefox 버그입니까, 아니면 구현 세부 사항이 누락 되었습니까? 당신은 파이어 폭스를 지원하지 않습니다 innerText와를 사용하고Firefox가 문서를 복제 함 복제본

var n = (function nScope(){ 
 
\t 'use strict'; 
 

 
\t function isDom(x){ 
 
\t \t return x.nodeType > 0; 
 
\t } 
 

 
\t function notDom(x){ 
 
\t \t return !isDom(x); 
 
\t } 
 

 
\t return function n(){ 
 
\t \t // Avoids conditional logic later by forcing a standard output. 
 
\t \t var args = [].map.call(arguments, function wrapDom(x){ 
 
\t \t \t return isDom(x) ? [ x ] : x; 
 
\t \t }); 
 
\t \t var dom = []; 
 
\t \t var vdom = []; 
 
\t \t // Render virtual DOM, then parse output. 
 
\t \t var view = m.apply(void 0, args); 
 
\t \t var cfg = view.attrs.config; 
 

 
\t \t if(view.children.forEach){ 
 
\t \t \t view.children.forEach(function divideChildren(x){ 
 
\t \t \t \t (isDom(x) ? dom : vdom).push(x); 
 
\t \t \t }); 
 
\t \t } 
 

 
\t \t if(dom.length === 0){ 
 
\t \t \t return view; 
 
\t \t } 
 

 
\t \t view.attrs.config = function appendDom(el, init, context){ 
 
\t \t \t // Only perform DOM insertion logic at config time: 
 
\t \t \t // Saves unnecessary execution during strategy none redraws. 
 
      if(!init){ 
 
       dom.forEach(function appendNode(node, index){ 
 
        // If a virtual DOM element occurs after the real node in the children list, grab it and find its 
 
        // index in the list of virtual elements as a reference point for inserting the real node. 
 
        var insertAt = vdom.indexOf(view.children.slice(view.children.indexOf(node)).filter(notDom)[ 0 ]); 
 
        // When a documentFragment is inserted into the document, the reference becomes empty. 
 
        // Therefore we need to insert clones of the original reference. 
 
        // Because this happens on every redraw, this means DOM nodes cannot be modified by prior reference 
 
        // between redraw cycles :(
 
        console.log('Original node:', node); 
 

 
        var clone = node.cloneNode(true); 
 

 
        console.log('Cloned node:', clone); 
 

 
        
 
        if(insertAt){ 
 
         el.insertBefore(clone, el.childNodes[ insertAt ]); 
 
        } 
 
        else { 
 
         el.appendChild(clone); 
 
        } 
 
       }); 
 
      } 
 

 
\t \t \t if(cfg){ 
 
\t \t \t \t return cfg(el, init, context); 
 
\t \t \t } 
 
\t \t }; 
 

 
\t \t // Make sure only the virtual elements are parsed by m.render. 
 
\t \t view.children = vdom; 
 

 
\t \t return view; 
 
\t }; 
 
}()); 
 

 
// To stop jsfiddle breaking 
 
m.route.mode = 'hash'; 
 

 
var links = document.createDocumentFragment(); 
 
var array = [ 1, 2 ]; 
 

 
array.forEach(function appendLink(index){ 
 
    var a = document.createElement('a'); 
 
    
 
    a.innerText = 'Page ' + index; 
 
    a.href  = '/route' + index; 
 
    
 
    m.route(a); 
 
    
 
    links.appendChild(a); 
 
    links.appendChild(document.createTextNode(' ')); 
 
}); 
 

 
var modules = array.map(function makeModule(index){ 
 
    return { 
 
     controller : function(){}, 
 
     view  : function(){ 
 
      return n(
 
       '.module', 
 
       [ 
 
        n('h1', { 
 
         onclick : function(){ 
 
          alert('Redraw incoming...'); 
 
         } 
 
        }, 'Page ' + index), 
 
        links 
 
       ] 
 
      ); 
 
     } 
 
    }; 
 
}); 
 

 
m.route(document.body, '/route1', { 
 
    '/route1' : modules[ 0 ], 
 
    '/route2' : modules[ 1 ] 
 
});
<script src="https://rawgit.com/lhorie/mithril.js/master/mithril.js"></script>

답변

0

: 'innerText' works in IE, but not in Firefox

당신은에 관해서는 대신

array.forEach(function appendLink(index){ 
    var a = document.createElement('a'); 

    a.textContent = 'Page ' + index; // <-- here 
    a.href  = '/route' + index; 

    m.route(a); 

    links.appendChild(a); 
    links.appendChild(document.createTextNode(' ')); 
}); 
+0

여전히 신비화는 TextContent 또는 innerHTML을 사용할 수 이유 복제 된 단편은 childNodes가없는 것으로 기록되지만 이것이 직각 인 것처럼 보입니다. 감사! – Barney

+0

요소에 단편을 추가하면 하위 노드가 요소로 전송되므로 렌더링이 끝나면 console.log (복제)를 실행하고 콘솔에서 해당 속성을 드릴 다운하면 빈 목록이 표시됩니다. console.log (clone.childNodes)를 사용하여 전송 된 NodeListCollection을 볼 수 있습니다. – LeoHorie

+0

놀랍게도 콘솔이 입력 내용을 즉시 평가하지 않는다는 사실이 놀랍습니다. 'clone.childNodes' 또한 비어 있습니다. 사실 이후에 동적 참조가 평가되기 때문입니다. 값에 의해 원시적으로 전달 된'clone.childNodes.length'는'4'입니다. 콘솔이 입력을 비동기 적으로 평가하기로 결정하는 방법을 이해할 수 없습니다 ... – Barney

관련 문제