This block contains invalid or unexpected content on Custom HTML - wordpress

I am editing a document in draft mode Wordpress 5.2.2 in the Gutenberg editor, and add this Custom HTML block:
<pre><code class="language-typescript">const simple = &ltT&gt(cl: T) => cl;
class Hero {
constructor(public position: [number, number]) {}
}
interface { hello: number }
const errorOne = &ltT&gt(cl: T) => new cl(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.</code></pre>
and it happily works as expected in preview. I save as draft.
When I return the HTML is ghosted and I get the error in the title. I can convert to HTML and it works again, but then it errors again when I return to it later.
It seems this error is talked about everywhere but the explanations are nonsense and resolve nothing.
If my Custom HTML is valid (which it seems to be), why does it work and then give an error. How do I fix this?

I think the main issue is not converting < & > properly in your code. They are missing the semicolon at the end of the string.
This code is working fine:
<pre><code class="language-typescript">const simple = <T>(cl: T) => cl;
class Hero {
constructor(public position: [number, number]) {}
}
interface { hello: number }
const errorOne = <T>(cl: T) => new cl(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.</code></pre>
When you insert the code with missing semicolon, WordPress saved as is. However, when trying the load the page again WordPress compares the saved content (with missing characters) to the one generated from the block (which is probably attempting to display correct HTML). This process led to an error as both texts were not identical.
If you want to check the error yourself you can check the console through developer tool in your browser (F12 in Chrome).

Related

How can I remove a message shown from a different module?

In Drupal 7, I could use the following code.
if ($_SESSION['messages']['status'][0] == t('Registration successful. You are now logged in.')) {
unset($_SESSION['messages']['status']);
}
What is the equivalent code for Drupal 8?
First of all, in Drupal 8, messages are stored in the same $_SESSION['messages'] variable as before. However, using it directly is not a good way, as there exist drupal_set_message and drupal_get_messages functions, which you may freely use.
Then, status messages are shown using status-messages theme. This means that you can write preprocess function for it and make your alteration there:
function mymodule_preprocess_status_messages(&$variables) {
$status_messages = $variables['message_list']['status'];
// Search for your message in $status_messages array and remove it.
}
The main difference with Drupal 7, however, is that now status messages are not always strings, they may be objects of Markup class. They are wrappers around strings and may be cast to underlying string using magic method __toString. This means that they can be compared with and as strings:
function mymodule_preprocess_status_messages(&$variables) {
if(isset($variables['message_list']['status'])){
$status_messages = $variables['message_list']['status'];
foreach($status_messages as $delta => $message) {
if ($message instanceof \Drupal\Component\Render\MarkupInterface) {
if ((string) $message == (string) t("Searched text")) {
unset($status_messages[$delta]);
break;
}
}
}
}
}
Upon reading the related change record, I have discovered \Drupal::messenger()->deleteAll(). I hope this is useful to someone. UPDATE: You should NOT do this, as it removes all subsequent messages as well. Instead, do unset(['_symfony_flashes']['status'][0]).
You can install the module Disable Messages and filter out messages by pattern in the configuration of the module.
For this particular case, you can filter out the message using the following pattern in the module configuration
Registration successful.*
Although the question is asked around Drupal 8 which is no longer supported, the module works for Drupal 7, 8, 9.
You can solve your problem in more than one way.
First way:
You can make minor change in core user module.
Go on:
\core\modules\user\src\RegisterForm.php
In that file you have line you can change:
drupal_set_message($this->t('Registration successful. You are now logged in.'));
NOTE: This is easiest way but in this case way you will edit Drupal core module and that is generally bad practice. In further development you could have problems like overwrite your changes when you do update.
Second way:
You can disable end user message using module. Disable message module have option you need. In module configuration you have text box where you can filter out messages shown to the end users.
Third way:
Messages in Drupal 8 are stored in a session variable and displayed in the page template via the $messages theme variable. When you want to modify the variables that are passed to the template before it's invoked you should use preprocess function. In your case here you can just search string in session variable and alert/remove it before it's displayed.
function yourmodule_preprocess_status_messages(&$variables) {
$message = 'Registration successful. You are now logged in.';
if (isset($_SESSION['messages'])) {
foreach ($_SESSION['messages'] as $type => $messages) {
if ($type == 'status') {
$key = array_search($message, $messages);
if ($key !== FALSE) {
unset($_SESSION['messages'][$type][$key]);
}
}
}
}
}
(Note:Untested code, beware of typos)
Hope this helps!

Dart encodeUriComponent Map.keys()

There is an example that I have seen in a number of places for encoding a Map as follows:
#import('dart:uri');
String encodeMap(Map data) {
return Strings.join(data.getKeys().map((k) {
return "${encodeUriComponent(k)}=${encodeUriComponent(data[k])}";
}), "&");
}
I'm running what appears to be the latest Dart editor (version 0.2.9_r 16323)
in the above example, for Dart M2, I believe that data.getKeys() has been changed to data.keys() which I have altered.
However, I get an error when running it in the Editor:
Exception: NoSuchMethodError : method not found: 'call'"
I have 2 questions:
I'm wondering if this above code should still work in M2 with the change indicated (Map.keys())?
I'm wondering if this above code does something different to: JSON.stringify(data);
Any other pointers are welcome.
TIA.
Two changes to do :
import syntax has changed.
getKeys() method became a getter called keys.
A working version :
import 'dart:uri';
String encodeMap(Map data) {
return Strings.join(data.keys.map((k) {
return "${encodeUriComponent(k)}=${encodeUriComponent(data[k])}";
}), "&");
}
The String generated by this encodeMap is quite different from the one generated by JSON.stringify as you can see in the bellow snippet :
main() {
final map = {"a":"b", "c":"d"};
assert(encodeMap(map) == "a=b&c=d");
assert(JSON.stringify(map) == '{"a":"b","c":"d"}');
}

InDesign SDK : Drag'n'Drop from a Flex Panel

I have a Flex panel, in InDesign, from which I drag an URL. If I drop this URL on a text editor or a web browser, it works. But when I try to drop it on my InDesign document, it's a little bit harder.
I have implemented a subclass of CDragDropTargetFlavorHelper. The drop works perfectly on Windows. But on mac, I have problems in the method CouldAcceptTypes :
DragDrop::TargetResponse AutocatDNDCustomFlavorHelper::CouldAcceptTypes(const DragDropTarget* target, DataObjectIterator* dataIter, const IDragDropSource* fromSource, const IDragDropController* controller) const
{
if (0 != dataIter && 0 != target)
{
DataExchangeResponse response = dataIter->FlavorExistsWithPriorityInAllObjects(kURLDExternalFlavor);
if (response.CanDo())
{
...
}
}
}
The problem is that response.canDo() answers kTrue on Windows, but kFalse on Mac. I tried to explore the content of dataIter, but a call on dataIter->First() returns nil. I tried a controller->GetItemCount(), which returns 1. But if I try a controller->GetDragItem(1), I get a nil pointer. I have the impress there is no item. Though, the drop works on another app than InDesign, as I said.
Is it a problem of internalization ? Or something else ? It let me dry.
Thanks in advance
-------------------------- EDIT -----------------------------------
I solved this problem, but discovered another one. The flavor sent by the flex panel has been changed, so that it's a text flavor instead of an URL flavor. My method couldAcceptType works now :
DragDrop::TargetResponse AutocatDNDCustomFlavorHelper::CouldAcceptTypes(const DragDropTarget* target, DataObjectIterator* dataIter, const IDragDropSource* fromSource, const IDragDropController* controller) const
{
if (0 != dataIter && 0 != target)
{
// Check for URL Flavor in the drag
DataExchangeResponse response = dataIter->FlavorExistsWithPriorityInAllObjects(kTEXTExternalFlavor);
if (response.CanDo())
{
return DragDrop::TargetResponse(response, DragDrop::kDropWillCopy);
}
}
return DragDrop::kWontAcceptTargetResponse;
}
The problem is now in the ProcessDragDropCommand method. Here is the code :
ErrorCode AutocatDNDCustomFlavorHelper::ProcessDragDropCommand(IDragDropTarget* target, IDragDropController* controller, DragDrop::eCommandType action)
{
// retrieve drop data
IPMDataObject* dragDataObject = controller->GetDragItem(1);
uint32 dataSize = dragDataObject->GetSizeOfFlavorData(kTEXTExternalFlavor) ;
...
}
The problem is the IMPDataObject I get is nil. There is no item in the controller. However, there were items in the CouldAcceptTypes method, in the DataObjectIterator. So, where are my items ?
I tried using a custom CDataExchangeHandlerFor, but could not really understand what its usage was for. It didn't work anyway.
Has anyone an idea ?
Regards,
RĂ©mi
The problem is the argument of the GetDragItem. It is 1 on PC. It is a strange value on Mac (something like 719853). The only dirty solution I found is doing a memcpy from the object retrieved drom the dataIter in the CouldAcceptTypes method, and use it in the ProcessDragDropCommand method.

comment between else and {

I just started using Razor instead of the WebForms-ViewEngine. Now in my Razor-View i have something like this:
#{
int i = 42;
string text;
if (i == 42)
{
text = "i is 42!";
}
else //i is not 42 //<- Error here
{
text = "i is something else";
}
}
I get a warning and at runtime it get an exception in the else line:
Expected a "{" but found a "/". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages.
Apparently the compiler doesn't like comments between the else and the {. I also tried commenting with #* and /*, which gave similar error-messages.
Is there anyway to make a comment in razor like I want it?
Disclaimer:
Yes i know i could fix it simply like this:
#{
int i = 42;
string text;
if (i == 42)
{
text = "i is 42!";
}
else
{ //i is not 42
text = "i is something else";
}
}
However it doesn't fit our coding guidelines and having the comment on the same line makes my intentions more clear.
That's how the Razor parser is built. You could always submit a bug/feature request on MS connect if you don't like the way it is and hope that people will vote for it and it will be fixed/implemented in a future version of the parser. Personally I wouldn't because I don't care (see below why).
This being said, why care? I mean you are not supposed to write code in a Razor page. A Razor page is intended to be used as a view. In ASP.NET MVC a view is used to display some information from the view model that is passed to it from the controller action. Markup primary, mixed with HTML helpers and displaying information from the view model. But C# code is a no no. So what you call code and what you have shown in your question has strictly nothing to do in a Razor view.

Javascript permission denied error when using Atalasoft DotImage

Have a real puzzler here. I'm using Atalasoft DotImage to allow the user to add some annotations to an image. When I add two annotations of the same type that contain text that have the same name, I get a javascript permission denied error in the Atalasoft's compressed js. The error is accessing the style member of a rule:
In the debugger (Visual Studio 2010 .Net 4.0) I can access
h._rule
but not
h._rule.style
What in javascript would cause permission denied when accessing a membere of an object?
Just wondering if anyone else has encountered this. I see several people using Atalasoft on SO and I even saw a response from someone with Atalasoft. And yes, I'm talking to them, but it never hurts to throw it out to the crowd. This only happens in IE8, not FireFox.
Thanks, Brian
Updates: Yes, using latest version: 9.0.2.43666
By same name (see comment below) I mean, I created default annotations and they are named so they can be added with javascript later.
// create a default annotation
TextData text = new TextData();
text.Name = "DefaultTextAnnotation";
text.Text = "Default Text Annotation:\n double-click to edit";
//text.Font = new AnnotationFont("Arial", 12f);
text.Font = new AnnotationFont(_strAnnotationFontName, _fltAnnotationFontSize);
text.Font.Bold = true;
text.FontBrush = new AnnotationBrush(Color.Black);
text.Fill = new AnnotationBrush(Color.Ivory);
text.Outline = new AnnotationPen(new AnnotationBrush(Color.White), 2);
WebAnnotationViewer1.Annotations.DefaultAnnotations.Add(text);
In javascript:
CreateAnnotation('TextData', 'DefaultTextAnnotation');
function CreateAnnotation(type, name) {
SetAnnotationModified(true);
WebAnnotationViewer1.DeselectAll();
var ann = WebAnnotationViewer1.CreateAnnotation(type, name);
WebThumbnailViewer1.Update();
}
There was a bug in an earlier version that allowed annotations to be saved with the same unique id's. This generally doesn't cause problems for any annotations except for TextAnnotations, since they use the unique id to create a CSS class for the text editor. CSS doesn't like having two or more classes defined by the same name, this is what causes the "Permission denied" error.
You can remove the unique id's from the annotations without it causing problems. I have provided a few code snippets below that demonstrate how this can be done. Calling ResetUniques() after you load the annotation data (on the server side) should make everything run smoothly.
-Dave C. from Atalasoft
protected void ResetUniques()
{
foreach (LayerAnnotation layerAnn in WebAnnotationViewer1.Annotations.Layers)
{
ResetLayer(layerAnn.Data as LayerData);
}
}
protected void ResetLayer(LayerData layer)
{
ResetUniqueID(layer);
foreach (AnnotationData data in layer.Items)
{
LayerData group = data as LayerData;
if (group != null)
{
ResetLayer(data as LayerData);
}
else
{
ResetUniqueID(data);
}
}
}
protected void ResetUniqueID(AnnotationData data)
{
data.SetExtraProperty("_atalaUniqueIndex", null);
}

Resources