Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | this.EXPORTED_SYMBOLS = ["ContentUtil"]; |
michael@0 | 7 | |
michael@0 | 8 | const XHTML_NS = "http://www.w3.org/1999/xhtml"; |
michael@0 | 9 | const nsIDOMKeyEvent = Components.interfaces.nsIDOMKeyEvent; |
michael@0 | 10 | |
michael@0 | 11 | this.ContentUtil = { |
michael@0 | 12 | populateFragmentFromString: function populateFragmentFromString(fragment, str) { |
michael@0 | 13 | let re = /^([^#]*)#(\d+)\b([^#]*)/, |
michael@0 | 14 | document = fragment.ownerDocument, |
michael@0 | 15 | // the remaining arguments are our {text, className} values |
michael@0 | 16 | replacements = Array.slice(arguments, 2), |
michael@0 | 17 | match; |
michael@0 | 18 | |
michael@0 | 19 | // walk over the string, building textNode/spans as nec. with replacement content |
michael@0 | 20 | // note that #1,#2 etc. may not appear in numerical order in the string |
michael@0 | 21 | while ((match = re.exec(str))) { |
michael@0 | 22 | let [mstring,pre,num,post] = match, |
michael@0 | 23 | replaceText = "", |
michael@0 | 24 | replaceClass, |
michael@0 | 25 | idx = num-1; // markers are 1-based, replacement indices 0 based |
michael@0 | 26 | |
michael@0 | 27 | str = str.substring(re.lastIndex+mstring.length); |
michael@0 | 28 | |
michael@0 | 29 | if (pre) |
michael@0 | 30 | fragment.appendChild(document.createTextNode(pre)); |
michael@0 | 31 | |
michael@0 | 32 | if (replacements[idx]) { |
michael@0 | 33 | replaceText = replacements[idx].text; |
michael@0 | 34 | let spanNode = document.createElementNS(XHTML_NS, "span"); |
michael@0 | 35 | spanNode.appendChild(document.createTextNode(replaceText)); |
michael@0 | 36 | // add class to the span when provided |
michael@0 | 37 | if(replacements[idx].className) |
michael@0 | 38 | spanNode.classList.add(replacements[idx].className); |
michael@0 | 39 | |
michael@0 | 40 | fragment.appendChild(spanNode); |
michael@0 | 41 | } else { |
michael@0 | 42 | // put it back if no replacement was provided |
michael@0 | 43 | fragment.appendChild(document.createTextNode("#"+num)); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | if(post) |
michael@0 | 47 | fragment.appendChild(document.createTextNode(post)); |
michael@0 | 48 | } |
michael@0 | 49 | if(str) |
michael@0 | 50 | fragment.appendChild(document.createTextNode(str)); |
michael@0 | 51 | |
michael@0 | 52 | return fragment; |
michael@0 | 53 | }, |
michael@0 | 54 | |
michael@0 | 55 | // Pass several objects in and it will combine them all into the first object and return it. |
michael@0 | 56 | // NOTE: Deep copy is not supported |
michael@0 | 57 | extend: function extend() { |
michael@0 | 58 | // copy reference to target object |
michael@0 | 59 | let target = arguments[0] || {}; |
michael@0 | 60 | let length = arguments.length; |
michael@0 | 61 | |
michael@0 | 62 | if (length === 1) { |
michael@0 | 63 | return target; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Handle case when target is a string or something |
michael@0 | 67 | if (typeof target != "object" && typeof target != "function") { |
michael@0 | 68 | target = {}; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | for (let i = 1; i < length; i++) { |
michael@0 | 72 | // Only deal with non-null/undefined values |
michael@0 | 73 | let options = arguments[i]; |
michael@0 | 74 | if (options != null) { |
michael@0 | 75 | // Extend the base object |
michael@0 | 76 | for (let name in options) { |
michael@0 | 77 | let copy = options[name]; |
michael@0 | 78 | |
michael@0 | 79 | // Prevent never-ending loop |
michael@0 | 80 | if (target === copy) |
michael@0 | 81 | continue; |
michael@0 | 82 | |
michael@0 | 83 | if (copy !== undefined) |
michael@0 | 84 | target[name] = copy; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // Return the modified object |
michael@0 | 90 | return target; |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | // Checks if a keycode is used for list navigation. |
michael@0 | 94 | isNavigationKey: function (keyCode) { |
michael@0 | 95 | let navigationKeys = [ |
michael@0 | 96 | nsIDOMKeyEvent.DOM_VK_DOWN, |
michael@0 | 97 | nsIDOMKeyEvent.DOM_VK_UP, |
michael@0 | 98 | nsIDOMKeyEvent.DOM_VK_LEFT, |
michael@0 | 99 | nsIDOMKeyEvent.DOM_VK_RIGHT, |
michael@0 | 100 | nsIDOMKeyEvent.DOM_VK_PAGE_UP, |
michael@0 | 101 | nsIDOMKeyEvent.DOM_VK_PAGE_DOWN, |
michael@0 | 102 | nsIDOMKeyEvent.DOM_VK_ESCAPE]; |
michael@0 | 103 | |
michael@0 | 104 | return navigationKeys.indexOf(keyCode) != -1; |
michael@0 | 105 | } |
michael@0 | 106 | }; |