DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

this.EXPORTED_SYMBOLS = [ ];

Cu.import("resource:///modules/devtools/gcli.jsm");

/**
 * 'pagemod' command
 */
gcli.addCommand({
  name: "pagemod",
  description: gcli.lookup("pagemodDesc"),
});

/**
 * The 'pagemod replace' command. This command allows the user to search and
 * replace within text nodes and attributes.
 */
gcli.addCommand({
  name: "pagemod replace",
  description: gcli.lookup("pagemodReplaceDesc"),
  params: [
    {
      name: "search",
      type: "string",
      description: gcli.lookup("pagemodReplaceSearchDesc"),
    },
    {
      name: "replace",
      type: "string",
      description: gcli.lookup("pagemodReplaceReplaceDesc"),
    },
    {
      name: "ignoreCase",
      type: "boolean",
      description: gcli.lookup("pagemodReplaceIgnoreCaseDesc"),
    },
    {
      name: "selector",
      type: "string",
      description: gcli.lookup("pagemodReplaceSelectorDesc"),
      defaultValue: "*:not(script):not(style):not(embed):not(object):not(frame):not(iframe):not(frameset)",
    },
    {
      name: "root",
      type: "node",
      description: gcli.lookup("pagemodReplaceRootDesc"),
      defaultValue: null,
    },
    {
      name: "attrOnly",
      type: "boolean",
      description: gcli.lookup("pagemodReplaceAttrOnlyDesc"),
    },
    {
      name: "contentOnly",
      type: "boolean",
      description: gcli.lookup("pagemodReplaceContentOnlyDesc"),
    },
    {
      name: "attributes",
      type: "string",
      description: gcli.lookup("pagemodReplaceAttributesDesc"),
      defaultValue: null,
    },
  ],
  exec: function(args, context) {
    let document = context.environment.contentDocument;
    let searchTextNodes = !args.attrOnly;
    let searchAttributes = !args.contentOnly;
    let regexOptions = args.ignoreCase ? 'ig' : 'g';
    let search = new RegExp(escapeRegex(args.search), regexOptions);
    let attributeRegex = null;
    if (args.attributes) {
      attributeRegex = new RegExp(args.attributes, regexOptions);
    }

    let root = args.root || document;
    let elements = root.querySelectorAll(args.selector);
    elements = Array.prototype.slice.call(elements);

    let replacedTextNodes = 0;
    let replacedAttributes = 0;

    function replaceAttribute() {
      replacedAttributes++;
      return args.replace;
    }
    function replaceTextNode() {
      replacedTextNodes++;
      return args.replace;
    }

    for (let i = 0; i < elements.length; i++) {
      let element = elements[i];
      if (searchTextNodes) {
        for (let y = 0; y < element.childNodes.length; y++) {
          let node = element.childNodes[y];
          if (node.nodeType == node.TEXT_NODE) {
            node.textContent = node.textContent.replace(search, replaceTextNode);
          }
        }
      }

      if (searchAttributes) {
        if (!element.attributes) {
          continue;
        }
        for (let y = 0; y < element.attributes.length; y++) {
          let attr = element.attributes[y];
          if (!attributeRegex || attributeRegex.test(attr.name)) {
            attr.value = attr.value.replace(search, replaceAttribute);
          }
        }
      }
    }

    return gcli.lookupFormat("pagemodReplaceResult",
                             [elements.length, replacedTextNodes,
                              replacedAttributes]);
  }
});

/**
 * 'pagemod remove' command
 */
gcli.addCommand({
  name: "pagemod remove",
  description: gcli.lookup("pagemodRemoveDesc"),
});


/**
 * The 'pagemod remove element' command.
 */
gcli.addCommand({
  name: "pagemod remove element",
  description: gcli.lookup("pagemodRemoveElementDesc"),
  params: [
    {
      name: "search",
      type: "string",
      description: gcli.lookup("pagemodRemoveElementSearchDesc"),
    },
    {
      name: "root",
      type: "node",
      description: gcli.lookup("pagemodRemoveElementRootDesc"),
      defaultValue: null,
    },
    {
      name: 'stripOnly',
      type: 'boolean',
      description: gcli.lookup("pagemodRemoveElementStripOnlyDesc"),
    },
    {
      name: 'ifEmptyOnly',
      type: 'boolean',
      description: gcli.lookup("pagemodRemoveElementIfEmptyOnlyDesc"),
    },
  ],
  exec: function(args, context) {
    let document = context.environment.contentDocument;
    let root = args.root || document;
    let elements = Array.prototype.slice.call(root.querySelectorAll(args.search));

    let removed = 0;
    for (let i = 0; i < elements.length; i++) {
      let element = elements[i];
      let parentNode = element.parentNode;
      if (!parentNode || !element.removeChild) {
        continue;
      }
      if (args.stripOnly) {
        while (element.hasChildNodes()) {
          parentNode.insertBefore(element.childNodes[0], element);
        }
      }
      if (!args.ifEmptyOnly || !element.hasChildNodes()) {
        element.parentNode.removeChild(element);
        removed++;
      }
    }

    return gcli.lookupFormat("pagemodRemoveElementResultMatchedAndRemovedElements",
                             [elements.length, removed]);
  }
});

/**
 * The 'pagemod remove attribute' command.
 */
gcli.addCommand({
  name: "pagemod remove attribute",
  description: gcli.lookup("pagemodRemoveAttributeDesc"),
  params: [
    {
      name: "searchAttributes",
      type: "string",
      description: gcli.lookup("pagemodRemoveAttributeSearchAttributesDesc"),
    },
    {
      name: "searchElements",
      type: "string",
      description: gcli.lookup("pagemodRemoveAttributeSearchElementsDesc"),
    },
    {
      name: "root",
      type: "node",
      description: gcli.lookup("pagemodRemoveAttributeRootDesc"),
      defaultValue: null,
    },
    {
      name: "ignoreCase",
      type: "boolean",
      description: gcli.lookup("pagemodRemoveAttributeIgnoreCaseDesc"),
    },
  ],
  exec: function(args, context) {
    let document = context.environment.contentDocument;

    let root = args.root || document;
    let regexOptions = args.ignoreCase ? 'ig' : 'g';
    let attributeRegex = new RegExp(args.searchAttributes, regexOptions);
    let elements = root.querySelectorAll(args.searchElements);
    elements = Array.prototype.slice.call(elements);

    let removed = 0;
    for (let i = 0; i < elements.length; i++) {
      let element = elements[i];
      if (!element.attributes) {
        continue;
      }

      var attrs = Array.prototype.slice.call(element.attributes);
      for (let y = 0; y < attrs.length; y++) {
        let attr = attrs[y];
        if (attributeRegex.test(attr.name)) {
          element.removeAttribute(attr.name);
          removed++;
        }
      }
    }

    return gcli.lookupFormat("pagemodRemoveAttributeResult",
                             [elements.length, removed]);
  }
});

/**
 * Make a given string safe to use  in a regular expression.
 *
 * @param string aString
 *        The string you want to use in a regex.
 * @return string
 *         The equivalent of |aString| but safe to use in a regex.
 */
function escapeRegex(aString) {
  return aString.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}