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     Sets parser-tracked source offsets for an attribute.
032     */
033    public static void attributeRange(
034        Attributes attributes,
035        String key,
036        LineMap lineMap,
037        int nameStart,
038        int nameEnd,
039        int valueStart,
040        int valueEnd
041    ) {
042        Validate.notNull(attributes);
043        Validate.notNull(key);
044        int index = attributes.visibleIndexOfKey(key);
045        if (index != Attributes.NotFound)
046            attributes.ensureSpans().attributeRange(index, lineMap, nameStart, nameEnd, valueStart, valueEnd);
047    }
048
049    /**
050     Copies an existing tracked attribute range onto another attribute set.
051     */
052    public static void attributeRange(Attributes attributes, String key, Range.AttributeRange range) {
053        Validate.notNull(attributes);
054        Validate.notNull(key);
055        Validate.notNull(range);
056        int index = attributes.visibleIndexOfKey(key);
057        if (index != Attributes.NotFound && range.isTracked())
058            attributes.ensureSpans().attributeRange(index, range);
059    }
060}