2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.lexer
;
18 import com
.intellij
.psi
.TokenType
;
19 import com
.intellij
.psi
.tree
.IElementType
;
20 import com
.intellij
.psi
.xml
.XmlTokenType
;
23 * Created by IntelliJ IDEA.
24 * User: Maxim.Mossienko
27 * To change this template use File | Settings | File Templates.
29 public class HtmlLexer
extends BaseHtmlLexer
{
30 private static IElementType ourStyleElementType
;
31 private static IElementType ourInlineStyleElementType
;
32 private static IElementType ourScriptElementType
;
33 private static IElementType ourInlineScriptElementType
;
35 private IElementType myTokenType
;
36 private int myTokenStart
;
37 private int myTokenEnd
;
40 public void start(CharSequence buffer
, int startOffset
, int endOffset
, int initialState
) {
42 super.start(buffer
, startOffset
, endOffset
, initialState
);
45 public void advance() {
50 public IElementType
getTokenType() {
51 if (myTokenType
!=null) return myTokenType
;
52 IElementType tokenType
= super.getTokenType();
54 myTokenStart
= super.getTokenStart();
55 myTokenEnd
= super.getTokenEnd();
58 if (hasSeenTag() && ourStyleElementType
!=null && isStartOfEmbeddmentTagContent(tokenType
)) {
59 myTokenEnd
= skipToTheEndOfTheEmbeddment();
60 tokenType
= ourStyleElementType
;
61 } else if (ourInlineStyleElementType
!=null && isStartOfEmbeddmentAttributeValue(tokenType
) && hasSeenAttribute()) {
62 tokenType
= ourInlineStyleElementType
;
64 } else if (hasSeenScript()) {
65 if (hasSeenTag() && ourScriptElementType
!=null && isStartOfEmbeddmentTagContent(tokenType
)) {
66 myTokenEnd
= skipToTheEndOfTheEmbeddment();
67 tokenType
= ourScriptElementType
;
68 } else if (hasSeenAttribute() && isStartOfEmbeddmentAttributeValue(tokenType
) && ourInlineScriptElementType
!=null) {
69 myTokenEnd
= skipToTheEndOfTheEmbeddment();
70 tokenType
= ourInlineScriptElementType
;
74 return myTokenType
= tokenType
;
77 private boolean isStartOfEmbeddmentAttributeValue(final IElementType tokenType
) {
78 return tokenType
== XmlTokenType
.XML_ATTRIBUTE_VALUE_TOKEN
;
81 private boolean isStartOfEmbeddmentTagContent(final IElementType tokenType
) {
82 return (tokenType
== XmlTokenType
.XML_DATA_CHARACTERS
||
83 tokenType
== XmlTokenType
.XML_CDATA_START
||
84 tokenType
== XmlTokenType
.XML_COMMENT_START
||
85 tokenType
== XmlTokenType
.XML_REAL_WHITE_SPACE
|| tokenType
== TokenType
.WHITE_SPACE
90 this(new MergingLexerAdapter(new FlexAdapter(new _HtmlLexer()), TOKENS_TO_MERGE
),true);
93 protected HtmlLexer(Lexer _baseLexer
, boolean _caseInsensitive
) {
94 super(_baseLexer
,_caseInsensitive
);
97 public static final void setStyleElementTypes(IElementType _styleElementType
,IElementType _inlineStyleElementType
) {
98 ourStyleElementType
= _styleElementType
;
99 ourInlineStyleElementType
= _inlineStyleElementType
;
102 protected boolean isHtmlTagState(int state
) {
103 return state
== _HtmlLexer
.START_TAG_NAME
|| state
== _HtmlLexer
.END_TAG_NAME
;
106 public int getTokenStart() {
107 if (myTokenType
!=null) {
110 return super.getTokenStart();
113 public int getTokenEnd() {
114 if (myTokenType
!=null) {
117 return super.getTokenEnd();
120 public static void setScriptElementTypes(final IElementType scriptElementType
, final IElementType inlineScriptElementType
) {
121 ourScriptElementType
= scriptElementType
;
122 ourInlineScriptElementType
= inlineScriptElementType
;