how to use regexp to replace some html tags in DOM

J

jiing24

I try to use regexp to replace some html tags in DOM, but the result
seems some problems,


================================
<Script language="javascript" type="text/javascript">

var config = document.getElementById("rootconfig");

alert(config.innerHTML);

regexp=new RegExp("CHECKED","g");


config.innerHTML=config.innerHTML.replace(regexp,"\Lchecked=\"checked\"");

regexp2= new RegExp("selected","g");


config.innerHTML=config.innerHTML.replace(regexp2,"\Lselected=\"selected\"");

alert(config.innerHTML);

xmlhttp.open ("POST", "http://100.0.0.1/cgi-bin/Upload.cgi",
false);

xmlhttp.send(config.innerHTML);

alert(xmlhttp.responseText);

}
</script>
<div id="rootconfig"><h1>hello</h1><h1>hi</h1><input type="checkbox"
checked="checked">abc </input><select><option>123</option><option
value="456" selected="selected">456</option></select> </div>
<input type="button" onclick="UpdateXML()" value="UpdateXML"></input>
================================

My result is:
If I use IE:
<H1>hello</H1>^M
<H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
</INPUT><SELECT><OPTION selected>123</OPTION><OPTION value=456
Lselected="selected">456</OPTION></SELECT>
^^^^
There is a unnecessary "L" in the text

If I use firefox:
<H1>hello</H1>^M
<H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
</INPUT><SELECT><OPTION selected>123</OPTION><OPTION value=456
lselected="selected">456</OPTION></SELECT>
^^^^

and in firefox:
there is some duplicate
<H1>hello</H1>^M
<H1>hi</H1><INPUT type=checkbox Lchecked="checked">abc
</INPUT><SELECT><OPTION selected
Lselected="selected">123</OPTION><OPTION

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
value=456>456</OPTION></SELECT>

================================

Could anyone help me?
Thanks in advanced.

/jiing/
 
L

Lasse Reichstein Nielsen

I try to use regexp to replace some html tags in DOM, but the result
seems some problems,
....
config.innerHTML=config.innerHTML.replace(regexp,"\Lchecked=\"checked\"");

"\L" is not a recognized escape in Javascript string literals, so it will
just become "L".

Lselected="selected">456</OPTION></SELECT>
^^^^
There is a unnecessary "L" in the text

Hardly surprising given the above :)

What did you expect "\L" to do?
/L
 
J

jiing

That's strange if no \L or no L
my result will the same as the original, but I want to replace CHECKED
with checked="checked" and selected with selected="selected"
 
T

Thomas 'PointedEars' Lahn

jiing said:
That's strange if no \L or no L

Pardon?

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
my result will the same as the original, but I want to replace CHECKED
with checked="checked" and selected with selected="selected"

Try this:

var config = {
innerHTML:
'Please leave checkboxes for selected materials checked:\n'
+ '<select multiple>\n'
+ '<option selected>This option is selected</option>\n'
+ '<option SELECTED>This option is also selected</option>\n'
+ '<option>This option is not selected</option>\n'
+ '</select>\n'
+ '<input type="checkbox" value="bar" checked>\n'
+ '<input type="checkbox" CHECKED value="42">\n'
+ '1 < select is not a select element, therefore selected and checked'
+ ' stay as they are.>\n'
+ '<!-- This is not an element, therefore selected and checked stay'
+ ' as they are. -->'
};

config.innerHTML = config.innerHTML.replace(
/(<\w[^>]*\b)(checked|selected)([^>]*>)/gi,
function(match, p1, p2, p3)
{
var p2 = p2.toLowerCase();
return p1 + p2 + '="' + p2 + '"' + p3;
});

window.alert(config.innerHTML);

Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
be Valid and is likely not to work. Also note that there are more boolean
attributes in HTML than just `checked' and `selected'. I really wonder
why you are attempting this in the first place.


PointedEars
 
J

jiing

