How Browser Engine works? - html

Here are my HTML test codes using Google HTML/CSS guide.
<table>
<thead>
<tr>
<th>Date
<th>Country
<tbody>
<tr>
<td>24/07/2018
<td>Myanmar
<tr>
<td>31/06/2018
<td>France
</table>
The following is how browser interprets it.
<table>
<thead>
<tr>
<th>Date
</th>
<th>Country
</th>
</tr>
</thead>
<tbody>
<tr>
<td>24/07/2018
</td>
<td>Myanmar
</td>
</tr>
<tr>
<td>31/06/2018
</td>
<td>France
</td>
</tr>
</tbody>
</table>
Here is my questions.
How the browser detect the lack of closing tag and how they interprets it.
It is preferable to use without closing elements? If it is, when should I use?
If it is not preferable, why?
Will it be impact on styling and adding interactivity on HTML semantic style?

Answers:
This is beyond the scope of SO, but just like any compiler detects something that opened is not closed. You can try and program something that identifies a valid bracket series, it would probably be similar.
Not using closing elements may break your page beyond being horribly un-maintainable. Always use closing elements.
See 2.
See 2.
Browsers sometimes can guess what you meant (better say, they parse luckily in a way that produces what you meant), but might also be wrong. See this:
<div>Hello<span>world
is this:
<div>Hello</div><span>world></span>
or
<div>Hello<span>world></span></div>
both are valid, and the browser has no idea which you meant. If you really want to get into how browsers are supposed to parse HTML, see this great link by
#modu and #kalido: http://w3c.github.io/html/syntax.html#tokenization . You may be able to workout how a browser should parse the above line.

Related

Why does Bloomberg terminal crop html formatted text?

I am trying to ease a common workflow of our users by giving them the opportunity to copy information from our app to the clipboard and paste it into Bloomberg chat. Since some of the content is tabular, I use the text/html mime type to format it as follows:
<div>
<span>some raw text<span>
<table>
<thead>...
<tbody>...
</table>
<div>
When pasting the resulting clipboard content into Word, Skype, or other rich text enabled targets, it works as expected. However, when pasting to Bloomberg terminal/chat, only the table is pasted while the text above is cropped.
How do I get the whole content to be pasted?
I found a workaround: since the BB input field seems to parse only tables and ignore the rest of the html document, we added the text as part of the table's header.
<table>
<thead>
<tr>
<td colspan="2">The text I want to appear "before" the table</td>
</tr>
<tr>
<td colspan="2">(manually break lines to avoid cut-off)</td>
</tr>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
</thead>
<tbody>...
</table>
Not great as text might be cropped depending on the number and width of columns and the rendering is not great. Happy to see better ideas :)

Phantom Showing up in my page

I have a page written in HTML/ASP that has a series of nested tables that I use for formatting the page the way I want it.
When the page loads however there is a white space between the two tables that is not in the code and when I inspect it in chrome it shows the code has a &nbsp character between them.
Why is this appearing in the page when it loads but it is not in the script? How can I remove it?
<table width=100% border=2 cellpadding=0>
<tr>
<table width=100%>
<tr>
<th width=10% align="right">Destination:</th>
<td width=60%>Here</td>
<td width=10% align="right">Date:</td>
<td width=20% align="left"> <%=FormatDateTime(d,2)%></td>
</tr>
</table>
</tr>
<tr>
<table width = 100%>
<tr>
<td width=2%> </td>
<td align="right">Time1:</td>
<td align="center"><%=formatTime(oRS1("time"))%></td>
<td><%=oRS1("location")%></td>
<td width=40> </td>
<td align="right">Driver</td>
<td> <%=fpn%></td>
</tr>
<tr>
</table>
I just had this same issue and here are the steps that I tried that eventually fixed it:
Cleared the cache in the browser
Cleared all browser data
Tried a different browser
None of the above worked in my case, and the phantom was not in my code (and I triple checked it!!!!), but it was obviously somewhere since it was showing in the console and other browsers:
So, (and this may not be the best way to solve this but it worked) as you can see from my console output, I added two paragraph tags above and two below (by carefully putting the cursor in front of the next element and using a carriage return and the arrow key to go back up) to get the phantom in the middle. After that I saved it, then proceeded to select and delete the two paragraph tags in the middle together (with the phantom between them). This is what finally worked, the delete of both the elements surrounding it together took the phantom with it...
Perhaps some small part of my html page became corrupted? Who knows... but if anyone else is having this issue, give this a try.

