<!DOCTYPE bindings [
  <!ENTITY NS_WRAP "'http://suika.fam.cx/www/style/text/wrap#'">
]>
<bindings xmlns="http://www.mozilla.org/xbl">
  <binding id="wbr-code">
    <implementation>
      <constructor type="application/x-javascript" xml:space="preserve">
        var targetNode = this;
        window.addEventListener ('load', function () {
          if (!targetNode.wrapWbrAdded ()) {
            targetNode.setAttributeNS (&NS_WRAP;, 'wrap:wbrAdded', '1');
            var resolver = document.createNSResolver (targetNode);
            var nodes = document.evaluate (
              'descendant::*/child::text () | child::text ()',
              targetNode, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
            );
            var pattern_before = targetNode.WrapPatternBefore;
            var pattern_after = targetNode.WrapPatternAfter;
            var range = document.createRange ();
            var wbr = document.createElementNS
                                  ('http://www.w3.org/1999/xhtml', "wbr");
            var lastIndex;
            for (var i = 0; i &lt; nodes.snapshotLength; i++) {
              var node = nodes.snapshotItem (i);
              range.selectNode (node);
              while (node) {
                var rangeString = range.toString ();
                lastIndex = [rangeString.search (pattern_before)];
                lastIndex[1] = RegExp.$1.length;
                lastIndex[2] = rangeString.search (pattern_after);
                lastIndex[3] = RegExp.$1.length;
                var j;
                if (lastIndex[0] != -1) {
                  if (lastIndex[2] != -1) {
                    j = lastIndex[2] > lastIndex[0] ? 0 : 1;
                  } else {
                    j = 0;
                  }
                } else {
                  if (lastIndex[2] != -1) {
                    j = 1;
                  } else {
                    break;
                  }
                }
                if (j == 0) {
                  range.setStart (node, lastIndex[0]);
                  range.insertNode (wbr.cloneNode (false));
                  node = node.nextSibling.nextSibling.splitText (lastIndex[1]);
                  range.selectNode (node);
                } else {
                  range.setStart (node, lastIndex[2] + lastIndex[3]);
                  range.insertNode (wbr.cloneNode (false));
                  node = node.nextSibling.nextSibling;
                  range.selectNode (node);
                }
              }
            }
            <![CDATA[
              var fc = targetNode.firstChild;
              if (fc && fc.nodeType == fc.TEXT_NODE &&
                  fc.data.length == 0) {
                fc = fc.nextSibling;
              }
              if (fc && fc.nodeType == fc.ELEMENT_NODE &&
                  fc.localName && fc.localName == 'wbr' &&
                  fc.namespaceURI == 'http://www.w3.org/1999/xhtml') {
                targetNode.removeChild (fc);
              }
            ]]>
            range.detach ();
          }
        }, false);
      </constructor>
      <field name="WrapPatternBefore">
        <![CDATA[/([(<\[{]{1,10})/;]]>
      </field>
      <field name="WrapPatternAfter">
        /([!"')\*\+,\-.\/:;=>?\]\^_`|}]{1,10})/;
      </field>
      <method name="wrapWbrAdded">
        <body>
          <![CDATA[
            const NS_WRAP = 'http://suika.fam.cx/www/style/text/wrap#';
            if (this.getAttributeNS (NS_WRAP, 'wbrAdded') ||
                this.getElementsByTagName ('wbr').length > 0) {
              return true;
            }
            var parents = new Array (this.parentNode);
            while (parents.length > 0) {
              var node = parents.shift ();
              if (node.nodeType == node.ELEMENT_NODE &&
                  node.getAttributeNS (NS_WRAP, 'wbrAdded')) {
                return true;
              } else if (node.parentNode) {
                parents.push (node.parentNode);
              }
            }
            return false;
          ]]>
        </body>
      </method>
    </implementation>
  </binding>
  <binding id="wbr-uri" extends="#wbr-code">
    <implementation>
      <field name="WrapPatternBefore">
        /([#(?\[]{1,10}|%..)/;
      </field>
      <field name="WrapPatternAfter">
        <![CDATA[/([!$&')\*\+,\-.\/:;=@\]_>]{1,10})/;]]>
      </field>
    </implementation>
  </binding>
  <binding id="wbr-dotted" extends="#wbr-code">
    <implementation>
      <field name="WrapPatternBefore">
        /([A-Z]+)/;
      </field>
      <field name="WrapPatternAfter">
        /([._@>-])/;
      </field>
    </implementation>
  </binding>
  <binding id="wbr-qname" extends="#wbr-code">
    <implementation>
      <field name="WrapPatternBefore">
        /([A-Z][A-Z]+(?=[A-Z][a-z]|[^A-Za-z]|$)|[A-Z][a-z])/;
      </field>
      <field name="WrapPatternAfter">
        /([._:->]+)(?!$)/;
      </field>
    </implementation>
  </binding>
</bindings>
<!-- ***** 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 wrap.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 ***** -->