⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtexthtmlparser.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtexthtmlparser_p.h"#include <qbytearray.h>#include <qtextcodec.h>#include <qapplication.h>#include <qstack.h>#include <qdebug.h>#include "qtextdocument.h"#include "qtextformat_p.h"#include "qtextdocument_p.h"#include "qtextcursor.h"#define MAX_ENTITY 259static const struct QTextHtmlEntity { const char *name; quint16 code; } entities[MAX_ENTITY]= {    { "AElig", 0x00c6 },    { "Aacute", 0x00c1 },    { "Acirc", 0x00c2 },    { "Agrave", 0x00c0 },    { "Alpha", 0x0391 },    { "AMP", 38 },    { "Aring", 0x00c5 },    { "Atilde", 0x00c3 },    { "Auml", 0x00c4 },    { "Beta", 0x0392 },    { "Ccedil", 0x00c7 },    { "Chi", 0x03a7 },    { "Dagger", 0x2021 },    { "Delta", 0x0394 },    { "ETH", 0x00d0 },    { "Eacute", 0x00c9 },    { "Ecirc", 0x00ca },    { "Egrave", 0x00c8 },    { "Epsilon", 0x0395 },    { "Eta", 0x0397 },    { "Euml", 0x00cb },    { "Gamma", 0x0393 },    { "GT", 62 },    { "Iacute", 0x00cd },    { "Icirc", 0x00ce },    { "Igrave", 0x00cc },    { "Iota", 0x0399 },    { "Iuml", 0x00cf },    { "Kappa", 0x039a },    { "Lambda", 0x039b },    { "LT", 60 },    { "Mu", 0x039c },    { "Ntilde", 0x00d1 },    { "Nu", 0x039d },    { "OElig", 0x0152 },    { "Oacute", 0x00d3 },    { "Ocirc", 0x00d4 },    { "Ograve", 0x00d2 },    { "Omega", 0x03a9 },    { "Omicron", 0x039f },    { "Oslash", 0x00d8 },    { "Otilde", 0x00d5 },    { "Ouml", 0x00d6 },    { "Phi", 0x03a6 },    { "Pi", 0x03a0 },    { "Prime", 0x2033 },    { "Psi", 0x03a8 },    { "QUOT", 34 },    { "Rho", 0x03a1 },    { "Scaron", 0x0160 },    { "Sigma", 0x03a3 },    { "THORN", 0x00de },    { "Tau", 0x03a4 },    { "Theta", 0x0398 },    { "Uacute", 0x00da },    { "Ucirc", 0x00db },    { "Ugrave", 0x00d9 },    { "Upsilon", 0x03a5 },    { "Uuml", 0x00dc },    { "Xi", 0x039e },    { "Yacute", 0x00dd },    { "Yuml", 0x0178 },    { "Zeta", 0x0396 },    { "aacute", 0x00e1 },    { "acirc", 0x00e2 },    { "acute", 0x00b4 },    { "aelig", 0x00e6 },    { "agrave", 0x00e0 },    { "alefsym", 0x2135 },    { "alpha", 0x03b1 },    { "amp", 38 },    { "and", 0x22a5 },    { "ang", 0x2220 },    { "apos", 0x0027 },    { "aring", 0x00e5 },    { "asymp", 0x2248 },    { "atilde", 0x00e3 },    { "auml", 0x00e4 },    { "bdquo", 0x201e },    { "beta", 0x03b2 },    { "brvbar", 0x00a6 },    { "bull", 0x2022 },    { "cap", 0x2229 },    { "ccedil", 0x00e7 },    { "cedil", 0x00b8 },    { "cent", 0x00a2 },    { "chi", 0x03c7 },    { "circ", 0x02c6 },    { "clubs", 0x2663 },    { "cong", 0x2245 },    { "copy", 0x00a9 },    { "crarr", 0x21b5 },    { "cup", 0x222a },    { "curren", 0x00a4 },    { "dArr", 0x21d3 },    { "dagger", 0x2020 },    { "darr", 0x2193 },    { "deg", 0x00b0 },    { "delta", 0x03b4 },    { "diams", 0x2666 },    { "divide", 0x00f7 },    { "eacute", 0x00e9 },    { "ecirc", 0x00ea },    { "egrave", 0x00e8 },    { "empty", 0x2205 },    { "emsp", 0x2003 },    { "ensp", 0x2002 },    { "epsilon", 0x03b5 },    { "equiv", 0x2261 },    { "eta", 0x03b7 },    { "eth", 0x00f0 },    { "euml", 0x00eb },    { "euro", 0x20ac },    { "exist", 0x2203 },    { "fnof", 0x0192 },    { "forall", 0x2200 },    { "frac12", 0x00bd },    { "frac14", 0x00bc },    { "frac34", 0x00be },    { "frasl", 0x2044 },    { "gamma", 0x03b3 },    { "ge", 0x2265 },    { "gt", 62 },    { "hArr", 0x21d4 },    { "harr", 0x2194 },    { "hearts", 0x2665 },    { "hellip", 0x2026 },    { "iacute", 0x00ed },    { "icirc", 0x00ee },    { "iexcl", 0x00a1 },    { "igrave", 0x00ec },    { "image", 0x2111 },    { "infin", 0x221e },    { "int", 0x222b },    { "iota", 0x03b9 },    { "iquest", 0x00bf },    { "isin", 0x2208 },    { "iuml", 0x00ef },    { "kappa", 0x03ba },    { "lArr", 0x21d0 },    { "lambda", 0x03bb },    { "lang", 0x2329 },    { "laquo", 0x00ab },    { "larr", 0x2190 },    { "lceil", 0x2308 },    { "ldquo", 0x201c },    { "le", 0x2264 },    { "lfloor", 0x230a },    { "lowast", 0x2217 },    { "loz", 0x25ca },    { "lrm", 0x200e },    { "lsaquo", 0x2039 },    { "lsquo", 0x2018 },    { "lt", 60 },    { "macr", 0x00af },    { "mdash", 0x2014 },    { "micro", 0x00b5 },    { "middot", 0x00b7 },    { "minus", 0x2212 },    { "mu", 0x03bc },    { "nabla", 0x2207 },    { "nbsp", 0x00a0 },    { "ndash", 0x2013 },    { "ne", 0x2260 },    { "ni", 0x220b },    { "not", 0x00ac },    { "notin", 0x2209 },    { "nsub", 0x2284 },    { "ntilde", 0x00f1 },    { "nu", 0x03bd },    { "oacute", 0x00f3 },    { "ocirc", 0x00f4 },    { "oelig", 0x0153 },    { "ograve", 0x00f2 },    { "oline", 0x203e },    { "omega", 0x03c9 },    { "omicron", 0x03bf },    { "oplus", 0x2295 },    { "or", 0x22a6 },    { "ordf", 0x00aa },    { "ordm", 0x00ba },    { "oslash", 0x00f8 },    { "otilde", 0x00f5 },    { "otimes", 0x2297 },    { "ouml", 0x00f6 },    { "para", 0x00b6 },    { "part", 0x2202 },    { "percnt", 0x0025 },    { "permil", 0x2030 },    { "perp", 0x22a5 },    { "phi", 0x03c6 },    { "pi", 0x03c0 },    { "piv", 0x03d6 },    { "plusmn", 0x00b1 },    { "pound", 0x00a3 },    { "prime", 0x2032 },    { "prod", 0x220f },    { "prop", 0x221d },    { "psi", 0x03c8 },    { "quot", 34 },    { "rArr", 0x21d2 },    { "radic", 0x221a },    { "rang", 0x232a },    { "raquo", 0x00bb },    { "rarr", 0x2192 },    { "rceil", 0x2309 },    { "rdquo", 0x201d },    { "real", 0x211c },    { "reg", 0x00ae },    { "rfloor", 0x230b },    { "rho", 0x03c1 },    { "rlm", 0x200f },    { "rsaquo", 0x203a },    { "rsquo", 0x2019 },    { "sbquo", 0x201a },    { "scaron", 0x0161 },    { "sdot", 0x22c5 },    { "sect", 0x00a7 },    { "shy", 0x00ad },    { "sigma", 0x03c3 },    { "sigmaf", 0x03c2 },    { "sim", 0x223c },    { "spades", 0x2660 },    { "sub", 0x2282 },    { "sube", 0x2286 },    { "sum", 0x2211 },    { "sup1", 0x00b9 },    { "sup2", 0x00b2 },    { "sup3", 0x00b3 },    { "sup", 0x2283 },    { "supe", 0x2287 },    { "szlig", 0x00df },    { "tau", 0x03c4 },    { "there4", 0x2234 },    { "theta", 0x03b8 },    { "thetasym", 0x03d1 },    { "thinsp", 0x2009 },    { "thorn", 0x00fe },    { "tilde", 0x02dc },    { "times", 0x00d7 },    { "trade", 0x2122 },    { "uArr", 0x21d1 },    { "uacute", 0x00fa },    { "uarr", 0x2191 },    { "ucirc", 0x00fb },    { "ugrave", 0x00f9 },    { "uml", 0x00a8 },    { "upsih", 0x03d2 },    { "upsilon", 0x03c5 },    { "uuml", 0x00fc },    { "weierp", 0x2118 },    { "xi", 0x03be },    { "yacute", 0x00fd },    { "yen", 0x00a5 },    { "yuml", 0x00ff },    { "zeta", 0x03b6 },    { "zwj", 0x200d },    { "zwnj", 0x200c },    { 0, 0 }};static bool operator<(const QString &entityStr, const QTextHtmlEntity &entity){    return entityStr < QLatin1String(entity.name);}static bool operator<(const QTextHtmlEntity &entity, const QString &entityStr){    return QLatin1String(entity.name) < entityStr;}static QChar resolveEntity(const QString &entity){    const QTextHtmlEntity *start = &entities[0];    const QTextHtmlEntity *end = &entities[MAX_ENTITY-1];    const QTextHtmlEntity *e = qBinaryFind(start, end, entity);    if (!e->name)        return QChar();    return e->code;}// the displayMode value is according to the what are blocks in the piecetable, not// what the w3c defines.static const QTextHtmlElement elements[Html_NumElements+1]= {    { "a",          Html_a,          QTextHtmlElement::DisplayInline },    { "address",    Html_address,    QTextHtmlElement::DisplayInline },    { "b",          Html_b,          QTextHtmlElement::DisplayInline },    { "big",        Html_big,        QTextHtmlElement::DisplayInline },    { "blockquote", Html_blockquote, QTextHtmlElement::DisplayBlock },    { "body",       Html_body,       QTextHtmlElement::DisplayBlock },    { "br",         Html_br,         QTextHtmlElement::DisplayInline },    { "center",     Html_center,     QTextHtmlElement::DisplayBlock },    { "cite",       Html_cite,       QTextHtmlElement::DisplayInline },    { "code",       Html_code,       QTextHtmlElement::DisplayInline },    { "dd",         Html_dd,         QTextHtmlElement::DisplayBlock },    { "dfn",        Html_dfn,        QTextHtmlElement::DisplayInline },    { "div",        Html_div,        QTextHtmlElement::DisplayBlock },    { "dl",         Html_dl,         QTextHtmlElement::DisplayBlock },    { "dt",         Html_dt,         QTextHtmlElement::DisplayBlock },    { "em",         Html_em,         QTextHtmlElement::DisplayInline },    { "font",       Html_font,       QTextHtmlElement::DisplayInline },    { "h1",         Html_h1,         QTextHtmlElement::DisplayBlock },    { "h2",         Html_h2,         QTextHtmlElement::DisplayBlock },    { "h3",         Html_h3,         QTextHtmlElement::DisplayBlock },    { "h4",         Html_h4,         QTextHtmlElement::DisplayBlock },    { "h5",         Html_h5,         QTextHtmlElement::DisplayBlock },    { "h6",         Html_h6,         QTextHtmlElement::DisplayBlock },    { "head",       Html_head,       QTextHtmlElement::DisplayNone },    { "hr",         Html_hr,         QTextHtmlElement::DisplayBlock },    { "html",       Html_html,       QTextHtmlElement::DisplayInline },    { "i",          Html_i,          QTextHtmlElement::DisplayInline },    { "img",        Html_img,        QTextHtmlElement::DisplayInline },    { "kbd",        Html_kbd,        QTextHtmlElement::DisplayInline },    { "li",         Html_li,         QTextHtmlElement::DisplayBlock },    { "meta",       Html_meta,       QTextHtmlElement::DisplayNone },    { "nobr",       Html_nobr,       QTextHtmlElement::DisplayInline },    { "ol",         Html_ol,         QTextHtmlElement::DisplayBlock },    { "p",          Html_p,          QTextHtmlElement::DisplayBlock },    { "pre",        Html_pre,        QTextHtmlElement::DisplayBlock },    { "qt",         Html_body /*deliberate mapping*/, QTextHtmlElement::DisplayBlock },    { "s",          Html_s,          QTextHtmlElement::DisplayInline },    { "samp",       Html_samp,       QTextHtmlElement::DisplayInline },    { "small",      Html_small,      QTextHtmlElement::DisplayInline },    { "span",       Html_span,       QTextHtmlElement::DisplayInline },    { "strong",     Html_strong,     QTextHtmlElement::DisplayInline },    { "style",      Html_style,      QTextHtmlElement::DisplayNone },    { "sub",        Html_sub,        QTextHtmlElement::DisplayInline },    { "sup",        Html_sup,        QTextHtmlElement::DisplayInline },    { "table",      Html_table,      QTextHtmlElement::DisplayTable },    { "tbody",      Html_tbody,      QTextHtmlElement::DisplayTable },    { "td",         Html_td,         QTextHtmlElement::DisplayBlock },    { "tfoot",      Html_tfoot,      QTextHtmlElement::DisplayTable },    { "th",         Html_th,         QTextHtmlElement::DisplayBlock },    { "thead",      Html_thead,      QTextHtmlElement::DisplayTable },    { "title",      Html_title,      QTextHtmlElement::DisplayNone },    { "tr",         Html_tr,         QTextHtmlElement::DisplayTable },    { "tt",         Html_tt,         QTextHtmlElement::DisplayInline },    { "u",          Html_u,          QTextHtmlElement::DisplayInline },    { "ul",         Html_ul,         QTextHtmlElement::DisplayBlock },    { "var",        Html_var,        QTextHtmlElement::DisplayInline },    { 0, 0,         QTextHtmlElement::DisplayNone }};static bool operator<(const QString &str, const QTextHtmlElement &e){    return str < QLatin1String(e.name);}static bool operator<(const QTextHtmlElement &e, const QString &str){    return QLatin1String(e.name) < str;}static const QTextHtmlElement *lookupElement(const QString &element){    const QTextHtmlElement *start = &elements[0];    const QTextHtmlElement *end = &elements[Html_NumElements];    const QTextHtmlElement *e = qBinaryFind(start, end, element);    Q_ASSERT(!e->name || e->name == element);    return e;}int QTextHtmlParser::lookupElement(const QString &element){    const QTextHtmlElement *e = ::lookupElement(element);    if (!e->name)        return -1;    return e->id;}// quotes newlines as "\\n"static QString quoteNewline(const QString &s){    QString n = s;    n.replace('\n', "\\n");    return n;}QTextHtmlParserNode::QTextHtmlParserNode()    : parent(0), id(-1), isBlock(false), isListItem(false), isListStart(false), isTableCell(false), isAnchor(false),      fontItalic(Unspecified), fontUnderline(Unspecified), fontOverline(Unspecified), fontStrikeOut(Unspecified), fontFixedPitch(Unspecified),      cssFloat(QTextFrameFormat::InFlow), hasOwnListStyle(false), hasFontPointSize(false), hasFontPixelSize(false), hasFontSizeAdjustment(false),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -