Selecting a header and the content below it

S

Spartanicus

I'd like to know if it's possible to use js to step through and select a
header in an HTML document, select it's content and all content below
it.

Context: I'm wondering if it's possible to use user javascript to extend
Opera 8's ability to be used as an aural browser. It has preliminary
support for aural CSS which can make a big difference in an author's
ability to tailor a better experience for aural access whilst retaining
the ability to include elements that are useful for visual access.

However Opera 8's capabilities seem geared towards creating voice
enabled applications. I presume Opera expects the feature to be used in
future as an alternative way to operate it's browser on mobile
platforms.

For Opera 8 to be used as an aural browser there are few options,
missing most notably is a non visual ability to select part of a
document to read out, such as navigating through a document's headers
and have a certain header and the content below it read out. Currently
the only option is to select all text and have that read out.
 
V

VK

However Opera 8's capabilities seem geared
towards creating voice enabled applications.

I'm not sure where Opera 8 is geared to (did not have time yet to study
it). But text-to-speach and AI technologies are geared to SALT (Speech
Application Language Tags):

<http://msdn.microsoft.com/library/d.../sasdk_salt/html/ST_Programmers_Reference.asp>

<http://www.w3.org/Voice/>

<http://www.saltforum.org/>

I admit that this is the main domain of my studies, and I invite you to
read these starting materials if you are serious about speach
technologies.
I'd like to know if it's possible to use js to step through and select a
header in an HTML document, select it's content and all content below
it.

As in the text range operations all wannabes are years behind of IE,
I'm using IE for this kind of scripting. *Of course* any of such
scripts (see a sample below) *may be emulated* on other browsers (the
algorithm is pretty clear). I just never did it myself as it would be a
boring and irrelevant to the matter task.

INTERNET EXPLORER ONLY:

<html>
<head>
<title>TextRange</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
var myTextRange = null;
var timerID = null;
var starter = null;
var out = null;

function f1(elm) {
starter = elm;
if (starter.innerText.indexOf('start') != -1) {
starter.innerText = 'Latina (Click to stop)';
out = document.forms[0].elements[0];
myTextRange = document.body.createTextRange();
if (myTextRange.findText('Lorem',null,2)) {
myTextRange.select();
out.value = myTextRange.text.toLowerCase();
timerID = window.setInterval('f2()',1000);
}
}
else {
f3();
}
}

function f2() {
myTextRange.moveStart('word');
myTextRange.expand('word');
myTextRange.select();
switch (myTextRange.text) {
case ', ' : out.value = '[period]';
break;
case '. ' : out.value = '[comma]';
break;
case '.' : f3();
default : out.value = myTextRange.text.toLowerCase();
}
}

function f3() {
window.clearInterval(timerID);
timerID = null;
myTextRange.collapse();
starter.innerText = 'Latina (Click to start)';
}
</script>
</head>

<body bgcolor="#FFFFFF">
<h3 onclick="f1(this)">Latina (Click to start).</h3>
<p>Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Phasellus in sem lacinia nunc rutrum tempor. Vivamus
facilisis.
Vivamus lectus eros, aliquet id, tincidunt in, tempus quis, erat. Etiam
scelerisque
sem et massa. Cum sociis natoque penatibus et magnis dis parturient
montes, nascetur
ridiculus mus. Ut at arcu. Praesent et turpis ac urna ornare hendrerit.
Mauris
vitae pede a libero scelerisque egestas. Nunc sollicitudin, orci et
commodo ultrices,
nisl libero vulputate urna, in posuere ligula arcu ut libero. In
ullamcorper neque
ut mi.</p>

<form name="form1">
<input type="text" name="text1" size="32">
</form>
</body>
</html>
 
M

Martin Honnen

Spartanicus said:
I'd like to know if it's possible to use js to step through and select a
header in an HTML document, select it's content and all content below
it.

Opera 8 has made some efforts to implement a few parts of the IE/Win
text range selection and manipulation API but as far as I can tell it is
restricted to manipulating the selection in text controls.
IE's API is documented here:
<http://msdn.microsoft.com/library/d...hor/dhtml/reference/objects/obj_textrange.asp>
so there you could do alike
var range = document.body.createTextRange();
var h1Elements = document.getElementsByTagName('h1');
for (var i = 0; i < h1Elements.length; i++) {
range.moveToElementText(h1Elements);
range.select();
}
to select through the contents of <h1> elements.
Opera 8 however has no body.createTextRange method, the only way there
to get at a text range outside of a text control seems to be
var range = document.selection.createRange();
When I run some code to inspect such a range object it shows that many
methods IE implements are not implemented:

