Skip to content

Commit

Permalink
fix: improve attribute detection regex
Browse files Browse the repository at this point in the history
Adjusted the regular expression to better identify obsolete attributes, including those with minimized forms. Updated tests to handle new minimized attribute cases and ensure comprehensive coverage.

Signed-off-by: Jens Oliver Meiert <jens@meiert.com>
  • Loading branch information
j9t committed Aug 17, 2024
1 parent 7f8b300 commit 0e8faec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/obsohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function findObsolete(filePath) {

// Check for obsolete attributes
obsoleteAttributes.forEach(attribute => {
const attributeRegex = new RegExp(`<[^>]*\\b${attribute}\\s*=\\s*(\"[^\"]*\"|'[^']*'|[^\"'\\s>]+)`, 'i');
const attributeRegex = new RegExp(`<[^>]*\\b${attribute}\\b(?=\\s*(=|\\s*[/]*>))`, 'i');
if (attributeRegex.test(content)) {
console.log(chalk.default.green(`Found obsolete attribute ${chalk.default.bold(`'${attribute}'`)} in ${filePath}`));
}
Expand Down
10 changes: 9 additions & 1 deletion bin/obsohtml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const { spawnSync } = require('child_process');
describe('ObsoHTML', () => {
const tempDir = path.join(__dirname, 'temp_test_dir');
const tempFile = path.join(tempDir, 'test.html');
const tempFileWithAttributes = path.join(tempDir, 'test_with_attribute.html');
const tempFileWithAttributes = path.join(tempDir, 'test_with_attributes.html');
const tempFileWithMinimizedAttributes = path.join(tempDir, 'test_with_minimized_attributes.html');

beforeAll(() => {
// Create a temporary directory and files
Expand All @@ -14,12 +15,14 @@ describe('ObsoHTML', () => {
}
fs.writeFileSync(tempFile, '<!DOCTYPE html><html><title>Test</title><body><center>Test</center></body></html>');
fs.writeFileSync(tempFileWithAttributes, '<!DOCTYPE html><html><title>Test</title><body><img src=test.jpg alt=Test align=left></body></html>');
fs.writeFileSync(tempFileWithMinimizedAttributes, '<!DOCTYPE html><html><title>Test</title><hr noshade><!-- this is not a <table> with a nowrap attribute -->');
});

afterAll(() => {
// Clean up the temporary directory and files
fs.unlinkSync(tempFile);
fs.unlinkSync(tempFileWithAttributes);
fs.unlinkSync(tempFileWithMinimizedAttributes);
fs.rmdirSync(tempDir);
});

Expand All @@ -46,4 +49,9 @@ describe('ObsoHTML', () => {
expect(result.stdout).toContain("Found obsolete element 'center'");
expect(result.stdout).toContain("Found obsolete attribute 'align'");
});

test('Detect obsolete minimized attributes', () => {
const result = spawnSync('node', ['bin/obsohtml.js', '-f', tempDir], { encoding: 'utf-8' });
expect(result.stdout).toContain("Found obsolete attribute 'noshade'");
});
});

0 comments on commit 0e8faec

Please sign in to comment.