Skip navigation
Currently Being Moderated

Need help with command - trying to target selected html in document

Sep 18, 2012 6:57 PM

Hi,

 

I am very new to working with JS. I can do simple stuff but when it comes to manipulating the dom, I don't know very much. So, I am trying to develop this command for Dreamweaver that will find all occurances of an abbreviation. Ex. GOC (Government of Canada) and wrap the text with <abbr> tags that include the title attribute. I have been using the change case example on the web that details how to go about setting up a DW command.

 

My JS file looks like this:

 

function canAcceptCommand(){

    var theDOM = dw.getDocumentDOM(); // Get the DOM of the current document

    var theSel = theDOM.getSelection(); // Get start and end of selection

    var theSelNode = theDOM.getSelectedNode(); // Get the selected node

    var theChildren = theSelNode.childNodes; // Get children of selected node

    return (theSel[0] != theSel[1] && (theSelNode.nodeType == Node.ELEMENT_NODE

    || theSelNode.hasChildNodes() && (theChildren[0].nodeType ==

    Node.ELEMENT_NODE)));

}

 

function abbr(){

 

  var theDOM = dw.getDocumentDOM();

          // Get the outerHTML of the HTML tag (the entire contents of the document).

          var theDocEl = theDOM.documentElement;

          var theWholeDoc = theDocEl.outerHTML;

    

 

          //while the position of a double space is not equal to -1, so there is one

 

          while(theWholeDoc.search("FSWEP") != "-1"){

                    //use reg exp to replace all spaces, which is useful for processing multiple lines at once

                    theWholeDoc = theWholeDoc.replace(/(FSWEP)/g, "<abbr>ASAP<\/abbr>");

          }

          //loop back around until there are no double spaces

          theDocEl.outerHTML = theWholeDoc;

          theDOM.formatRange(0, theWholeDoc.length);

 

          window.close(); // close extension UI

 

}

 

In it's current state, it will do a replacement of FSWEP anywhere it shows up in the document. What I need it to do is only replace those occurances that are part of the selected HTML block and are part of a text node.

 

If there is anybody out there who can help me with this, I would really appreciate any assistance you might provide. At a very minimum, could you point me to some helpful resources that will help me figure out how to target chuncks of code in a DW HTML file.

 

Thanks,


Rob

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points