mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Use a JS markup serializer.
This makes MarkupAccumulator and all related code into dead code. R=ojan@chromium.org Review URL: https://codereview.chromium.org/771093002
This commit is contained in:
parent
1f1d90bc70
commit
f1a6cd9799
@ -279,10 +279,6 @@ public:
|
||||
// frame will be separated by an empty line.
|
||||
virtual WebString contentAsText(size_t maxChars) const = 0;
|
||||
|
||||
// Returns HTML text for the contents of this frame. This is generated
|
||||
// from the DOM.
|
||||
virtual WebString contentAsMarkup() const = 0;
|
||||
|
||||
// Returns a text representation of the render tree. This method is used
|
||||
// to support layout tests.
|
||||
virtual WebString renderTreeAsText(RenderAsTextControls toShow = RenderAsTextNormal) const = 0;
|
||||
|
||||
@ -648,13 +648,6 @@ WebString WebLocalFrameImpl::contentAsText(size_t maxChars) const
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
WebString WebLocalFrameImpl::contentAsMarkup() const
|
||||
{
|
||||
if (!frame())
|
||||
return WebString();
|
||||
return createMarkup(frame()->document());
|
||||
}
|
||||
|
||||
WebString WebLocalFrameImpl::renderTreeAsText(RenderAsTextControls toShow) const
|
||||
{
|
||||
RenderAsTextBehavior behavior = RenderAsTextBehaviorNormal;
|
||||
|
||||
@ -121,7 +121,6 @@ public:
|
||||
virtual void setCaretVisible(bool) override;
|
||||
|
||||
virtual WebString contentAsText(size_t maxChars) const override;
|
||||
virtual WebString contentAsMarkup() const override;
|
||||
virtual WebString renderTreeAsText(RenderAsTextControls toShow = RenderAsTextNormal) const override;
|
||||
|
||||
virtual bool selectionStartHasSpellingMarkerFor(int from, int length) const override;
|
||||
|
||||
74
framework/dom-serializer.sky
Normal file
74
framework/dom-serializer.sky
Normal file
@ -0,0 +1,74 @@
|
||||
<script>
|
||||
const kEntityMap = new Map([
|
||||
['\u00a0', ' '],
|
||||
['&', '&'],
|
||||
['<', '<'],
|
||||
['>', '>'],
|
||||
['"', '"'],
|
||||
]);
|
||||
|
||||
const kTextEscapePattern = /&|<|>|"|\u00a0/g;
|
||||
const kAttributeEscapePattern = /&|>|"|\u00a0/g;
|
||||
|
||||
function escapeText(value, pattern) {
|
||||
return (value || '').replace(pattern, function(match) {
|
||||
return kEntityMap.get(match);
|
||||
});
|
||||
}
|
||||
|
||||
function serializeAttributes(element) {
|
||||
var buffer = "";
|
||||
var attributes = element.getAttributes();
|
||||
|
||||
for (var i = 0; i < attributes.length; ++i) {
|
||||
var attribute = attributes[i];
|
||||
buffer += ' ';
|
||||
buffer += attribute.name;
|
||||
buffer += '="';
|
||||
buffer += escapeText(attribute.value, kAttributeEscapePattern);
|
||||
buffer += '"';
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function getFirstChild(element) {
|
||||
if (element.tagName === 'template')
|
||||
return element.content.firstChild;
|
||||
return element.firstChild;
|
||||
}
|
||||
|
||||
function serializeElementChildren(element) {
|
||||
if (element.tagName == "script" || element.tagName == "style")
|
||||
return element.textContent;
|
||||
return serializeChildren(getFirstChild(element));
|
||||
}
|
||||
|
||||
function serializeChildren(firstChild) {
|
||||
var buffer = "";
|
||||
for (var child = firstChild; child; child = child.nextSibling)
|
||||
buffer += serializeNode(child);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function serializeElement(element) {
|
||||
var buffer = '<' + element.tagName + serializeAttributes(element) + '>';
|
||||
buffer += serializeElementChildren(element);
|
||||
buffer += '</' + element.tagName + '>';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function serializeNode(node) {
|
||||
if (node instanceof Text)
|
||||
return escapeText(node.data, kTextEscapePattern);
|
||||
if (node instanceof Element)
|
||||
return serializeElement(node);
|
||||
if (node instanceof Document)
|
||||
return serializeChildren(node.firstChild);
|
||||
throw new Error("Cannot serialize node");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
serializeNode: serializeNode,
|
||||
};
|
||||
</script>
|
||||
@ -17,6 +17,7 @@ M <p></p>
|
||||
N
|
||||
O <br></br>
|
||||
P
|
||||
Q <template><div>contents</div></template>
|
||||
|
||||
</isindex></keygen></body>
|
||||
</html>
|
||||
|
||||
@ -19,5 +19,6 @@ M <p></p>
|
||||
N </p>
|
||||
O <br/>
|
||||
P </br>
|
||||
Q <template><div>contents</div></template>
|
||||
|
||||
</body>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<import src="/sky/framework/dom-serializer.sky" as="DomSerializer" />
|
||||
<script>
|
||||
window.addEventListener('load', function() {
|
||||
internals.notifyTestComplete(internals.contentAsMarkup());
|
||||
internals.notifyTestComplete(DomSerializer.serializeNode(document));
|
||||
});
|
||||
</script>
|
||||
|
||||
@ -45,7 +45,6 @@ gin::ObjectTemplateBuilder Internals::GetObjectTemplateBuilder(
|
||||
return Wrappable<Internals>::GetObjectTemplateBuilder(isolate)
|
||||
.SetMethod("renderTreeAsText", &Internals::RenderTreeAsText)
|
||||
.SetMethod("contentAsText", &Internals::ContentAsText)
|
||||
.SetMethod("contentAsMarkup", &Internals::ContentAsMarkup)
|
||||
.SetMethod("notifyTestComplete", &Internals::NotifyTestComplete)
|
||||
.SetMethod("connectToService", &Internals::ConnectToService);
|
||||
}
|
||||
@ -63,12 +62,6 @@ std::string Internals::ContentAsText() {
|
||||
1024*1024).utf8();
|
||||
}
|
||||
|
||||
std::string Internals::ContentAsMarkup() {
|
||||
if (!document_view_)
|
||||
return std::string();
|
||||
return document_view_->web_view()->mainFrame()->contentAsMarkup().utf8();
|
||||
}
|
||||
|
||||
void Internals::NotifyTestComplete(const std::string& test_result) {
|
||||
test_harness_->OnTestComplete(test_result);
|
||||
}
|
||||
|
||||
@ -29,7 +29,6 @@ class Internals : public gin::Wrappable<Internals> {
|
||||
|
||||
std::string RenderTreeAsText();
|
||||
std::string ContentAsText();
|
||||
std::string ContentAsMarkup();
|
||||
void NotifyTestComplete(const std::string& test_result);
|
||||
|
||||
mojo::Handle ConnectToService(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user