WIP: Note editing, markdown to html
[oweals/karmaworld.git] / karmaworld / apps / wysihtml5 / static / wysihtml5 / wysihtml-0.4.17 / src / commands / removeLink.js
1 (function(wysihtml5) {
2   var dom = wysihtml5.dom;
3
4   function _removeFormat(composer, anchors) {
5     var length  = anchors.length,
6         i       = 0,
7         anchor,
8         codeElement,
9         textContent;
10     for (; i<length; i++) {
11       anchor      = anchors[i];
12       codeElement = dom.getParentElement(anchor, { nodeName: "code" });
13       textContent = dom.getTextContent(anchor);
14
15       // if <a> contains url-like text content, rename it to <code> to prevent re-autolinking
16       // else replace <a> with its childNodes
17       if (textContent.match(dom.autoLink.URL_REG_EXP) && !codeElement) {
18         // <code> element is used to prevent later auto-linking of the content
19         codeElement = dom.renameElement(anchor, "code");
20       } else {
21         dom.replaceWithChildNodes(anchor);
22       }
23     }
24   }
25
26   wysihtml5.commands.removeLink = {
27     /*
28      * If selection is a link, it removes the link and wraps it with a <code> element
29      * The <code> element is needed to avoid auto linking
30      *
31      * @example
32      *    wysihtml5.commands.createLink.exec(composer, "removeLink");
33      */
34
35     exec: function(composer, command) {
36       var anchors = this.state(composer, command);
37       if (anchors) {
38         composer.selection.executeAndRestore(function() {
39           _removeFormat(composer, anchors);
40         });
41       }
42     },
43
44     state: function(composer, command) {
45       return wysihtml5.commands.formatInline.state(composer, command, "A");
46     }
47   };
48 })(wysihtml5);