Search in sources :

Example 1 with Html4Document

use of nokogiri.Html4Document in project nokogiri by sparklemotion.

the class NokogiriHelpers method convertEncodingByNKFIfNecessary.

public static CharSequence convertEncodingByNKFIfNecessary(ThreadContext context, XmlDocument doc, CharSequence str) {
    if (!(doc instanceof Html4Document)) {
        return str;
    }
    String parsed_encoding = ((Html4Document) doc).getPraedEncoding();
    if (parsed_encoding == null) {
        return str;
    }
    String ruby_encoding = rubyStringToString(doc.getEncoding());
    if (ruby_encoding == null) {
        return str;
    }
    Charset encoding = Charset.forName(ruby_encoding);
    if (Charset.forName(parsed_encoding).compareTo(encoding) == 0) {
        return str;
    }
    // no need to convert
    if (str.length() == 0) {
        return str;
    }
    return NokogiriHelpers.nkf(context, encoding, str);
}
Also used : Html4Document(nokogiri.Html4Document) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString)

Example 2 with Html4Document

use of nokogiri.Html4Document in project nokogiri by sparklemotion.

the class HtmlDomParserContext method wrapDocument.

@Override
protected XmlDocument wrapDocument(ThreadContext context, RubyClass klass, Document document) {
    Html4Document htmlDocument = new Html4Document(context.runtime, klass, document);
    htmlDocument.setDocumentNode(context.runtime, document);
    Helpers.invoke(context, htmlDocument, "initialize");
    if (ruby_encoding.isNil()) {
        // ruby_encoding might have detected by Html4Document::EncodingReader
        if (detected_encoding != null && !detected_encoding.isNil()) {
            ruby_encoding = detected_encoding;
        } else {
            // no encoding given & no encoding detected, then try to get it
            String charset = tryGetCharsetFromHtml5MetaTag(document);
            ruby_encoding = stringOrNil(context.runtime, charset);
        }
    }
    htmlDocument.setEncoding(ruby_encoding);
    htmlDocument.setParsedEncoding(java_encoding);
    return htmlDocument;
}
Also used : Html4Document(nokogiri.Html4Document)

Example 3 with Html4Document

use of nokogiri.Html4Document in project nokogiri by sparklemotion.

the class NokogiriHelpers method getCachedNodeOrCreate.

/**
 * Get the XmlNode associated with the underlying
 * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
 * or XmlNamespace wrapping <code>node</code> if there is no cached
 * value.
 */
public static IRubyObject getCachedNodeOrCreate(Ruby runtime, Node node) {
    if (node == null) {
        return runtime.getNil();
    }
    if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
        XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
        if (!(xmlDocument instanceof Html4Document)) {
            String prefix = getLocalNameForNamespace(((Attr) node).getName(), null);
            String href = ((Attr) node).getValue();
            XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
            if (xmlNamespace != null) {
                return xmlNamespace;
            }
            return XmlNamespace.createFromAttr(runtime, (Attr) node);
        }
    }
    XmlNode xmlNode = getCachedNode(node);
    if (xmlNode == null) {
        xmlNode = (XmlNode) constructNode(runtime, node);
        node.setUserData(CACHED_NODE, xmlNode, null);
    }
    return xmlNode;
}
Also used : XmlNode(nokogiri.XmlNode) Html4Document(nokogiri.Html4Document) XmlNamespace(nokogiri.XmlNamespace) XmlDocument(nokogiri.XmlDocument) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Aggregations

Html4Document (nokogiri.Html4Document)3 RubyString (org.jruby.RubyString)2 Charset (java.nio.charset.Charset)1 XmlAttr (nokogiri.XmlAttr)1 XmlDocument (nokogiri.XmlDocument)1 XmlNamespace (nokogiri.XmlNamespace)1 XmlNode (nokogiri.XmlNode)1 Attr (org.w3c.dom.Attr)1