473,414 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Finding line numbers of HTML elements

Hi,

I'm helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn't have a FOR attribute). There
doesn't seem to be a straightforward way with Javascript to find out at
which line a piece of a page's HTML code is, so I've been reading
through the XBL/XPCOM source code of Mozilla Firefox's Javascript
Console for some insight. While I'm slowly making progress with that
method, I was wondering if I haven't overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?

Thanks,
Eric

Aug 13 '06 #1
12 3355
e271828 said the following on 8/13/2006 6:18 PM:
Hi,

I'm helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn't have a FOR attribute). There
doesn't seem to be a straightforward way with Javascript to find out at
which line a piece of a page's HTML code is, so I've been reading
through the XBL/XPCOM source code of Mozilla Firefox's Javascript
Console for some insight. While I'm slowly making progress with that
method, I was wondering if I haven't overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?
How you would find that line number depends on how you read the source.
But, you could always read the entire page as a string, find the
occurrence of the offending code, get a substring from 0 to the point of
the offending code, then split that substring on \r\n and the length of
the resulting array minus one will be your line number.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 14 '06 #2
I intially thougt of doing something along those lines as well, but the
following pieces of Javascript that I tried didn't work quite right:

alert("'" + document.toString() + "'"); returns [objectHTMLelement] --
definitely not what I'm looking for. Applying the .length method to
that returns the number of characters (21) in the document.toString()
result. I also tried alert("'" +
document.getElementsByTagName("html").toString() + "'" and
document.getElementsByTagName("html").innerHTML + "'");, but had no
luck there either. If there isn't a way to get the document as a
string, then there's little option to proceed. I think I might try my
hand at extending Firefox's Javascript Debuggers XBL bindings (which
feels like too much work for a fairly mundane feature), unless anybody
has other ideas.

Thanks for the input though.

Randy Webb wrote:
e271828 said the following on 8/13/2006 6:18 PM:
Hi,

I'm helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn't have a FOR attribute). There
doesn't seem to be a straightforward way with Javascript to find out at
which line a piece of a page's HTML code is, so I've been reading
through the XBL/XPCOM source code of Mozilla Firefox's Javascript
Console for some insight. While I'm slowly making progress with that
method, I was wondering if I haven't overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?

How you would find that line number depends on how you read the source.
But, you could always read the entire page as a string, find the
occurrence of the offending code, get a substring from 0 to the point of
the offending code, then split that substring on \r\n and the length of
the resulting array minus one will be your line number.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 14 '06 #3
I got it. For future reference, one way to get the innerHTML source of
a page is to use the method document.documentElement.innerHTML . Since
there apparently aren't carriage returns after the page has been
rendered, the best way to split the HTML source by line number is to
put a document.documentElement.innerHTML.split("\n") onto the end.

Thanks for the suggestions Randy. I will have to postpone my suicidal
foray in Mozilla's XBL/XPCOM for another day.

-Eric

Aug 14 '06 #4
document.documentElement.innerHTML apparently parses the HTML into a
slightly (but significantly) different form, so it is not a solution to
finding the line numbers of a given HTML page.

The main problem with using innerHTML to find line numbers is that it
takes out any tabs and new lines between the opening and closing angle
brackets of a single tag. So the HTML source:

<div id="NavBar"
class="VerticalBarIcon"
onmouseover="document['asdf'].mouseover(this)"
onmousedown="document['fdsa'].mousedown(this)"
onmouseout="document['sdaf'].mouseout(this)" ></div>

returns, after innerHTML is applied, as:

<div id="NavBar" class="VerticalBarIcon"
onmouseover="document['asdf'].mouseover(this)"
onmousedown="document['fdsa'].mousedown(this)"
onmouseout="document['sdaf'].mouseout(this)" ></div>
This precludes the chance of breaking along the new lines of the actual
source, and presents a big problem. If anyone knows the general
direction I should go in to begin solving this, any advice would be
appreciated.

Aug 18 '06 #5
e271828 wrote:
document.documentElement.innerHTML apparently parses the HTML into a
slightly (but significantly) different form, so it is not a solution to
finding the line numbers of a given HTML page.

The main problem with using innerHTML to find line numbers is that it
takes out any tabs and new lines between the opening and closing angle
brackets of a single tag.
[...]
var file = 'e.html'; // file must exist!
var xmlhttp = false;
var counter = 0;
var theresult = '';

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}

xmlhttp.open('GET', file, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
lines = (xmlhttp.responseText).split('\r\n')
for (var i=0; i<lines.length; ++i) {
++ counter
theresult = theresult + counter + ' ' + lines[i] + '\r\n'
}
alert(theresult)
}
}

