コード例 #1
0
ファイル: HtmlFilter.cpp プロジェクト: Artanomell/schat
/*!
 * Оптимизация и дополнительная фильтрация токенов.
 */
void HtmlFilter::optimize(QList<HtmlToken> &tokens) const
{
  int index = -1;
  for (int i = 0; i < tokens.size(); ++i) {
    if (!tokens.at(i).simple) {
      index = i;
      break;
    }
  }

  if (index == -1)
    return;

  HtmlToken token = tokens.at(index);

  if (token.tag == LS("a") || token.tag == LS("font")) {
    tokens[index].simple = true;
    int gt = endTag(token.tag, tokens, index);

    // Если закрывающий тег не найден, удаляем тег.
    if (gt == -1) {
      tokens.removeAt(index);
      return optimize(tokens);
    }

    // Если закрывающий следует сразу после открывающего, удаляем тег и закрывающий тег.
    if (gt - index == 1) {
      tokens.removeAt(index);
      if (index < tokens.size())
        tokens.removeAt(index);

      return optimize(tokens);
    }

    // Удаляем все не текстовые токены внутри тега.
    for (int i = gt - 1; i > index; --i) {
      if (tokens.at(i).type != HtmlToken::Text) {
        tokens.removeAt(i);
        gt--;
      }
      else {
        tokens[i].parent = token.tag;
      }
    }

    AbstractTag *tag = 0;
    if (token.tag == LS("a"))
      tag = new HtmlATag(token);
    else if (token.tag == LS("font"))
      tag = new HtmlFontTag(token);

    if (!tag->valid || gt - index == 1) {
      tokens.removeAt(gt);
      tokens.removeAt(index);
    }
    else
      tokens[index].text = tag->toText();

    delete tag;
    return optimize(tokens);
  }
  /// Тег span в зависимости от css стилей преобразуется в теги b, i, u, s и font
  /// и полностью удаляется из текста. Тег font используется для установки цвета элемента.
  else if (token.tag == LS("span")) {
    QList<HtmlToken> tags;
    const Range range = attrBody(LS("style"), token.text);

    if (range.first != -1) {
      QString value;

      if (cssValue(LS("font-weight"), token.text, range, value) && (value == LS("bold") || value == LS("bolder") || value == LS("600") || value == LS("700") || value == LS("800") || value == LS("900")))
        tags.append(HtmlToken(HtmlToken::Tag, LS("<b>")));

      if (cssValue(LS("font-style"), token.text, range, value) && value == LS("italic"))
        tags.append(HtmlToken(HtmlToken::Tag, LS("<i>")));

      if (cssValue(LS("text-decoration"), token.text, range, value)) {
        if (value == LS("underline"))
          tags.append(HtmlToken(HtmlToken::Tag, LS("<u>")));
        else if (value == LS("line-through"))
          tags.append(HtmlToken(HtmlToken::Tag, LS("<s>")));
      }

      if (cssValue(LS("color"), token.text, range, value) && colorValue(value)) {
        HtmlToken token(HtmlToken::Tag, LS("<font color=\"") + value + LS("\">"));
        token.simple = true;
        tags.append(token);
      }
    }

    tokens.removeAt(index);
    int gt = endTag(token.tag, tokens, index);

    if (gt == -1)
      return optimize(tokens);

    tokens.removeAt(gt);
    if (index == gt)
      return optimize(tokens);

    gt++;

    foreach (HtmlToken tag, tags) {
      tokens.insert(index, tag);
      tokens.insert(gt, tag.toEndTag());
      index++;
      gt++;
    }
コード例 #2
0
ファイル: StyleFetchedImage.cpp プロジェクト: mirror/chromium
CSSValue* StyleFetchedImage::computedCSSValue() const {
  return cssValue();
}