All files / src/jsp jsp-extractor.ts

100% Statements 77/77
92.85% Branches 13/14
100% Functions 1/1
100% Lines 77/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 782x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 8x 8x 8x 8x 8x 8x 8x 2x 2x 8x 1x 1x 8x 1x 1x 5x 4x 4x 4x 6x 8x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 9x 9x 9x  
export interface JspSection {
  language: 'javascript' | 'css' | 'java';
  code: string;
  lineOffset: number;
  sourceStart: number;
  sourceEnd: number;
  scriptletKind?: 'statement' | 'declaration' | 'expression';
}
 
export function extractJspSections(content: string): JspSection[] {
  const sections: JspSection[] = [];
 
  const scriptRegex = /<script\b[^>]*>([\s\S]*?)<\/script\s*>/gi;
  let match: RegExpExecArray | null;
  while ((match = scriptRegex.exec(content)) !== null) {
    const code = match[1];
    const beforeMatch = content.substring(0, match.index);
    const lineOffset = beforeMatch.split('\n').length - 1;
    sections.push({
      language: 'javascript',
      code,
      lineOffset,
      sourceStart: match.index,
      sourceEnd: match.index + match[0].length,
    });
  }
 
  const styleRegex = /<style\b[^>]*>([\s\S]*?)<\/style\s*>/gi;
  while ((match = styleRegex.exec(content)) !== null) {
    const code = match[1];
    const beforeMatch = content.substring(0, match.index);
    const lineOffset = beforeMatch.split('\n').length - 1;
    sections.push({
      language: 'css',
      code,
      lineOffset,
      sourceStart: match.index,
      sourceEnd: match.index + match[0].length,
    });
  }
 
  const jspTagRegex = /<%--([\s\S]*?)--%>|<%!([\s\S]*?)%>|<%=([\s\S]*?)%>|<%@([\s\S]*?)%>|<%([\s\S]*?)%>/g;
  while ((match = jspTagRegex.exec(content)) !== null) {
    const beforeMatch = content.substring(0, match.index);
    const lineOffset = beforeMatch.split('\n').length - 1;
 
    let code: string | undefined;
    let scriptletKind: JspSection['scriptletKind'] | undefined;
 
    if (match[1] !== undefined || match[4] !== undefined) {
      continue;
    }
    if (match[2] !== undefined) {
      code = match[2];
      scriptletKind = 'declaration';
    } else if (match[3] !== undefined) {
      code = match[3];
      scriptletKind = 'expression';
    } else if (match[5] !== undefined) {
      code = match[5];
      scriptletKind = 'statement';
    }
 
    if (code === undefined) { continue; }
 
    sections.push({
      language: 'java',
      code,
      lineOffset,
      sourceStart: match.index,
      sourceEnd: match.index + match[0].length,
      scriptletKind,
    });
  }
 
  return sections;
}