xmlhttp.send(null)

--
Bart

Aug 19 '06 #6
Bart Van der Donck wrote:
<snip>
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}

xmlhttp.open('GET', file, true);
<snip>

Every one of the above branches includes the possibility that -
xmlhttp - will be set to boolean false, and as each is guarded in some
way none may be entered in some environments. Yet the call to - open -
is made on the assumption that the instantiation of the XML HTTP request
object was successful. This will have the code error-out in environments
such as IE with ActiveX disabled and any browser not supporting XML HTTP
requests in any form. Code that predictably may terminate with a runtime
error cannot be considered well designed. Partly because the in the
event that the user be shown an error message the site author's and
owners would look less than competent (so responsible, trustworthy,
reliable, etc.), but mostly because it denies the possibility of the
code failing under its own control and planned clean degradation.

Richard.
Aug 19 '06 #7
Richard Cornford wrote:
[...]
Every one of the above branches includes the possibility that -
xmlhttp - will be set to boolean false, and as each is guarded in some
way none may be entered in some environments. Yet the call to - open -
is made on the assumption that the instantiation of the XML HTTP request
object was successful. This will have the code error-out in environments
such as IE with ActiveX disabled and any browser not supporting XML HTTP
requests in any form. Code that predictably may terminate with a runtime
error cannot be considered well designed. Partly because the in the
event that the user be shown an error message the site author's and
owners would look less than competent (so responsible, trustworthy,
reliable, etc.), but mostly because it denies the possibility of the
code failing under its own control and planned clean degradation.
If this is true then the FAQ is wrong, since the routine comes straight
from http://www.jibbering.com/faq/#FAQ4_38.

--
Bart

Aug 19 '06 #8
On 19/08/2006 14:01, Bart Van der Donck wrote:
Richard Cornford wrote:
>[...]
Every one of the above branches includes the possibility that -
xmlhttp - will be set to boolean false, and as each is guarded in some
way none may be entered in some environments. Yet the call to - open -
is made on the assumption that the instantiation of the XML HTTP request
object was successful. This will have the code error-out in environments
such as IE with ActiveX disabled and any browser not supporting XML HTTP
requests in any form. Code that predictably may terminate with a runtime
error cannot be considered well designed. Partly because the in the
event that the user be shown an error message the site author's and
owners would look less than competent (so responsible, trustworthy,
reliable, etc.), but mostly because it denies the possibility of the
code failing under its own control and planned clean degradation.

If this is true then the FAQ is wrong, since the routine comes straight
from http://www.jibbering.com/faq/#FAQ4_38.
No, /your application/ of code obtained from a resource /referenced/ by
the FAQ is wrong.

Nowhere did Jim write that the xmlhttp variable shouldn't be checked. In
fact, the paragraph that leads into the object creation code makes it
quite clear that such checks are important.

Don't blame others for your mistakes.

Mike
Aug 19 '06 #9

Michael Winter schreef:
On 19/08/2006 14:01, Bart Van der Donck wrote:
If this is true then the FAQ is wrong, since the routine comes straight
from http://www.jibbering.com/faq/#FAQ4_38.

No, /your application/ of code obtained from a resource /referenced/ by
the FAQ is wrong.
What would be this "correct application" of that code then ?
Nowhere did Jim write that the xmlhttp variable shouldn't be checked.
A little common sense, please. Why doesn't he just check it then in the
code ?

--
Bart

Aug 19 '06 #10
Bart Van der Donck wrote:
Richard Cornford wrote:
>[...]
Every one of the above branches includes the possibility
that - xmlhttp - will be set to boolean false, and as each
is guarded in some way none may be entered in some
environments. Yet the call to - open - is made on the
assumption that the instantiation of the XML HTTP request
object was successful. This will have the code error-out in
environments such as IE with ActiveX disabled and any
browser not supporting XML HTTP requests in any form. Code
that predictably may terminate with a runtime error cannot
be considered well designed. Partly because the in the event
that the user be shown an error message the site author's
and owners would look less than competent (so responsible,
trustworthy, reliable, etc.), but mostly because it denies
the possibility of the code failing under its own control
and planned clean degradation.

If this is true then the FAQ is wrong, since the routine
comes straight from http://www.jibbering.com/faq/#FAQ4_38.
No it doesn't. The code you posted is a concatenation of two separate
pieces of example code from a page referred to by the FAQ. The faults in
your posted code may follow from the unsuitability of 'the concatenation
of examples' as a code design strategy, but that does not mean they are
not present.

