1515import org .jsoup .nodes .Node ;
1616import org .jsoup .nodes .TextNode ;
1717import org .jsoup .nodes .XmlDeclaration ;
18+ import org .jsoup .select .Elements ;
1819import org .jspecify .annotations .Nullable ;
1920
2021import java .io .Reader ;
@@ -60,11 +61,23 @@ protected void initialiseParse(Reader input, String baseUri, Parser parser) {
6061 @ Override
6162 void initialiseParseFragment (@ Nullable Element context ) {
6263 super .initialiseParseFragment (context );
63- if (context != null ) {
64- TokeniserState textState = context .tag ().textState ();
65- if (textState != null ) tokeniser .transition (textState );
64+ if (context == null ) return ;
65+
66+ // transition to the tag's text state if available
67+ TokeniserState textState = context .tag ().textState ();
68+ if (textState != null ) tokeniser .transition (textState );
69+
70+ // reconstitute the namespace stack by traversing the element and its parents (top down)
71+ Elements chain = context .parents ();
72+ chain .add (0 , context );
73+ for (int i = chain .size () - 1 ; i >= 0 ; i --) {
74+ Element el = chain .get (i );
75+ HashMap <String , String > namespaces = new HashMap <>(namespacesStack .peek ());
76+ namespacesStack .push (namespaces );
77+ if (el .attributesSize () > 0 ) {
78+ processNamespaces (el .attributes (), namespaces );
79+ }
6680 }
67-
6881 }
6982
7083 Document parse (Reader input , String baseUri ) {
@@ -130,14 +143,16 @@ void insertElementFor(Token.StartTag startTag) {
130143 HashMap <String , String > namespaces = new HashMap <>(namespacesStack .peek ());
131144 namespacesStack .push (namespaces );
132145
133- if (startTag .attributes != null ) {
134- startTag .attributes .deduplicate (settings );
135- processNamespaces (startTag .attributes , namespaces );
146+ Attributes attributes = startTag .attributes ;
147+ if (attributes != null ) {
148+ attributes .deduplicate (settings );
149+ processNamespaces (attributes , namespaces );
150+ applyNamespacesToAttributes (attributes , namespaces );
136151 }
137152
138153 String ns = resolveNamespace (startTag .tagName , namespaces );
139154 Tag tag = tagFor (startTag .tagName , startTag .normalName , ns , settings );
140- Element el = new Element (tag , null , settings .normalizeAttributes (startTag . attributes ));
155+ Element el = new Element (tag , null , settings .normalizeAttributes (attributes ));
141156 currentElement ().appendChild (el );
142157 push (el );
143158
@@ -162,6 +177,9 @@ private static void processNamespaces(Attributes attributes, HashMap<String, Str
162177 namespaces .put (nsPrefix , value );
163178 }
164179 }
180+ }
181+
182+ private static void applyNamespacesToAttributes (Attributes attributes , HashMap <String , String > namespaces ) {
165183 // second pass, apply namespace to attributes. Collects them first then adds (as userData is an attribute)
166184 Map <String , String > attrPrefix = new HashMap <>();
167185 for (Attribute attr : attributes ) {
0 commit comments