tools/designer/src/lib/shared/htmlhighlighter.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Designer of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/QTextStream>
       
    43 
       
    44 #include "htmlhighlighter_p.h"
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 namespace qdesigner_internal {
       
    49 
       
    50 HtmlHighlighter::HtmlHighlighter(QTextEdit *textEdit)
       
    51     : QSyntaxHighlighter(textEdit)
       
    52 {
       
    53     QTextCharFormat entityFormat;
       
    54     entityFormat.setForeground(Qt::red);
       
    55     setFormatFor(Entity, entityFormat);
       
    56 
       
    57     QTextCharFormat tagFormat;
       
    58     tagFormat.setForeground(Qt::darkMagenta);
       
    59     tagFormat.setFontWeight(QFont::Bold);
       
    60     setFormatFor(Tag, tagFormat);
       
    61 
       
    62     QTextCharFormat commentFormat;
       
    63     commentFormat.setForeground(Qt::gray);
       
    64     commentFormat.setFontItalic(true);
       
    65     setFormatFor(Comment, commentFormat);
       
    66 
       
    67     QTextCharFormat attributeFormat;
       
    68     attributeFormat.setForeground(Qt::black);
       
    69     attributeFormat.setFontWeight(QFont::Bold);
       
    70     setFormatFor(Attribute, attributeFormat);
       
    71 
       
    72     QTextCharFormat valueFormat;
       
    73     valueFormat.setForeground(Qt::blue);
       
    74     setFormatFor(Value, valueFormat);
       
    75 }
       
    76 
       
    77 void HtmlHighlighter::setFormatFor(Construct construct,
       
    78                                    const QTextCharFormat &format)
       
    79 {
       
    80     m_formats[construct] = format;
       
    81     rehighlight();
       
    82 }
       
    83 
       
    84 void HtmlHighlighter::highlightBlock(const QString &text)
       
    85 {
       
    86     static const QLatin1Char tab = QLatin1Char('\t');
       
    87     static const QLatin1Char space = QLatin1Char(' ');
       
    88     static const QLatin1Char amp = QLatin1Char('&');
       
    89     static const QLatin1Char startTag = QLatin1Char('<');
       
    90     static const QLatin1Char endTag = QLatin1Char('>');
       
    91     static const QLatin1Char quot = QLatin1Char('"');
       
    92     static const QLatin1Char apos = QLatin1Char('\'');
       
    93     static const QLatin1Char semicolon = QLatin1Char(';');
       
    94     static const QLatin1Char equals = QLatin1Char('=');
       
    95     static const QLatin1String startComment = QLatin1String("<!--");
       
    96     static const QLatin1String endComment = QLatin1String("-->");
       
    97     static const QLatin1String endElement = QLatin1String("/>");
       
    98 
       
    99     int state = previousBlockState();
       
   100     int len = text.length();
       
   101     int start = 0;
       
   102     int pos = 0;
       
   103 
       
   104     while (pos < len) {
       
   105         switch (state) {
       
   106         case NormalState:
       
   107         default:
       
   108             while (pos < len) {
       
   109                 QChar ch = text.at(pos);
       
   110                 if (ch == startTag) {
       
   111                     if (text.mid(pos, 4) == startComment) {
       
   112                         state = InComment;
       
   113                     } else {
       
   114                         state = InTag;
       
   115                         start = pos;
       
   116                         while (pos < len && text.at(pos) != space
       
   117                                && text.at(pos) != endTag
       
   118                                && text.at(pos) != tab
       
   119                                && text.mid(pos, 2) != endElement)
       
   120                             ++pos;
       
   121                         if (text.mid(pos, 2) == endElement)
       
   122                             ++pos;
       
   123                         setFormat(start, pos - start,
       
   124                                   m_formats[Tag]);
       
   125                         break;
       
   126                     }
       
   127                     break;
       
   128                 } else if (ch == amp) {
       
   129                     start = pos;
       
   130                     while (pos < len && text.at(pos++) != semicolon)
       
   131                         ;
       
   132                     setFormat(start, pos - start,
       
   133                               m_formats[Entity]);
       
   134                 } else {
       
   135                     // No tag, comment or entity started, continue...
       
   136                     ++pos;
       
   137                 }
       
   138             }
       
   139             break;
       
   140         case InComment:
       
   141             start = pos;
       
   142             while (pos < len) {
       
   143                 if (text.mid(pos, 3) == endComment) {
       
   144                     pos += 3;
       
   145                     state = NormalState;
       
   146                     break;
       
   147                 } else {
       
   148                     ++pos;
       
   149                 }
       
   150             }
       
   151             setFormat(start, pos - start, m_formats[Comment]);
       
   152             break;
       
   153         case InTag:
       
   154             QChar quote = QChar::Null;
       
   155             while (pos < len) {
       
   156                 QChar ch = text.at(pos);
       
   157                 if (quote.isNull()) {
       
   158                     start = pos;
       
   159                     if (ch == apos || ch == quot) {
       
   160                         quote = ch;
       
   161                     } else if (ch == endTag) {
       
   162                         ++pos;
       
   163                         setFormat(start, pos - start, m_formats[Tag]);
       
   164                         state = NormalState;
       
   165                         break;
       
   166                     } else if (text.mid(pos, 2) == endElement) {
       
   167                         pos += 2;
       
   168                         setFormat(start, pos - start, m_formats[Tag]);
       
   169                         state = NormalState;
       
   170                         break;
       
   171                     } else if (ch != space && text.at(pos) != tab) {
       
   172                         // Tag not ending, not a quote and no whitespace, so
       
   173                         // we must be dealing with an attribute.
       
   174                         ++pos;
       
   175                         while (pos < len && text.at(pos) != space
       
   176                                && text.at(pos) != tab
       
   177                                && text.at(pos) != equals)
       
   178                             ++pos;
       
   179                         setFormat(start, pos - start, m_formats[Attribute]);
       
   180                         start = pos;
       
   181                     }
       
   182                 } else if (ch == quote) {
       
   183                     quote = QChar::Null;
       
   184 
       
   185                     // Anything quoted is a value
       
   186                     setFormat(start, pos - start, m_formats[Value]);
       
   187                 }
       
   188                 ++pos;
       
   189             }
       
   190             break;
       
   191         }
       
   192     }
       
   193     setCurrentBlockState(state);
       
   194 }
       
   195 
       
   196 } // namespace qdesigner_internal
       
   197 
       
   198 QT_END_NAMESPACE