Thomas 'PointedEars' Lahn 寫é“:
Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
be Valid and is likely not to work. Also note that there are more boolean
attributes in HTML than just `checked' and `selected'. I really wonder
why you are attempting this in the first place.
PointedEars

Hi, PointerdEars

My goal is to make the IE's DOM to fit the W3C standard
for example, IE's DOM will let
<input type="checkbox" CHECKED> rather than
<input type="checkbox" checked="checked"> //W3C standard

Do you know any good method to transfer IE's DOM to W3C standard.
Although I am a newbie in javascript, but I can see that your code is
very beautiful.
Thanks a lot.

/jiing/
 
T

Thomas 'PointedEars' Lahn

jiing said:
Thomas 'PointedEars' Lahn ???
Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
be Valid and is likely not to work. Also note that there are more
boolean attributes in HTML than just `checked' and `selected'. I really
wonder why you are attempting this in the first place.
[...]

Please do not quote signatures unless you are referring directly to them.
[...]
My goal is to make the IE's DOM to fit the W3C standard
for example, IE's DOM will let
<input type="checkbox" CHECKED> rather than
<input type="checkbox" checked="checked"> //W3C standard

How did you get that idea? The former is Valid HTML which is an SGML
application; the latter is _almost_ Valid XHTML which is an XML application
and must therefore additionally adhere to XML well-formedness (XML is a
more strictly defined subset of SGML). Likewise for `selected' and other
boolean attributes. HTML 4.01 is a W3C Recommendation -- and therefore
considered a (World Wide) _Web_ (W3) standard; W3C is not a pure
standardization organization -- as are XHTML 1.0 and XHTML 1.1 to date.
Do not fall for the XHTML hype, see below.

<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>
<URL:http://www.w3.org/TR/xhtml1/#C_10>
Do you know any good method to transfer IE's DOM to W3C standard.

There is none reasonable. Are you misguidedly serving XHTML as text/_html_
to IE and now are wondering why .inner_HTML_ reads _HTML_ and not XHTML?
Do not serve it as text/html but as application/xhtml+xml (and IE will
eventually fail to display what it in fact does not support at all), and
do use an XML serializer then, or serve _HTML_ as text/html instead.

Although I am a newbie in javascript, but I can see that your code is
very beautiful.
Thanks.

Thanks a lot.

You're welcome.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 1/26/2006 10:46 PM:
jiing said:
Thomas 'PointedEars' Lahn ???
Note that the resulting _HTML_ (it is inner_HTML_ for a reason!) will not
be Valid and is likely not to work. Also note that there are more
boolean attributes in HTML than just `checked' and `selected'. I really
wonder why you are attempting this in the first place.
[...]

Please do not quote signatures unless you are referring directly to them.

There is no "signature" in your post according to the specs you like to
quote so much. A "proper signature" is delimited by <dash><dash><space>
and since your post does not contain that sequence of characters then
the only sensible conclusion is that your post has no signature. So, how
does one quote something that doesn't exist?

If you don't want your "signature" quoted, then please delimit it
properly. Thanks.
[...]
My goal is to make the IE's DOM to fit the W3C standard
for example, IE's DOM will let
<input type="checkbox" CHECKED> rather than
<input type="checkbox" checked="checked"> //W3C standard

How did you get that idea? The former is Valid HTML which is an SGML
application;

Both are Valid HTML 4.01 Strict.
Do not fall for the XHTML hype, see below.

<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>
<URL:http://www.w3.org/TR/xhtml1/#C_10>


There is none reasonable. Are you misguidedly serving XHTML as text/_html_
to IE and now are wondering why .inner_HTML_ reads _HTML_ and not XHTML?
Do not serve it as text/html but as application/xhtml+xml (and IE will
eventually fail to display what it in fact does not support at all), and
do use an XML serializer then, or serve _HTML_ as text/html instead.

A better alternative is to simply server it as text/html with an
appropriate DTD and not fall for the temptation of XHTML no matter who
hypes it. At least until some version of IE supports it. Until then, it
is not worth the effort to code to XHTML standards.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top