Commits

Luke Plant committed 6eedb89

Fixed bug with styles/commands being lost when the element is changed.

If an element is changed from e.g. h1 -> h2, the id attribute would be lost
and all styles/commands lost, with no visual clue of what had happened.

  • Participants
  • Parent commits 26a9934

Comments (0)

Files changed (2)

semanticeditor/common.py

         if n.tag in BLOCKDEF:
             sect_id = n.get('id')
             if sect_id is not None:
-                if not sect_id.startswith(n.tag) or sect_id in sect_ids:
-                    # don't use invalid or duplicate ids.
-                    # remove
+                if sect_id in sect_ids:
+                    # don't use duplicate ids.
                     del n.attrib['id']
                 else:
                     # reserve

semanticeditor/media/semanticeditor/javascript/wymeditor/plugins/semantic/wymeditor.semantic.js

                   self.docKeydown(evt);
               })
         .bind("mouseup", function(evt) {
+                  // In case the user clicked somewhere else
+                  // in the document, we have to update class list
                   self.updateClassListItemAll();
               });
     jQuery(this.wym._options.containersSelector).find("a")
-         .bind("click", function(evt) {
-                   self.updateClassListItemAll();
-               });
+        .bind("mousedown", function(evt) {
+                  // Need to save element id before the 'click' event
+                  // handler for the button runs and changes the id.
+                  self.saveCurrentElemId();
+              })
+        .bind("click", function(evt) {
+                  // Need to restore the element id before anything else
+                  // runs and calls ensureId
+                  self.restoreCurrentElemId();
+                  self.updateClassListItemAll();
+              });
 
     // Insert rewriting of HTML before the WYMeditor updates the textarea.
     jQuery(this.wym._options.updateSelector)
     }
 };
 
+PresentationControls.prototype.saveCurrentElemId = function() {
+
+    // When a container tag is clicked, the tag of the current node is changed,
+    // and the 'id' attribute is removed. This causes the styling and commands
+    // to be lost. So we save the id and restore after the change.  This can
+    // result in strange ids e.g. an h2 element with the id='h1_1'.  However,
+    // this doesn't matter at all - it only matters that ids are unique
+    var nodeId = null;
+    var s = this.wym._iframe.contentWindow.getSelection();
+    if (s.focusNode != null) {
+        var node = s.focusNode;
+        if (node.nodeName == "#text") {
+            // always true?
+            node = node.parentNode;
+        }
+        nodeId = node.id;
+    }
+    this._savedNodeId = nodeId;
+};
+
+PresentationControls.prototype.restoreCurrentElemId = function() {
+
+    if (this._savedNodeId == null) {
+        return;
+    }
+    var s = this.wym._iframe.contentWindow.getSelection();
+    if (s.focusNode != null) {
+        var node = s.focusNode;
+        if (node.nodeName == "#text") {
+            // always true?
+            node = node.parentNode;
+        }
+        if (node.id == null || node.id == "") {
+            node.id = this._savedNodeId;
+        }
+    }
+    this._savedNodeId = null;
+};
+
 // ---- Manipulation of edited document ----
 
 PresentationControls.prototype.setHtml = function(html) {