Skip to content

Commit 6401324

Browse files
committed
minor optimizations
1 parent c6d2cca commit 6401324

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

src/main/java/org/htmlunit/cyberneko/HTMLTagBalancer.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ public void startDocument(final XMLLocator locator, final String encoding,
446446

447447
// reset state
448448
fElementStack.clear();
449+
449450
if (fragmentContextStack_ != null) {
450451
fragmentContextStackSize_ = fragmentContextStack_.length;
451452
// use indexed loop to avoid Iterator allocation
@@ -818,7 +819,7 @@ else if (!fSeenRootElement && !fDocumentFragment) {
818819
fErrorReporter.reportWarning("HTML2002", new Object[]{ename, pname});
819820
}
820821
final QName qname = createQName(pname);
821-
final boolean parentCreated = forceStartElement(qname, fEmptyXMLAttributes, synthesizedAugs());
822+
final boolean parentCreated = forceStartElementParentCreated(qname, fEmptyXMLAttributes, synthesizedAugs());
822823
if (!parentCreated) {
823824
if (!isForcedCreation) {
824825
notifyDiscardedStartElement(elem, attrs, augs);
@@ -838,7 +839,7 @@ else if (!fSeenRootElement && !fDocumentFragment) {
838839
}
839840

840841
final QName qname = createQName(pname);
841-
final boolean parentCreated = forceStartElement(qname, fEmptyXMLAttributes, synthesizedAugs());
842+
final boolean parentCreated = forceStartElementParentCreated(qname, fEmptyXMLAttributes, synthesizedAugs());
842843
if (!parentCreated) {
843844
if (!isForcedCreation) {
844845
notifyDiscardedStartElement(elem, attrs, augs);
@@ -964,14 +965,25 @@ else if (!fTemplateFragment && elementCode == HTMLElements.SELECT) {
964965
* the element has been forced.
965966
* @return <code>true</code> if creation could be done (TABLE's creation for instance can't be forced)
966967
*/
967-
private boolean forceStartElement(final QName elem, final XMLAttributes attrs,
968+
private boolean forceStartElementParentCreated(final QName elem, final XMLAttributes attrs,
968969
final Augmentations augs) throws XNIException {
969970
forcedStartElement_ = true;
970971
startElement(elem, attrs, augs);
971972

972973
return fElementStack.length > 0 && elem.equals(fElementStack.peek().qname);
973974
}
974975

976+
/**
977+
* Forces an element start, taking care to set the information to allow startElement to "see" that's
978+
* the element has been forced.
979+
* @return <code>true</code> if creation could be done (TABLE's creation for instance can't be forced)
980+
*/
981+
private void forceStartElement(final QName elem, final XMLAttributes attrs,
982+
final Augmentations augs) throws XNIException {
983+
forcedStartElement_ = true;
984+
startElement(elem, attrs, augs);
985+
}
986+
975987
private static QName createQName(final String tagName) {
976988
return new QName(null, tagName, tagName, NamespaceBinder.XHTML_1_0_URI);
977989
}
@@ -1162,7 +1174,11 @@ public void endElement(final QName element, final Augmentations augs) throws XNI
11621174
}
11631175

11641176
// check for end of document
1165-
if (elementCode == HTMLElements.HTML) {
1177+
if (elementCode == HTMLElements.BR) {
1178+
forceStartElement(element, fEmptyXMLAttributes, synthesizedAugs());
1179+
return;
1180+
}
1181+
else if (elementCode == HTMLElements.HTML) {
11661182
fSeenRootElementEnd = true;
11671183
}
11681184
else if (fIgnoreOutsideContent) {
@@ -1196,10 +1212,6 @@ else if (elementCode == HTMLElements.HEAD && !isForcedEndElement) {
11961212
endElement(element, augs);
11971213
return;
11981214
}
1199-
if (elementCode == HTMLElements.BR) {
1200-
forceStartElement(element, fEmptyXMLAttributes, synthesizedAugs());
1201-
return;
1202-
}
12031215

12041216
// Headings: if a heading end tag appears but there is no matching open tag,
12051217
// close the nearest open heading (H1..H6).
@@ -1239,12 +1251,7 @@ else if (elementCode == HTMLElements.HEAD && !isForcedEndElement) {
12391251
}
12401252

12411253
private static boolean isHeading(final short code) {
1242-
return code == HTMLElements.H1
1243-
|| code == HTMLElements.H2
1244-
|| code == HTMLElements.H3
1245-
|| code == HTMLElements.H4
1246-
|| code == HTMLElements.H5
1247-
|| code == HTMLElements.H6;
1254+
return HTMLElements.H1 <= code && code <= HTMLElements.H6;
12481255
}
12491256

12501257
/**
@@ -1552,7 +1559,7 @@ public Info pop() {
15521559

15531560
// Resets the stack and releases all Info references so they can be GC'd.
15541561
public void clear() {
1555-
Arrays.fill(data, 0, length, null);
1562+
Arrays.fill(data, null);
15561563
length = 0;
15571564
}
15581565

0 commit comments

Comments
 (0)