Viewing 1 post (of 1 total)
  • Author
    Posts
  • #10356
    Stefan
    Participant


    Select whole HTML / XML tag

    How this looks:

    Some tips:

    – This is version 0.01 but works good for me.

    – The purpose is to select an whole

    – Therefore the script takes the word under the cursor (the TAG, i hope)
    – then it looks at each line till EOF for the closing tag with same name.
    – On the way it counts other opening and closing tag’s.
    – only if (opening – closing == 0) i assume the right closing tag is found.
    That way i can find nested tag’s also.

    – it works only for proper written tags without spaces due to:

    //Set the search pattern:
    OpenTag = “<" + TagName;
    CloseTag = ““;

    Examples:
    = OK
    = OK
    = OK
    /> = OK
    < tag > = wrong
    = wrong

    – put your cursor ON the word of the opening tag or at one side:

    Examples:
    <|tag yxz> = OK
    = OK
    = OK
    = OK
    = wrong

    (but i think that can be improved,
    as the whole macro could be more clever)

    The macro
    – save this script as “SelectWholeTag.jsee”
    – assign an shortcut to it like “Ctrl+Alt+T”
    – or put it as macro into the snippet plugin.



    //Select Whole HTML / XML tag, v 0.01, 05/2012 Stefan
    /////////////////////////////////////////////////////

    //for less typing:
    o = document.selection;

    // MUCH quicker. But less informative.
    Redraw = false;

    //get the start position:
    PosStartLine = o.GetActivePointY(eePosLogical);
    PosOrigColumn = o.GetActivePointX(eePosLogical);

    //get the current word under cursor:
    o.SelectWord();
    TagName = o.Text;

    //This did not work if tag name length==1 AND cursor at the right. So:
    if (TagName.length == 1){
    ValidSigns = "abcdefghijklmnopqrstuvwxyz"; //means: no '>' or blank.
    if (ValidSigns.indexOf(TagName) == -1){
    o.SetActivePoint( eePosLogical, PosOrigColumn -1, PosStartLine, false );
    o.SelectWord();
    TagName = o.Text;
    }
    }

    //Set the search pattern:
    OpenTag = "<" + TagName;
    CloseTag = "</" + TagName + ">";

    //Get the position of the first opening tag:
    TextThisLine = document.GetLine(PosStartLine);
    PosStartColumn = TextThisLine.indexOf(OpenTag);

    //some initializing:
    EndTagFound = false;


    //start first round:
    DoIt(CloseTag);

    //If no closing tag found, try an another syntax:
    if (EndTagFound == false){
    o.SetActivePoint( eePosLogical, PosOrigColumn, PosStartLine, false );
    //alert("EOF reached but have not found the matching tag: " + CloseTag
    //+ "nnFound opening " + CountOpeningTAGS + " but "
    //+ CountClosingTAGS + " closing tags.nnWill try again...");

    CloseTag = "/>";
    DoIt(CloseTag);
    if (EndTagFound == false){
    o.SetActivePoint( eePosLogical, PosOrigColumn, PosStartLine, false );
    //alert("EOF reached but have not found the matching tag: " + CloseTag
    //+ "nnFound opening " + CountOpeningTAGS + " but "
    //+ CountClosingTAGS + " closing tags.nnSorry, i giving up.");
    }
    }

    //The main function to search the right closing tag:
    function DoIt(CloseTag){
    CountOpeningTAGS=0;
    CountClosingTAGS=0;

    //For Each Line till end of document:
    while (true){
    //For debugging, iterate a few lines only:
    //for( L=1; L < 60; L++){

    //Get current line:
    PosThisLine = o.GetActivePointY(eePosLogical);
    TextThisLine = document.GetLine(PosThisLine);

    //Found an opening tag?
    if (TextThisLine.indexOf(OpenTag) > -1){
    CountOpeningTAGS++;
    LastCol = TextThisLine.indexOf(OpenTag) + 1;
    }
    //check for an second at the same line:
    if (TextThisLine.indexOf(OpenTag, LastCol +1) > -1){
    CountOpeningTAGS++;
    }


    //Found an closing tag?
    if (TextThisLine.indexOf(CloseTag) > -1){
    CountClosingTAGS++;
    LastCol = TextThisLine.indexOf(CloseTag);
    }
    //check for an second at the same line:
    if (TextThisLine.indexOf(CloseTag, LastCol +1) > -1){
    CountClosingTAGS++;
    }

    //Found the right closing tag?
    //alert(CountOpeningTAGS +'-'+ CountClosingTAGS);
    if (CountOpeningTAGS - CountClosingTAGS == 0){
    PosEndColumn = TextThisLine.indexOf(CloseTag) + 1;
    PosEndColumn = TextThisLine.indexOf(">", PosEndColumn) + 2;

    o.SetActivePoint( eePosLogical, PosEndColumn, PosThisLine, false );
    o.SetAnchorPoint( eePosLogical, PosStartColumn +1, PosStartLine );
    EndTagFound = true;
    break;
    }

    //If EOF? Then stop:
    if (PosThisLine >= document.GetLines()){break;}
    //Else move one line down:
    o.StartOfLine( false , eeLineLogical );
    o.LineDown();
    //alert( ' Now on line: ' +PosThisLine);

    } //Loop
    } // function DoIt();



Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.