public void testBaseHtmlEmbedding() throws Exception {
    FileObject file = getTestFile("testfiles/embedding/base-file.tpl");
    Document doc = getDocument(file);

    th = TokenHierarchy.get(doc);
    ts = th.tokenSequence(TplTopTokenId.language());

    // hr|ef
    int offset = 5;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertEquals(HTMLTokenId.ARGUMENT, getHtmlToken(offset).id());

    // {|$variable
    offset = 35;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY_OPEN_DELIMITER);
    assertNullEmbeddingForLanguage(HTMLTokenId.language());

    // {$v|ariable
    offset = 37;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY);
    assertNullEmbeddingForLanguage(HTMLTokenId.language());

    // <|/a>
    offset = 47;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertEquals(HTMLTokenId.TAG_OPEN_SYMBOL, getHtmlToken(offset).id());

    // {p|hp}
    offset = 53;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY);
    assertNullEmbeddingForLanguage(HTMLTokenId.language());

    // {php}\n|
    offset = 57;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_PHP);
    assertNullEmbeddingForLanguage(HTMLTokenId.language());

    // <d|iv
    offset = 91;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // my|DivId
    offset = 100;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertEquals(HTMLTokenId.VALUE_CSS, getHtmlToken(offset).id());

    // Bl|ah
    offset = 160;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertEquals(HTMLTokenId.SCRIPT, getHtmlToken(offset).id());
  }
 private Token<HTMLTokenId> getHtmlToken(int offset) {
   TokenSequence<HTMLTokenId> htmlts = ts.embeddedJoined(HTMLTokenId.language());
   assertNotNull(htmlts);
   htmlts.move(offset);
   assertTrue(htmlts.moveNext());
   return htmlts.token();
 }
 public boolean verifyState(Document doc, int offset) {
   TokenHierarchy hi = TokenHierarchy.get(doc);
   TokenSequence<HTMLTokenId> ts = hi.tokenSequence(HTMLTokenId.language());
   if (ts != null) {
     ts.move(offset);
     ts.moveNext();
     Token<HTMLTokenId> tok = ts.token();
     int newOffset = ts.offset();
     String matcherText = tok.text().toString();
     Matcher m = MY_SPECIAL_PATTERN.matcher(matcherText);
     if (m.matches()) {
       target = m.group(1);
       int idx = matcherText.indexOf(target);
       targetStart = newOffset + idx;
       targetEnd = targetStart + target.length();
       return true;
     }
   }
   return false;
 }
  public void testBasePhpEmbedding() throws Exception {
    FileObject file = getTestFile("testfiles/embedding/base-file.tpl");
    Document doc = getDocument(file);

    th = TokenHierarchy.get(doc);
    ts = th.tokenSequence(TplTopTokenId.language());

    // hr|ef
    int offset = 5;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // {|$variable
    offset = 35;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY_OPEN_DELIMITER);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // {$v|ariable
    offset = 37;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // <|/a>
    offset = 47;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // {p|hp}
    offset = 53;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_SMARTY);
    assertNullEmbeddingForLanguage(HTMLTokenId.language());

    // {php}\n|
    offset = 57;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_PHP);
    assertEquals(getPhpToken(offset).id(), PHPTokenId.WHITESPACE);

    // e|cho
    offset = 62;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_PHP);
    assertEquals(getPhpToken(offset).id(), PHPTokenId.PHP_ECHO);

    // any| PHP
    offset = 69;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_PHP);
    assertEquals(getPhpToken(offset).id(), PHPTokenId.WHITESPACE);

    // <d|iv
    offset = 91;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // my|DivId
    offset = 100;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());

    // Bl|ah
    offset = 160;
    assertEquals(getTplTopToken(offset).id(), TplTopTokenId.T_HTML);
    assertNullEmbeddingForLanguage(PHPTopTokenId.language());
  }