Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into beta/0.14.2
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Sep 20, 2023
2 parents 9ffe8ee + aafcda7 commit ae0e7a1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 8 deletions.
18 changes: 18 additions & 0 deletions bridge/core/html/parser/html_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <utility>

#include "core/dom/comment.h"
#include "core/dom/document.h"
#include "core/dom/element.h"
#include "core/dom/text.h"
Expand Down Expand Up @@ -92,6 +93,23 @@ void HTMLParser::traverseHTML(Node* root_node, GumboNode* node) {
} else if (child->type == GUMBO_NODE_TEXT) {
auto* text = context->document()->createTextNode(AtomicString(ctx, child->v.text.text), ASSERT_NO_EXCEPTION());
root_container->AppendChild(text);
} else if (child->type == GUMBO_NODE_WHITESPACE) {
bool isBlankSpace = true;
int nLen = strlen(child->v.text.text);
for (int j = 0; j < nLen; ++j) {
isBlankSpace = child->v.text.text[j] == ' ';
if (!isBlankSpace) {
break;
}
}

if (isBlankSpace) {
if (nLen > 0) {
auto* textNode =
context->document()->createTextNode(AtomicString(ctx, child->v.text.text), ASSERT_NO_EXCEPTION());
root_container->appendChild(textNode, ASSERT_NO_EXCEPTION());
}
}
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions integration_tests/scripts/html_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ const loader = function(source) {
}
})

const htmlString = root.toString().replace(/['\n]/g, function(c){
return {'\n': '','\'': '\\'}[c];
});

return `
describe('HTMLSpec/${testRelativePath}', () => {
// Use html_parse to parser html in html file.
const html_parse = () => __webf_parse_html__(\`${htmlString}\`);
const html_parse = () => __webf_parse_html__(\`${root.toString()}\`);
var index = 0;
const snapshotAction = async () => { await snapshot(null, '${snapshotFilepath}', ${scripts.length === 0 ? 'null' : 'index.toString()'}); index++; };
${isFit ? 'fit' : isXit ? 'xit' : 'it'}("should work", async (done) => {\
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@
</body>
<script>
await snapshotAction();
await sleep(0.1);
</script>
</html>
34 changes: 34 additions & 0 deletions integration_tests/specs/css/css-empty/empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<style>

.emptyStyle:empty {
background: #ff0000;
height: 50px;
width: 300px;
}
</style>
</head>

<body class="emptyStyle">
<p>p</p>
<p class="emptyStyle"></p>
<p>p1</p>
<p class="emptyStyle"> </p>
<p>h1</p>
<h1 class="emptyStyle"> </h1>
<p>h2</p>
<h2 class="emptyStyle"></h2>
<p>h3</p>
<p>h4</p>
<div class="emptyStyle" id="debug"></div>
<div class="empty" id="debug1">xx</div>
<div class="emptyStyle" id="debug"> </div>
<div class="empty" id="debug1">xx11</div>
<div class="emptyStyle" id="debug"><!-- xx --></div>
<div class="empty" id="debug1">xx22</div>
<p class="emptyStyle" id="debug"></p>
<div class="empty" id="debug1">xx33</div>
</body>
</html>
4 changes: 3 additions & 1 deletion webf/lib/src/css/query_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ class SelectorEvaluator extends SelectorVisitor {

// http://dev.w3.org/csswg/selectors-4/#the-empty-pseudo
case 'empty':
return _element!.childNodes.every((n) => !(n is Element || n is TextNode && n.data.isNotEmpty));
return _element!.childNodes.every((n) =>
!(n is Element || (n is TextNode && n.data.isNotEmpty))
);

// http://dev.w3.org/csswg/selectors-4/#the-blank-pseudo
case 'blank':
Expand Down
3 changes: 3 additions & 0 deletions webf/lib/src/rendering/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class WebFRenderParagraph extends RenderBox

/// Compute distance to baseline of last text line
double computeDistanceToLastLineBaseline() {
if (_lineOffset.isEmpty) {
return 0.0;
}
double lastLineOffset = _lineOffset[_lineOffset.length - 1];
ui.LineMetrics lastLineMetrics = _lineMetrics[_lineMetrics.length - 1];

Expand Down
8 changes: 7 additions & 1 deletion webf/lib/src/rendering/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ class RenderTextBox extends RenderBox with RenderObjectWithChildMixin<RenderBox>

// ' a b c' => 'a b c'
static String _trimLeftWhitespace(String string) {
return string.replaceAllMapped(_trimLeftWhitespaceReg, (Match m) => '${m[1]}');
String result = string.replaceAllMapped(_trimLeftWhitespaceReg, (Match m) =>
'${m[1]}'
);
if (result.startsWith(' ')) {
return '';
}
return result;
}

// 'a b c ' => 'a b c'
Expand Down

0 comments on commit ae0e7a1

Please sign in to comment.