Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Unified Diff: sdk/lib/html/dart2js/html_dart2js.dart

Issue 11888019: Fix new Element.html with tables in IE (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updated Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
Download patch
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/html/dart2js/html_dart2js.dart
diff --git a/sdk/lib/html/dart2js/html_dart2js.dart b/sdk/lib/html/dart2js/html_dart2js.dart
index 614cb9f318ad5c75f87c2370f6aca21179ec390d..343eb1ae9df52d05aea53dcf2a1b894f34dce264 100644
--- a/sdk/lib/html/dart2js/html_dart2js.dart
+++ b/sdk/lib/html/dart2js/html_dart2js.dart
@@ -8688,11 +8688,12 @@ abstract class Element extends Node implements ElementTraversal native "*Element
final _START_TAG_REGEXP = new RegExp('<(\\w+)');
class _ElementFactoryProvider {
- static final _CUSTOM_PARENT_TAG_MAP = const {
+ static const _CUSTOM_PARENT_TAG_MAP = const {
'body' : 'html',
'head' : 'html',
'caption' : 'table',
'td': 'tr',
+ 'th': 'tr',
'colgroup': 'table',
'col' : 'colgroup',
'tr' : 'tbody',
@@ -8702,6 +8703,19 @@ class _ElementFactoryProvider {
'track' : 'audio',
};
+ // TODO(jmesserly): const set would be better
+ static const _TABLE_TAGS = const {
+ 'caption': null,
+ 'col': null,
+ 'colgroup': null,
+ 'tbody': null,
+ 'td': null,
+ 'tfoot': null,
+ 'th': null,
+ 'thead': null,
+ 'tr': null,
+ };
+
/** @domName Document.createElement */
static Element createElement_html(String html) {
// TODO(jacobr): this method can be made more robust and performant.
@@ -8715,29 +8729,89 @@ class _ElementFactoryProvider {
final match = _START_TAG_REGEXP.firstMatch(html);
if (match != null) {
tag = match.group(1).toLowerCase();
- if (_CUSTOM_PARENT_TAG_MAP.containsKey(tag)) {
- parentTag = _CUSTOM_PARENT_TAG_MAP[tag];
+ if (_Device.isIE && _TABLE_TAGS.containsKey(tag)) {
+ return _createTableForIE(html, tag);
}
+ parentTag = _CUSTOM_PARENT_TAG_MAP[tag];
+ if (parentTag == null) parentTag = 'div';
}
- final Element temp = new Element.tag(parentTag);
+
+ final temp = new Element.tag(parentTag);
temp.innerHtml = html;
Element element;
if (temp.children.length == 1) {
element = temp.children[0];
} else if (parentTag == 'html' && temp.children.length == 2) {
- // Work around for edge case in WebKit and possibly other browsers where
- // both body and head elements are created even though the inner html
- // only contains a head or body element.
+ // In html5 the root <html> tag will always have a <body> and a <head>,
+ // even though the inner html only contains one of them.
element = temp.children[tag == 'head' ? 0 : 1];
} else {
- throw new ArgumentError('HTML had ${temp.children.length} '
- 'top level elements but 1 expected');
+ _singleNode(temp.children);
}
element.remove();
return element;
}
+ /**
+ * IE table elements don't support innerHTML (even in standards mode).
+ * Instead we use a div and inject the table element in the innerHtml string.
+ * This technique works on other browsers too, but it's probably slower,
+ * so we only use it when running on IE.
+ *
+ * See also innerHTML:
+ * <http://msdn.microsoft.com/en-us/library/ie/ms533897(v=vs.85).aspx>
+ * and Building Tables Dynamically:
+ * <http://msdn.microsoft.com/en-us/library/ie/ms532998(v=vs.85).aspx>.
+ */
+ static Element _createTableForIE(String html, String tag) {
+ var div = new Element.tag('div');
+ div.innerHtml = '<table>$html</table>';
+ var table = _singleNode(div.children);
+ Element element;
+ switch (tag) {
+ case 'td':
+ case 'th':
+ element = _singleNode(_singleNode(table.rows).cells);
+ break;
+ case 'tr':
+ element = _singleNode(table.rows);
+ break;
+ case 'tbody':
+ element = _singleNode(table.tBodies);
+ break;
+ case 'thead':
+ element = table.tHead;
+ break;
+ case 'tfoot':
+ element = table.tFoot;
+ break;
+ case 'caption':
+ element = table.caption;
+ break;
+ case 'colgroup':
+ element = _getColgroup(table);
+ break;
+ case 'col':
+ element = _singleNode(_getColgroup(table).children);
+ break;
+ }
+ element.remove();
+ return element;
+ }
+
+ static TableColElement _getColgroup(TableElement table) {
+ // TODO(jmesserly): is there a better way to do this?
+ return _singleNode(table.children.where((n) => n.tagName == 'COLGROUP')
+ .toList());
+ }
+
+ static Node _singleNode(List<Node> list) {
+ if (list.length == 1) return list[0];
+ throw new ArgumentError('HTML had ${list.length} '
+ 'top level elements but 1 expected');
+ }
+
/** @domName Document.createElement */
// Optimization to improve performance until the dart2js compiler inlines this
// method.
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698