collapse: function collapse() { [native code] }
compareEndPoints: undefined
duplicate: function duplicate() { [native code] }
execCommand: undefined
expand: undefined
findText: undefined
getBookmark: undefined
inRange: undefined
isEqual: undefined
move: function move() { [native code] }
moveEnd: function moveEnd() { [native code] }
moveStart: function moveStart() { [native code] }
moveToElementText: undefined
moveToPoint: undefined
parentElement: undefined
pasteHTML: undefined
select: function select() { [native code] }
setEndPoint: undefined

As you can see moveToElementText for instance is not implemented and
IE's API does not have another method to relate a range to a DOM element
node so I see no way currently in Opera 8.01 to create a text range on
the contents of an element node and select that contents.
 
M

Martin Bialasinski

Spartanicus said:
All content below a certain header (in the source).

If you have

H1 A
H2 B
H2 C
H2 D
H1 E

and the user selects "H2 B" wouldn't it be more useful to read from
"H2 B" to "H2 C" and not until the end of the document?

Bye,
Martin
 
V

VK

I'd like to know if it's possible to use js to step through and select a
header in an HTML document, select it's content and all content below it.

OK, only in the name of speach technologies! :)

This is how it was done at NN4 times (with write() instead of innerHTML
of course).

Tested to work under Opera 8 / Windows.
Still they are looking very funny with all these text-to-speach
statements on the front page and the most essential methods missing in
their browser. Reminds me an investment seeker with X millions business
plan in his hand and a big blue under his left eye.

-------------------------------

<html>
<head>
<title>Pseudo-TextRange (Opera)</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
var myTextBlock = null;
var myLexems = [];
var myLexemsLength = 0;
var starter = null;
var timerID = null;
var myLexemReader = null;
var ptr = 0;
var re = /([\.\,\:\-\?\!])/;

function f1(thisStarter,myID) {
if (thisStarter.innerText.indexOf('start') != -1) {
thisStarter.innerText = 'Click here to stop';
myLexemReader = document.forms[0].elements[0];
myTextBlock = document.getElementById(myID);
myLexems = myTextBlock.innerText.split(' ');
myLexemsLength = myLexems.length;
timerID = window.setTimeout('f2()',1000);
}
else {
window.clearTimeout(timerID);
thisStarter.innerText = 'Click here to start';
timerID = null;
}
}

function f2() {
var startPoint = myLexems.slice(0,ptr).join(' ');
var endPoint = myLexems.slice(ptr+1).join(' ');
var selText = ' <span class="pseudoSelection">' + myLexems[ptr] +
'</span> ';
myTextBlock.innerHTML = startPoint + selText + endPoint;
myLexemReader.value = myLexems[ptr].replace(re," +[$1]");
ptr++;
if (ptr < myLexemsLength) {
timerID = window.setTimeout('f2()',1000);
}
else {
thisStarter.innerText = 'Click here to start';
ptr = 0;
}
}
</script>

<style type="text/css">
..pseudoSelection { color: #FFFFFF; background-color: #000000}
</style>

</head>

<body bgcolor="#FFFFFF">
<h2 onclick="f1(this,'p1')">Click here to start</h2>
<p id="p1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Phasellus in sem lacinia nunc rutrum tempor. Vivamus facilisis.
Vivamus lectus eros, aliquet id, tincidunt in, tempus quis, erat. Etiam

scelerisque sem et massa. Cum sociis natoque penatibus et magnis dis
parturient
montes, nascetur ridiculus mus. Ut at arcu. Praesent et turpis ac urna
ornare hendrerit.
Mauris vitae pede a libero scelerisque egestas. Nunc sollicitudin, orci
et
commodo ultrices, nisl libero vulputate urna, in posuere ligula arcu ut
libero. In
ullamcorper neque ut mi.</p>

<form>
<input type="text" name="output" size="32">
</form>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top