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    /** Records where this processing instruction's data starts in the original input. */
041    public static void sourceDataStart(ProcessingInstruction pi, int pos) {
042        Validate.notNull(pi);
043        pi.sourceDataStart(pos);
044    }
045
046    /**
047     Sets parser-tracked source offsets for an attribute.
048     */
049    public static void attributeRange(
050        Attributes attributes,
051        String key,
052        LineMap lineMap,
053        int nameStart,
054        int nameEnd,
055        int valueStart,
056        int valueEnd
057    ) {
058        Validate.notNull(attributes);
059        Validate.notNull(key);
060        int index = attributes.visibleIndexOfKey(key);
061        if (index != Attributes.NotFound)
062            attributes.ensureSpans().attributeRange(index, lineMap, nameStart, nameEnd, valueStart, valueEnd);
063    }
064
065    /**
066     Copies an existing tracked attribute range onto another attribute set.
067     */
068    public static void attributeRange(Attributes attributes, String key, Range.AttributeRange range) {
069        Validate.notNull(attributes);
070        Validate.notNull(key);
071        Validate.notNull(range);
072        int index = attributes.visibleIndexOfKey(key);
073        if (index != Attributes.NotFound && range.isTracked())
074            attributes.ensureSpans().attributeRange(index, range);
075    }
076}