WebKit的曆史項管理

來源:互聯網
上載者:User

標籤:webkit

標準定義

關於曆史的管理,和HTML頁面載入一樣,都有其相應的標準。地址如下:
WhatWG: https://html.spec.whatwg.org/multipage/browsers.html#history

其中關於曆史項的要點如下:

1. 在onload之前,非使用者操作引起的導航操作不建立曆史項。

非使用者操作比如頁面中指定的Timer修改location或iframe的src引發的導航操作。而使用者點擊發起的Timer,則在timer中記錄下在發起timer時標記手勢來源。但有一個例外是由另一個Timer發起的Timer或是重複執行的Timer, 則僅針對第一次執行有效(以nesting level標識)。

2. 子Frame載入完成仍有未載入的上層Frame, 則不建立曆史項。

3. 當前Frame只有一個曆史項,且為about:blank, 則不建立曆史項。

4. 如果頁面跳轉的間隔小於1s,則不建立曆史項。


關於Timer的nesting level可以參考這裡:
http://www.w3.org/html/wg/drafts/html/CR/webappapis.html#timer-nesting-level


以上規則對應於以下三個WebKit的函數:

a. LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame)
LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame)
{
    // Non-user navigation before the page has finished firing onload should not create a new back/forward item.
    // See https://webkit.org/b/42861 for the original motivation for this.   
    if (!ScriptController::processingUserGesture() && targetFrame.loader().documentLoader() && !targetFrame.loader().documentLoader()->wasOnloadHandled())
        return LockBackForwardList::Yes;
   
    // Navigation of a subframe during loading of an ancestor frame does not create a new back/forward item.
    // The definition of "during load" is any time before all handlers for the load event have been run.
    // See https://bugs.webkit.org/show_bug.cgi?id=14957 for the original motivation for this.
    for (Frame* ancestor = targetFrame.tree().parent(); ancestor; ancestor = ancestor->tree().parent()) {
        Document* document = ancestor->document();
        if (!ancestor->loader().isComplete() || (document && document->processingLoadEvent()))
            return LockBackForwardList::Yes;
    }
    return LockBackForwardList::No;
}


b. HistoryController::currentItemShouldBeReplaced() const
{
    // From the HTML5 spec for location.assign():
    //  "If the browsing context‘s session history contains only one Document,
    //   and that was the about:blank Document created when the browsing context
    //   was created, then the navigation must be done with replacement enabled."
    return m_currentItem && !m_previousItem && equalIgnoringCase(m_currentItem->urlString(), blankURL());
}


c. void NavigationScheduler::scheduleRedirect(double delay, const URL& url)
{
    if (!shouldScheduleNavigation(url))
        return;
    if (delay < 0 || delay > INT_MAX / 1000)
        return;
    if (url.isEmpty())
        return;
    // We want a new back/forward list item if the refresh timeout is > 1 second.
    if (!m_redirect || delay <= m_redirect->delay()) {
        LockBackForwardList lockBackForwardList = delay <= 1 ? LockBackForwardList::Yes : LockBackForwardList::No;
        schedule(std::make_unique<ScheduledRedirect>(delay, m_frame.document()->securityOrigin(), url, LockHistory::Yes, lockBackForwardList));
    }
}

頁面問題分析


1. Timer問題Timer最大的問題來源於Timer Nesting Level的認定。最初WebKit建一個全域的變數記錄, 容易受到不同頁面,frame中Timer的影響。
比如使用者點擊一條連結就使用一個Timer跳轉,如果這個Timer建立時,恰好有另一個repeat timer還在迴圈執行, 且未載入完成(onload事件沒有執行), 那它就無法建立曆史項,因為這時全域的nesting level大於1,將不會記錄為手勢觸發的跳轉。WebKit提交的(https://bugs.webkit.org/show_bug.cgi?id=136401 [^]), 本意用於避免多線程訪問的問題,也剛好解決了嵌套層次錯誤的問題。
現在仍然有一種情況下,這個問題還會存在。見 https://bugs.webkit.org/show_bug.cgi?id=137631。如下面測試頁面所示,當在一個document中,有一個repeat timer執行後, 且未載入完成(onload事件沒有執行),其它Timer的跳轉仍然無法建立曆史項。<html>
  <head>
  </head>
  <body>
  <input type="button" value="Goto next page" onclick="gotoNextPage();">
  <input type="button" value="Start repeating timer" onclick="startRepeatingTimer();">
  <p>
  <div id="Timer">Paused</div>
  </p>
  <script type="text/javascript">

  function gotoNextPage(){
    setTimeout(function(){location.href="http://www.webkit.org/";},300);
  }

  function startRepeatingTimer(){
    setInterval(function(){document.getElementById("Timer").innerHTML="Running...";},500);
  }
  </script>

  <img src="http://therecoveringpolitician.com/wp-content/uploads/2013/01/moderate.jpg" width="640" height="480">
  <img src="http://cullogo.com/full/wallpapers-high-resolution-widescreen-hd-pk.jpg" width="640" height="480">
  <img src="http://www.highresolutionwallpapers.net/wallpapers/autumn-1680x1050.jpg" width="640" height="480">
  </body>
</html>2. 載入規則導致多餘曆史項問題有點頁面(華聲論壇)由於頁面在iframe的初始化及動態改變時,看起來兩個src是相同的,都帶有ampersand, 一個由HTML指定,另一個由JavaScript設定。問題在於JavaScript不會轉換ampersand, 這樣就相當於iframe載入了兩個不同的頁面,從而產生了兩個曆史項。
使用下面的測試頁面可以重現這個問題, 注意頁面中兩處http://m.baidu.com的寫法:<html>
  <head>
  </head>
  <body>
  <p>
  <div id="info">Loading...</div>
  </p>
  <script type="text/javascript">
    setTimeout(function(){ attr("sub_iframe","http://m.baidu.com/?abc=23&amp;t=20080225");},5000);

    function attr(id,url){
        var obj=document.getElementById(id);
        obj.setAttribute("src", url);
    }
    
    window.onload=function(){
        document.getElementById("info").innerHTML="Loaded.";
    }

  </script>
  <iframe src="http://m.baidu.com/?abc=23&amp;t=20080225" id="sub_iframe" width="320" height="480"></iframe><br/>
  </body>
</html>



WebKit的曆史項管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.