コード例 #1
0
void WebFrameSerializerImpl::openTagToString(
    Element* element,
    SerializeDomParam* param)
{
    bool needSkip;
    StringBuilder result;
    // Do pre action for open tag.
    result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip));
    if (needSkip)
        return;
    // Add open tag
    result.append('<');
    result.append(element->nodeName().lower());

    // Find out if this element owns a frame.
    WebFrame* frame = nullptr;
    if (element->isFrameOwnerElement()) {
        frame = WebFrame::fromFrame(
            toHTMLFrameOwnerElement(element)->contentFrame());
    }

    // Go through all attributes and serialize them.
    for (const auto& it : element->attributes()) {
        const QualifiedName& attrName = it.name();
        String attrValue = it.value();

        // Rewrite the attribute value if requested.
        if (element->hasLegalLinkAttribute(attrName)) {
            // For links start with "javascript:", we do not change it.
            if (!attrValue.startsWith("javascript:", TextCaseInsensitive)) {
                // Get the absolute link.
                KURL completeURL = param->document->completeURL(attrValue);

                // Check whether we have a local file to link to.
                WebString rewrittenLink;
                if (frame && m_delegate->rewriteFrameSource(frame, &rewrittenLink)) {
                    attrValue = rewrittenLink;
                } else if (m_delegate->rewriteLink(completeURL, &rewrittenLink)) {
                    attrValue = rewrittenLink;
                } else {
                    attrValue = completeURL;
                }
            }
        }

        appendAttribute(result, param->isHTMLDocument, attrName.toString(), attrValue);
    }

    // Do post action for open tag.
    String addedContents = postActionAfterSerializeOpenTag(element, param);
    // Complete the open tag for element when it has child/children.
    if (element->hasChildren() || param->haveAddedContentsBeforeEnd)
        result.append('>');
    // Append the added contents generate in  post action of open tag.
    result.append(addedContents);
    // Save the result to data buffer.
    saveHTMLContentToBuffer(result.toString(), param);
}
コード例 #2
0
void WebPageSerializerImpl::openTagToString(Element* element,
                                            SerializeDomParam* param)
{
    bool needSkip;
    StringBuilder result;
    // Do pre action for open tag.
    result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip));
    if (needSkip)
        return;
    // Add open tag
    result.append('<');
    result.append(element->nodeName().lower());
    // Go through all attributes and serialize them.
    if (element->hasAttributes()) {
        unsigned numAttrs = element->attributeCount();
        for (unsigned i = 0; i < numAttrs; i++) {
            result.append(' ');
            // Add attribute pair
            const Attribute& attribute = element->attributeItem(i);
            result.append(attribute.name().toString());
            result.appendLiteral("=\"");
            if (!attribute.value().isEmpty()) {
                const String& attrValue = attribute.value();

                // Check whether we need to replace some resource links
                // with local resource paths.
                const QualifiedName& attrName = attribute.name();
                if (element->hasLegalLinkAttribute(attrName)) {
                    // For links start with "javascript:", we do not change it.
                    if (attrValue.startsWith("javascript:", false))
                        result.append(attrValue);
                    else {
                        // Get the absolute link
                        WebLocalFrameImpl* subFrame = WebLocalFrameImpl::fromFrameOwnerElement(element);
                        String completeURL = subFrame ? subFrame->frame()->document()->url() :
                                                        param->document->completeURL(attrValue);
                        // Check whether we have local files for those link.
                        if (m_localLinks.contains(completeURL)) {
                            if (!param->directoryName.isEmpty()) {
                                result.appendLiteral("./");
                                result.append(param->directoryName);
                                result.append('/');
                            }
                            result.append(m_localLinks.get(completeURL));
                        } else
                            result.append(completeURL);
                    }
                } else {
                    if (param->isHTMLDocument)
                        result.append(m_htmlEntities.convertEntitiesInString(attrValue));
                    else
                        result.append(m_xmlEntities.convertEntitiesInString(attrValue));
                }
            }
            result.append('\"');
        }
    }

    // Do post action for open tag.
    String addedContents = postActionAfterSerializeOpenTag(element, param);
    // Complete the open tag for element when it has child/children.
    if (element->hasChildren() || param->haveAddedContentsBeforeEnd)
        result.append('>');
    // Append the added contents generate in  post action of open tag.
    result.append(addedContents);
    // Save the result to data buffer.
    saveHTMLContentToBuffer(result.toString(), param);
}
コード例 #3
0
void WebPageSerializerImpl::openTagToString(const Element* element,
                                            SerializeDomParam* param)
{
    // FIXME: use StringBuilder instead of String.
    bool needSkip;
    // Do pre action for open tag.
    String result = preActionBeforeSerializeOpenTag(element, param, &needSkip);
    if (needSkip)
        return;
    // Add open tag
    result += "<" + element->nodeName().lower();
    // Go through all attributes and serialize them.
    const NamedNodeMap *attrMap = element->attributes(true);
    if (attrMap) {
        unsigned numAttrs = attrMap->length();
        for (unsigned i = 0; i < numAttrs; i++) {
            result += " ";
            // Add attribute pair
            const Attribute *attribute = attrMap->attributeItem(i);
            result += attribute->name().toString();
            result += "=\"";
            if (!attribute->value().isEmpty()) {
                const String& attrValue = attribute->value();

                // Check whether we need to replace some resource links
                // with local resource paths.
                const QualifiedName& attrName = attribute->name();
                if (elementHasLegalLinkAttribute(element, attrName)) {
                    // For links start with "javascript:", we do not change it.
                    if (attrValue.startsWith("javascript:", false))
                        result += attrValue;
                    else {
                        // Get the absolute link
                        String completeURL = param->document->completeURL(attrValue);
                        // Check whether we have local files for those link.
                        if (m_localLinks.contains(completeURL)) {
                            if (!m_localDirectoryName.isEmpty())
                                result += "./" + m_localDirectoryName + "/";
                            result += m_localLinks.get(completeURL);
                        } else
                            result += completeURL;
                    }
                } else {
                    if (param->isHTMLDocument)
                        result += m_htmlEntities.convertEntitiesInString(attrValue);
                    else
                        result += m_xmlEntities.convertEntitiesInString(attrValue);
                }
            }
            result += "\"";
        }
    }

    // Do post action for open tag.
    String addedContents = postActionAfterSerializeOpenTag(element, param);
    // Complete the open tag for element when it has child/children.
    if (element->hasChildNodes() || param->haveAddedContentsBeforeEnd)
        result += ">";
    // Append the added contents generate in  post action of open tag.
    result += addedContents;
    // Save the result to data buffer.
    saveHTMLContentToBuffer(result, param);
}
コード例 #4
0
void WebFrameSerializerImpl::openTagToString(
    Element* element,
    SerializeDomParam* param)
{
    bool needSkip;
    StringBuilder result;
    // Do pre action for open tag.
    result.append(preActionBeforeSerializeOpenTag(element, param, &needSkip));
    if (needSkip)
        return;
    // Add open tag
    result.append('<');
    result.append(element->nodeName().lower());

    // Find out if we need to do frame-specific link rewriting.
    WebFrame* frame = nullptr;
    if (element->isFrameOwnerElement()) {
        frame = WebFrame::fromFrame(
            toHTMLFrameOwnerElement(element)->contentFrame());
    }
    WebString rewrittenFrameLink;
    bool shouldRewriteFrameSrc =
        frame && m_delegate->rewriteFrameSource(frame, &rewrittenFrameLink);
    bool didRewriteFrameSrc = false;

    // Go through all attributes and serialize them.
    for (const auto& it : element->attributes()) {
        const QualifiedName& attrName = it.name();
        String attrValue = it.value();

        // Skip srcdoc attribute if we will emit src attribute (for frames).
        if (shouldRewriteFrameSrc && attrName == HTMLNames::srcdocAttr)
            continue;

        // Rewrite the attribute value if requested.
        if (element->hasLegalLinkAttribute(attrName)) {
            // For links start with "javascript:", we do not change it.
            if (!attrValue.startsWith("javascript:", TextCaseInsensitive)) {
                // Get the absolute link.
                KURL completeURL = param->document->completeURL(attrValue);

                // Check whether we have a local file to link to.
                WebString rewrittenURL;
                if (shouldRewriteFrameSrc) {
                    attrValue = rewrittenFrameLink;
                    didRewriteFrameSrc = true;
                } else if (m_delegate->rewriteLink(completeURL, &rewrittenURL)) {
                    attrValue = rewrittenURL;
                } else {
                    attrValue = completeURL;
                }
            }
        }

        appendAttribute(result, param->isHTMLDocument, attrName.toString(), attrValue);
    }

    // For frames where link rewriting was requested, ensure that src attribute
    // is written even if the original document didn't have that attribute
    // (mainly needed for iframes with srcdoc, but with no src attribute).
    if (shouldRewriteFrameSrc && !didRewriteFrameSrc && isHTMLIFrameElement(element)) {
        appendAttribute(
            result, param->isHTMLDocument, HTMLNames::srcAttr.toString(), rewrittenFrameLink);
    }

    // Do post action for open tag.
    String addedContents = postActionAfterSerializeOpenTag(element, param);
    // Complete the open tag for element when it has child/children.
    if (element->hasChildren() || param->haveAddedContentsBeforeEnd)
        result.append('>');
    // Append the added contents generate in  post action of open tag.
    result.append(addedContents);
    // Save the result to data buffer.
    saveHTMLContentToBuffer(result.toString(), param);
}