0

I want to add another MSHTML question. Thanks to all responses.

We use in Delphi the standard TWebbrowser component, that uses mshtml.dll internally. Additionaly we use the registry to ensure that the pages renders with the new rendering engine (Web-Browser-Control-Specifying-the-IE-Version, MSDN: FEATURE_BROWSER_EMULATION). So we use the rendering of IE 10 but we have the same results with ie 8 to ie 11.

Using the standard rendering machine of MSHTML (IE7) works right, but due to new rendering options we need the new rendering of MSHTML.

We use the design mode of the control to enabled the user to make changes in the documents:

var
  mDocument: IHTMLDocument2;
begin
  mDocument := ((ASender as TWebBrowser).Document as IHTMLDocument2);
  mDocument.designMode := 'on';

Now we have the following problem: We load th following (simplified) HTML via the IPersistStreamInit.Load(...) into the WebBrowser:

<html>
  <body>
    What should I do 
    with some of the 
    spaces.
  </body>
</html>

In the WebBrowser user can see the following: Text in WebBrowser, "with" selected

Now, when selecting the word "with" in the WebBrowser in editing mode, and typing a character, some spaces appear. The HTML now has &nbsp; in it - exactly as many as there has been spaces in the HTML before editing:

Text in the WebBrowser, the user has entered a "n"

The code is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=unicode">
<META name="GENERATOR" content="MSHTML 10.00.9200.16540"></HEAD>
<BODY>    What should I do &nbsp;&nbsp;&nbsp;&nbsp; n some of the     spaces. 
</BODY></HTML>

The same effect can be achieved by replacing the word "spaces" in the WebBrowser.

This is a bad behaviour for users using our application.

Every HTML code with white spaces infront of text, has the same behaviour. The mess is, that MSHTML itself generates such HTML.

By now we think of a solution to remove all the spaces on the left of each line, but we think that such workarounds could end in a bigger mess, because they change the HTML. This could cause some different behaviour of the rendering.

elite
  • 191
  • 2
  • 13
  • Instead of asking a new question, I suggest you add this "sub question" regarding `IPersistStreamInit.Load` to your previous question and delete this one. obviously this is related. in any case IMO you either read/write via `IPersistStreamInit` and forget about MSHTML editing, or use MSHTML editing and workaround it's "by design" behavior... this is interesting question as I already mentioned. – kobik Nov 20 '14 at 08:06
  • I don't think that this is a subquestion. The behaviour is different. Here we just use IPresistStreamInit.Load(...) and don't nothing with PasteHTML(). Here we want to provide the user the possibility to change the content. But when he changes the content the spaces appear. – elite Nov 20 '14 at 08:24
  • 1
    Well, you got a -1 from someone who thinks (like me) it the same behavior. It does not matter if you do `PasteHTML` or any other DOM manipulation. once the DOM itself changes (in design mode) IE will restructure the HTML. test/log the source via **reading** from `IPersistStreamInit` once you are in design mode. see what you have... – kobik Nov 20 '14 at 08:30
  • The problem goes further. I can take a document, completely generated with the WebBrowser in edit mode (should be restructured HTML compatible to DOM) and load it again via IPersistStreamInit and have the same behaviour. – elite Nov 20 '14 at 08:47

1 Answers1

0

Thinking about removing the spaces before each line, puts you somewhere in the right direction, but nowhere near what you should be doing: convert the data info HTML before IPersistStreamInit.Load.

Since the HTML specification prescribes any whitespace in the HTML code should be treated as a single instance of whitespace (except inside <pre> tags), it's understandable that IE's design-mode is confused what to with these extra spaces when you edit around them. You've stumbled upon a border case.

I suggest you either don't use IPersistStreamInit.Load

but Navigate('about:<html><body></body></html>'); and document.body.innerText:=... instead,

or take care to properly format the initial HTML:

  • parse the text to collapse any/all consecutive whitespace,
  • replace all & with &amp;, < with &lt; etc...
  • (perhaps also #13#10 with '<br />' and #13#10#13#10 with '</p><p>'?)
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
  • Thanks for your response. For now I tried two other ways, but none worked. The first is to write the file to the disk and load it with `WebBrowser.Navigate2(...)`. The second is to follow the suggestion of @Stijn Sanders with the `document.body.innerText := ....` – elite Nov 20 '14 at 13:43