General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Help with .IsAdjacent
-
edporterIII January 8, 2016 at 5:00 pm
Help with .IsAdjacent
January 8, 2016 at 5:00 pmParticipants 1Replies 2Last Activity 7 years, 2 months agoI am trying to walk through a document and catch errant spaces at the end of elements. Generally, I want to remove them; however, in the instance of spaces at the end of
followed immediately by another i would like to recognize that the first tag is adjacent to the other, and preserve the space. I've written the following code, but .IsAdjacent appears to always return false, regardless of whether or not the next element actually is adjacent to the range in question:
[code]
while (rng_Document.MoveToElement()) {
switch (rng_Document.ContainerName) {
case “emph”:
var rng_test = rng_Document.Duplicate;
rng_Document.SelectContainerContents();
var text = rng_Document.text;
rng_test.MoveToElement();
var neighbor = rng_test.ContainerName;
var firstChar = text.charAt(0);
var lastChar = text.charAt(text.length – 1);
if (firstChar === space && lastChar === space && (!(neighbor = 'chapnum' && rng_Document.IsAdjacent(rng_test)) && !(neighbor = 'emph' && rng_Document.IsAdjacent(rng_test))) {
text = “” + text.slice(1, (text.length – 1)) + ““;
rng_Document.text = text;}
else if (firstChar === space) {
text = “” + text.slice(1, text.length);
rng_Document.text = text;}
else if (lastChar === space && (!(neighbor = 'chapnum' && rng_Document.IsAdjacent(rng_test)) && !(neighbor = 'emph' && rng_Document.IsAdjacent(rng_test)))) {
text = text.slice(0, text.length – 1) + ““;
rng_Document.text = text;}
rng_Document.Collapse(1);
break;
[/code]So, for XML looking something like this:
[code]40(6) [Nov.–Dec. 2014].
[/code].isAdjacent is still returning false. I'm clearly missing something. Any ideas?
Derek Read January 8, 2016 at 9:01 pm
Reply to: Help with .IsAdjacent
January 8, 2016 at 9:01 pm.IsAdjacent returns true when the selection (or insertion point, aka: cursor) is immediately adjacent to the Range you are testing (either immediately to the left or right). If the selection includes or is inside another element node then that will also return false.
So, in your example, for a test using .IsAdjacent on the second
to return true the selection needs to be between the closing tag of the first emph and the opening tag of the second one (where I have marked ***here***): 40(6) ***here***[Nov.–Dec. 2014]. Given what you are doing, I'm not sure how useful .IsAdjacent is going to be. Perhaps using DOM nextSibling and previousSibling would be better?
edporterIII January 11, 2016 at 7:36 pm
Reply to: Help with .IsAdjacent
January 11, 2016 at 7:36 pm.previous/nextSibling got me where I needed to go. I was having trouble with those methods previously, but I figured out my mistakes.
-
AuthorPosts
- You must be logged in to reply to this topic.