<?xml version="1.0" encoding="iso-2022-jp"?>
<!DOCTYPE bindings [
  <!ENTITY toc.header "$BL\<!(B">
]>
<bindings xmlns="http://www.mozilla.org/xbl">
  <binding id="after">
    <implementation>
      <constructor type="application/x-javascript" xml:space="preserve">
        <![CDATA[
          const NS_TOC = 'http://suika.fam.cx/~wakaba/archive/2005/3/toc#';
          const NS_X1 = 'http://www.w3.org/1999/xhtml';
          const NS_X2 = 'http://www.w3.org/2002/06/xhtml2';
          const NS_X2005 = 'http://www.w3.org/2002/06/xhtml2/';
          const NS_XLINK = 'http://www.w3.org/1999/xlink';
          const NS_XML = 'http://www.w3.org/XML/1998/namespace';
          const NS_DEFAULT = NS_X1;
          const NS_HREF = null;
  
        var hasToc = document.documentElement.getAttributeNS (NS_TOC, 'hasToc');
        if (!hasToc) {
          document.documentElement.setAttributeNS (NS_TOC, 'toc:hasToc', '1');

          var toc = document.createElementNS (NS_X1, 'div');
          toc.setAttributeNS (NS_TOC, 'toc:toc', '1');
          /*
          var tocH = document.createElementNS (NS_X2005, 'h');
          tocH.textContent = ']]>&toc.header;<![CDATA[';
          toc.appendChild (tocH);
          */

          var elementTypeInfo = {};
          elementTypeInfo[NS_X1] = {
              head: {type: 'ignore'},
              body: {type: 'body', id: [null, 'id']},
              div: {type: 'div', id: [null, 'id']},
              h1: {type: 'h1', id: [null, 'id']},
              h2: {type: 'hn', id: [null, 'id']},
              h3: {type: 'hn', id: [null, 'id']},
              h4: {type: 'hn', id: [null, 'id']},
              h5: {type: 'hn', id: [null, 'id']},
              h6: {type: 'hn', id: [null, 'id']},
              header: {type: 'header', id: [null, 'id']},
              section: {type: 'section', id: [null, 'id']},
              p: {type: 'ignore'},
              pre: {type: 'ignore'},
              dl: {type: 'ignore'},
              ul: {type: 'ignore'},
              ol: {type: 'ignore'},
              blockquote: {type: 'ignore'},
              table: {type: 'table'},
          };
          elementTypeInfo[NS_X1]['*'] = {type: 'container', id: [null, 'id']};
          elementTypeInfo[NS_X2] = {
            head: {type: 'ignore'},
            body: {type: 'body', id: [null, 'id']},
            section: {type: 'section', id: [null, 'id']},
            h: {type: 'h', id: [null, 'id']},
            p: {type: 'ignore'},
            pre: {type: 'ignore'},
            dl: {type: 'ignore'},
            nl: {type: 'ignore'},
            ul: {type: 'ignore'},
            ol: {type: 'ignore'},
            blockquote: {type: 'ignore'},
            table: {type: 'table'},
          };
          elementTypeInfo[NS_X2]['*'] = {type: 'container', id: [null, 'id']};
          elementTypeInfo[NS_X2005] = {
            head: {type: 'ignore'},
            body: {type: 'body', id: [null, 'id']},
            section: {type: 'section', id: [null, 'id']},
            h: {type: 'h', id: [null, 'id']},
            p: {type: 'ignore'},
            pre: {type: 'ignore'},
            dl: {type: 'ignore'},
            nl: {type: 'ignore'},
            ul: {type: 'ignore'},
            ol: {type: 'ignore'},
            blockquote: {type: 'ignore'},
            table: {type: 'table'},
          };
          elementTypeInfo[NS_X2005]['*'] = {type: 'container', id: [null, 'id']};
          elementTypeInfo[NS_TOC] = {};
          elementTypeInfo[NS_TOC]['*'] = {type: 'ignore'};

          var setAnonId = function (elNode) {
            var docEl = document.documentElement;
            var seq = docEl.getAttributeNS (NS_TOC, 'anonId');
            if (!seq) { seq = 0 } else { seq = parseInt (seq) }
            docEl.setAttributeNS (NS_TOC, 'toc:anonId', ++seq);
            return 'AUTOTOC--ANON' + seq;
          };

          var isHTML = (document.documentElement.nodeName == 'HTML');

          var appendTOCList = function (tocParent, tocLabel, docParent) {
            var docChildren = docParent.childNodes;
            for (var i = 0; i < docChildren.length; i++) {
              var docChild = docChildren.item (i);
              if (docChild.nodeType == docChild.ELEMENT_NODE) {
                var ns = docChild.namespaceURI;
                var ln = docChild.localName;
                if (!ns && isHTML) {
                  ln = docChild.nodeName.toLowerCase ();
                  ns = NS_X1;
                }
                if (!ns) { ns = NS_DEFAULT; ln = docChild.nodeName }
                var et = elementTypeInfo[ns]
                           ? elementTypeInfo[ns][ln]
                               ? elementTypeInfo[ns][ln]
                               : elementTypeInfo[ns]['*']
                           : null;
                if (!et) { et = {type: 'container'} }
                if (et.type == 'div') {
                  var clss = docChild.getAttributeNS (null, 'class')
                                      .split (/\s+/);
                  for (var j = 0; j < clss.length; j++) {
                    var cls = clss[j];
                    if (cls == 'section' || cls == 'wiki-section' ||
                        cls == 'article') {
                      et = {type: 'section', id: et.id};
                      break;
                    } else if (cls == 'header') {
                      et = {type: 'header', id: et.id};
                      break;
                    } else if (cls == 'navigation') {
                      et = {type: 'ignore'};
                      break;
                    }
                  }
                } else if (et.type == 'hn' || et.type == 'h1') {
                  var pt = docParent.getAttributeNS (NS_TOC, 'type');
                  if (pt == 'section' ||
                      (pt == 'body' && et.type == 'h1')) {
                    et = {type: 'h', id: et.id};
                  } else {
                    et = {type: 'hintro', id: et.id};
                  }
                }
                docChild.setAttributeNS (NS_TOC, 'toc:type', et.type);
                
                if (et.type == 'container' || et.type == 'div') {
                  appendTOCList (tocParent, tocLabel, docChild);
                } else if (et.type == 'section' || et.type == 'body') {
                  var li = document.createElementNS (NS_X2005, 'li');
                  var nl = document.createElementNS (NS_X2005, 'nl');
                  var lb = document.createDocumentFragment ();
                  appendTOCList (nl, lb, docChild);
                  var href;
                  if (et.id) {
                    var idv = docChild.getAttributeNS (et.id[0], et.id[1]);
                    if (idv) {
                      href = '#' + idv;
                    } else {
                      href = setAnonId (docChild);
                      docChild.setAttributeNS (et.id[0], et.id[1], href);
                      href = '#' + href;
                    }
                  } else {
                    href = setAnonId (docChild);
                    docChild.setAttributeNS (NS_XML, 'xml:id', href);
                    href = '#' + href;
                  }
                  if (nl.childNodes.length > 0) {
                    var lbl = document.createElementNS (NS_X2005, 'label');
                    if (lb.childNodes.length > 0) {
                      lbl.appendChild (lb);
                    } else {
                      lbl.appendChild (document.createTextNode ('#'));
                    }
                    lbl.setAttributeNS (NS_HREF, 'href', href);
                    nl.insertBefore (lbl, nl.childNodes.item (0));
                    li.appendChild (nl);
                  } else {
                    if (lb) {
                      li.appendChild (lb);
                    } else {
                      li.appendChild (document.createTextNode ('#'));
                    }
                    li.setAttributeNS (NS_HREF, 'href', href);
                  }
                  tocParent.appendChild (li);
                } else if (et.type == 'h' && !tocLabel.textContent) {
                  tocLabel.textContent = docChild.textContent;
                } else if (et.type == 'header' && !tocLabel.textContent) {
                  var hel = docChild.getElementsByTagNameNS (NS_X2005, 'h')[0] ||
                            docChild.getElementsByTagNameNS (NS_X2, 'h')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h1')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h1')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h2')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h2')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h3')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h3')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h4')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h4')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h5')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h5')[0] ||
                            docChild.getElementsByTagNameNS (NS_X1, 'h6')[0] ||
                            docChild.getElementsByTagNameNS (null, 'h6')[0];
                  if (hel) {
                    tocLabel.textContent = hel.textContent;
                  }
                } else if (et.type == 'hintro' || et.type == 'h') {
                  var li = document.createElementNS (NS_X2005, 'li');
                  li.textContent = docChild.textContent;
                  li.setAttributeNS (NS_TOC, 'toc:element-type', ln);
                  li.setAttributeNS (NS_TOC, 'toc:element-type-ns-uri', ns);
                  tocParent.appendChild (li);
                  var href;
                  if (et.id) {
                    var idv = docChild.getAttributeNS (et.id[0], et.id[1]);
                    if (idv) {
                      href = '#' + idv;
                    } else {
                      href = setAnonId (docChild);
                      docChild.setAttributeNS (et.id[0], et.id[1], href);
                      href = '#' + href;
                    }
                  } else {
                    href = setAnonId (docChild);
                    docChild.setAttributeNS (NS_XML, 'xml:id', href);
                    href = '#' + href;
                  }
                  li.setAttributeNS (NS_HREF, 'href', href);
                }
                /* 2nd or more header's should also be supported?? */
              }
            }
          };

          var nl = document.createElementNS (NS_X2005, 'nl');
          var lb = document.createDocumentFragment ();
          appendTOCList (nl, lb, document);

          if (nl.childNodes.length > 0) {
            var lb = document.createElementNS (NS_X2005, 'label');
            lb.textContent = ']]>&toc.header;<![CDATA[';
            nl.insertBefore (lb, nl.childNodes[0]);
            toc.appendChild (nl);
            this.parentNode.insertBefore (toc, this.nextSibling);
          }
        }
        ]]>
      </constructor>
    </implementation>
  </binding>
</bindings>

<!-- Revision: $Date: 2005/07/25 10:58:26 $ -->

<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - <http://www.mozilla.org/MPL/>
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is toc.xbl code.
   -
   - The Initial Developer of the Original Code is Wakaba.
   - Portions created by the Initial Developer are Copyright (C) 2005
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -   Wakaba <w@suika.fam.cx>
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the LGPL or the GPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->
