fix: harden parseProfilePage class and program extraction
This commit is contained in:
+4
-2
@@ -48,7 +48,9 @@
|
|||||||
|
|
||||||
function parseProfilePage(html) {
|
function parseProfilePage(html) {
|
||||||
const result = {};
|
const result = {};
|
||||||
const bMatch = /<b>([\s\S]*?)<\/b>/i.exec(html);
|
const h3Match = /<h3[^>]*>([\s\S]*?)<\/h3>/i.exec(html);
|
||||||
|
const bMatch = (h3Match && /<b>([\s\S]*?)<\/b>/i.exec(h3Match[1])) ||
|
||||||
|
/<b>([\s\S]*?)<\/b>/i.exec(html);
|
||||||
if (bMatch) result.program = cleanText(bMatch[1]);
|
if (bMatch) result.program = cleanText(bMatch[1]);
|
||||||
|
|
||||||
const smallMatch = /<small>([\s\S]*?)<\/small>/i.exec(html);
|
const smallMatch = /<small>([\s\S]*?)<\/small>/i.exec(html);
|
||||||
@@ -57,7 +59,7 @@
|
|||||||
result.semester = lines[lines.length - 1] || '';
|
result.semester = lines[lines.length - 1] || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const kvRe = /<td[^>]*>([\s\S]*?)<\/td>\s*<td[^>]*class="bold"[^>]*>([\s\S]*?)<\/td>/gi;
|
const kvRe = /<td[^>]*>([\s\S]*?)<\/td>\s*<td[^>]*class=["'][^"']*\bbold\b[^"']*["'][^>]*>([\s\S]*?)<\/td>/gi;
|
||||||
let m;
|
let m;
|
||||||
while ((m = kvRe.exec(html)) !== null) {
|
while ((m = kvRe.exec(html)) !== null) {
|
||||||
const key = cleanText(m[1]);
|
const key = cleanText(m[1]);
|
||||||
|
|||||||
@@ -34,6 +34,26 @@ test('parseProfilePage extracts header fields', () => {
|
|||||||
assert.strictEqual(p.fullName, 'ADRIAN ALFRED CHIONG BONPIN');
|
assert.strictEqual(p.fullName, 'ADRIAN ALFRED CHIONG BONPIN');
|
||||||
assert.strictEqual(p.studentId, '22103625');
|
assert.strictEqual(p.studentId, '22103625');
|
||||||
assert.strictEqual(p.yearLevel, '4');
|
assert.strictEqual(p.yearLevel, '4');
|
||||||
|
assert.strictEqual(p.studentType, 'CONTINUING');
|
||||||
assert.strictEqual(p.program, 'BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY');
|
assert.strictEqual(p.program, 'BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY');
|
||||||
assert.strictEqual(p.semester, '1ST Semester 2026');
|
assert.strictEqual(p.semester, '1ST Semester 2026');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('parseProfilePage matches bold class token with single quotes and extra classes', () => {
|
||||||
|
const html = `
|
||||||
|
<h3><b>BACHELOR OF SCIENCE</b></h3>
|
||||||
|
<table>
|
||||||
|
<tr><td>Full Name</td><td class='bold text-right'>JOHN DOE</td></tr>
|
||||||
|
<tr><td>Student Type</td><td class="bold">CONTINUING</td></tr>
|
||||||
|
</table>
|
||||||
|
`;
|
||||||
|
const p = parseProfilePage(html);
|
||||||
|
assert.strictEqual(p.fullName, 'JOHN DOE');
|
||||||
|
assert.strictEqual(p.studentType, 'CONTINUING');
|
||||||
|
assert.strictEqual(p.program, 'BACHELOR OF SCIENCE');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseProfilePage degrades gracefully on empty input', () => {
|
||||||
|
const p = parseProfilePage('');
|
||||||
|
assert.deepStrictEqual(p, {});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user