001package org.jsoup.nodes;
002
003import org.jsoup.helper.Validate;
004import org.jsoup.internal.LineMap;
005
006/**
007 Internal hooks used by the parser and cleaner to attach source ranges to nodes and attributes.
008 <p>This class is public only because jsoup's internal packages need to cross package boundaries; it is not a supported
009 user API.</p>
010 */
011public final class NodeInternals {
012    private NodeInternals() {}
013
014    /**
015     Sets the source range for a node's start.
016     */
017    public static void sourceRange(Node node, LineMap lineMap, int startPos, int endPos) {
018        Validate.notNull(node);
019        node.ensureSpans().sourceRange(lineMap, startPos, endPos);
020    }
021
022    /**
023     Sets the source range for an element's end tag.
024     */
025    public static void endSourceRange(Element element, LineMap lineMap, int startPos, int endPos) {
026        Validate.notNull(element);
027        element.ensureSpans().endSourceRange(lineMap, startPos, endPos);
028    }
029
030    /**
031     Clears a copied end-tag range so a parser-recreated element can track its own close.
032     */
033    public static void clearEndSourceRange(Element element) {
034        Validate.notNull(element);
035        Range.Spans spans = element.spans();
036        if (spans != null)
037            spans.clearEndSourceRange();
038    }
039
040    /**
041     Sets parser-tracked source offsets for an attribute.
042     */
043    public static void attributeRange(
044        Attributes attributes,
045        String key,
046        LineMap lineMap,
047        int nameStart,
048        int nameEnd,
049        int valueStart,
050        int valueEnd
051    ) {
052        Validate.notNull(attributes);
053        Validate.notNull(key);
054        int index = attributes.visibleIndexOfKey(key);
055        if (index != Attributes.NotFound)
056            attributes.ensureSpans().attributeRange(index, lineMap, nameStart, nameEnd, valueStart, valueEnd);
057    }
058
059    /**
060     Copies an existing tracked attribute range onto another attribute set.
061     */
062    public static void attributeRange(Attributes attributes, String key, Range.AttributeRange range) {
063        Validate.notNull(attributes);
064        Validate.notNull(key);
065        Validate.notNull(range);
066        int index = attributes.visibleIndexOfKey(key);
067        if (index != Attributes.NotFound && range.isTracked())
068            attributes.ensureSpans().attributeRange(index, range);
069    }
070}