예제 #1
0
StyleRule HtmlFormatter::ComputeStyleRule(HtmlToken* t) {
    StyleRule rule;
    // get style rules ordered by specificity
    StyleRule* prevRule = FindStyleRule(Tag_Body, nullptr, 0);
    if (prevRule)
        rule.Merge(*prevRule);
    prevRule = FindStyleRule(Tag_Any, nullptr, 0);
    if (prevRule)
        rule.Merge(*prevRule);
    prevRule = FindStyleRule(t->tag, nullptr, 0);
    if (prevRule)
        rule.Merge(*prevRule);
    // TODO: support multiple class names
    AttrInfo* attr = t->GetAttrByName("class");
    if (attr) {
        prevRule = FindStyleRule(Tag_Any, attr->val, attr->valLen);
        if (prevRule)
            rule.Merge(*prevRule);
        prevRule = FindStyleRule(t->tag, attr->val, attr->valLen);
        if (prevRule)
            rule.Merge(*prevRule);
    }
    attr = t->GetAttrByName("style");
    if (attr) {
        StyleRule newRule = StyleRule::Parse(attr->val, attr->valLen);
        rule.Merge(newRule);
    }
    return rule;
}
예제 #2
0
void HtmlFormatter::ParseStyleSheet(const char* data, size_t len) {
    CssPullParser parser(data, len);
    while (parser.NextRule()) {
        StyleRule rule = StyleRule::Parse(&parser);
        const CssSelector* sel;
        while ((sel = parser.NextSelector()) != nullptr) {
            if (Tag_NotFound == sel->tag)
                continue;
            StyleRule* prevRule = FindStyleRule(sel->tag, sel->clazz, sel->clazzLen);
            if (prevRule) {
                prevRule->Merge(rule);
            } else {
                rule.tag = sel->tag;
                rule.classHash = sel->clazz ? MurmurHash2(sel->clazz, sel->clazzLen) : 0;
                styleRules.Append(rule);
            }
        }
    }
}