use chromiumoxide::Page;
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::error::{AuditError, Result};
use crate::taxonomy::Severity;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HeadingStructure {
pub h1_count: usize,
pub h1_text: Option<String>,
pub headings: Vec<HeadingInfo>,
pub issues: Vec<HeadingIssue>,
pub total_count: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeadingInfo {
pub level: u8,
pub text: String,
pub length: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeadingIssue {
pub issue_type: String,
pub message: String,
pub severity: Severity,
}
pub async fn analyze_heading_structure(page: &Page) -> Result<HeadingStructure> {
info!("Analyzing heading structure...");
let js_code = r#"
(() => {
const headings = [];
document.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(h => {
const level = parseInt(h.tagName.charAt(1));
const text = h.textContent.trim();
headings.push({ level, text, length: text.length });
});
return JSON.stringify(headings);
})()
"#;
let js_result = page
.evaluate(js_code)
.await
.map_err(|e| AuditError::CdpError(format!("Heading analysis failed: {}", e)))?;
let json_str = js_result.value().and_then(|v| v.as_str()).unwrap_or("[]");
let headings: Vec<HeadingInfo> = serde_json::from_str(json_str).unwrap_or_default();
let h1_headings: Vec<_> = headings.iter().filter(|h| h.level == 1).collect();
let h1_count = h1_headings.len();
let h1_text = h1_headings.first().map(|h| h.text.clone());
let mut issues = Vec::new();
if h1_count == 0 {
issues.push(HeadingIssue {
issue_type: "missing_h1".to_string(),
message: "Page is missing an H1 heading".to_string(),
severity: Severity::High,
});
}
if h1_count > 1 {
issues.push(HeadingIssue {
issue_type: "multiple_h1".to_string(),
message: format!("Page has {} H1 headings (should have exactly 1)", h1_count),
severity: Severity::Medium,
});
}
let mut prev_level = 0u8;
for heading in &headings {
if prev_level > 0 && heading.level > prev_level + 1 {
issues.push(HeadingIssue {
issue_type: "skipped_level".to_string(),
message: format!(
"Heading level skipped: H{} to H{} (\"{}\")",
prev_level,
heading.level,
truncate(&heading.text, 40)
),
severity: Severity::Medium,
});
}
prev_level = heading.level;
}
for heading in &headings {
if heading.text.is_empty() {
issues.push(HeadingIssue {
issue_type: "empty_heading".to_string(),
message: format!("Empty H{} heading found", heading.level),
severity: Severity::High,
});
}
}
for heading in &headings {
if heading.length > 70 {
issues.push(HeadingIssue {
issue_type: "long_heading".to_string(),
message: format!(
"H{} is too long ({} chars): \"{}...\"",
heading.level,
heading.length,
truncate(&heading.text, 30)
),
severity: Severity::Medium,
});
}
}
info!(
"Heading structure: {} total, {} H1s, {} issues",
headings.len(),
h1_count,
issues.len()
);
Ok(HeadingStructure {
h1_count,
h1_text,
total_count: headings.len(),
headings,
issues,
})
}
fn truncate(s: &str, max: usize) -> String {
if s.len() <= max {
return s.to_string();
}
let target = max.saturating_sub(3);
let boundary = s
.char_indices()
.take_while(|(i, _)| *i <= target)
.last()
.map(|(i, _)| i)
.unwrap_or(0);
format!("{}...", &s[..boundary])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_heading_info() {
let heading = HeadingInfo {
level: 1,
text: "Test Heading".to_string(),
length: 12,
};
assert_eq!(heading.level, 1);
assert_eq!(heading.length, 12);
}
}