@@ -52,7 +52,7 @@ public class Element extends Node implements Iterable<Element> {
5252 private static final List <Element > EmptyChildren = Collections .emptyList ();
5353 private static final Pattern ClassSplit = Pattern .compile ("\\ s+" );
5454 private static final String BaseUriKey = Attributes .internalKey ("baseUri" );
55- private Tag tag ;
55+ Tag tag ;
5656 private @ Nullable WeakReference <List <Element >> shadowChildrenRef ; // points to child elements shadowed from node children
5757 List <Node > childNodes ;
5858 @ Nullable Attributes attributes ; // field is nullable but all methods for attributes are non-null
@@ -72,7 +72,7 @@ public Element(String tag, String namespace) {
7272 * @see #Element(String tag, String namespace)
7373 */
7474 public Element (String tag ) {
75- this (Tag . valueOf ( tag , Parser .NamespaceHtml , ParseSettings . preserveCase ), "" , null );
75+ this (tag , Parser .NamespaceHtml );
7676 }
7777
7878 /**
@@ -216,7 +216,8 @@ public Element tagName(String tagName) {
216216 public Element tagName (String tagName , String namespace ) {
217217 Validate .notEmptyParam (tagName , "tagName" );
218218 Validate .notEmptyParam (namespace , "namespace" );
219- tag = Tag .valueOf (tagName , namespace , NodeUtils .parser (this ).settings ()); // maintains the case option of the original parse
219+ Parser parser = NodeUtils .parser (this );
220+ tag = parser .tagSet ().valueOf (tagName , namespace , parser .settings ()); // maintains the case option of the original parse
220221 return this ;
221222 }
222223
@@ -229,6 +230,18 @@ public Tag tag() {
229230 return tag ;
230231 }
231232
233+ /**
234+ Change the Tag of this element.
235+ @param tag the new tag
236+ @return this element, for chaining
237+ @since 1.20.1
238+ */
239+ public Element tag (Tag tag ) {
240+ Validate .notNull (tag );
241+ this .tag = tag ;
242+ return this ;
243+ }
244+
232245 /**
233246 * Test if this element is a block-level element. (E.g. {@code <div> == true} or an inline element
234247 * {@code <span> == false}).
@@ -784,7 +797,8 @@ public Element appendElement(String tagName) {
784797 * @return the new element, in the specified namespace
785798 */
786799 public Element appendElement (String tagName , String namespace ) {
787- Element child = new Element (Tag .valueOf (tagName , namespace , NodeUtils .parser (this ).settings ()), baseUri ());
800+ Parser parser = NodeUtils .parser (this );
801+ Element child = new Element (parser .tagSet ().valueOf (tagName , namespace , parser .settings ()), baseUri ());
788802 appendChild (child );
789803 return child ;
790804 }
@@ -808,7 +822,8 @@ public Element prependElement(String tagName) {
808822 * @return the new element, in the specified namespace
809823 */
810824 public Element prependElement (String tagName , String namespace ) {
811- Element child = new Element (Tag .valueOf (tagName , namespace , NodeUtils .parser (this ).settings ()), baseUri ());
825+ Parser parser = NodeUtils .parser (this );
826+ Element child = new Element (parser .tagSet ().valueOf (tagName , namespace , parser .settings ()), baseUri ());
812827 prependChild (child );
813828 return child ;
814829 }
@@ -1463,7 +1478,7 @@ public TextAccumulator(StringBuilder accum) {
14631478 if (node instanceof Element ) {
14641479 Element element = (Element ) node ;
14651480 Node next = node .nextSibling ();
1466- if (element .isBlock () && (next instanceof TextNode || next instanceof Element && ! ((Element ) next ).tag .formatAsBlock ()) && !lastCharIsWhitespace (accum ))
1481+ if (! element .tag . isInline () && (next instanceof TextNode || next instanceof Element && ((Element ) next ).tag .isInline ()) && !lastCharIsWhitespace (accum ))
14671482 accum .append (' ' );
14681483 }
14691484
@@ -1564,10 +1579,8 @@ static boolean preserveWhitespace(@Nullable Node node) {
15641579 public Element text (String text ) {
15651580 Validate .notNull (text );
15661581 empty ();
1567- // special case for script/style in HTML: should be data node
1568- Document owner = ownerDocument ();
1569- // an alternate impl would be to run through the parser
1570- if (owner != null && owner .parser ().isContentForTagData (normalName ()))
1582+ // special case for script/style in HTML (or customs): should be data node
1583+ if (tag ().is (Tag .Data ))
15711584 appendChild (new DataNode (text ));
15721585 else
15731586 appendChild (new TextNode (text ));
@@ -1797,20 +1810,8 @@ public Range endSourceRange() {
17971810 return Range .of (this , false );
17981811 }
17991812
1800- boolean shouldIndent (final Document .OutputSettings out ) {
1801- return out .prettyPrint () && isFormatAsBlock (out ) && !isInlineable (out ) && !preserveWhitespace (parentNode );
1802- }
1803-
18041813 @ Override
1805- void outerHtmlHead (final Appendable accum , int depth , final Document .OutputSettings out ) throws IOException {
1806- if (shouldIndent (out )) {
1807- if (accum instanceof StringBuilder ) {
1808- if (((StringBuilder ) accum ).length () > 0 )
1809- indent (accum , depth , out );
1810- } else {
1811- indent (accum , depth , out );
1812- }
1813- }
1814+ void outerHtmlHead (final Appendable accum , Document .OutputSettings out ) throws IOException {
18141815 accum .append ('<' ).append (safeTagName (out .syntax ()));
18151816 if (attributes != null ) attributes .html (accum , out );
18161817
@@ -1826,13 +1827,8 @@ void outerHtmlHead(final Appendable accum, int depth, final Document.OutputSetti
18261827 }
18271828
18281829 @ Override
1829- void outerHtmlTail (Appendable accum , int depth , Document .OutputSettings out ) throws IOException {
1830+ void outerHtmlTail (Appendable accum , Document .OutputSettings out ) throws IOException {
18301831 if (!(childNodes .isEmpty () && tag .isSelfClosing ())) {
1831- if (out .prettyPrint () && (!childNodes .isEmpty () && (
1832- (tag .formatAsBlock () && !preserveWhitespace (parentNode )) ||
1833- (out .outline () && (childNodes .size ()>1 || (childNodes .size ()==1 && (childNodes .get (0 ) instanceof Element ))))
1834- )))
1835- indent (accum , depth , out );
18361832 accum .append ("</" ).append (safeTagName (out .syntax ())).append ('>' );
18371833 }
18381834 }
@@ -1857,12 +1853,16 @@ public String html() {
18571853 }
18581854
18591855 @ Override
1860- public <T extends Appendable > T html (T appendable ) {
1861- final int size = childNodes .size ();
1862- for (int i = 0 ; i < size ; i ++)
1863- childNodes .get (i ).outerHtml (appendable );
1864-
1865- return appendable ;
1856+ public <T extends Appendable > T html (T accum ) {
1857+ Node child = firstChild ();
1858+ if (child != null ) {
1859+ Printer printer = Printer .printerFor (child , accum );
1860+ while (child != null ) {
1861+ NodeTraversor .traverse (printer , child );
1862+ child = child .nextSibling ();
1863+ }
1864+ }
1865+ return accum ;
18661866 }
18671867
18681868 /**
@@ -1969,17 +1969,4 @@ private static final class NodeList extends ChangeNotifyingArrayList<Node> {
19691969 owner .nodelistChanged ();
19701970 }
19711971 }
1972-
1973- private boolean isFormatAsBlock (Document .OutputSettings out ) {
1974- return tag .isBlock () || (parent () != null && parent ().tag ().formatAsBlock ()) || out .outline ();
1975- }
1976-
1977- private boolean isInlineable (Document .OutputSettings out ) {
1978- if (!tag .isInline ())
1979- return false ;
1980- return (parent () == null || parent ().isBlock ())
1981- && !isEffectivelyFirst ()
1982- && !out .outline ()
1983- && !nameIs ("br" );
1984- }
19851972}
0 commit comments