idMangler.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (C) 2014 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. /*global define, document, dojo, dijit */
  25. define("webodf/editor/widgets/dialogWidgets/idMangler", ["dojo", "dijit"], function (dojo, dijit) {
  26. "use strict";
  27. var instanceCount = 0;
  28. /**
  29. * Mangles html id attributes and associated references so that the same HTML code can be copied into
  30. * a page multiple times without identifier clashes. This also provides helper functions for retrieving
  31. * these now-mangled identifiers.
  32. * @constructor
  33. */
  34. function IdMangler() {
  35. var suffix;
  36. /**
  37. * Returns the supplied text with identifiers mangled
  38. * @param {!string} text
  39. * @return {!string}
  40. */
  41. function mangleIds(text) {
  42. /*jslint regexp: true*/
  43. var newText = text.replace(/((id|for|data-dojo-id)\s*=\s*["'][^"']+)/g, "$1" + suffix);
  44. /*jslint regexp: false*/
  45. return newText;
  46. }
  47. this.mangleIds = mangleIds;
  48. /**
  49. * Replacement method for ContentPane's ioMethod
  50. * @return {*} See http://dojotoolkit.org/api/?qs=1.9/dojo/_base/xhr#1_9dojo__base_xhr_get for return details
  51. */
  52. this.ioMethod = function() {
  53. var args = Array.prototype.slice.call(arguments, 0);
  54. return dojo.xhr.get.apply(dojo, args).then(function(html) {
  55. return mangleIds(html);
  56. });
  57. };
  58. /**
  59. * Replacement for dijit.byId
  60. * @param {!string} id
  61. * @return {*} See http://dojotoolkit.org/api/?qs=1.9/dojo/_base/xhr#1_9dijit_registry_byId for return details
  62. */
  63. this.byId = function(id) {
  64. return dijit.byId(id + suffix);
  65. };
  66. /**
  67. * Replacement for document.getElementById
  68. * @param {!string} id
  69. * @return {HTMLElement|*}
  70. */
  71. this.getElementById = function(id) {
  72. return document.getElementById(id + suffix);
  73. };
  74. function init() {
  75. suffix = "_" + instanceCount;
  76. instanceCount += 1;
  77. }
  78. init();
  79. }
  80. return IdMangler;
  81. });