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 * @author Maxim.Mossienko
25 public class HtmlLexer
extends BaseHtmlLexer
{
26 private static IElementType ourStyleElementType
;
27 private static IElementType ourInlineStyleElementType
;
28 private static IElementType ourScriptElementType
;
29 private static IElementType ourInlineScriptElementType
;
31 private IElementType myTokenType
;
32 private int myTokenStart
;
33 private int myTokenEnd
;
36 public void start(CharSequence buffer
, int startOffset
, int endOffset
, int initialState
) {
38 super.start(buffer
, startOffset
, endOffset
, initialState
);
41 public void advance() {
46 public IElementType
getTokenType() {
47 if (myTokenType
!=null) return myTokenType
;
48 IElementType tokenType
= super.getTokenType();
50 myTokenStart
= super.getTokenStart();
51 myTokenEnd
= super.getTokenEnd();
54 if (hasSeenTag() && ourStyleElementType
!=null && isStartOfEmbeddmentTagContent(tokenType
)) {
55 myTokenEnd
= skipToTheEndOfTheEmbeddment();
56 tokenType
= ourStyleElementType
;
57 } else if (ourInlineStyleElementType
!=null && isStartOfEmbeddmentAttributeValue(tokenType
) && hasSeenAttribute()) {
58 tokenType
= ourInlineStyleElementType
;
60 } else if (hasSeenScript()) {
61 if (hasSeenTag() && ourScriptElementType
!=null && isStartOfEmbeddmentTagContent(tokenType
)) {
62 myTokenEnd
= skipToTheEndOfTheEmbeddment();
63 tokenType
= ourScriptElementType
;
64 } else if (hasSeenAttribute() && isStartOfEmbeddmentAttributeValue(tokenType
) && ourInlineScriptElementType
!=null) {
65 myTokenEnd
= skipToTheEndOfTheEmbeddment();
66 tokenType
= ourInlineScriptElementType
;
70 return myTokenType
= tokenType
;
73 private static boolean isStartOfEmbeddmentAttributeValue(final IElementType tokenType
) {
74 return tokenType
== XmlTokenType
.XML_ATTRIBUTE_VALUE_TOKEN
;
77 private static boolean isStartOfEmbeddmentTagContent(final IElementType tokenType
) {
78 return (tokenType
== XmlTokenType
.XML_DATA_CHARACTERS
||
79 tokenType
== XmlTokenType
.XML_CDATA_START
||
80 tokenType
== XmlTokenType
.XML_COMMENT_START
||
81 tokenType
== XmlTokenType
.XML_REAL_WHITE_SPACE
|| tokenType
== TokenType
.WHITE_SPACE
86 this(new MergingLexerAdapter(new FlexAdapter(new _HtmlLexer()), TOKENS_TO_MERGE
),true);
89 protected HtmlLexer(Lexer _baseLexer
, boolean _caseInsensitive
) {
90 super(_baseLexer
,_caseInsensitive
);
93 public static void setStyleElementTypes(IElementType _styleElementType
,IElementType _inlineStyleElementType
) {
94 ourStyleElementType
= _styleElementType
;
95 ourInlineStyleElementType
= _inlineStyleElementType
;
98 protected boolean isHtmlTagState(int state
) {
99 return state
== _HtmlLexer
.START_TAG_NAME
|| state
== _HtmlLexer
.END_TAG_NAME
;
102 public int getTokenStart() {
103 if (myTokenType
!=null) {
106 return super.getTokenStart();
109 public int getTokenEnd() {
110 if (myTokenType
!=null) {
113 return super.getTokenEnd();
116 public static void setScriptElementTypes(final IElementType scriptElementType
, final IElementType inlineScriptElementType
) {
117 ourScriptElementType
= scriptElementType
;
118 ourInlineScriptElementType
= inlineScriptElementType
;