SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is there a rule that says the attribute's value will be first parsed as HTML?

    i wonder if there is a rule that says:

    the attribute's value will be first parsed as HTML

    ?

    for example:

    <a href="foo.php?a=1&amp;b=2">

    the value will be converted into foo.php?a=1&b=2


    and

    <a href="#" onclick="changeIt( &quot;hello world &quot; ); return false;">Click me</a>

    the value part will be converted into changeIt("hello world"); return false;

    is there such a rule? hm... i wonder if any book or webpage actually state this rule? (as many of us learn HTML / CSS / Javascript by ourselves... i haven't heard of this rule before). thanks.
    Last edited by winterheat; Apr 6, 2009 at 20:20.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,817
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If it is in the HTML file and recognised as PCDATA then it will be parsed as HTML. Only CDATA sections don't get parsed as HTML.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    If it is in the HTML file and recognised as PCDATA then it will be parsed as HTML. Only CDATA sections don't get parsed as HTML.
    so do you mean anything that is part of the HTML code is PCDATA?
    what about javascript code inside of <script> tag?

    because the following code will work. so the code inside <script> is CDATA?

    Code HTML4Strict:
    <a href="#" id="link1">Click me</a>
    <a href="#" id="link2">Click me</a>    
    <div id="divContent"></div>  
     
     
    <script>
     
    document.getElementById('link1').onclick = function() {
    	changeIt("hello world"); return false;
    }
     
    document.getElementById('link2').onclick = function() {
    	changeIt("hello &quot; world"); return false;
    }
     
    function changeIt(title) {
    	document.getElementById('divContent').innerHTML = title + " " + new Date();	
    }
     
    </script>

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,817
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    In HTML code inside a script tag is CDATA, in XHTML it is PCDATA - you need to look at the doctype definition to see what is defined as PCDATA and what as CDATA.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    In HTML code inside a script tag is CDATA, in XHTML it is PCDATA - you need to look at the doctype definition to see what is defined as PCDATA and what as CDATA.
    is it that you need to give the MIME type as XHTML too? also, if IE doesn't support XHTML, then usually we serve it as HTML anyway?

  6. #6
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a friend of mine suggested (see the color text):

    <a href="#" onclick='changeIt("hello &amp;quot; world"); return false;'>Click me</a>
    <div id="divContent"></div>


    <script>

    function changeIt(title) {
    document.getElementById('divContent').innerHTML = title + " " + new Date();
    }

    </script>


    seems like lazy but it works...

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by winterheat View Post
    i wonder if there is a rule that says:

    the attribute's value will be first parsed as HTML

    ?
    Yes, HTML attribute values are parsed as HTML, so you'll need to use HTML-style escapes (entity referenes or numeric character references) to 'hide' special characters.

    Quote Originally Posted by winterheat View Post
    is it that you need to give the MIME type as XHTML too?
    Yes, otherwise you're not using XHTML at all. If you serve anything as text/html it is HTML, and HTML parsing rules apply.

    Quote Originally Posted by winterheat View Post
    a friend of mine suggested (see the color text):

    <a href="#" onclick='changeIt("hello &amp;quot; world"); return false;'>Click me</a>
    Yes, the easiest way to handle quotes inside an attribute value is to use the 'other' quotation marks around the whole value:
    Code HTML4Strict:
    <img src="car.jpg" alt="Mary's car">
    <img src="car.jpg" alt='My "new" car'>
    However, if you need both double quotes and apostrophes inside an attribute value you'll have to escape one of them:
    Code HTML4Strict:
    <img src="car.jpg" alt="Mary's &quot;new&quot; car">
    <img src="car.jpg" alt='Mary&#39;s "new" car'>
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •