简体   繁体   English

使用Google Closure编译器处理jQuery parseHTML()和html()时,

[英]Dealing with jQuery parseHTML() and html() when using Google closure compiler,,,

I have a simple set of statements that actually work (I have run that code and it does what I expect of it) that looks as follow: 我有一组实际可行的简单语句(我已经运行了该代码,并且可以完成我所期望的),如下所示:

result_xml = result.jqxhr.responseXML;
a = jQuery("data[name='calendar']", result_xml).text();
new_calendar = jQuery.parseHTML(a);
jQuery("div.calendar").html(jQuery("div.calendar table.calendar-table", new_calendar));

result comes from an AJAX reply, you should recognize the jqxhr field. result来自AJAX回复,您应该识别jqxhr字段。

I expect a to be a string since I get a block of data with text() . 我希望a是一个字符串,因为我通过text()获得了一个数据块。 The data is an HTML string that I saved in the XML document. 数据是我保存在XML文档中的HTML字符串。 So I can cast it this way: 因此,我可以这样进行转换:

a = /** @type {string} */ jQuery("data[name='calendar']", result_xml).text();

Now I convert that string to an HTML DOM thinking it would be an Element: 现在,我将该字符串转换为HTML DOM并认为它将是一个Element:

new_calendar = /** @type {Element} */ jQuery.parseHTML(a);

And that's where I get this weird error: 那就是我得到这个奇怪的错误的地方:

WARNING - invalid cast - must be a subtype or supertype 警告-强制转换-必须是子类型或超类型
from: (Array.<(Element|null)>|null) 来自:(Array。<(Element | null)> | null)
to : (Element|null) 到:(元素|空)

So parseHTML() would be returning an array of elements (or null)? 那么parseHTML()将返回元素数组(或null)吗?

Then I was trying to use the output of the parseHTML() in the html() function and there too, I have a hard time to understand what is going on. 然后,我试图在html()函数中使用parseHTML()的输出,在那里,我也很难理解发生了什么。

WARNING - invalid cast - must be a subtype or supertype found : (Array.<(Element|null)>|null) 警告-强制类型转换-必须是找到的子类型或超类型:(Array。<(Element | null)> | null)
required: (Document|Element|Object.|jQuery|null|undefined) 必需:(文档|元素|对象。| jQuery |空|未定义)

Frankly, I do not see why the Google compiler makes it such that the output of one function cannot be the input of another, even if it works just fine in the real world. 坦白说,我不明白为什么Google编译器会这样做,即使一个函数的输出在现实世界中工作正常,它也不能成为另一个函数的输入。

Would an array of Element be considered a Document ? 是否将Element数组视为Document

Then finally (yeah! all of that for 3 lines of JavaScript!) I get another error in regard to the input of the html() function: 然后,最后(是的!所有3行JavaScript都用了!)关于html()函数的输入,我又遇到了另一个错误:

WARNING - actual parameter 1 of jQuery.prototype.html does not match formal parameter found : jQuery 警告-jQuery.prototype.html的实际参数1与找到的形式参数不匹配:jQuery
required: (function (number, string): ?|string|undefined) 必填:(函数(数字,字符串):? | string | undefined)
jQuery("div.calendar").html(jQuery("div.calendar table.calendar-table", new_calendar)); jQuery(“ div.calendar”)。html(jQuery(“ div.calendar table.calendar-table”,new_calendar));

Here too, it works in the real world... will the jQuery object automatically be converted to a string and then re-transformed to a set of tags to be added in my calendar? 在这里,它也可以在现实世界中工作... jQuery对象会自动转换为字符串,然后重新转换为要添加到我的日历中的一组标签吗?

Are all of these normal limitations of the closure compiler? 闭包编译器是否所有这些正常限制


Based on Chad answer, I changed my code this way: 基于乍得的答案,我以这种方式更改了代码:

result_xml = result.jqxhr.responseXML;
a = /** @type {string} */ (jQuery("data[name='calendar']", result_xml).text());
new_calendar = jQuery.parseHTML(a);
jQuery("div.calendar").empty().append(jQuery("div.calendar table.calendar-table", new_calendar[0]));
  1. I cast the text() output to string in closure; 我将text()输出转换为闭包形式的字符串;
  2. I use element 0 of new_calendar instead of directly new_calendar . 我使用new_calendar元素0而不是直接使用new_calendar
  3. I changed the html() with empty().append() since the append() function accepts a jQuery object as input 我将html()更改为empty().append()因为append()函数接受jQuery对象作为输入

This makes the closure compiler happy. 这使闭包编译器感到满意。

Yes Absolutely this is expected behavior. 是的,这绝对是预期的行为。 There are many things that "work" in JavaScript. 在JavaScript中,有许多东西可以“工作”。 Closure-compiler attempts to force good behavior when asked. 当被询问时,闭包编译器试图强制行为良好。

As for jQuery - these are the normal limitations of the published jQuery API. 至于jQuery-这些是已发布的jQuery API的常规限制。 With jQuery the code often supports undocumented behaviors (like you are listing). 使用jQuery时,代码通常支持未记录的行为(如您正在列出的代码)。 However the jQuery team is free to change behaviors that are not documented at any time. 但是,jQuery团队可以随时更改未记录的行为。

Closure-compiler warns you if you are not matching what is listed as the API spec (and that definition is community maintained - so not always 100% right). 如果您与API规范中列出的内容不匹配(并且该定义由社区维护-因此并非总是100%正确),那么Closure-compiler会警告您。

Here's the relevant specifications: 相关规格如下:

I use DOM API instead of jQuery. 我使用DOM API而不是jQuery。 There is a full HTML text in data. 数据中有完整的HTML文本。

// create DOM parser object
var dom_parser = new DOMParser();
// parse HTML into DOM document object
var doc = dom_parser.parseFromString(data , "text/html");
// doc.body.innerHTML = body part in text, not object
// <body> tag is stripped.
// append the body part into div with the id "contentsarea".
$("#contentsarea").empty().append(doc.body.innerHTML);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM