Fix Dreamweaver’s Open document context menu to work with backslash paths
Yesterday I ran into an issue where I opened a large ASP web site in Dreamweaver and right-clicked on an include statement in the code window that looked like:
[html]<!– #include virtual="\root\relative\path\to\some.asp"–>[/html]
Clicking on Open “\root\relative\path\to\some.asp” threw an error dialog that said:
C:\path\to\siteroot\root\relative\path\to\\root\relative\path\to\some.asp contains an invalid path.
Notice that the “\root\relative\path\to\some.asp” reference from my include statement was simply appended to the path of the current file’s folder, leading backslash and all, like so:
- C:\path\to\siteroot\root\relative\path\to\
+ \root\relative\path\to\some.asp
This would never do!
Tracking down the bug
Now, you might say that the bug here lies in the markup; given that web URLs typically use forward slashes Dreamweaver might be forgiven for interpreting a backslash as starting a relative URL. So one fix would be to update the markup.
In my scenario, however, I didn’t write that code and don’t really have permissions to globally change the include file format across the entire site, so that option was off the table; time to crack open the menus.txt file.
On my machine the menus.txt file lives here:
[bat]%APPDATA%/Macromedia/Dreamweaver8/Configuration/Menus/menus.txt [/bat]
but your mileage will vary based on OS and Dreamweaver versions.
Inside the menus.txt file I found:
[xml]<menuitem id="DWContext_HTML_OpenDocument" dynamic file="Menus/MM/Open_Document.htm" />[/xml]
So I opened up the Open_Document.htm and found the problem in it’s associated Open_Document.js file:
[js firstline=”49”]if (inArg[0] == ‘/’) { var aDOM = dw.getDocumentDOM(); if (aDOM != null) { absPath = aDOM.siteRelativeToLocalPath(inArg); } } else { var docPath = dw.getDocumentDOM().URL; absPath = dw.relativeToAbsoluteURL(docPath,’‘,inArg); }[/js]
Because my include target did not start with a forward slash, it was indeed resolved as a relative reference relative to the current document’s location.
And now the fix
I introduced a normalized version of the include target that replaced all backslashes with forward slashes:
[js normalizedarg=”inArg.replace(//g,” 1=”'/');” 2=”if” 3=”(normalizedArg[0” language=”49”] == ‘/’) { var aDOM = dw.getDocumentDOM(); if (aDOM != null) { absPath = aDOM.siteRelativeToLocalPath(normalizedArg); } } else { var docPath = dw.getDocumentDOM().URL; absPath = dw.relativeToAbsoluteURL(docPath,’‘,normalizedArg); }[/js]
Reload my extensions to activate the changes and voila!
Right-clicking an include that starts with a backslash gets properly resolved as a site-root-relative path and my include file opens as expected.