xpath ignore node

I'm looking for a way to select a node in xpath, giving that a node on it's path may exist or not. Just like '?' works in regexp ;)
For instance, I'd like to figure out a xpath query to get to <td> regardless of the case whether <tbody> node exists or not, with something like /table/(tbody)?/tr/td. I'd like it to work in both cases:
<table>
<tr>
<td />
</tr>
</table>
and
<table>
<tbody>
<tr>
<td />
</tr>
</tbody>
</table>
This may fail to cover more complex cases, but in this example using /table/tbody/tr/td | /table/tr/td should do the trick.
You can do:
//table/descendant::tr/td
or
//table//tr/td
depending on your taste. The double slash is a "look that up somewhere on this level or deeper" (more formally, descendant-or-self:: axis). The spec is, surprisingly, a very good read on this!

Why would someone use invalid html?

I think I found some typos in code I'm supporting and posted it to a local site as a funny example of invalid code, and then someone said that sometimes this invalid usage is correct.
Why would someone might need this code?
<table>
<form>
<tr>
<td></td>
</tr>
</form>
<form>
<tr>
<td></td>
</tr>
</form>
</table>
When can it be better than the following?
<table>
<tr>
<td>
<form></form>
</td>
</tr>
<tr>
<td>
<form></form>
</td>
</tr>
</table>
Or this:
<form>
<table>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<form>
<table>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
I played with JSFiddle and can't find HTML decision, so maybe that's a reason.
But I think that JavaScript or CSS way would be better.
And then some person said me that sometimes this invalid usage is correct.
By definition, invalid code isn't correct.
Can you please explain me why someone might need this code?
With a single cell per row? There isn't even a half good reason. There shouldn't be a table there in the first place because it wouldn't be tabular data.
If there were multiple cells per row, then it would be nice to be able to have a form per row (for an "edit this entry" thing). It isn't necessary though, as you can give each input a unique name, and then determine which row to process based on which submit button was clicked (since only the clicked submit button will be successful and therefore have its name/value pair submitted to the server).
This is a way to get the two form elements to align with each other using table-based UI alignment. It used to be quite common before CSS was widely supported in browsers.
Today one would do that in CSS with your preferred markup.

Is there a tag in XHTML that you can put anywhere in the body - even inside TABLE elements?

I would like to be able to place an empty tag anywhere in my document as a marker that can be addressed by jQuery. However, it is important that the XHTML still validates.
To give you a bit of background as to what I'm doing: I've compared the current and previous versions of a particular document and I'm placing markers in the html where the differences are. I'm then intending to use jQuery to highlight the parent block-level elements when highlightchanges=true is in the URL's query string.
At the moment I'm using <span> tags but it occurred to me that this sort of thing wouldn't validate:
<table>
<tr>
<td>Old row</td>
</tr>
<span class="diff"></span><tr>
<td>Just added</td>
</tr>
</table>
So is there a tag I can use anywhere? Meta tag maybe?
Thanks for your help!
Iain
Edit: On the advice of codeka, I may look for a better difference engine and I may have found one that is attuned to finding differences in XHTML: http://www.rohland.co.za/index.php/2009/10/31/csharp-html-diff-algorithm/
You can use HTML comments and this plugin (or this one).
Can you not just modify the class of elements that have changed?
<p class="diff other-class">Something changed</p>
<table>
<tr>
<td>Old row</td>
</tr>
<tr class="diff">
<td>Just added</td>
</tr>
</table>