1. function validateHTML(html) {
  2. try {
  3. const parser = new DOMParser();
  4. const doc = parser.parseFromString(html, 'text/html');
  5. // Check for basic structural errors
  6. if (doc.documentElement.nodeName !== 'HTML') {
  7. return { errors: [{ message: 'Document must be an HTML document' }] };
  8. }
  9. // Check for required elements (e.g., head, body)
  10. if (!doc.querySelector('head') || !doc.querySelector('body')) {
  11. return { errors: [{ message: 'Missing required elements (head or body)' }] };
  12. }
  13. //Check for invalid/missing attributes
  14. const elements = doc.getElementsByTagName('*');
  15. for (let i = 0; i < elements.length; i++) {
  16. const element = elements[i];
  17. if (element.hasAttributes) {
  18. for (let j = 0; j < element.attributes.length; j++) {
  19. const attr = element.attributes[j];
  20. if (!/^[a-zA-Z0-9._-]+$/.test(attr.name)) {
  21. return { errors: [{ message: `Invalid attribute name: ${attr.name}` }] };
  22. }
  23. }
  24. }
  25. }
  26. // Check for common invalid HTML tags. Extend as needed.
  27. const invalidTags = ['iframe', 'object', 'embed'];
  28. for (let i = 0; i < invalidTags.length; i++) {
  29. if (doc.querySelector(invalidTags[i])) {
  30. return { errors: [{ message: `Invalid tag: ${invalidTags[i]}` }] };
  31. }
  32. }
  33. // Check for invalid doctype declaration (basic check)
  34. const doctype = doc.querySelector('<!DOCTYPE html>').textContent;
  35. if (!doctype.startsWith('<!DOCTYPE html>')) {
  36. return { errors: [{ message: 'Invalid doctype declaration' }] };
  37. }
  38. return { errors: [] }; // No errors found
  39. } catch (error) {
  40. return { errors: [{ message: `Parsing error: ${error.message}` }] };
  41. }
  42. }

Add your comment