Skip to content

Commit

Permalink
Merge pull request #1918 from stevecheckoway/linenum
Browse files Browse the repository at this point in the history
Add `Nokogiri::XML::Node#line=`
  • Loading branch information
flavorjones committed Nov 26, 2019
2 parents 70c3953 + a730f85 commit d3f18d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ext/nokogiri/xml_node.c
Expand Up @@ -1332,6 +1332,25 @@ static VALUE line(VALUE self)
return INT2NUM(xmlGetLineNo(node));
}

/*
* call-seq:
* line=(num)
*
* Sets the line for this Node. num must be less than 65535.
*/
static VALUE set_line(VALUE self, VALUE num)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);

int value = NUM2INT(num);
if (value < 65535) {
node->line = value;
}

return num;
}

/*
* call-seq:
* add_namespace_definition(prefix, href)
Expand Down Expand Up @@ -1728,6 +1747,7 @@ void init_xml_node()
rb_define_method(klass, "create_external_subset", create_external_subset, 3);
rb_define_method(klass, "pointer_id", pointer_id, 0);
rb_define_method(klass, "line", line, 0);
rb_define_method(klass, "line=", set_line, 1);
rb_define_method(klass, "content", get_native_content, 0);
rb_define_method(klass, "native_content=", set_native_content, 1);
rb_define_method(klass, "lang", get_lang, 0);
Expand Down
8 changes: 8 additions & 0 deletions test/xml/test_node.rb
Expand Up @@ -1222,6 +1222,14 @@ def test_line
assert_equal 2, node.line
end

def test_set_line
skip "Only supported with libxml2" unless Nokogiri.uses_libxml?
document = Nokogiri::XML::Document.new
node = document.create_element('a')
node.line = 54321
assert_equal 54321, node.line
end

def test_xpath_results_have_document_and_are_decorated
x = Module.new do
def awesome! ; end
Expand Down

0 comments on commit d3f18d2

Please sign in to comment.