net.html: use `or {}` in .writeln() method calls (fix #8942) (#8953)

pull/8968/head
StunxFS 2021-02-25 08:24:30 -04:00 committed by GitHub
parent 1a8e502e2c
commit 2e381f427a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 44 deletions

View File

@ -12,7 +12,7 @@ mut:
[inline] [inline]
fn is_null(data int) bool { fn is_null(data int) bool {
return data == null_element return data == html.null_element
} }
[inline] [inline]
@ -21,15 +21,11 @@ fn (stack Stack) is_empty() bool {
} }
fn (stack Stack) peek() int { fn (stack Stack) peek() int {
return if !stack.is_empty() { return if !stack.is_empty() { stack.elements[stack.size - 1] } else { html.null_element }
stack.elements[stack.size - 1]
} else {
null_element
}
} }
fn (mut stack Stack) pop() int { fn (mut stack Stack) pop() int {
mut to_return := null_element mut to_return := html.null_element
if !stack.is_empty() { if !stack.is_empty() {
to_return = stack.elements[stack.size - 1] to_return = stack.elements[stack.size - 1]
stack.size-- stack.size--

View File

@ -25,7 +25,7 @@ mut:
fn (mut dom DocumentObjectModel) print_debug(data string) { fn (mut dom DocumentObjectModel) print_debug(data string) {
$if debug { $if debug {
if data.len > 0 { if data.len > 0 {
dom.debug_file.writeln(data) dom.debug_file.writeln(data) or { panic(err) }
} }
} }
} }
@ -114,9 +114,10 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
if is_close_tag(tag) { if is_close_tag(tag) {
temp_int = stack.peek() temp_int = stack.peek()
temp_string = tag.name[1..] temp_string = tag.name[1..]
for !is_null(temp_int) && temp_string != tag_list[temp_int].name && !tag_list[temp_int].closed { for !is_null(temp_int) && temp_string != tag_list[temp_int].name
dom.print_debug(temp_string + ' >> ' + tag_list[temp_int].name + ' ' + (temp_string == && !tag_list[temp_int].closed {
tag_list[temp_int].name).str()) dom.print_debug(temp_string + ' >> ' + tag_list[temp_int].name + ' ' +
(temp_string == tag_list[temp_int].name).str())
stack.pop() stack.pop()
temp_int = stack.peek() temp_int = stack.peek()
} }
@ -142,7 +143,8 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.print_debug("Added ${tag.name} as child of '" + tag_list[temp_int].name + dom.print_debug("Added ${tag.name} as child of '" + tag_list[temp_int].name +
"' which now has ${dom.btree.get_children().len} childrens") "' which now has ${dom.btree.get_children().len} childrens")
*/ */
dom.print_debug("Added $tag.name as child of '" + temp_tag.name + "' which now has $temp_tag.children.len childrens") dom.print_debug("Added $tag.name as child of '" + temp_tag.name +
"' which now has $temp_tag.children.len childrens")
} else { // dom.new_root(tag) } else { // dom.new_root(tag)
stack.push(root_index) stack.push(root_index)
} }
@ -168,20 +170,12 @@ pub fn (mut dom DocumentObjectModel) get_tag_by_attribute_value(name string, val
// get_tag retrieves all the tags in the document that has the given tag name. // get_tag retrieves all the tags in the document that has the given tag name.
pub fn (dom DocumentObjectModel) get_tag(name string) []&Tag { pub fn (dom DocumentObjectModel) get_tag(name string) []&Tag {
return if name in dom.tag_type { return if name in dom.tag_type { dom.tag_type[name] } else { []&Tag{} }
dom.tag_type[name]
} else {
[]&Tag{}
}
} }
// get_tag_by_attribute retrieves all the tags in the document that has the given attribute name. // get_tag_by_attribute retrieves all the tags in the document that has the given attribute name.
pub fn (dom DocumentObjectModel) get_tag_by_attribute(name string) []&Tag { pub fn (dom DocumentObjectModel) get_tag_by_attribute(name string) []&Tag {
return if name in dom.all_attributes { return if name in dom.all_attributes { dom.all_attributes[name] } else { []&Tag{} }
dom.all_attributes[name]
} else {
[]&Tag{}
}
} }
// get_root returns the root of the document. // get_root returns the root of the document.

View File

@ -14,7 +14,7 @@ mut:
opened_code_type string opened_code_type string
line_count int line_count int
lexeme_builder strings.Builder = strings.Builder{} lexeme_builder strings.Builder = strings.Builder{}
code_tags map[string]bool = { code_tags map[string]bool = map{
'script': true 'script': true
'style': true 'style': true
} }
@ -25,12 +25,12 @@ pub struct Parser {
mut: mut:
dom DocumentObjectModel dom DocumentObjectModel
lexical_attributes LexicalAttributes = LexicalAttributes{ lexical_attributes LexicalAttributes = LexicalAttributes{
current_tag: &Tag{} current_tag: &Tag{}
} }
filename string = 'direct-parse' filename string = 'direct-parse'
initialized bool initialized bool
tags []&Tag tags []&Tag
debug_file os.File debug_file os.File
} }
// This function is used to add a tag for the parser ignore it's content. // This function is used to add a tag for the parser ignore it's content.
@ -53,7 +53,7 @@ fn (parser Parser) builder_str() string {
fn (mut parser Parser) print_debug(data string) { fn (mut parser Parser) print_debug(data string) {
$if debug { $if debug {
if data.len > 0 { if data.len > 0 {
parser.debug_file.writeln(data) parser.debug_file.writeln(data) or { panic(err) }
} }
} }
} }
@ -99,8 +99,8 @@ fn (mut parser Parser) generate_tag() {
if parser.lexical_attributes.open_tag { if parser.lexical_attributes.open_tag {
return return
} }
if parser.lexical_attributes.current_tag.name.len > 0 || if parser.lexical_attributes.current_tag.name.len > 0
parser.lexical_attributes.current_tag.content.len > 0 { || parser.lexical_attributes.current_tag.content.len > 0 {
parser.tags << parser.lexical_attributes.current_tag parser.tags << parser.lexical_attributes.current_tag
} }
parser.lexical_attributes.current_tag = &Tag{} parser.lexical_attributes.current_tag = &Tag{}
@ -119,8 +119,8 @@ pub fn (mut parser Parser) split_parse(data string) {
} }
if parser.lexical_attributes.open_code { // here will verify all needed to know if open_code finishes and string in code if parser.lexical_attributes.open_code { // here will verify all needed to know if open_code finishes and string in code
parser.lexical_attributes.lexeme_builder.write_b(chr) parser.lexical_attributes.lexeme_builder.write_b(chr)
if parser.lexical_attributes.open_string > 0 && if parser.lexical_attributes.open_string > 0
parser.lexical_attributes.open_string == string_code { && parser.lexical_attributes.open_string == string_code {
parser.lexical_attributes.open_string = 0 parser.lexical_attributes.open_string = 0
} else if is_quote { } else if is_quote {
parser.lexical_attributes.open_string = string_code parser.lexical_attributes.open_string = string_code
@ -167,8 +167,8 @@ pub fn (mut parser Parser) split_parse(data string) {
parser.lexical_attributes.lexeme_builder.write_b(chr) parser.lexical_attributes.lexeme_builder.write_b(chr)
} else if chr == `>` { // close tag > } else if chr == `>` { // close tag >
complete_lexeme := parser.builder_str().to_lower() complete_lexeme := parser.builder_str().to_lower()
parser.lexical_attributes.current_tag.closed = (complete_lexeme.len > 0 && parser.lexical_attributes.current_tag.closed = (complete_lexeme.len > 0
complete_lexeme[complete_lexeme.len - 1] == `/`) // if equals to / && complete_lexeme[complete_lexeme.len - 1] == `/`) // if equals to /
if complete_lexeme.len > 0 && complete_lexeme[0] == `/` { if complete_lexeme.len > 0 && complete_lexeme[0] == `/` {
parser.dom.close_tags[complete_lexeme] = true parser.dom.close_tags[complete_lexeme] = true
} }
@ -210,8 +210,9 @@ pub fn (mut parser Parser) split_parse(data string) {
} else if chr == `<` { // open tag '<' } else if chr == `<` { // open tag '<'
temp_string := parser.builder_str() temp_string := parser.builder_str()
if parser.lexical_attributes.lexeme_builder.len >= 1 { if parser.lexical_attributes.lexeme_builder.len >= 1 {
if parser.lexical_attributes.current_tag.name.len > 1 && if parser.lexical_attributes.current_tag.name.len > 1
parser.lexical_attributes.current_tag.name[0] == 47 && !blank_string(temp_string) { && parser.lexical_attributes.current_tag.name[0] == 47
&& !blank_string(temp_string) {
parser.tags << &Tag{ parser.tags << &Tag{
name: 'text' name: 'text'
content: temp_string content: temp_string

View File

@ -34,7 +34,7 @@ fn test_giant_string() {
fn test_script_tag() { fn test_script_tag() {
mut parser := Parser{} mut parser := Parser{}
script_content := "\nvar googletag = googletag || {};\ngoogletag.cmd = googletag.cmd || [];if(3 > 5) {console.log(\'Birl\');}\n" script_content := "\nvar googletag = googletag || {};\ngoogletag.cmd = googletag.cmd || [];if(3 > 5) {console.log('Birl');}\n"
temp_html := '<html><body><script>$script_content</script></body></html>' temp_html := '<html><body><script>$script_content</script></body></html>'
parser.parse_html(temp_html) parser.parse_html(temp_html)
assert parser.tags[2].content.len == script_content.replace('\n', '').len assert parser.tags[2].content.len == script_content.replace('\n', '').len

View File

@ -54,11 +54,7 @@ pub fn (tag &Tag) str() string {
html_str.write_string('="$value"') html_str.write_string('="$value"')
} }
} }
html_str.write_string(if tag.closed && tag.close_type == .in_name { html_str.write_string(if tag.closed && tag.close_type == .in_name { '/>' } else { '>' })
'/>'
} else {
'>'
})
html_str.write_string(tag.content) html_str.write_string(tag.content)
if tag.children.len > 0 { if tag.children.len > 0 {
for child in tag.children { for child in tag.children {