browser/metro/modules/ContentUtil.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/modules/ContentUtil.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +this.EXPORTED_SYMBOLS = ["ContentUtil"];
    1.10 +
    1.11 +const XHTML_NS = "http://www.w3.org/1999/xhtml";
    1.12 +const nsIDOMKeyEvent = Components.interfaces.nsIDOMKeyEvent;
    1.13 +
    1.14 +this.ContentUtil = {
    1.15 +  populateFragmentFromString: function populateFragmentFromString(fragment, str) {
    1.16 +    let re = /^([^#]*)#(\d+)\b([^#]*)/,
    1.17 +        document = fragment.ownerDocument,
    1.18 +        // the remaining arguments are our {text, className} values
    1.19 +        replacements = Array.slice(arguments, 2),
    1.20 +        match;
    1.21 +
    1.22 +    // walk over the string, building textNode/spans as nec. with replacement content
    1.23 +    // note that #1,#2 etc. may not appear in numerical order in the string
    1.24 +    while ((match = re.exec(str))) {
    1.25 +      let [mstring,pre,num,post] = match,
    1.26 +          replaceText = "",
    1.27 +          replaceClass,
    1.28 +          idx = num-1; // markers are 1-based, replacement indices 0 based
    1.29 +
    1.30 +      str = str.substring(re.lastIndex+mstring.length);
    1.31 +
    1.32 +      if (pre)
    1.33 +          fragment.appendChild(document.createTextNode(pre));
    1.34 +
    1.35 +      if (replacements[idx]) {
    1.36 +        replaceText = replacements[idx].text;
    1.37 +        let spanNode = document.createElementNS(XHTML_NS, "span");
    1.38 +        spanNode.appendChild(document.createTextNode(replaceText));
    1.39 +        // add class to the span when provided
    1.40 +        if(replacements[idx].className)
    1.41 +          spanNode.classList.add(replacements[idx].className);
    1.42 +
    1.43 +        fragment.appendChild(spanNode);
    1.44 +      } else {
    1.45 +        // put it back if no replacement was provided
    1.46 +        fragment.appendChild(document.createTextNode("#"+num));
    1.47 +      }
    1.48 +
    1.49 +      if(post)
    1.50 +        fragment.appendChild(document.createTextNode(post));
    1.51 +    }
    1.52 +    if(str)
    1.53 +      fragment.appendChild(document.createTextNode(str));
    1.54 +
    1.55 +    return fragment;
    1.56 +  },
    1.57 +
    1.58 +  // Pass several objects in and it will combine them all into the first object and return it.
    1.59 +  // NOTE: Deep copy is not supported
    1.60 +  extend: function extend() {
    1.61 +    // copy reference to target object
    1.62 +    let target = arguments[0] || {};
    1.63 +    let length = arguments.length;
    1.64 +
    1.65 +    if (length === 1) {
    1.66 +      return target;
    1.67 +    }
    1.68 +
    1.69 +    // Handle case when target is a string or something
    1.70 +    if (typeof target != "object" && typeof target != "function") {
    1.71 +      target = {};
    1.72 +    }
    1.73 +
    1.74 +    for (let i = 1; i < length; i++) {
    1.75 +      // Only deal with non-null/undefined values
    1.76 +      let options = arguments[i];
    1.77 +      if (options != null) {
    1.78 +        // Extend the base object
    1.79 +        for (let name in options) {
    1.80 +          let copy = options[name];
    1.81 +
    1.82 +          // Prevent never-ending loop
    1.83 +          if (target === copy)
    1.84 +            continue;
    1.85 +
    1.86 +          if (copy !== undefined)
    1.87 +            target[name] = copy;
    1.88 +        }
    1.89 +      }
    1.90 +    }
    1.91 +
    1.92 +    // Return the modified object
    1.93 +    return target;
    1.94 +  },
    1.95 +
    1.96 +  // Checks if a keycode is used for list navigation.
    1.97 +  isNavigationKey: function (keyCode) {
    1.98 +    let navigationKeys = [
    1.99 +      nsIDOMKeyEvent.DOM_VK_DOWN,
   1.100 +      nsIDOMKeyEvent.DOM_VK_UP,
   1.101 +      nsIDOMKeyEvent.DOM_VK_LEFT,
   1.102 +      nsIDOMKeyEvent.DOM_VK_RIGHT,
   1.103 +      nsIDOMKeyEvent.DOM_VK_PAGE_UP,
   1.104 +      nsIDOMKeyEvent.DOM_VK_PAGE_DOWN,
   1.105 +      nsIDOMKeyEvent.DOM_VK_ESCAPE];
   1.106 +
   1.107 +    return navigationKeys.indexOf(keyCode) != -1;
   1.108 +  }
   1.109 +};

mercurial