Skip to content

Commit dd1c973

Browse files
util: split getSectionElements from CasePage
1 parent 37d981f commit dd1c973

4 files changed

Lines changed: 35 additions & 22 deletions

File tree

src/wiki/DeputyCasePage.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import DeputyCase from './DeputyCase';
55
import sectionHeadingId from './util/sectionHeadingId';
66
import isWikiHeading from './util/isWikiHeading';
77
import getWikiHeadingLevel from './util/getWikiHeadingLevel';
8+
import getSectionElements from './util/getSectionElements';
89

910
export type ContributionSurveyHeading = HTMLHeadingElement;
1011

@@ -254,27 +255,12 @@ export default class DeputyCasePage extends DeputyCase {
254255
sectionHeading = this.normalizeSectionHeading( sectionHeading );
255256
const sectionHeadingLevel = getWikiHeadingLevel( sectionHeading );
256257

257-
const sectionMembers: Node[] = [];
258-
259-
let nextSibling = sectionHeading.nextSibling;
260-
while (
261-
// Not the end of rendered page content and
262-
nextSibling != null &&
263-
// Next node is not...
264-
!(
265-
// An element
266-
nextSibling instanceof Element &&
267-
// A heading (of any level)
268-
isWikiHeading( nextSibling ) &&
269-
// Higher than the current heading level
270-
sectionHeadingLevel >= getWikiHeadingLevel( nextSibling )
271-
)
272-
) {
273-
sectionMembers.push( nextSibling );
274-
nextSibling = nextSibling.nextSibling as HTMLElement;
275-
}
276-
277-
return sectionMembers;
258+
return getSectionElements(
259+
this.normalizeSectionHeading( sectionHeading ),
260+
( el ) =>
261+
isWikiHeading( el ) &&
262+
sectionHeadingLevel >= getWikiHeadingLevel( el )
263+
);
278264
}
279265

280266
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import isWikiHeading from './isWikiHeading';
2+
3+
/**
4+
* Finds section elements from a given section heading (and optionally a predicate)
5+
*
6+
* @param sectionHeading
7+
* @param sectionHeadingPredicate
8+
* @return Section headings.
9+
*/
10+
export default function getSectionElements(
11+
sectionHeading: HTMLElement,
12+
sectionHeadingPredicate: ( el: HTMLElement ) => boolean = isWikiHeading
13+
): HTMLElement[] {
14+
const sectionMembers: HTMLElement[] = [];
15+
16+
let nextSibling = sectionHeading.nextElementSibling as HTMLElement;
17+
while ( nextSibling != null && !sectionHeadingPredicate( nextSibling ) ) {
18+
sectionMembers.push( nextSibling );
19+
nextSibling = nextSibling.nextElementSibling as HTMLElement;
20+
}
21+
22+
return sectionMembers;
23+
}

src/wiki/util/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import errorToOO from './errorToOO';
44
import getApiErrorText from './getApiErrorText';
55
import getNativeRange from './getNativeRange';
66
import getPageContent from './getPageContent';
7+
import getPageExists from './getPageExists';
78
import getPageTitle from './getPageTitle';
89
import getRevisionContent from './getRevisionContent';
910
import getRevisionDiffURL from './getRevisionDiffURL';
1011
import getRevisionURL from './getRevisionURL';
12+
import getSectionElements from './getSectionElements';
1113
import getSectionHTML from './getSectionHTML';
1214
import getSectionId from './getSectionId';
1315
import getWikiHeadingLevel from './getWikiHeadingLevel';
@@ -33,10 +35,12 @@ export default {
3335
getApiErrorText: getApiErrorText,
3436
getNativeRange: getNativeRange,
3537
getPageContent: getPageContent,
38+
getPageExists: getPageExists,
3639
getPageTitle: getPageTitle,
3740
getRevisionContent: getRevisionContent,
3841
getRevisionDiffURL: getRevisionDiffURL,
3942
getRevisionURL: getRevisionURL,
43+
getSectionElements: getSectionElements,
4044
getSectionHTML: getSectionHTML,
4145
getSectionId: getSectionId,
4246
getWikiHeadingLevel: getWikiHeadingLevel,

src/wiki/util/normalizeTitle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type TitleLike = string | mw.Title | { namespace: number, title: string }
55
* the current page.
66
*
77
* @param title The title to normalize. Default is current page.
8-
* @return {mw.Title} A mw.Title object.
8+
* @return {mw.Title} A mw.Title object. `null` if not a valid title.
99
* @private
1010
*/
1111
export default function normalizeTitle( title?: TitleLike ): mw.Title {

0 commit comments

Comments
 (0)