W3C home > Mailing lists > Public > public-html-commits@w3.org > November 2010

html5/spec dfn.js,NONE,1.1 styler.js,NONE,1.1 toc.js,NONE,1.1

From: Michael Smith via cvs-syncmail <cvsmail@w3.org>
Date: Thu, 11 Nov 2010 02:34:23 +0000
To: public-html-commits@w3.org
Message-Id: <E1PGMzX-0007iR-H2@lionel-hutz.w3.org>
Update of /sources/public/html5/spec
In directory hutz:/tmp/cvs-serv29062

Added Files:
	dfn.js styler.js toc.js 
Log Message:
new


--- NEW FILE: toc.js ---
function filterToc(toc, callback) {
  var li = toc.firstChild;
  var allmatched = true;
  while (li) {
    if (li.nodeType == Node.ELEMENT_NODE) {
      var id = li.firstChild.hash.substr(1);
      var target = document.getElementById(id);
      var matched = false;
      while (target && !matched && target != document.body) {
        if (target.className == 'impl')
          matched = true;
        else
          target = target.parentNode;
      }
      if (matched) {
        li.className = 'impl';
      } else {
        allmatched = false;
        var ol = li.getElementsByTagName('ol')[0];
        if (ol)
          filterToc(ol);
      }
    }
    li = li.nextSibling;
  }
  if (allmatched)
    toc.className += ' impl';
}

function initToc() {
  var ols = document.getElementsByTagName('ol');
  var toc;
  for (var index = 0; index < ols.length; index += 1) {
    if (ols[index].className == 'toc') {
      toc = ols[index];
      break;
    }
  }
  if (!toc)
    throw "Failed to find TOC";

  // filter 'impl' bits
  filterToc(toc);

  if (toc.getElementsByTagName('ol').length < 10)
    return; // don't bother generating minitoc if there's not much to minify

  // generate short toc once it's done
  var newtoc = document.createElement('ol');
  newtoc.className = 'brief toc';
  var li = toc.firstChild;
  var id = 1;
  while (li) {
    if (li.nodeType == Node.ELEMENT_NODE) {
      var newli = document.createElement('li');
      newli.className = li.className;
      var newa = li.firstChild.cloneNode(true);
      li.id = 'auto-toc-' + id;
      newa.href = '#auto-toc-' + id;
      newli.appendChild(newa);
      newtoc.appendChild(newli);
      id += 1;
    }
    li = li.nextSibling;
  }
  var scrollPos = scrollY;
  var originalTop = toc.offsetTop;
  toc.parentNode.insertBefore(newtoc, toc);
  var h3 = document.createElement('h3');
  h3.textContent = 'Full table of contents';
  toc.parentNode.insertBefore(h3, toc);
  if (scrollPos >= originalTop)
    scrollTo(scrollX, scrollPos + (toc.offsetTop - originalTop));
}

// setup
var tocTimer = new Date();
initToc();
if (getCookie('profile') == '1')
  document.getElementsByTagName('h2')[0].textContent += '; toc.js: ' + (new Date() - tocTimer) + 'ms';

--- NEW FILE: dfn.js ---
// dfn.js
// makes <dfn> elements link back to all uses of the term
// no copyright is asserted on this file

var dfnTimer = new Date();

var dfnMapTarget = -1;
var dfnMapDone = false;
var dfnMap = {};
function initDfn() {
  var links = [];
  dfnMapTarget = document.links.length;
  for (var i = 0; i < dfnMapTarget; i += 1)
    links[i] = document.links[i];
  var k = 0;
  var n = 0;
  var initDfnInternal = function () {
    n += 1;
    var start = new Date();
    while (k < dfnMapTarget) {
      if (links[k].hash.length > 1) {
        if (links[k].className != "no-backref" && links[k].parentNode.className != "no-backref") {
          var s = links[k].hash.substr(1);
          if (!(s in dfnMap))
            dfnMap[s] = [];
          dfnMap[s].push(links[k]);
        }
      }
      k += 1;
      if (new Date() - start > 1000) {
        setTimeout(initDfnInternal, 10000);
        return;
      }
    }
    dfnMapDone = true;
    document.body.className += " dfnEnabled";
    if (getCookie('profile') == '1')
      document.getElementsByTagName('h2')[0].textContent += '; dfn.js: ' + (new Date() - dfnTimer) + 'ms to do ' + dfnMapTarget + ' links in ' + n + ' loops';
  }
  initDfnInternal();
}

