Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

28
LINES

< > BotCompany Repo | #1005097 // findContainerOrEmptyTag - HTML/XML parsing

JavaX fragment (include)

1  
// tok must come from htmlcoarsetok
2  
// returns all container tags found (including content) as CNC
3  
// honors nested tags correctly
4  
static LL<S> findContainerOrEmptyTag(L<S> tok, S tag) {
5  
  new LL<S> l;
6  
  for (int i = 1; i < l(tok); i += 2)
7  
    if (isEmptyTag(tok.get(i), tag))
8  
      l.add(tok.subList(i-1, i+1));
9  
    else if (isOpeningTag(tok.get(i), tag)) {
10  
      int j, level = 1;
11  
      for (j = i+2; j < tok.size(); j += 2)
12  
        if (isOpeningTag(tok.get(j), tag))
13  
          ++level;
14  
        else if (isTag(tok.get(j), "/" + tag)) {
15  
          --level;
16  
          if (level == 0) {
17  
            l.add(subList(tok, i-1, j+2)); // actual CNC
18  
            break;
19  
          }
20  
        }
21  
      i = j;
22  
    }
23  
  ret l;
24  
}
25  
26  
static LL<S> findContainerOrEmptyTag(S html, S tag) {
27  
  ret findContainerOrEmptyTag(htmlTok(html), tag);
28  
}

Author comment

Began life as a copy of #1002309

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005097
Snippet name: findContainerOrEmptyTag - HTML/XML parsing
Eternal ID of this version: #1005097/3
Text MD5: 176a163bf36f402b2db1dbd44cbf8ae4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 03:13:06
Source code size: 847 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 592 / 576
Version history: 2 change(s)
Referenced in: [show references]