Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

duplicate anchors #20

Open
kryde opened this issue Jan 7, 2012 · 2 comments
Open

duplicate anchors #20

kryde opened this issue Jan 7, 2012 · 2 comments

Comments

@kryde
Copy link

kryde commented Jan 7, 2012

It'd be good if HTML::Lint noticed duplicated name="" etc attributes. I believe the specs call for anchors to be unique, and in any case if they're not then it's almost certainly some sort of mistake or typo by the author.

I get some joy from the patch below (is this awful gist thing the way to attach patches to tickets? :-(. It picked up some duplications in some of my html caused by too much cut and paste :-).

https://gist.github.com/1576415

@petdance
Copy link
Owner

The way to do it is to fork my repo into your own repo, make your changes, and then make a Pull Request.

@petdance
Copy link
Owner

petdance commented Apr 19, 2017

From the gist mentioned above

From 1bcf38e043cc9564510415e58ff3a81ff4dafcfb Mon Sep 17 00:00:00 2001
From: Kevin Ryde <[email protected]>
Date: Sun, 8 Jan 2012 09:35:27 +1100
Subject: [PATCH] New anchor-repeated reporting duplicated anchors

---
 Changes                |    4 ++++
 lib/HTML/Lint.pm       |   14 ++++++++++++++
 lib/HTML/Lint/Error.pm |    1 +
 t/anchor-repeated.t    |   28 ++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 0 deletions(-)
 create mode 100644 t/anchor-repeated.t

diff --git a/Changes b/Changes
index 78fd6d2..4c91669 100644
--- a/Changes
+++ b/Changes
@@ -6,6 +6,10 @@ NOTE: All bugs and requests are now being handled through GitHub.
 
 DO NOT send bug reports to http://rt.cpan.org/ or http://code.google.com/
 
+2.12
+        [ENHANCEMENTS]
+        * Report duplicate anchors <a name=""> and id="".
+
 2.10    Tue Dec  6 11:16:16 CST 2011
 
         HTML::Lint is now explicitly licensed under Artistic License 2.0,
diff --git a/lib/HTML/Lint.pm b/lib/HTML/Lint.pm
index 1a17cfd..7491153 100644
--- a/lib/HTML/Lint.pm
+++ b/lib/HTML/Lint.pm
@@ -252,6 +252,7 @@ sub newfile {
     $self->{_line} = 0;
     $self->{_column} = 0;
     $self->{_first_seen} = {};
+    $self->{_anchor_seen} = {};
 
     return $self->{_file};
 } # newfile
@@ -341,6 +342,19 @@ sub _start {
             if ( $validattr && ( !$validattr->{$attr} ) ) {
                 $self->gripe( 'attr-unknown', tag => $tag, attr => $attr );
             }
+
+            # HTML4 spec 7.5.2 says <a name=""> and id="" are
+            # case-sensitive, must be unique in the document, and they share
+            # namespace
+            if ($attr eq 'id' || ($tag eq 'a' && $attr eq 'name')) {
+                if ( my $where = $self->{_anchor_seen}{$val} ) {
+                    $self->gripe( 'anchor-repeated',
+                                  anchor => $val,
+                                  where => HTML::Lint::Error::where(@$where));
+                } else {
+                    $self->{_anchor_seen}{$val} = [$line,$column];
+                }
+            }
         } # while attribs
     }
     else {
diff --git a/lib/HTML/Lint/Error.pm b/lib/HTML/Lint/Error.pm
index b4f5d55..861e6dd 100644
--- a/lib/HTML/Lint/Error.pm
+++ b/lib/HTML/Lint/Error.pm
@@ -256,6 +256,7 @@ Andy Lester, C<andy at petdance.com>
 
     'attr-repeated' =>          ['${attr} attribute in <${tag}> is repeated', STRUCTURE],
     'attr-unknown' =>           ['Unknown attribute "${attr}" for tag <${tag}>', FLUFF],
+    'anchor-repeated' =>        ['Anchor name "${anchor}" repeated, already appeared at ${where}', STRUCTURE],
 
     'text-use-entity' =>        ['Invalid character ${char} should be written as ${entity}', STRUCTURE],
 );
diff --git a/t/anchor-repeated.t b/t/anchor-repeated.t
new file mode 100644
index 0000000..e8d20db
--- /dev/null
+++ b/t/anchor-repeated.t
@@ -0,0 +1,28 @@
+use warnings;
+use strict;
+require 't/LintTest.pl';
+
+checkit([
+         [ 'anchor-repeated' => qr/Anchor name "foo" repeated, already appeared at/ ],
+         [ 'anchor-repeated' => qr/Anchor name "quux" repeated, already appeared at/ ],
+         [ 'anchor-repeated' => qr/Anchor name "xyzzy" repeated, already appeared at/ ],
+        ],
+        [<DATA>] );
+    
+__DATA__
+<html>
+<head><title>blah</title></head>
+<body>
+<a name="foo">
+<a name="foo">
+
+<a name="bar">
+<a name="Bar">  <!-- case sensitive, so not a duplicate -->
+
+<a name="quux">
+<p id="quux">
+
+<!-- name= and id= are allows together, believe still must be unique -->
+<a name="xyzzy" id="xyzzy">
+
+</html>
-- 
1.7.7.3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants