/[suikacvs]/test/suikawebwww/style/ui/toc.ja.xbl
Suika

Contents of /test/suikawebwww/style/ui/toc.ja.xbl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Mon Jul 25 10:58:26 2005 UTC (20 years, 11 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +37 -14 lines
New namespace for XHTML 2 supported

1 wakaba 1.1 <?xml version="1.0" encoding="iso-2022-jp"?>
2     <!DOCTYPE bindings [
3     <!ENTITY toc.header "$BL\<!(B">
4     ]>
5     <bindings xmlns="http://www.mozilla.org/xbl">
6     <binding id="after">
7     <implementation>
8     <constructor type="application/x-javascript" xml:space="preserve">
9     <![CDATA[
10     const NS_TOC = 'http://suika.fam.cx/~wakaba/archive/2005/3/toc#';
11     const NS_X1 = 'http://www.w3.org/1999/xhtml';
12     const NS_X2 = 'http://www.w3.org/2002/06/xhtml2';
13 wakaba 1.3 const NS_X2005 = 'http://www.w3.org/2002/06/xhtml2/';
14 wakaba 1.1 const NS_XLINK = 'http://www.w3.org/1999/xlink';
15     const NS_XML = 'http://www.w3.org/XML/1998/namespace';
16     const NS_DEFAULT = NS_X1;
17     const NS_HREF = null;
18    
19     var hasToc = document.documentElement.getAttributeNS (NS_TOC, 'hasToc');
20     if (!hasToc) {
21 wakaba 1.3 document.documentElement.setAttributeNS (NS_TOC, 'toc:hasToc', '1');
22 wakaba 1.1
23     var toc = document.createElementNS (NS_X1, 'div');
24 wakaba 1.3 toc.setAttributeNS (NS_TOC, 'toc:toc', '1');
25     /*
26     var tocH = document.createElementNS (NS_X2005, 'h');
27 wakaba 1.1 tocH.textContent = ']]>&toc.header;<![CDATA[';
28     toc.appendChild (tocH);
29 wakaba 1.3 */
30 wakaba 1.1
31     var elementTypeInfo = {};
32     elementTypeInfo[NS_X1] = {
33     head: {type: 'ignore'},
34     body: {type: 'body', id: [null, 'id']},
35     div: {type: 'div', id: [null, 'id']},
36     h1: {type: 'h1', id: [null, 'id']},
37     h2: {type: 'hn', id: [null, 'id']},
38     h3: {type: 'hn', id: [null, 'id']},
39     h4: {type: 'hn', id: [null, 'id']},
40     h5: {type: 'hn', id: [null, 'id']},
41     h6: {type: 'hn', id: [null, 'id']},
42 wakaba 1.2 header: {type: 'header', id: [null, 'id']},
43 wakaba 1.3 section: {type: 'section', id: [null, 'id']},
44 wakaba 1.1 p: {type: 'ignore'},
45     pre: {type: 'ignore'},
46     dl: {type: 'ignore'},
47     ul: {type: 'ignore'},
48     ol: {type: 'ignore'},
49     blockquote: {type: 'ignore'},
50     table: {type: 'table'},
51     };
52     elementTypeInfo[NS_X1]['*'] = {type: 'container', id: [null, 'id']};
53     elementTypeInfo[NS_X2] = {
54     head: {type: 'ignore'},
55     body: {type: 'body', id: [null, 'id']},
56     section: {type: 'section', id: [null, 'id']},
57     h: {type: 'h', id: [null, 'id']},
58     p: {type: 'ignore'},
59     pre: {type: 'ignore'},
60     dl: {type: 'ignore'},
61     nl: {type: 'ignore'},
62     ul: {type: 'ignore'},
63     ol: {type: 'ignore'},
64     blockquote: {type: 'ignore'},
65     table: {type: 'table'},
66     };
67     elementTypeInfo[NS_X2]['*'] = {type: 'container', id: [null, 'id']};
68 wakaba 1.3 elementTypeInfo[NS_X2005] = {
69     head: {type: 'ignore'},
70     body: {type: 'body', id: [null, 'id']},
71     section: {type: 'section', id: [null, 'id']},
72     h: {type: 'h', id: [null, 'id']},
73     p: {type: 'ignore'},
74     pre: {type: 'ignore'},
75     dl: {type: 'ignore'},
76     nl: {type: 'ignore'},
77     ul: {type: 'ignore'},
78     ol: {type: 'ignore'},
79     blockquote: {type: 'ignore'},
80     table: {type: 'table'},
81     };
82     elementTypeInfo[NS_X2005]['*'] = {type: 'container', id: [null, 'id']};
83 wakaba 1.1 elementTypeInfo[NS_TOC] = {};
84     elementTypeInfo[NS_TOC]['*'] = {type: 'ignore'};
85    
86     var setAnonId = function (elNode) {
87     var docEl = document.documentElement;
88     var seq = docEl.getAttributeNS (NS_TOC, 'anonId');
89     if (!seq) { seq = 0 } else { seq = parseInt (seq) }
90 wakaba 1.3 docEl.setAttributeNS (NS_TOC, 'toc:anonId', ++seq);
91 wakaba 1.1 return 'AUTOTOC--ANON' + seq;
92     };
93    
94     var isHTML = (document.documentElement.nodeName == 'HTML');
95    
96     var appendTOCList = function (tocParent, tocLabel, docParent) {
97     var docChildren = docParent.childNodes;
98     for (var i = 0; i < docChildren.length; i++) {
99     var docChild = docChildren.item (i);
100     if (docChild.nodeType == docChild.ELEMENT_NODE) {
101     var ns = docChild.namespaceURI;
102     var ln = docChild.localName;
103     if (!ns && isHTML) {
104     ln = docChild.nodeName.toLowerCase ();
105     ns = NS_X1;
106     }
107     if (!ns) { ns = NS_DEFAULT; ln = docChild.nodeName }
108     var et = elementTypeInfo[ns]
109     ? elementTypeInfo[ns][ln]
110     ? elementTypeInfo[ns][ln]
111     : elementTypeInfo[ns]['*']
112     : null;
113     if (!et) { et = {type: 'container'} }
114     if (et.type == 'div') {
115 wakaba 1.2 var clss = docChild.getAttributeNS (null, 'class')
116 wakaba 1.1 .split (/\s+/);
117 wakaba 1.2 for (var j = 0; j < clss.length; j++) {
118     var cls = clss[j];
119 wakaba 1.1 if (cls == 'section' || cls == 'wiki-section' ||
120     cls == 'article') {
121     et = {type: 'section', id: et.id};
122     break;
123 wakaba 1.2 } else if (cls == 'header') {
124     et = {type: 'header', id: et.id};
125     break;
126 wakaba 1.1 } else if (cls == 'navigation') {
127     et = {type: 'ignore'};
128     break;
129     }
130     }
131     } else if (et.type == 'hn' || et.type == 'h1') {
132     var pt = docParent.getAttributeNS (NS_TOC, 'type');
133     if (pt == 'section' ||
134     (pt == 'body' && et.type == 'h1')) {
135     et = {type: 'h', id: et.id};
136     } else {
137     et = {type: 'hintro', id: et.id};
138     }
139     }
140 wakaba 1.2 docChild.setAttributeNS (NS_TOC, 'toc:type', et.type);
141 wakaba 1.1
142     if (et.type == 'container' || et.type == 'div') {
143     appendTOCList (tocParent, tocLabel, docChild);
144     } else if (et.type == 'section' || et.type == 'body') {
145 wakaba 1.3 var li = document.createElementNS (NS_X2005, 'li');
146     var nl = document.createElementNS (NS_X2005, 'nl');
147 wakaba 1.1 var lb = document.createDocumentFragment ();
148     appendTOCList (nl, lb, docChild);
149     var href;
150     if (et.id) {
151     var idv = docChild.getAttributeNS (et.id[0], et.id[1]);
152     if (idv) {
153     href = '#' + idv;
154     } else {
155     href = setAnonId (docChild);
156     docChild.setAttributeNS (et.id[0], et.id[1], href);
157     href = '#' + href;
158     }
159     } else {
160     href = setAnonId (docChild);
161 wakaba 1.3 docChild.setAttributeNS (NS_XML, 'xml:id', href);
162 wakaba 1.1 href = '#' + href;
163     }
164     if (nl.childNodes.length > 0) {
165 wakaba 1.3 var lbl = document.createElementNS (NS_X2005, 'label');
166 wakaba 1.1 if (lb.childNodes.length > 0) {
167     lbl.appendChild (lb);
168     } else {
169     lbl.appendChild (document.createTextNode ('#'));
170     }
171     lbl.setAttributeNS (NS_HREF, 'href', href);
172     nl.insertBefore (lbl, nl.childNodes.item (0));
173     li.appendChild (nl);
174     } else {
175     if (lb) {
176     li.appendChild (lb);
177     } else {
178     li.appendChild (document.createTextNode ('#'));
179     }
180     li.setAttributeNS (NS_HREF, 'href', href);
181     }
182     tocParent.appendChild (li);
183     } else if (et.type == 'h' && !tocLabel.textContent) {
184     tocLabel.textContent = docChild.textContent;
185 wakaba 1.2 } else if (et.type == 'header' && !tocLabel.textContent) {
186 wakaba 1.3 var hel = docChild.getElementsByTagNameNS (NS_X2005, 'h')[0] ||
187     docChild.getElementsByTagNameNS (NS_X2, 'h')[0] ||
188 wakaba 1.2 docChild.getElementsByTagNameNS (NS_X1, 'h1')[0] ||
189     docChild.getElementsByTagNameNS (null, 'h1')[0] ||
190     docChild.getElementsByTagNameNS (NS_X1, 'h2')[0] ||
191     docChild.getElementsByTagNameNS (null, 'h2')[0] ||
192     docChild.getElementsByTagNameNS (NS_X1, 'h3')[0] ||
193     docChild.getElementsByTagNameNS (null, 'h3')[0] ||
194     docChild.getElementsByTagNameNS (NS_X1, 'h4')[0] ||
195     docChild.getElementsByTagNameNS (null, 'h4')[0] ||
196     docChild.getElementsByTagNameNS (NS_X1, 'h5')[0] ||
197     docChild.getElementsByTagNameNS (null, 'h5')[0] ||
198     docChild.getElementsByTagNameNS (NS_X1, 'h6')[0] ||
199     docChild.getElementsByTagNameNS (null, 'h6')[0];
200     if (hel) {
201     tocLabel.textContent = hel.textContent;
202     }
203 wakaba 1.1 } else if (et.type == 'hintro' || et.type == 'h') {
204 wakaba 1.3 var li = document.createElementNS (NS_X2005, 'li');
205 wakaba 1.1 li.textContent = docChild.textContent;
206 wakaba 1.3 li.setAttributeNS (NS_TOC, 'toc:element-type', ln);
207     li.setAttributeNS (NS_TOC, 'toc:element-type-ns-uri', ns);
208 wakaba 1.1 tocParent.appendChild (li);
209     var href;
210     if (et.id) {
211     var idv = docChild.getAttributeNS (et.id[0], et.id[1]);
212     if (idv) {
213     href = '#' + idv;
214     } else {
215     href = setAnonId (docChild);
216     docChild.setAttributeNS (et.id[0], et.id[1], href);
217     href = '#' + href;
218     }
219     } else {
220     href = setAnonId (docChild);
221 wakaba 1.2 docChild.setAttributeNS (NS_XML, 'xml:id', href);
222 wakaba 1.1 href = '#' + href;
223     }
224     li.setAttributeNS (NS_HREF, 'href', href);
225     }
226 wakaba 1.2 /* 2nd or more header's should also be supported?? */
227 wakaba 1.1 }
228     }
229     };
230    
231 wakaba 1.3 var nl = document.createElementNS (NS_X2005, 'nl');
232 wakaba 1.1 var lb = document.createDocumentFragment ();
233     appendTOCList (nl, lb, document);
234    
235     if (nl.childNodes.length > 0) {
236 wakaba 1.3 var lb = document.createElementNS (NS_X2005, 'label');
237     lb.textContent = ']]>&toc.header;<![CDATA[';
238     nl.insertBefore (lb, nl.childNodes[0]);
239 wakaba 1.1 toc.appendChild (nl);
240     this.parentNode.insertBefore (toc, this.nextSibling);
241     }
242     }
243     ]]>
244     </constructor>
245     </implementation>
246     </binding>
247     </bindings>
248    
249 wakaba 1.3 <!-- Revision: $Date: 2005/07/19 11:59:05 $ -->
250 wakaba 1.1
251     <!-- ***** BEGIN LICENSE BLOCK *****
252     - Version: MPL 1.1/GPL 2.0/LGPL 2.1
253     -
254     - The contents of this file are subject to the Mozilla Public License Version
255     - 1.1 (the "License"); you may not use this file except in compliance with
256     - the License. You may obtain a copy of the License at
257     - <http://www.mozilla.org/MPL/>
258     -
259     - Software distributed under the License is distributed on an "AS IS" basis,
260     - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
261     - for the specific language governing rights and limitations under the
262     - License.
263     -
264     - The Original Code is toc.xbl code.
265     -
266     - The Initial Developer of the Original Code is Wakaba.
267     - Portions created by the Initial Developer are Copyright (C) 2005
268     - the Initial Developer. All Rights Reserved.
269     -
270     - Contributor(s):
271     - Wakaba <w@suika.fam.cx>
272     -
273     - Alternatively, the contents of this file may be used under the terms of
274     - either the GNU General Public License Version 2 or later (the "GPL"), or
275     - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
276     - in which case the provisions of the GPL or the LGPL are applicable instead
277     - of those above. If you wish to allow use of your version of this file only
278     - under the terms of either the GPL or the LGPL, and not to allow others to
279     - use your version of this file under the terms of the MPL, indicate your
280     - decision by deleting the provisions above and replace them with the notice
281     - and other provisions required by the LGPL or the GPL. If you do not delete
282     - the provisions above, a recipient may use your version of this file under
283     - the terms of any one of the MPL, the GPL or the LGPL.
284     -
285     - ***** END LICENSE BLOCK ***** -->

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24