Richard.
Aug 19 '06 #11
On 19/08/2006 15:24, Bart Van der Donck wrote:
Michael Winter schreef:
>On 19/08/2006 14:01, Bart Van der Donck wrote:
>>If this is true then the FAQ is wrong, since the routine comes straight
from http://www.jibbering.com/faq/#FAQ4_38.

No, /your application/ of code obtained from a resource /referenced/ by
the FAQ is wrong.

What would be this "correct application" of that code then ?
If an author needs to be told how to use an if statement, AJAX is far
beyond their means. That is a simplification, of course, but clean
degradation isn't a trivial implementation issue; it's a design
consideration.
>Nowhere did Jim write that the xmlhttp variable shouldn't be checked.

A little common sense, please.
A little common sense would have people read descriptions before getting
ahead of themselves and using code without thinking. Jim states that the
variable will evaluate to false if no suitable request object is
obtained. It should be clear that that is the most basic starting point.
Why doesn't he just check it then in the code ?
The code is separated by topic. The first part describes how to obtain a
request object. The following sections describe the sequence of method
calls used to perform a certain operation. It isn't meant to be taken as
a complete example, but rather separate code snippets.

Even if the code did seem "complete", that doesn't mean one should
disregard the warning to check for support first: examples are often
simplified to ensure that the operation that is demonstrated isn't
obscured by supporting code.

Mike
Aug 19 '06 #12
Bart Van der Donck wrote:
Michael Winter schreef:
>On 19/08/2006 14:01, Bart Van der Donck wrote:
>>If this is true then the FAQ is wrong, since the routine
comes straight from http://www.jibbering.com/faq/#FAQ4_38.

No, /your application/ of code obtained from a resource
/referenced/ by the FAQ is wrong.

What would be this "correct application" of that code then ?
There could be no single 'this "correct application" of that code', and
a comprehensive list of 'correct applications' would be an unrealistic
expectation (not least because of the subjectivity in the assessment of
correctness, but mostly because the context of application significantly
modifies the 'correct' strategy of use). However, there will not be many
contexts in which a script erroring-out at runtime would be considered
correct (intrinsic event handlers where failure in the event handler
directly results in page replacement (as a style of clean degradation)
may be the only possible exception, though I still would not encourage
the possibility of the user being shown an error message).
>Nowhere did Jim write that the xmlhttp variable shouldn't be
checked.

A little common sense, please.
Most appeals to "common sense" are made in support of bad ideas. (I am
often reminded of William Edgell's appeal to "common sense" in his 1927
defence of the idea of a flat Earth (<URL:
http://www.litotes.demon.co.uk/\dTeR...rthRotate.html >).)
Why doesn't he just check it then in the code ?
Is the subject of the page the behaviour of the XML HTTP request object
or is it general script design?

However, if the page did include specific examples of 'safe' employment
of the object those examples may be take as representing a definitive
strategy. In such small examples the tendency would be to wrap the test
around the code that actually used the object, while the bigger picture
may suggest some form of 'gateway' testing during page initialisation,
or a 'test on first use only' or 'Russian Doll' style of implementation,
particularly if multiple requests were to be expected on the same page.

Without a genuine context examples that do test for the availability of
features tend to test too far into the code (at the point where the
features is employed) and propose superficial and stupid responses to
failures (such as popping up some sort of "your browser does not support
this script" message). These examples get carried through to actual web
pages unaltered with sufficient frequency as to demonstrate that that
type of example can be as much of a hindrance to learning as an aid. In
practice designing for clean-degradation in response to negative results
from feature testing is so intimately linked to the context in which a
script is used that demonstrations of a single test and response
strategy are insufficient (even misleading).

Richard.
Aug 19 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
10
by: Dixie | last post by:
I need to delete some relationships in code. How do I know what the names of those relationships are?
9
by: Harsha Srinath | last post by:
Athough this might not be directly relayed to C syntax, I thought some of you may find this an interesting problem :- 1.One number, in an array integers from 1 to 1,000,000 is present twice. How...
3
by: lawrencec | last post by:
Hi there, I'm trying to add the values of a number of form fields and to get a result at the end. It must loop and be able to dynamically update the result of calculation. I have attached the...
14
by: kelvin.jones | last post by:
Hi, if I had the ID of an input element, how can I find the input's FORM in javascript? Basically, given a input's dom id, I want to insert something in the onSubmit of the Form that that input...
25
by: Subra | last post by:
Hi, What is the best way to find the 1000 largest numbers from the file having hell lot of entries ? Can you please help me to find out the way ? Do I need to go for B+ trees ?? Please help,...
5
by: Ramdas | last post by:
I am doing some HTML scrapping for a side project. I need a method using sgmllib or HTMLParser to parse an HTML file and get line nos of all the tags I tried a few things, but I am just not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.