var dfnPanel;
var dfnUniqueId = 0;
var dfnTimeout;
document.addEventListener('click', dfnShow, false);
function dfnShow(event) {
  if (dfnTimeout) {
    clearTimeout(dfnTimeout);
    dfnTimeout = null;
  }
  if (dfnPanel) {
    dfnPanel.parentNode.removeChild(dfnPanel);
    dfnPanel = null;
  }
  if (dfnMapDone) {
    var node = event.target;
    while (node && (node.nodeType != event.target.ELEMENT_NODE || node.tagName != "DFN"))
      node = node.parentNode;
    if (node) {
      var panel = document.createElement('div');
      panel.className = 'dfnPanel';
      if (node.id) {
        var permalinkP = document.createElement('p');
        var permalinkA = document.createElement('a');
        permalinkA.href = '#' + node.id;
        permalinkA.textContent = '#' + node.id;
        permalinkP.appendChild(permalinkA);
        panel.appendChild(permalinkP);
      }
      var p = document.createElement('p');
      panel.appendChild(p);
      if (node.id in dfnMap || node.parentNode.id in dfnMap) {
        p.textContent = 'Referenced in:';
        var ul = document.createElement('ul');
        var lastHeader;
        var lastLi;
        var n;
        var sourceLinks = [];
        if (node.id in dfnMap)
          for (var i = 0; i < dfnMap[node.id].length; i += 1)
            sourceLinks.push(dfnMap[node.id][i]);
        if (node.parentNode.id in dfnMap)
          for (var i = 0; i < dfnMap[node.parentNode.id].length; i += 1)
            sourceLinks.push(dfnMap[node.parentNode.id][i]);
        for (var i = 0; i < sourceLinks.length; i += 1) {
          var link = sourceLinks[i];
          var header = dfnGetCaption(link);
          var a = document.createElement('a');
          if (!link.id)
            link.id = 'dfnReturnLink-' + dfnUniqueId++;
          a.href = '#' + link.id;
          if (header != lastHeader) {
            lastHeader = header;
            n = 1;
            var li = document.createElement('li');
            var cloneHeader = header.cloneNode(true);
            while (cloneHeader.hasChildNodes())
              a.appendChild(cloneHeader.firstChild);
            lastLi = li;
            li.appendChild(a);
            ul.appendChild(li);
          } else {
            n += 1;
            a.appendChild(document.createTextNode('(' + n + ')'));
            lastLi.appendChild(document.createTextNode(' '));
            lastLi.appendChild(a);
          }
        }
        panel.appendChild(ul);
      } else {
        p.textContent = 'No references in this file.';
      }
      node.appendChild(panel);
      dfnPanel = panel;
    }
  } else {
    dfnTimeout = setTimeout(dfnShow, 250, event);
  }
}

function dfnGetCaption(link) {
  var node = link;
  while (node) {
    if (node.nodeType == node.ELEMENT_NODE && node.tagName.match(/^H[1-6]$/)) {
      return node;
    } else if (!node.previousSibling) {
      node = node.parentNode;
    } else {
      node = node.previousSibling;
      if (node.nodeType == node.ELEMENT_NODE && node.className == "impl") {
        node = node.lastChild;
      }
    }
  }
  return null;
}

// setup (disabled for multipage copy)
if (document.getElementById('head'))
  initDfn();

--- NEW FILE: styler.js ---
function setStyleLink(link, style) {
  document.getElementById(link).disabled = link != style;
  if (link == style)
    document.getElementById(link + '-radio').checked = true;
}

function setStyle(style) {
  var date = new Date();
  date.setFullYear(date.getFullYear() + 1);
  document.cookie = 'style=' + encodeURIComponent(style) + '; expires=' + date.toGMTString() + '; path=/';
  setStyleLink('complete', style);
  setStyleLink('author', style);
  setStyleLink('highlight', style);
}

function initStyler() {
  var configUI = document.getElementById('configUI');
  var stylesUI = document.createElement('p');
  stylesUI.innerHTML =
    '<label><input type=radio name=styles id=complete-radio value=complete onchange=setStyle(value)> Normal view</label>' + 
    '<label><input type=radio name=styles id=author-radio value=author onchange=setStyle(value)> Web dev view</label>' +
    '<label><input type=radio name=styles id=highlight-radio value=highlight onchange=setStyle(value)> Implementor view</label>' ;
  configUI.appendChild(stylesUI);
  // reset the style for webkit... XXX
  setStyleLink('complete', '');
  setStyleLink('author', '');
  setStyleLink('highlight', '');
  // put the style back to whatever the user last selected
  var style = getCookie('style');
  if (style != 'complete' && style != 'author' && style != 'highlight')
    style = 'complete';
  setStyle(style);
  var hashChecker = function () {
    if (style != 'author')
      return;
    var s = document.location.hash.substr(1);
    if (s.length <= 0)
      return;
    var e = document.getElementById(s);
    if (!e)
      return;
    while (e && (e != document.body)) {
      if (getComputedStyle(e, '').display == 'none') {
        var path = location.pathname;
        var file = path.substr(path.lastIndexOf('/')+1);
        showAlert('The link you followed is only available in the complete version of the specification:', file + '?style=highlight#' + s);
        return;
      }
      e = e.parentNode;
    }
  };
  window.addEventListener('hashchange', hashChecker, false);
  hashChecker();
}

var stylerTimer = new Date();
initStyler();
if (getCookie('profile') == '1')
  document.getElementsByTagName('h2')[0].textContent += '; styler.js: ' + (new Date() - stylerTimer) + 'ms';
Received on Thursday, 11 November 2010 02:34:26 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:10:12 UTC